aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/note_fragment.php
blob: 4d8c1a6ec9a36d3eba093f75811ce8e9678012c2 (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
<?php

use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\Model\Interaction;
use Digitigrade\Model\InteractionKind;
use Digitigrade\Model\Note;
use Digitigrade\Model\UserAccount;
use Digitigrade\Router;

Router::getInstance()->mount('/fragment/note/:id/:action', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $action = $args['action'];
    if (!(in_array($action, ['like', 'dislike', 'reshare']))) {
        throw new BadRequest("invalid action $action");
    }
    $note = Note::find($args['id']);
    $interaction = Interaction::findWhere('author = ? AND target = ? AND kind = ? AND deleted = false', [
        $user->actor->id, $note->id, $action
    ]);

    if ($interaction == null) {
        // we are adding a new interaction
        $interaction = Interaction::create($user->actor, InteractionKind::from($action), $note);
        $interaction->publish();
        $interaction->processTimelineAdditions();
        $active = true;
    } else {
        // we are removing the existing one
        $interaction->markDeleted();
        $active = false;
    }

    render_template('interaction_button', [
        'action' => $args['action'],
        'active' => $active,
        'note' => $note,
        'count' => $note->countInteractionsOfKind($interaction->kind)
    ]);
});

Router::getInstance()->mount('/fragment/note/:id/reply', function (array $args) {
    // replying to a note
    $author = UserAccount::requireByCurrentSession();
    $note = Note::find($args['id']);
    if (isset($_POST['content'])) {
        $reply = Note::create($author->actor, $_POST['content'], 'unk');
        $reply->inReplyTo = $note;
        $reply->threadApex = $note->threadApex ?? $note;
        $reply->save();
        $reply->publish();
        $reply->processTimelineAdditions();
    }
    render_template('reply_form', ['note' => $note]);
});

Router::getInstance()->mount('/fragment/note', function (array $args) {
    // posting a new note
    $author = UserAccount::requireByCurrentSession();
    if (isset($_POST['content'])) {
        // TODO: summary field, post language, privacy, etc
        $note = Note::create($author->actor, $_POST['content'], 'unk');
        $note->publish();
        $note->processTimelineAdditions();
    }
    render_template('write_note_form');
});