From 56b8710b269915c739372cb52e47bfcce859db37 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Wed, 23 Mar 2022 13:18:44 +0000 Subject: [PATCH] [PLUGIN][ActivityPub][Notification] Fix some issues with targetting --- plugins/ActivityPub/ActivityPub.php | 8 ++++---- plugins/ActivityPub/Entity/ActivitypubObject.php | 10 ++++++++++ plugins/Favourite/Controller/Favourite.php | 4 ++-- plugins/Favourite/Entity/NoteFavourite.php | 10 +++++----- plugins/RepeatNote/Controller/Repeat.php | 4 ++-- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/plugins/ActivityPub/ActivityPub.php b/plugins/ActivityPub/ActivityPub.php index e11a6ada97..f54b2f1e8b 100644 --- a/plugins/ActivityPub/ActivityPub.php +++ b/plugins/ActivityPub/ActivityPub.php @@ -143,9 +143,9 @@ class ActivityPub extends Plugin ); DB::flush(); - if ($ap_act->getToNotifyTargets() !== []) { - if (Event::handle('ActivityPubNewNotification', [$actor, $ap_act->getActivity(), $ap_act->getAttentionTargets(), _m('{actor_id} triggered a notification via ActivityPub.', ['{actor_id}' => $actor->getId()])]) === Event::next) { - Event::handle('NewNotification', [$actor, $ap_act->getActivity(), $ap_act->getAttentionTargets(), _m('{actor_id} triggered a notification via ActivityPub.', ['{nickname}' => $actor->getId()])]); + if (($att_targets = $ap_act->getAttentionTargets()) !== []) { + if (Event::handle('ActivityPubNewNotification', [$actor, ($act = $ap_act->getActivity()), $att_targets, _m('{actor_id} triggered a notification via ActivityPub.', ['{actor_id}' => $actor->getId()])]) === Event::next) { + Event::handle('NewNotification', [$actor, $act, $att_targets, _m('{actor_id} triggered a notification via ActivityPub.', ['{nickname}' => $actor->getId()])]); } } @@ -468,7 +468,7 @@ class ActivityPub extends Plugin try { if (FreeNetworkActorProtocol::canIAddr('activitypub', $addr = Discovery::normalize($target))) { $ap_actor = DB::wrapInTransaction(fn () => ActivitypubActor::getByAddr($addr)); - $actor = Actor::getById($ap_actor->getActorId()); + $actor = Actor::getById($ap_actor->getActorId()); FreeNetworkActorProtocol::protocolSucceeded('activitypub', $actor->getId(), $addr); return Event::stop; } else { diff --git a/plugins/ActivityPub/Entity/ActivitypubObject.php b/plugins/ActivityPub/Entity/ActivitypubObject.php index 7a24ffd5b8..5ad0f82e14 100644 --- a/plugins/ActivityPub/Entity/ActivitypubObject.php +++ b/plugins/ActivityPub/Entity/ActivitypubObject.php @@ -115,6 +115,16 @@ class ActivitypubObject extends Entity return DB::findOneBy($this->getObjectType(), ['id' => $this->getObjectId()]); } + public function getAttentionTargetIds(): array + { + return $this->getObject()->getAttentionTargetIds(); + } + + public function getAttentionTargets(): array + { + return $this->getObject()->getAttentionTargets(); + } + public static function schemaDef(): array { return [ diff --git a/plugins/Favourite/Controller/Favourite.php b/plugins/Favourite/Controller/Favourite.php index bd2bb4f549..c4abb9f985 100644 --- a/plugins/Favourite/Controller/Favourite.php +++ b/plugins/Favourite/Controller/Favourite.php @@ -78,7 +78,7 @@ class Favourite extends FeedController if ($form_add_to_favourite->isSubmitted()) { if (!\is_null($activity = \Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) { DB::flush(); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{actor_id} favoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, $activity->getAttentionTargets(), _m('{actor_id} favoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]); } else { throw new ClientException(_m('Note already favoured!')); } @@ -138,7 +138,7 @@ class Favourite extends FeedController if ($form_remove_favourite->isSubmitted()) { if (!\is_null($activity = \Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) { DB::flush(); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{actor_id} unfavoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, $activity->getAttentionTargets(), _m('{actor_id} unfavoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]); } else { throw new ClientException(_m('Note already unfavoured!')); } diff --git a/plugins/Favourite/Entity/NoteFavourite.php b/plugins/Favourite/Entity/NoteFavourite.php index e6bd0806c9..d28df12655 100644 --- a/plugins/Favourite/Entity/NoteFavourite.php +++ b/plugins/Favourite/Entity/NoteFavourite.php @@ -110,11 +110,11 @@ class NoteFavourite extends Entity self::cacheKeys($note)['favourites-actors'], fn () => DB::dql( <<<'EOF' - select a from actor a - inner join note_favourite nf - with a.id = nf.actor_id - where nf.note_id = :note_id - order by nf.created DESC + SELECT a FROM actor AS a + INNER JOIN note_favourite AS nf + WITH a.id = nf.actor_id + WHERE nf.note_id = :note_id + ORDER BY nf.created DESC EOF, ['note_id' => $note->getId()], ), diff --git a/plugins/RepeatNote/Controller/Repeat.php b/plugins/RepeatNote/Controller/Repeat.php index 9f15b05f79..56e4c93e7f 100644 --- a/plugins/RepeatNote/Controller/Repeat.php +++ b/plugins/RepeatNote/Controller/Repeat.php @@ -75,7 +75,7 @@ class Repeat extends Controller if ($form_add_to_repeat->isSubmitted()) { $repeat_activity = \Plugin\RepeatNote\RepeatNote::repeatNote(note: $note, actor_id: $actor_id); DB::flush(); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $repeat_activity->getObjectId()])]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, $repeat_activity->getAttentionTargets(), _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $repeat_activity->getObjectId()])]); // Redirect user to where they came from // Prevent open redirect @@ -132,7 +132,7 @@ class Repeat extends Controller if ($form_remove_repeat->isSubmitted()) { if (!\is_null($undo_repeat_activity = \Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $note_id, actor_id: $actor_id))) { DB::flush(); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $note_id])]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, $undo_repeat_activity->getAttentionTargets(), _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $note_id])]); } else { throw new ClientException(_m('Note wasn\'t repeated!')); }