diff --git a/src/Core/Entity.php b/src/Core/Entity.php index c060c130a7..f9898b386e 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -144,10 +144,27 @@ abstract class Entity } /** - * @return array of Actors + * Who should be notified about this object? + * + * @return array of ids of Actors */ - public function getNotificationTargets(array $ids_already_known = []): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array { + // Additional actors that should know about this + if (array_key_exists('additional', $ids_already_known)) { + return $ids_already_known['additional']; + } return []; } + + /** + * Who should be notified about this object? + * + * @return array of Actors + */ + public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array + { + $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id); + return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); + } } diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index d83e49d763..015ce7cea5 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -154,10 +154,11 @@ class Activity extends Entity } /** + * Who should be notified about this object? * - * @return array of Actors + * @return array of ids of Actors */ - public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array { $target_ids = []; @@ -182,7 +183,7 @@ class Activity extends Entity if (!is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) { $target_ids[] = $this->getObject()->getActorId(); } - array_push($target_ids, ...$this->getObject()->getNotificationTargets($ids_already_known)); + array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known)); } // Additional actors that should know about this @@ -190,8 +191,7 @@ class Activity extends Entity array_push($target_ids, ...$ids_already_known['additional']); } - $target_ids = array_unique($target_ids); - return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); + return array_unique($target_ids); } public static function schemaDef(): array diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 0efae67759..6a4e247ed1 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -28,6 +28,7 @@ use App\Core\DB\DB; use App\Core\Entity; use App\Core\Event; use App\Core\VisibilityScope; +use App\Util\Formatting; use Component\Avatar\Avatar; use DateTimeInterface; @@ -347,26 +348,52 @@ class Note extends Entity } /** - * Find all mentioned actors in this note * - * @TODO: Seems rather inneficient to be rendering just for this + * @return array of ids of Actors */ - public function getNotificationTargets(array $ids_already_known = []): array + public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array { - $rendered = null; - $mentions = []; - Event::handle('RenderNoteContent', [$this->getContent(), - $this->getContentType(), - &$rendered, - $this->getActor(), - \is_null($this->getLanguageId()) ? null : Language::getById($this->getLanguageId())->getLocale(), - &$mentions, ]); - $mentioned = []; - foreach ($mentions as $mention) { - foreach ($mention['mentioned'] as $m) { - $mentioned[] = $m; + $target_ids = []; + if (!array_key_exists('object', $ids_already_known)) { + $mentions = Formatting::findMentions($this->getContent(), $this->getActor()); + foreach ($mentions as $mention) { + foreach ($mention['mentioned'] as $m) { + $target_ids[] = $m->getId(); + } } } + + // Additional actors that should know about this + if (array_key_exists('additional', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['additional']); + } + + return array_unique($target_ids); + } + + /** + * + * @return array of Actors + */ + public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array + { + if (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]); + } + + $mentioned = []; + if (!array_key_exists('object', $ids_already_known)) { + $mentions = Formatting::findMentions($this->getContent(), $this->getActor()); + foreach ($mentions as $mention) { + foreach ($mention['mentioned'] as $m) { + $mentioned[] = $m; + } + } + } else { + $mentioned = $ids_already_known['object'] === [] ? [] : DB::findBy('actor', ['id' => $ids_already_known['object']]); + } + return $mentioned; }