aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/note_fragment.php
diff options
context:
space:
mode:
Diffstat (limited to 'routes/note_fragment.php')
-rw-r--r--routes/note_fragment.php26
1 files changed, 24 insertions, 2 deletions
diff --git a/routes/note_fragment.php b/routes/note_fragment.php
index 3cb1fcc..9170e2b 100644
--- a/routes/note_fragment.php
+++ b/routes/note_fragment.php
@@ -1,13 +1,35 @@
<?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']);
- // TODO: actually like/dislike/reshare the note as the currently signed in user
- render_template('interaction_button', ['action' => $args['action'], 'active' => true, 'note' => $note]);
+ $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) {