From 5e42723624aeab93ccc8a2b3c855344ffd0c35df Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Sun, 26 Dec 2021 06:50:36 +0000 Subject: [PATCH] [ENTITY][Note] Include reply_to's targets in child's --- plugins/ActivityPub/Util/Model/Note.php | 2 +- plugins/Favourite/Entity/Favourite.php | 4 ++-- plugins/RepeatNote/Entity/NoteRepeat.php | 4 ++-- src/Core/Entity.php | 6 +++--- src/Entity/Activity.php | 15 ++++++++++++-- src/Entity/Note.php | 26 +++++++++++++++++++----- 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index af8fd21813..8e1beb0825 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -323,7 +323,7 @@ class Note extends Model 'content' => $object->getRendered(), 'attachment' => [], 'tag' => [], - 'inReplyTo' => $object->getReplyTo() === null ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())), + 'inReplyTo' => is_null($object->getReplyTo()) ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())), 'inConversation' => $object->getConversationUri(), 'directMessage' => $object->getScope() === VisibilityScope::MESSAGE, ]; diff --git a/plugins/Favourite/Entity/Favourite.php b/plugins/Favourite/Entity/Favourite.php index b90f5dbe5d..86a6c1d5c5 100644 --- a/plugins/Favourite/Entity/Favourite.php +++ b/plugins/Favourite/Entity/Favourite.php @@ -82,7 +82,7 @@ class Favourite extends Entity // @codeCoverageIgnoreEnd // }}} Autocode - public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { if (!\array_key_exists('object', $ids_already_known)) { $target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds(); @@ -91,7 +91,7 @@ class Favourite extends Entity } // Additional actors that should know about this - if (\array_key_exists('additional', $ids_already_known)) { + if ($include_additional && \array_key_exists('additional', $ids_already_known)) { array_push($target_ids, ...$ids_already_known['additional']); } else { return $target_ids; diff --git a/plugins/RepeatNote/Entity/NoteRepeat.php b/plugins/RepeatNote/Entity/NoteRepeat.php index 8052e615cc..00098f0f77 100644 --- a/plugins/RepeatNote/Entity/NoteRepeat.php +++ b/plugins/RepeatNote/Entity/NoteRepeat.php @@ -90,7 +90,7 @@ class NoteRepeat extends Entity ); } - public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { if (!\array_key_exists('object', $ids_already_known)) { $target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds(); @@ -99,7 +99,7 @@ class NoteRepeat extends Entity } // Additional actors that should know about this - if (\array_key_exists('additional', $ids_already_known)) { + if ($include_additional && \array_key_exists('additional', $ids_already_known)) { array_push($target_ids, ...$ids_already_known['additional']); } else { return $target_ids; diff --git a/src/Core/Entity.php b/src/Core/Entity.php index f9898b386e..a3fb636f38 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -148,7 +148,7 @@ abstract class Entity * * @return array of ids of Actors */ - public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { // Additional actors that should know about this if (array_key_exists('additional', $ids_already_known)) { @@ -162,9 +162,9 @@ abstract class Entity * * @return array of Actors */ - public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { - $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id); + $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id, $include_additional); return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); } } diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index 6e80d31129..7cb6da654a 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -153,7 +153,7 @@ class Activity extends Entity * * @return array of ids of Actors */ - public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { $target_ids = []; @@ -172,13 +172,24 @@ class Activity extends Entity } // Object's targets + $object_included_already = false; if (\array_key_exists('object', $ids_already_known)) { array_push($target_ids, ...$ids_already_known['object']); } else { if (!\is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) { $target_ids[] = $this->getObject()->getActorId(); } - array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known)); + array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known, include_additional: false)); + $object_included_already = true; + } + + // Object's related targets + if (\array_key_exists('object-related', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['object-related']); + } else { + if (!$object_included_already) { + array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known, include_additional: false)); + } } // Additional actors that should know about this diff --git a/src/Entity/Note.php b/src/Entity/Note.php index be8b6528a9..3dc2bdfe68 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -344,7 +344,7 @@ class Note extends Entity */ public function getReplyToNote(): ?self { - return self::getByPK($this->getReplyTo()); + return is_null($this->getReplyTo()) ? null : self::getById($this->getReplyTo()); } /** @@ -387,7 +387,7 @@ class Note extends Entity $this->object_mentions_ids = $mentions; return $this; } - public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { $target_ids = $this->object_mentions_ids ?? []; if ($target_ids === []) { @@ -403,8 +403,16 @@ class Note extends Entity } } + if (!\array_key_exists('object-related', $ids_already_known)) { + if (!is_null($parent = $this->getReplyToNote())) { + array_push($target_ids, ...$parent->getNotificationTargetIds()); + } + } else { + array_push($target_ids, ...$ids_already_known['object-related']); + } + // Additional actors that should know about this - if (\array_key_exists('additional', $ids_already_known)) { + if ($include_additional && \array_key_exists('additional', $ids_already_known)) { array_push($target_ids, ...$ids_already_known['additional']); } @@ -414,9 +422,9 @@ class Note extends Entity /** * @return array of Actors */ - public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array { - if (\array_key_exists('additional', $ids_already_known)) { + if ($include_additional && \array_key_exists('additional', $ids_already_known)) { $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id); return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); } @@ -433,6 +441,14 @@ class Note extends Entity $mentioned = $ids_already_known['object'] === [] ? [] : DB::findBy('actor', ['id' => $ids_already_known['object']]); } + if (!\array_key_exists('object-related', $ids_already_known)) { + if (!is_null($parent = $this->getReplyToNote())) { + array_push($mentioned, ...$parent->getNotificationTargets()); + } + } else { + array_push($mentioned, ...$ids_already_known['object-related']); + } + return $mentioned; }