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
|
<?php
use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\HttpResponseStatus\TemporaryRedirect;
use Digitigrade\HttpResponseStatus\Unauthorized;
use Digitigrade\Model\Actor;
use Digitigrade\Model\Note;
use Digitigrade\Router;
Router::getInstance()->mount('/actor/:id', function (array $args) {
$actor = Actor::find($args['id']);
if ($actor == null || $actor->deleted) {
throw new NotFound("i don't know that actor");
}
if (!$actor->isLocal) {
throw new NotFound("that actor is not local!");
}
if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'text/html')) {
throw new TemporaryRedirect("/@/$actor->handle");
}
json_response($actor);
});
Router::getInstance()->mount('/actor/:id/basicFeed', function (array $args) {
$actor = Actor::find($args['id']);
$notes = Note::findAllWithAuthor($actor);
// TODO: implement pagination
json_response([
'page' => 1,
'totalPages' => 1,
'nextPage' => null,
'previousPage' => null,
'items' => array_map(function (Note $note) {
return $note->uri;
}, $notes)
]);
});
Router::getInstance()->mount('/actor/:id/fullFeed', function (array $args) {
$actor = Actor::find($args['id']);
$notes = Note::findAllWithAuthor($actor);
// TODO: implement pagination here as well
json_response([
'page' => 1,
'totalPages' => 1,
'nextPage' => null,
'previousPage' => null,
'items' => $notes
]);
});
Router::getInstance()->mount('/actor/:id/follow', function (array $args) {
$target = Actor::find($args['id']);
if ($target == null || !$target->isLocal || $target->deleted) {
throw new NotFound();
}
$initiator = Actor::findByRequestHeaders();
if ($initiator == null) {
throw new Unauthorized('please authenticate yourself on behalf of the initiating actor');
}
$initiator->follow($target);
});
Router::getInstance()->mount('/actor/:id/unfollow', function (array $args) {
$target = Actor::find($args['id']);
if ($target == null || !$target->isLocal || $target->deleted) {
throw new NotFound();
}
$initiator = Actor::findByRequestHeaders();
if ($initiator == null) {
throw new Unauthorized('please authenticate yourself on behalf of the initiating actor');
}
$initiator->unfollow($target);
});
Router::getInstance()->mount('/actor/:id/acceptedFollow', function (array $args) {
$initiator = Actor::find($args['id']);
if ($initiator == null || !$initiator->isLocal || $initiator->deleted) {
throw new NotFound();
}
$target = Actor::findByRequestHeaders();
if ($target == null) {
throw new Unauthorized('please authenticate yourself on behalf of the followed actor');
}
$target->acceptPendingFollowFrom($initiator);
});
Router::getInstance()->mount('/actor/:id/rejectedFollow', function (array $args) {
$initiator = Actor::find($args['id']);
if ($initiator == null || !$initiator->isLocal || $initiator->deleted) {
throw new NotFound();
}
$target = Actor::findByRequestHeaders();
if ($target == null) {
throw new Unauthorized('please authenticate yourself on behalf of the followed actor');
}
$target->rejectPendingFollowFrom($initiator);
});
|