Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;
use App\Models\EpisodeModel;
use App\Models\PodcastModel;
use CodeIgniter\HTTP\URI;
use CodeIgniter\I18n\Time;
class Note extends \ActivityPub\Controllers\NoteController
{
/**
* @var \App\Entities\Podcast
*/
protected $podcast;
protected $helpers = ['auth', 'activitypub', 'svg', 'components', 'misc'];
public function _remap($method, ...$params)
{
if (
!($this->podcast = (new PodcastModel())->getPodcastByName(
$params[0],
))
) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
$this->actor = $this->podcast->actor;
if (count($params) > 1) {
if (!($this->note = model('NoteModel')->getNoteById($params[1]))) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
}
unset($params[0]);
unset($params[1]);
return $this->$method(...$params);
}
public function index()
{
helper('persons');
$persons = [];
construct_person_array($this->podcast->persons, $persons);
$data = [
'podcast' => $this->podcast,
'actor' => $this->actor,
'note' => $this->note,
'persons' => $persons,
];
// if user is logged in then send to the authenticated activity view
if (can_user_interact()) {
helper('form');
return view('podcast/note_authenticated', $data);
} else {
return view('podcast/note', $data);
}
}
public function attemptCreate()
{
$rules = [
'message' => 'required|max_length[500]',
'episode_url' => 'valid_url|permit_empty',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$message = $this->request->getPost('message');
$newNote = new \App\Entities\Note([
'actor_id' => interact_as_actor_id(),
'published_at' => Time::now(),
'created_by' => user_id(),
]);
// get episode if episodeUrl has been set
$episodeUri = $this->request->getPost('episode_url');
if (
$episodeUri &&
($params = extract_params_from_episode_uri(new URI($episodeUri)))
) {
if (
$episode = (new EpisodeModel())->getEpisodeBySlug(
$params['podcastName'],
$params['episodeSlug'],
)
) {
$newNote->episode_id = $episode->id;
}
}
$newNote->message = $message;
if (
!model('NoteModel')->addNote(
$newNote,
$newNote->episode_id ? false : true,
true,
)
) {
return redirect()
->back()
->withInput()
->with('errors', model('NoteModel')->errors());
}
// Note has been successfully created
return redirect()->back();
}
public function attemptReply()
{
$rules = [
'message' => 'required|max_length[500]',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$newNote = new \ActivityPub\Entities\Note([
'actor_id' => interact_as_actor_id(),
'in_reply_to_id' => $this->note->id,
'message' => $this->request->getPost('message'),
'published_at' => Time::now(),
'created_by' => user_id(),
]);
if (!model('NoteModel')->addReply($newNote)) {
return redirect()
->back()
->withInput()
->with('errors', model('NoteModel')->errors());
}
// Reply note without preview card has been successfully created
return redirect()->back();
}
public function attemptFavourite()
{
model('FavouriteModel')->toggleFavourite(
interact_as_actor(),
$this->note,
);
return redirect()->back();
}
public function attemptReblog()
{
model('NoteModel')->toggleReblog(interact_as_actor(), $this->note);
return redirect()->back();
}
public function attemptAction()
{
$rules = [
'action' => 'required|in_list[favourite,reblog,reply]',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
switch ($this->request->getPost('action')) {
case 'favourite':
return $this->attemptFavourite();
case 'reblog':
return $this->attemptReblog();
case 'reply':
return $this->attemptReply();
}
}
public function remoteAction($action)
{
$data = [
'podcast' => $this->podcast,
'actor' => $this->actor,
'note' => $this->note,
'action' => $action,
];
helper('form');
return view('podcast/note_remote_action', $data);
}
}