[ENTITY][Note] Include reply_to's targets in child's

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-26 06:50:36 +00:00
parent f5f7fc6056
commit 5e42723624
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
6 changed files with 42 additions and 15 deletions

View File

@ -323,7 +323,7 @@ class Note extends Model
'content' => $object->getRendered(), 'content' => $object->getRendered(),
'attachment' => [], 'attachment' => [],
'tag' => [], '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(), 'inConversation' => $object->getConversationUri(),
'directMessage' => $object->getScope() === VisibilityScope::MESSAGE, 'directMessage' => $object->getScope() === VisibilityScope::MESSAGE,
]; ];

View File

@ -82,7 +82,7 @@ class Favourite extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} 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)) { if (!\array_key_exists('object', $ids_already_known)) {
$target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds(); $target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds();
@ -91,7 +91,7 @@ class Favourite extends Entity
} }
// Additional actors that should know about this // 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']); array_push($target_ids, ...$ids_already_known['additional']);
} else { } else {
return $target_ids; return $target_ids;

View File

@ -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)) { if (!\array_key_exists('object', $ids_already_known)) {
$target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds(); $target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds();
@ -99,7 +99,7 @@ class NoteRepeat extends Entity
} }
// Additional actors that should know about this // 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']); array_push($target_ids, ...$ids_already_known['additional']);
} else { } else {
return $target_ids; return $target_ids;

View File

@ -148,7 +148,7 @@ abstract class Entity
* *
* @return array of ids of Actors * @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 // Additional actors that should know about this
if (array_key_exists('additional', $ids_already_known)) { if (array_key_exists('additional', $ids_already_known)) {
@ -162,9 +162,9 @@ abstract class Entity
* *
* @return array of Actors * @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]); return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]);
} }
} }

View File

@ -153,7 +153,7 @@ class Activity extends Entity
* *
* @return array of ids of Actors * @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 = []; $target_ids = [];
@ -172,13 +172,24 @@ class Activity extends Entity
} }
// Object's targets // Object's targets
$object_included_already = false;
if (\array_key_exists('object', $ids_already_known)) { if (\array_key_exists('object', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['object']); array_push($target_ids, ...$ids_already_known['object']);
} else { } else {
if (!\is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) { if (!\is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) {
$target_ids[] = $this->getObject()->getActorId(); $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 // Additional actors that should know about this

View File

@ -344,7 +344,7 @@ class Note extends Entity
*/ */
public function getReplyToNote(): ?self 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; $this->object_mentions_ids = $mentions;
return $this; 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 ?? []; $target_ids = $this->object_mentions_ids ?? [];
if ($target_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 // 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']); array_push($target_ids, ...$ids_already_known['additional']);
} }
@ -414,9 +422,9 @@ class Note extends Entity
/** /**
* @return array of Actors * @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); $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id);
return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); 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']]); $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; return $mentioned;
} }