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

use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\HttpResponseStatus\Forbidden;
use Digitigrade\Model\Actor;
use Digitigrade\Model\Interaction;
use Digitigrade\Model\InteractionKind;
use Digitigrade\Model\Note;
use Digitigrade\Model\NoteAttachment;
use Digitigrade\Model\NotePrivacyScope;
use Digitigrade\Model\StorageObject;
use Digitigrade\Model\UserAccount;
use Digitigrade\Router;
use Digitigrade\StorageProvider;
use Digitigrade\StorageProvider\FilesystemStorage;

Router::getInstance()->mount('/fragment/note/:id', function (array $args) {
    if ($_SERVER['REQUEST_METHOD'] != 'DELETE') {
        throw new RuntimeException('method not implemented');
    }
    // assuming we're deleting the note
    $user = UserAccount::requireByCurrentSession();
    $note = Note::find($args['id']);
    if ($note->author != $user->actor && !$user->isAdmin) {
        throw new Forbidden('you may not delete that note!');
    }
    if (!$note->author->isLocal) {
        throw new BadRequest("can't delete a non-local note");
    }

    $note->markDeleted();
    // no need to return any template because it's just gone
});

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,
            NotePrivacyScope::from($_POST['scope'])
        );
        $reply->inReplyTo = $note;
        $reply->threadApex = $note->threadApex ?? $note;
        foreach (($_POST['mentions'] ?? []) as $uid) {
            $reply->mentions[] = Actor::find($uid);
        }
        $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'])
        );
        foreach (($_POST['mentions'] ?? []) as $uid) {
            $note->mentions[] = Actor::find($uid);
        }
        $index = 0;
        $attachments = $_POST['attachments'] ?? [];
        $altTexts = $_POST['altTexts'] ?? [];
        for ($i = 0; $i < count($attachments); $i++) {
            $obj = StorageObject::findByOid($attachments[$i]);
            $note->attachments[] = NoteAttachment::fromStorageObject($obj, $index++, $altTexts[$i] ?: null);
        }
        $note->save();
        $note->publish();
        $note->processTimelineAdditions();
        $note->processNotifications();
    }
    render_template('write_note_form', ['actor' => $author->actor]);
});

Router::getInstance()->mount('/fragment/note/mentionPill', function (array $args) {
    // adding a mention to a new post
    if (isset($_GET['handle'])) {
        // look up and return a pill for the corresponding actor
        $actor = Actor::findLocalByHandle($_GET['handle']) ?? Actor::findByWebfinger($_GET['handle']);
        if ($actor == null) {
            // too bad
            render_template('user_mention_pill', ['type' => 'edit', 'invalid' => true, 'value' => $_GET['handle']]);
        } else {
            render_template('user_mention_pill', ['type' => 'user', 'actor' => $actor]);
            render_template('user_mention_pill', ['type' => 'add']);
        }
    } else {
        // start editing
        render_template('user_mention_pill', ['type' => 'edit']);
    }
});

Router::getInstance()->mount('/fragment/note/attachmentPill', function (array $args) {
    // adding an attachment to a new post
    if (isset($_FILES['file'])) {
        // store the file and return a pill for it
        // TODO: allow other providers
        $obj = StorageObject::createFromUpload(FilesystemStorage::class, $_FILES['file']);
        $type = str_starts_with($obj->mimetype, 'image/') ? 'image' : 'file';
        render_template('attachment_pill', [
            'type' => $type,
            'oid' => $obj->oid,
            'name' => $obj->filename,
            'href' => $obj->getUrl()
        ]);
    }
});