[COMPONENT][Notification] Do not re-render content just to grab attentions

Other minor improvements and bug fixes
This commit is contained in:
Diogo Peralta Cordeiro 2021-12-21 16:04:50 +00:00
parent e2c0505620
commit 8b5286c383
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 66 additions and 22 deletions

View File

@ -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]);
}
}

View File

@ -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

View File

@ -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, ]);
$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;
}