aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorwinter2025-01-13 18:06:05 +0000
committerwinter2025-01-13 18:06:05 +0000
commitfb9efd9f4682f528b1832622d192b9b5544584a9 (patch)
tree5d55870b8bb8910183fc7b35220e9ae3f83999d3
parent765bf9729cc31b7ff552922c6363373f218c1f24 (diff)
make the interaction buttons actually work
-rw-r--r--Digitigrade/Model/Instance.php5
-rw-r--r--Digitigrade/Model/Note.php4
-rw-r--r--Digitigrade/Model/PushableModel.php2
-rw-r--r--routes/note_fragment.php26
-rw-r--r--templates/note.php17
5 files changed, 51 insertions, 3 deletions
diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php
index 4242638..b3a179f 100644
--- a/Digitigrade/Model/Instance.php
+++ b/Digitigrade/Model/Instance.php
@@ -1,6 +1,7 @@
<?php
namespace Digitigrade\Model;
+use Digitigrade\GlobalConfig;
use Digitigrade\Job\RefreshOutboundAuthToken;
class Instance extends FetchableModel {
@@ -26,6 +27,10 @@ class Instance extends FetchableModel {
}
public static function findByHostname(string $host, bool $autoSave = true, bool $forceRefetch = false): ?self {
+ if ($host == GlobalConfig::getInstance()->getCanonicalHost()) {
+ // refuse to fetch ourselves
+ return null;
+ }
return self::findByUri("https://$host/.well-known/pawpub-instance", $autoSave, $forceRefetch);
}
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php
index 7389617..6d83a90 100644
--- a/Digitigrade/Model/Note.php
+++ b/Digitigrade/Model/Note.php
@@ -115,6 +115,10 @@ class Note extends PushableModel implements TimelineIncludeable {
return Interaction::findAllWithTarget($this);
}
+ public function getInteractionsWithAuthor(Actor $author): array {
+ return Interaction::findAllWhere('author = ? AND target = ? AND deleted = false', [$author->id, $this->id]);
+ }
+
protected function getRelevantServers(): array {
$recipientActors = match ($this->privacy->scope) {
NotePrivacyScope::NONE => [],
diff --git a/Digitigrade/Model/PushableModel.php b/Digitigrade/Model/PushableModel.php
index 9de3507..4df90c8 100644
--- a/Digitigrade/Model/PushableModel.php
+++ b/Digitigrade/Model/PushableModel.php
@@ -40,6 +40,8 @@ abstract class PushableModel extends FetchableModel implements \JsonSerializable
*/
public function publish() {
foreach ($this->getRelevantServers() as $instance) {
+ if ($instance == null) // really this should not happen but i'm lazy so it might
+ continue;
(new PushObject($this, $instance))->submit();
}
}
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) {
diff --git a/templates/note.php b/templates/note.php
index 83770aa..fdc0dc8 100644
--- a/templates/note.php
+++ b/templates/note.php
@@ -1,3 +1,14 @@
+<?php
+use Digitigrade\Model\InteractionKind;
+use Digitigrade\Model\UserAccount;
+
+$user = UserAccount::findByCurrentSession();
+$interKinds = [];
+if ($user != null) {
+ $interactions = $note->getInteractionsWithAuthor($user->actor);
+ $interKinds = array_map(fn($inter) => $inter->kind, $interactions);
+}
+?>
<article class="note">
<a href="/@/<?= $note->author->getFullHandle() ?>">
<?php call_template('actor_avatar', ['actor' => $note->author]); ?>
@@ -21,7 +32,11 @@
</div>
<div class="buttons">
<?php foreach (['like', 'dislike', 'reshare'] as $action) {
- render_template('interaction_button', ['action' => $action, 'note' => $note]);
+ render_template('interaction_button', [
+ 'action' => $action,
+ 'note' => $note,
+ 'active' => in_array(InteractionKind::from($action), $interKinds)
+ ]);
} ?>
</div>
</div>