aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/note_fragment.php
blob: 9170e2b45250b42322e6f37b78b60a842316cde7 (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
<?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
        $newint = Interaction::create($user->actor, InteractionKind::from($action), $note);
        $newint->publish();
        $active = true;
    } else {
        // we are removing the existing one
        $interaction->markDeleted();
        $active = false;
    }

    render_template('interaction_button', ['action' => $args['action'], 'active' => $active, '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();
    }
    render_template('write_note_form');
});