aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/actor.php
blob: d453a11d1aa200eca5886e75fc94eea757ea5432 (plain)
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
<?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/:handle', function (array $args) {
    $actor = Actor::findLocalByHandle($args['handle']);
    if ($actor == null || $actor->deleted) {
        throw new NotFound("i don't know any local actor called " . $args['handle']);
    }
    if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'text/html')) {
        throw new TemporaryRedirect("/@/$actor->handle");
    }
    json_response($actor);
});

Router::getInstance()->mount('/actor/:handle/basicFeed', function (array $args) {
    $actor = Actor::findLocalByHandle($args['handle']);
    $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/:handle/fullFeed', function (array $args) {
    $actor = Actor::findLocalByHandle($args['handle']);
    $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/:handle/follow', function (array $args) {
    $target = Actor::findLocalByHandle($args['handle']);
    if ($target == null) {
        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/:handle/unfollow', function (array $args) {
    $target = Actor::findLocalByHandle($args['handle']);
    if ($target == null) {
        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/:handle/acceptedFollow', function (array $args) {
    $initiator = Actor::findLocalByHandle($args['handle']);
    if ($initiator == null) {
        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/:handle/rejectedFollow', function (array $args) {
    $initiator = Actor::findLocalByHandle($args['handle']);
    if ($initiator == null) {
        throw new NotFound();
    }
    $target = Actor::findByRequestHeaders();
    if ($target == null) {
        throw new Unauthorized('please authenticate yourself on behalf of the followed actor');
    }
    $target->rejectPendingFollowFrom($initiator);
});