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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* @copyright 2021 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace ActivityPub\Controllers;
use ActivityPub\Objects\OrderedCollectionObject;
use ActivityPub\Objects\OrderedCollectionPage;
use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;
class ActorController extends Controller
{
protected $helpers = ['activitypub'];
/**
* @var \ActivityPub\Entities\Actor
*/
protected $actor;
/**
* @var \ActivityPub\Config\ActivityPub
*/
protected $config;
public function __construct()
{
$this->config = config('ActivityPub');
}
public function _remap($method, ...$params)
{
if (count($params) > 0) {
if (
!($this->actor = model('ActorModel')->getActorByUsername(
$params[0],
))
) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
}
unset($params[0]);
return $this->$method(...$params);
}
public function index()
{
$actorObjectClass = $this->config->actorObject;
$actorObject = new $actorObjectClass($this->actor);
return $this->response
->setContentType('application/activity+json')
->setBody($actorObject->toJSON());
}
/**
* Handles incoming requests from fediverse servers
*/
public function inbox()
{
// get json body and parse it
$payload = $this->request->getJSON();
// retrieve payload actor from database or create it if it doesn't exist
$payloadActor = get_or_create_actor_from_uri($payload->actor);
// store activity to database
$activityId = model('ActivityModel')->newActivity(
$payload->type,
$payloadActor->id,
$this->actor->id,
null,
json_encode($payload),
);
// switch/case on activity type
switch ($payload->type) {
case 'Create':
switch ($payload->object->type) {
case 'Note':
if (!$payload->object->inReplyTo) {
return $this->response
->setStatusCode(501)
->setJSON([]);
}
$replyToNote = model('NoteModel')->getNoteByUri(
$payload->object->inReplyTo,
);
// TODO: strip content from html to retrieve message
// remove all html tags and reconstruct message with mentions?
extract_text_from_html($payload->object->content);
$reply = new \ActivityPub\Entities\Note([
'uri' => $payload->object->id,
'actor_id' => $payloadActor->id,
'in_reply_to_id' => $replyToNote->id,
'message' => $payload->object->content,
'published_at' => Time::parse(
$payload->object->published,
),
]);
$noteId = model('NoteModel')->addReply(
$reply,
true,
false,
);
model('ActivityModel')->update($activityId, [
'note_id' => service('uuid')
->fromBytes($noteId)
->getString(),
]);
return $this->response->setStatusCode(200)->setJSON([]);
default:
// return not handled undo error (501 = not implemented)
return $this->response->setStatusCode(501)->setJSON([]);
}
break;
case 'Delete':
$noteToDelete = model('NoteModel')->getNoteByUri(
$payload->object->id,
);
model('NoteModel')->removeNote($noteToDelete, false);
return $this->response->setStatusCode(200)->setJSON([]);
case 'Follow':
// add to followers table
model('FollowModel')->addFollower(
$payloadActor,
$this->actor,
false,
);
// Automatically accept follow by returning accept activity
accept_follow($this->actor, $payloadActor, $payload->id);
// TODO: return 202 (Accepted) followed!
return $this->response->setStatusCode(202)->setJSON([]);
case 'Like':
// get favourited note
$note = model('NoteModel')->getNoteByUri($payload->object);
// Like side-effect
model('FavouriteModel')->addFavourite(
$payloadActor,
$note,
false,
);
model('ActivityModel')->update($activityId, [
'note_id' => $note->id,
]);
return $this->response->setStatusCode(200)->setJSON([]);
case 'Announce':
$note = model('NoteModel')->getNoteByUri($payload->object);
model('ActivityModel')->update($activityId, [
'note_id' => $note->id,
]);
model('NoteModel')->reblog($payloadActor, $note, false);
return $this->response->setStatusCode(200)->setJSON([]);
case 'Undo':
// switch/case on the type of activity to undo
switch ($payload->object->type) {
case 'Follow':
// revert side-effect by removing follow from database
model('FollowModel')->removeFollower(
$payloadActor,
$this->actor,
false,
);
// TODO: undo has been accepted! (202 - Accepted)
return $this->response->setStatusCode(202)->setJSON([]);
case 'Like':
$note = model('NoteModel')->getNoteByUri(
$payload->object->object,
);
// revert side-effect by removing favourite from database
model('FavouriteModel')->removeFavourite(
$payloadActor,
$note,
false,
);
model('ActivityModel')->update($activityId, [
'note_id' => $note->id,
]);
return $this->response->setStatusCode(200)->setJSON([]);
case 'Announce':
$note = model('NoteModel')->getNoteByUri(
$payload->object->object,
);
$reblogNote = model('NoteModel')
->where([
'actor_id' => $payloadActor->id,
'reblog_of_id' => service('uuid')
->fromString($note->id)
->getBytes(),
])
->first();
model('NoteModel')->undoReblog($reblogNote, false);
model('ActivityModel')->update($activityId, [
'note_id' => $note->id,
]);
return $this->response->setStatusCode(200)->setJSON([]);
default:
// return not handled undo error (501 = not implemented)
return $this->response->setStatusCode(501)->setJSON([]);
}
default:
// return not handled activity error (501 = not implemented)
return $this->response->setStatusCode(501)->setJSON([]);
}
}
public function outbox()
{
// get published activities by publication date
$actorActivity = model('ActivityModel')
->where('actor_id', $this->actor->id)
->where('`created_at` <= NOW()', null, false)
->orderBy('created_at', 'DESC');
$pageNumber = $this->request->getGet('page');
if (!isset($pageNumber)) {
$actorActivity->paginate(12);
$pager = $actorActivity->pager;
$collection = new OrderedCollectionObject(null, $pager);
} else {
$paginatedActivity = $actorActivity->paginate(
12,
'default',
$pageNumber,
);
$pager = $actorActivity->pager;
$orderedItems = [];
foreach ($paginatedActivity as $activity) {
array_push($orderedItems, $activity->payload);
}
$collection = new OrderedCollectionPage($pager, $orderedItems);
}
return $this->response
->setContentType('application/activity+json')
->setBody($collection->toJSON());
}
public function followers()
{
// get followers for a specific actor
$followers = model('ActorModel')
->join(
'activitypub_follows',
'activitypub_follows.actor_id = id',
'inner',
)
->where('activitypub_follows.target_actor_id', $this->actor->id)
->orderBy('activitypub_follows.created_at', 'DESC');
$pageNumber = $this->request->getGet('page');
if (!isset($pageNumber)) {
$followers->paginate(12);
$pager = $followers->pager;
$followersCollection = new OrderedCollectionObject(null, $pager);
} else {
$paginatedFollowers = $followers->paginate(
12,
'default',
$pageNumber,
);
$pager = $followers->pager;
$orderedItems = [];
foreach ($paginatedFollowers as $follower) {
array_push($orderedItems, $follower->uri);
}
$followersCollection = new OrderedCollectionPage(
$pager,
$orderedItems,
);
}
return $this->response
->setContentType('application/activity+json')
->setBody($followersCollection->toJSON());
}
public function attemptFollow()
{
$rules = [
'handle' =>
'regex_match[/^@?(?P<username>[\w\.\-]+)@(?P<host>[\w\.\-]+)(?P<port>:[\d]+)?$/]',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
helper('text');
// get webfinger data from actor
// parse activityPub id to get actor and domain
// check if actor and domain exist
try {
if ($parts = split_handle($this->request->getPost('handle'))) {
extract($parts);
$data = get_webfinger_data($username, $domain);
}
} catch (\CodeIgniter\HTTP\Exceptions\HTTPException $e) {
return redirect()
->back()
->withInput()
->with('error', lang('ActivityPub.follow.accountNotFound'));
}
$ostatusKey = array_search(
'http://ostatus.org/schema/1.0/subscribe',
array_column($data->links, 'rel'),
);
if (!$ostatusKey) {
// TODO: error, couldn't subscribe to activitypub account
// The instance doesn't allow its users to follow others
return $this->response->setJSON([]);
}
return redirect()->to(
str_replace(
'{uri}',
urlencode($this->actor->uri),
$data->links[$ostatusKey]->template,
),
);
}
public function activity($activityId)
{
if (
!($activity = model('ActivityModel')->getActivityById($activityId))
) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return $this->response
->setContentType('application/activity+json')
->setBody(json_encode($activity->payload));
}
}