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

use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\Model\Interaction;
use Digitigrade\Model\InteractionKind;
use Digitigrade\Model\Note;
use Digitigrade\Model\NotePrivacyScope;
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();
        $interaction->processNotifications();
        $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']) && strlen(trim($_POST['content'])) > 0) {
        $summary = trim($_POST['summary'] ?? '');
        $summary = $summary == '' ? null : $summary;
        $reply = Note::create(
            $author->actor,
            trim($_POST['content']),
            'unk',
            $summary,
            $note->privacy->scope
        );
        $reply->inReplyTo = $note;
        $reply->threadApex = $note->threadApex ?? $note;
        // TODO: do mentions properly and not just like this
        $reply->mentions = [$note->author];
        $reply->save();
        $reply->publish();
        $reply->processTimelineAdditions();
        $reply->processNotifications();
    }
    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']) && strlen(trim($_POST['content'])) > 0) {
        // TODO: post language, other privacy options, etc
        $summary = trim($_POST['summary'] ?? '');
        $summary = $summary == '' ? null : $summary;
        $note = Note::create(
            $author->actor,
            trim($_POST['content']),
            'unk',
            $summary,
            NotePrivacyScope::from($_POST['scope'])
        );
        $note->publish();
        $note->processTimelineAdditions();
        $note->processNotifications();
    }
    render_template('write_note_form', ['actor' => $author->actor]);
});