[COMPONENT][Notification] Make logic more generic and robust

Fixed various bugs

Some important concepts to bear in mind:

* Notification: Associated with activities, won't be reconstructed
together with objects, can be thought of as transient

* Attention: Associated with objects, will be reconstructed with them, can
be thought as persistent

* Notifications and Attentions have no direct implications.

* Mentions are a specific form of attentions in notes, leads to the creation of Attentions.

Finally,

Potential PHP issue detected and reported: https://github.com/php/php-src/issues/8199
`static::method()` from a non static context (such as a class method) calls `__call`, rather than
the expected `__callStatic`. Can be fixed by using `(static fn() => static::method())()`, but the
usage of the magic method is strictly unnecessary in this case.
This commit is contained in:
Diogo Peralta Cordeiro 2022-03-13 18:23:19 +00:00
parent e1cceac150
commit 888c3798b7
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
32 changed files with 438 additions and 494 deletions

View File

@ -145,7 +145,7 @@ class Post extends Controller
content_type: $content_type, content_type: $content_type,
locale: $data['language'], locale: $data['language'],
scope: VisibilityScope::from($data['visibility']), scope: VisibilityScope::from($data['visibility']),
targets: [(int) $data['in']], attentions: [(int) $data['in']],
reply_to: $data['reply_to_id'], reply_to: $data['reply_to_id'],
attachments: $data['attachments'], attachments: $data['attachments'],
process_note_content_extra_args: $extra_args, process_note_content_extra_args: $extra_args,

View File

@ -51,12 +51,17 @@ class Group extends Component
* Enqueues a notification for an Actor (such as person or group) which means * Enqueues a notification for an Actor (such as person or group) which means
* it shows up in their home feed and such. * it shows up in their home feed and such.
*/ */
public function onNewNotificationWithTargets(Actor $sender, Activity $activity, array $targets = [], ?string $reason = null): bool public function onNewNotificationStart(Actor $sender, Activity $activity, array $targets = [], ?string $reason = null): bool
{ {
foreach ($targets as $target) { foreach ($targets as $target) {
if ($target->isGroup()) { if ($target->isGroup()) {
// The Group announces to its subscribers // The Group announces to its subscribers
Notification::notify($target, $activity, $target->getSubscribers(), $reason); Notification::notify(
sender: $target,
activity: $activity,
targets: $target->getSubscribers(),
reason: $reason,
);
} }
} }

View File

@ -53,9 +53,9 @@ class Feed extends Controller
WHERE n.id IN ( WHERE n.id IN (
SELECT act.object_id FROM \App\Entity\Activity AS act SELECT act.object_id FROM \App\Entity\Activity AS act
WHERE act.object_type = 'note' AND act.id IN WHERE act.object_type = 'note' AND act.id IN
(SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :id) (SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :target_id)
) )
EOF, ['id' => $user->getId()]); EOF, [':target' => $user->getId()]);
return [ return [
'_template' => 'collection/notes.html.twig', '_template' => 'collection/notes.html.twig',
'page_title' => _m('Notifications'), 'page_title' => _m('Notifications'),

View File

@ -24,11 +24,21 @@ namespace Component\Notification\Entity;
use App\Core\Entity; use App\Core\Entity;
/** /**
* Entity for note attentions * Entity for object attentions
*
* An attention is a form of persistent notification.
* It exists together and for as long as the object it belongs to.
* Creating an attention requires creating a Notification.
* *
* @category DB * @category DB
* @package GNUsocial * @package GNUsocial
* *
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet Inc.
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
* @author Diogo Peralta Cordeiro <@diogo.site> * @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2022 Free Software Foundation, Inc http://www.fsf.org * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
@ -37,18 +47,30 @@ class Attention extends Entity
{ {
// {{{ Autocode // {{{ Autocode
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $note_id; private string $object_type;
private int $object_id;
private int $target_id; private int $target_id;
public function setNoteId(int $note_id): self public function setObjectType(string $object_type): self
{ {
$this->note_id = $note_id; $this->object_type = mb_substr($object_type, 0, 32);
return $this; return $this;
} }
public function getNoteId(): int public function getObjectType(): string
{ {
return $this->note_id; return $this->object_type;
}
public function setObjectId(int $object_id): self
{
$this->object_id = $object_id;
return $this;
}
public function getObjectId(): int
{
return $this->object_id;
} }
public function setTargetId(int $target_id): self public function setTargetId(int $target_id): self
@ -68,15 +90,16 @@ class Attention extends Entity
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [
'name' => 'note_attention', 'name' => 'attention',
'description' => 'Note attentions to actors (that are not a mention)', 'description' => 'Attentions to actors (these are not mentions)',
'fields' => [ 'fields' => [
'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'note_id to give attention'], 'object_type' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'the name of the table this object refers to'],
'target_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'], 'object_id' => ['type' => 'int', 'not null' => true, 'description' => 'id in the referenced table'],
'target_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'],
], ],
'primary key' => ['note_id', 'target_id'], 'primary key' => ['object_type', 'object_id', 'target_id'],
'indexes' => [ 'indexes' => [
'attention_note_id_idx' => ['note_id'], 'attention_object_id_idx' => ['object_id'],
'attention_target_id_idx' => ['target_id'], 'attention_target_id_idx' => ['target_id'],
], ],
]; ];

View File

@ -28,17 +28,17 @@ use App\Entity\Actor;
use DateTimeInterface; use DateTimeInterface;
/** /**
* Entity for attentions * Entity for Notifications
*
* A Notification when isolated is a form of transient notification.
* When together with a persistent form of notification such as attentions or mentions,
* it records that the target was notified - which avoids re-notifying upon objects reconstructions.
* *
* @category DB * @category DB
* @package GNUsocial * @package GNUsocial
* *
* @author Zach Copley <zach@status.net> * @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2010 StatusNet Inc. * @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class Notification extends Entity class Notification extends Entity

View File

@ -64,14 +64,43 @@ class Notification extends Component
/** /**
* Enqueues a notification for an Actor (such as person or group) which means * Enqueues a notification for an Actor (such as person or group) which means
* it shows up in their home feed and such. * it shows up in their home feed and such.
* WARNING: It's highly advisable to have flushed any relevant objects before triggering this event.
* OBSERVATION: $sender->getSubscribers() will always be pre-included, thus why $targets=[] is normal
*
* $targets should be of the shape:
* ['source' => (int|Actor)[]] // Prefer Actor whenever possible
* Example of $targets:
* [[42, $actor_alice, $actor_bob]] // Avoid repeating actors or ids
*
* @param Actor $sender The one responsible for this activity, take care not to include it in targets
* @param Activity $activity The activity responsible for the object being given to known to targets
* @param array $targets attentions, Mentions, any other source
* @param null|string $reason An optional reason explaining why this notification exists
*/ */
public function onNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool public function onNewNotification(Actor $sender, Activity $activity, array $targets = [], ?string $reason = null): bool
{ {
$targets = $activity->getNotificationTargets(ids_already_known: $ids_already_known, sender_id: $sender->getId()); // Ensure targets are all actor objects and unique
if (Event::handle('NewNotificationWithTargets', [$sender, $activity, $targets, $reason]) === Event::next) { $effective_targets = [];
self::notify($sender, $activity, $targets, $reason); foreach ($targets as $target) {
if (\is_int($target)) {
$target_id = $target;
$target_object = null;
} else {
$target_id = $target->getId();
$target_object = $target;
}
if (!\array_key_exists(key: $target_id, array: $effective_targets)) {
$target_object ??= Actor::getById($target_id);
$effective_targets[$target_id] = $target_object;
}
}
unset($targets);
if (Event::handle('NewNotificationStart', [$sender, $activity, $effective_targets, $reason]) === Event::next) {
self::notify($sender, $activity, $effective_targets, $reason);
} }
Event::handle('NewNotificationEnd', [$sender, $activity, $effective_targets, $reason]);
return Event::next; return Event::next;
} }
@ -91,7 +120,8 @@ class Notification extends Component
} }
/** /**
* Bring given Activity to Targets's attention * Bring given Activity to Targets' knowledge.
* This will flush a Notification to DB.
* *
* @return true if successful, false otherwise * @return true if successful, false otherwise
*/ */
@ -100,8 +130,8 @@ class Notification extends Component
$remote_targets = []; $remote_targets = [];
foreach ($targets as $target) { foreach ($targets as $target) {
if ($target->getIsLocal()) { if ($target->getIsLocal()) {
if ($target->hasBlocked($activity->getActor())) { if ($target->hasBlocked($author = $activity->getActor())) {
Log::info("Not saving reply to actor {$target->getId()} from sender {$sender->getId()} because of a block."); Log::info("Not saving notification to actor {$target->getId()} from sender {$sender->getId()} because receiver blocked author {$author->getId()}.");
continue; continue;
} }
if (Event::handle('NewNotificationShould', [$activity, $target]) === Event::next) { if (Event::handle('NewNotificationShould', [$activity, $target]) === Event::next) {
@ -113,7 +143,7 @@ class Notification extends Component
} }
Queue::enqueue( Queue::enqueue(
payload: [$sender, $activity, $target, $reason], payload: [$sender, $activity, $target, $reason],
queue: 'notification_local', queue: 'NotificationLocal',
priority: true, priority: true,
); );
} else { } else {
@ -124,7 +154,7 @@ class Notification extends Component
} }
// XXX: Unideal as in failures the rollback will leave behind a false notification, // XXX: Unideal as in failures the rollback will leave behind a false notification,
// but most notifications (all) require flushing the objects first // but most notifications (all) require flushing the objects first
// Should be okay as long as implementors bear this in mind // Should be okay as long as implementations bear this in mind
try { try {
DB::wrapInTransaction(fn () => DB::persist(Entity\Notification::create([ DB::wrapInTransaction(fn () => DB::persist(Entity\Notification::create([
'activity_id' => $activity->getId(), 'activity_id' => $activity->getId(),
@ -132,7 +162,7 @@ class Notification extends Component
'reason' => $reason, 'reason' => $reason,
]))); ])));
} catch (Exception|Throwable $e) { } catch (Exception|Throwable $e) {
// We do our best not to record duplicated notifications, but it's not insane that can happen // We do our best not to record duplicate notifications, but it's not insane that can happen
Log::error('It was attempted to record an invalid notification!', [$e]); Log::error('It was attempted to record an invalid notification!', [$e]);
} }
} }
@ -140,7 +170,7 @@ class Notification extends Component
if ($remote_targets !== []) { if ($remote_targets !== []) {
Queue::enqueue( Queue::enqueue(
payload: [$sender, $activity, $remote_targets, $reason], payload: [$sender, $activity, $remote_targets, $reason],
queue: 'notification_remote', queue: 'NotificationRemote',
priority: false, priority: false,
); );
} }

View File

@ -52,7 +52,7 @@ class Posting extends Controller
content_type: $data['content_type'], content_type: $data['content_type'],
locale: $data['language'], locale: $data['language'],
scope: VisibilityScope::from($data['visibility']), scope: VisibilityScope::from($data['visibility']),
targets: isset($target) ? [$target] : [], attentions: isset($target) ? [$target] : [],
reply_to: \array_key_exists('reply_to_id', $data) ? $data['reply_to_id'] : null, reply_to: \array_key_exists('reply_to_id', $data) ? $data['reply_to_id'] : null,
attachments: $data['attachments'], attachments: $data['attachments'],
process_note_content_extra_args: $extra_args, process_note_content_extra_args: $extra_args,
@ -61,9 +61,9 @@ class Posting extends Controller
return Core\Form::forceRedirect($form, $request); return Core\Form::forceRedirect($form, $request);
} }
} catch (FormSizeFileException $e) { } catch (FormSizeFileException $e) {
throw new ClientException(_m('Invalid file size given'), previous: $e); throw new ClientException(_m('Invalid file size given.'), previous: $e);
} }
} }
throw new ClientException(_m('Invalid form submission')); throw new ClientException(_m('Invalid form submission.'));
} }
} }

View File

@ -48,7 +48,6 @@ use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation; use Component\Conversation\Conversation;
use Component\Language\Entity\Language; use Component\Language\Entity\Language;
use Component\Notification\Entity\Attention; use Component\Notification\Entity\Attention;
use Functional as F;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -94,7 +93,7 @@ class Posting extends Component
string $content_type, string $content_type,
?string $locale = null, ?string $locale = null,
?VisibilityScope $scope = null, ?VisibilityScope $scope = null,
array $targets = [], array $attentions = [],
null|int|Note $reply_to = null, null|int|Note $reply_to = null,
array $attachments = [], array $attachments = [],
array $processed_attachments = [], array $processed_attachments = [],
@ -104,13 +103,13 @@ class Posting extends Component
string $source = 'web', string $source = 'web',
?string $title = null, ?string $title = null,
): array { ): array {
[$activity, $note, $attention_ids] = self::storeLocalNote( [$activity, $note, $effective_attentions] = self::storeLocalNote(
actor: $actor, actor: $actor,
content: $content, content: $content,
content_type: $content_type, content_type: $content_type,
locale: $locale, locale: $locale,
scope: $scope, scope: $scope,
targets: $targets, attentions: $attentions,
reply_to: $reply_to, reply_to: $reply_to,
attachments: $attachments, attachments: $attachments,
processed_attachments: $processed_attachments, processed_attachments: $processed_attachments,
@ -125,10 +124,18 @@ class Posting extends Component
if ($flush_and_notify) { if ($flush_and_notify) {
// Flush before notification // Flush before notification
DB::flush(); DB::flush();
Event::handle('NewNotification', [$actor, $activity, ['object' => $attention_ids], _m('{nickname} created a page {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [
$actor,
$activity,
$effective_attentions,
_m('Actor {actor_id} created page {note_id}.', [
'{actor_id}' => $actor->getId(),
'{note_id}' => $activity->getObjectId(),
]),
]);
} }
return [$activity, $note, $attention_ids]; return [$activity, $note, $effective_attentions];
} }
/** /**
@ -141,7 +148,7 @@ class Posting extends Component
* @param string $content_type Indicating one of the various supported content format (Plain Text, Markdown, LaTeX...) * @param string $content_type Indicating one of the various supported content format (Plain Text, Markdown, LaTeX...)
* @param null|string $locale Note's written text language, set by the default Actor language or upon filling * @param null|string $locale Note's written text language, set by the default Actor language or upon filling
* @param null|VisibilityScope $scope The visibility of this Note * @param null|VisibilityScope $scope The visibility of this Note
* @param array $targets Actor|int[]: In Group/To Person or Bot, registers an attention between note and target * @param array $attentions Actor|int[]: In Group/To Person or Bot, registers an attention between note and target
* @param null|int|Note $reply_to The soon-to-be Note parent's id, if it's a Reply itself * @param null|int|Note $reply_to The soon-to-be Note parent's id, if it's a Reply itself
* @param array $attachments UploadedFile[] to be stored as GSFiles associated to this note * @param array $attachments UploadedFile[] to be stored as GSFiles associated to this note
* @param array $processed_attachments Array of [Attachment, Attachment's name][] to be associated to this $actor and Note * @param array $processed_attachments Array of [Attachment, Attachment's name][] to be associated to this $actor and Note
@ -154,7 +161,7 @@ class Posting extends Component
* @throws DuplicateFoundException * @throws DuplicateFoundException
* @throws ServerException * @throws ServerException
* *
* @return array [Activity, Note, int[]] Activity, Note, Attention Ids * @return array [Activity, Note, Effective Attentions]
*/ */
public static function storeLocalNote( public static function storeLocalNote(
Actor $actor, Actor $actor,
@ -162,7 +169,7 @@ class Posting extends Component
string $content_type, string $content_type,
?string $locale = null, ?string $locale = null,
?VisibilityScope $scope = null, ?VisibilityScope $scope = null,
array $targets = [], array $attentions = [],
null|int|Note $reply_to = null, null|int|Note $reply_to = null,
array $attachments = [], array $attachments = [],
array $processed_attachments = [], array $processed_attachments = [],
@ -209,7 +216,7 @@ class Posting extends Component
if (!\is_null($reply_to_id)) { if (!\is_null($reply_to_id)) {
Cache::incr(Note::cacheKeys($reply_to_id)['replies-count']); Cache::incr(Note::cacheKeys($reply_to_id)['replies-count']);
// Not having them cached doesn't mean replies don't exist, but don't push it to the // Not having them cached doesn't mean replies don't exist, but don't push it to the
// list, as that means they need to be refetched, or some would be missed // list, as that means they need to be re-fetched, or some would be missed
if (Cache::exists(Note::cacheKeys($reply_to_id)['replies'])) { if (Cache::exists(Note::cacheKeys($reply_to_id)['replies'])) {
Cache::listPushRight(Note::cacheKeys($reply_to_id)['replies'], $note); Cache::listPushRight(Note::cacheKeys($reply_to_id)['replies'], $note);
} }
@ -221,12 +228,12 @@ class Posting extends Component
Event::handle('ProcessNoteContent', [$note, $content, $content_type, $process_note_content_extra_args]); Event::handle('ProcessNoteContent', [$note, $content, $content_type, $process_note_content_extra_args]);
} }
// These are note attachments now, and not just attachments, ensure these relations are ensured // These are note attachments now, and not just attachments, ensure these relations are respected
if ($processed_attachments !== []) { if ($processed_attachments !== []) {
foreach ($processed_attachments as [$a, $fname]) { foreach ($processed_attachments as [$a, $fname]) {
// Most attachments should already be associated with its author, but maybe it didn't make sense // Most attachments should already be associated with its author, but maybe it didn't make sense
//for this attachment, or it's simply a repost of an attachment by a different actor //for this attachment, or it's simply a repost of an attachment by a different actor
if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]) === 0) { if (DB::count(ActorToAttachment::class, $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]) === 0) {
DB::persist(ActorToAttachment::create($args)); DB::persist(ActorToAttachment::create($args));
} }
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname])); DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname]));
@ -242,13 +249,38 @@ class Posting extends Component
]); ]);
DB::persist($activity); DB::persist($activity);
$attention_ids = []; $effective_attentions = [];
foreach ($targets as $target) { foreach ($attentions as $target) {
$target_id = \is_int($target) ? $target : $target->getId(); if (\is_int($target)) {
DB::persist(Attention::create(['note_id' => $note->getId(), 'target_id' => $target_id])); $target_id = $target;
$attention_ids[$target_id] = true; $add = !\array_key_exists($target_id, $effective_attentions);
$effective_attentions[$target_id] = $target;
} else {
$target_id = $target->getId();
if ($add = !\array_key_exists($target_id, $effective_attentions)) {
$effective_attentions[$target_id] = $target_id;
}
}
if ($add) {
DB::persist(Attention::create(['object_type' => Note::schemaName(), 'object_id' => $note->getId(), 'target_id' => $target_id]));
}
}
foreach ($mentions as $m) {
foreach ($m['mentioned'] ?? [] as $mentioned) {
$target_id = $mentioned->getId();
if (!\array_key_exists($target_id, $effective_attentions)) {
DB::persist(Attention::create(['object_type' => Note::schemaName(), 'object_id' => $note->getId(), 'target_id' => $target_id]));
}
$effective_attentions[$target_id] = $mentioned;
}
}
foreach ($actor->getSubscribers() as $subscriber) {
$target_id = $subscriber->getId();
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $target_id]));
$effective_attentions[$target_id] = $subscriber;
} }
$attention_ids = array_keys($attention_ids);
if ($flush_and_notify) { if ($flush_and_notify) {
// Flush before notification // Flush before notification
@ -256,18 +288,15 @@ class Posting extends Component
Event::handle('NewNotification', [ Event::handle('NewNotification', [
$actor, $actor,
$activity, $activity,
[ $effective_attentions,
'note-attention' => $attention_ids, _m('Actor {actor_id} created note {note_id}.', [
'object' => F\unique(F\flat_map($mentions, fn (array $m) => F\map($m['mentioned'] ?? [], fn (Actor $a) => $a->getId()))), '{actor_id}' => $actor->getId(),
],
_m('{nickname} created a note {note_id}.', [
'{nickname}' => $actor->getNickname(),
'{note_id}' => $activity->getObjectId(), '{note_id}' => $activity->getObjectId(),
]), ]),
]); ]);
} }
return [$activity, $note, $attention_ids]; return [$activity, $note, $effective_attentions];
} }
public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?string $language = null, array &$mentions = []) public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?string $language = null, array &$mentions = [])

View File

@ -114,27 +114,6 @@ class ActorSubscription extends Entity
]; ];
} }
/**
* @see Entity->getNotificationTargetIds
*/
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 = [$this->getSubscribedId()]; // The object of any subscription is the one subscribed (or unsubscribed)
} else {
$target_ids = $ids_already_known['object'];
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
} else {
return $target_ids;
}
return array_unique($target_ids);
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -37,6 +37,7 @@ use App\Util\Common;
use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Component\Notification\Entity\Attention;
use Component\Subscription\Controller\Subscribers as SubscribersController; use Component\Subscription\Controller\Subscribers as SubscribersController;
use Component\Subscription\Controller\Subscriptions as SubscriptionsController; use Component\Subscription\Controller\Subscriptions as SubscriptionsController;
@ -97,20 +98,21 @@ class Subscription extends Component
$subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true); $subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true);
$activity = null; $activity = null;
if (\is_null($subscription)) { if (\is_null($subscription)) {
DB::persist(Entity\ActorSubscription::create($opts)); DB::persist($subscription = Entity\ActorSubscription::create($opts));
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $subscriber_id, 'actor_id' => $subscriber_id,
'verb' => 'subscribe', 'verb' => 'subscribe',
'object_type' => 'actor', 'object_type' => Actor::schemaName(),
'object_id' => $subscribed_id, 'object_id' => $subscribed_id,
'source' => $source, 'source' => $source,
]); ]);
DB::persist($activity); DB::persist($activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $subscribed_id]));
Event::handle('NewNotification', [ Event::handle('NewNotification', [
\is_int($subject) ? $subject : Actor::getById($subscriber_id), \is_int($subject) ? $subject : Actor::getById($subscriber_id),
$activity, $activity,
['object' => [$activity->getObjectId()]], [$subscribed_id],
_m('{subject} subscribed to {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $activity->getObjectId()]), _m('{subject} subscribed to {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $activity->getObjectId()]),
]); ]);
} }
@ -146,21 +148,22 @@ class Subscription extends Component
if (!\is_null($subscription)) { if (!\is_null($subscription)) {
// Remove Subscription // Remove Subscription
DB::remove($subscription); DB::remove($subscription);
$previous_follow_activity = DB::findBy('activity', ['verb' => 'subscribe', 'object_type' => 'actor', 'object_id' => $subscribed_id], order_by: ['created' => 'DESC'])[0]; $previous_follow_activity = DB::findBy(Activity::class, ['verb' => 'subscribe', 'object_type' => Actor::schemaName(), 'object_id' => $subscribed_id], order_by: ['created' => 'DESC'])[0];
// Store Activity // Store Activity
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $subscriber_id, 'actor_id' => $subscriber_id,
'verb' => 'undo', 'verb' => 'undo',
'object_type' => 'activity', 'object_type' => Activity::schemaName(),
'object_id' => $previous_follow_activity->getId(), 'object_id' => $previous_follow_activity->getId(),
'source' => $source, 'source' => $source,
]); ]);
DB::persist($activity); DB::persist($activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $subscribed_id]));
Event::handle('NewNotification', [ Event::handle('NewNotification', [
\is_int($subject) ? $subject : Actor::getById($subscriber_id), \is_int($subject) ? $subject : Actor::getById($subscriber_id),
$activity, $activity,
['object' => [$previous_follow_activity->getObjectId()]], [],
_m('{subject} unsubscribed from {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $previous_follow_activity->getObjectId()]), _m('{subject} unsubscribed from {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $previous_follow_activity->getObjectId()]),
]); ]);
} }

View File

@ -141,14 +141,12 @@ class ActivityPub extends Plugin
$ap_actor->getActorId(), $ap_actor->getActorId(),
Discovery::normalize($actor->getNickname() . '@' . parse_url($ap_actor->getInboxUri(), \PHP_URL_HOST)), Discovery::normalize($actor->getNickname() . '@' . parse_url($ap_actor->getInboxUri(), \PHP_URL_HOST)),
); );
$already_known_ids = [];
if (!empty($ap_act->_object_mention_ids)) {
$already_known_ids = $ap_act->_object_mention_ids;
}
DB::flush(); DB::flush();
if (Event::handle('ActivityPubNewNotification', [$actor, $ap_act->getActivity(), $already_known_ids, _m('{nickname} attentioned you.', ['{nickname}' => $actor->getNickname()])]) === Event::next) { if ($ap_act->getToNotifyTargets() !== []) {
Event::handle('NewNotification', [$actor, $ap_act->getActivity(), $already_known_ids, _m('{nickname} attentioned you.', ['{nickname}' => $actor->getNickname()])]); 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()])]);
}
} }
return Event::stop; return Event::stop;
@ -324,8 +322,7 @@ class ActivityPub extends Plugin
string $inbox, string $inbox,
array $to_actors, array $to_actors,
array &$retry_args, array &$retry_args,
): bool ): bool {
{
try { try {
$data = Model::toJson($activity); $data = Model::toJson($activity);
if ($sender->isGroup()) { if ($sender->isGroup()) {
@ -391,7 +388,7 @@ class ActivityPub extends Plugin
foreach ($to_addr as $inbox => $to_actors) { foreach ($to_addr as $inbox => $to_actors) {
Queue::enqueue( Queue::enqueue(
payload: [$sender, $activity, $inbox, $to_actors], payload: [$sender, $activity, $inbox, $to_actors],
queue: 'activitypub_postman', queue: 'ActivitypubPostman',
priority: false, priority: false,
); );
} }
@ -530,7 +527,7 @@ class ActivityPub extends Plugin
* *
* @return null|Actor|mixed|Note got from URI * @return null|Actor|mixed|Note got from URI
*/ */
public static function getObjectByUri(string $resource, bool $try_online = true) public static function getObjectByUri(string $resource, bool $try_online = true): mixed
{ {
// Try known object // Try known object
$known_object = DB::findOneBy(ActivitypubObject::class, ['object_uri' => $resource], return_null: true); $known_object = DB::findOneBy(ActivitypubObject::class, ['object_uri' => $resource], return_null: true);
@ -544,18 +541,6 @@ class ActivityPub extends Plugin
return $known_activity->getActivity(); return $known_activity->getActivity();
} }
// Try local Note
if (Common::isValidHttpUrl($resource)) {
$resource_parts = parse_url($resource);
// TODO: Use URLMatcher
if ($resource_parts['host'] === Common::config('site', 'server')) {
$local_note = DB::findOneBy('note', ['url' => $resource], return_null: true);
if (!\is_null($local_note)) {
return $local_note;
}
}
}
// Try Actor // Try Actor
try { try {
return Explorer::getOneFromUri($resource, try_online: false); return Explorer::getOneFromUri($resource, try_online: false);
@ -563,20 +548,50 @@ class ActivityPub extends Plugin
// Ignore, this is brute forcing, it's okay not to find // Ignore, this is brute forcing, it's okay not to find
} }
// Try remote // Is it a HTTP URL?
if (!$try_online) { if (Common::isValidHttpUrl($resource)) {
return; $resource_parts = parse_url($resource);
// If it is local
if ($resource_parts['host'] === Common::config('site', 'server')) {
// Try Local Note
$local_note = DB::findOneBy(Note::class, ['url' => $resource], return_null: true);
if (!\is_null($local_note)) {
return $local_note;
}
// Try local Activity
try {
$match = Router::match($resource_parts['path']);
$local_activity = DB::findOneBy(Activity::class, ['id' => $match['id']], return_null: true);
if (!\is_null($local_activity)) {
return $local_activity;
} else {
throw new InvalidArgumentException('Tried to retrieve a non-existent local activity.');
}
} catch (\Exception) {
// Ignore, this is brute forcing, it's okay not to find
}
throw new BugFoundException('ActivityPub failed to retrieve local resource: "' . $resource . '". This is a big issue.');
} else {
// Then it's remote
if (!$try_online) {
throw new Exception("Remote resource {$resource} not found without online resources.");
}
$response = HTTPClient::get($resource, ['headers' => self::HTTP_CLIENT_HEADERS]);
// If it was deleted
if ($response->getStatusCode() == 410) {
//$obj = Type::create('Tombstone', ['id' => $resource]);
return null;
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
throw new Exception('Non Ok Status Code for given Object id.');
} else {
return Model::jsonToType($response->getContent());
}
}
} }
$response = HTTPClient::get($resource, ['headers' => self::HTTP_CLIENT_HEADERS]); return null;
// If it was deleted
if ($response->getStatusCode() == 410) {
//$obj = Type::create('Tombstone', ['id' => $resource]);
return;
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
throw new Exception('Non Ok Status Code for given Object id.');
} else {
return Model::jsonToType($response->getContent());
}
} }
} }

View File

@ -38,7 +38,6 @@ use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Queue\Queue; use App\Core\Queue\Queue;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use Exception; use Exception;
@ -164,7 +163,7 @@ class Inbox extends Controller
Queue::enqueue( Queue::enqueue(
payload: [$ap_actor, $actor, $type], payload: [$ap_actor, $actor, $type],
queue: 'activitypub_inbox', queue: 'ActivitypubInbox',
priority: false, priority: false,
); );

View File

@ -101,27 +101,17 @@ class ActivitypubActivity extends Entity
public function getActivity(): Activity public function getActivity(): Activity
{ {
return DB::findOneBy('activity', ['id' => $this->getActivityId()]); return DB::findOneBy(Activity::class, ['id' => $this->getActivityId()]);
} }
public array $_object_mention_ids = []; public function getAttentionTargetIds(): array
public function setObjectMentionIds(array $mentions): self
{ {
$this->_object_mention_ids = $mentions; return $this->getActivity()->getAttentionTargetIds();
return $this;
} }
/** public function getAttentionTargets(): array
* @see Entity->getNotificationTargetIds
*/
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
{ {
// Additional actors that should know about this return $this->getActivity()->getAttentionTargets();
if (\array_key_exists('additional', $ids_already_known)) {
return $ids_already_known['additional'];
} else {
return $this->_object_mention_ids;
}
} }
public static function schemaDef(): array public static function schemaDef(): array

View File

@ -44,6 +44,7 @@ use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotImplementedException; use App\Util\Exception\NotImplementedException;
use DateTimeInterface; use DateTimeInterface;
use InvalidArgumentException; use InvalidArgumentException;
use const JSON_UNESCAPED_SLASHES;
use Plugin\ActivityPub\ActivityPub; use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity; use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Util\Explorer; use Plugin\ActivityPub\Util\Explorer;
@ -145,7 +146,7 @@ class Activity extends Model
* *
* @throws ClientException * @throws ClientException
*/ */
public static function toJson(mixed $object, int $options = \JSON_UNESCAPED_SLASHES): string public static function toJson(mixed $object, int $options = JSON_UNESCAPED_SLASHES): string
{ {
if ($object::class !== GSActivity::class) { if ($object::class !== GSActivity::class) {
throw new InvalidArgumentException('First argument type must be an Activity.'); throw new InvalidArgumentException('First argument type must be an Activity.');
@ -169,10 +170,16 @@ class Activity extends Model
'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL), 'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
]; ];
$attr['to'] = [];
$attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public'];
foreach ($object->getAttentionTargets() as $target) {
$attr['cc'][] = $target->getUri();
}
// Get object or Tombstone // Get object or Tombstone
try { try {
$object = $object->getObject(); // Throws NotFoundException $child = $object->getObject(); // Throws NotFoundException
$attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($object)) : ActivityPub::getUriByObject($object); $attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($child)) : ActivityPub::getUriByObject($child);
} catch (NotFoundException) { } catch (NotFoundException) {
// It seems this object was deleted, refer to it as a Tombstone // It seems this object was deleted, refer to it as a Tombstone
$uri = match ($object->getObjectType()) { $uri = match ($object->getObjectType()) {
@ -180,18 +187,7 @@ class Activity extends Model
'actor' => Router::url('actor_view_id', ['id' => $object->getObjectId()], type: Router::ABSOLUTE_URL), 'actor' => Router::url('actor_view_id', ['id' => $object->getObjectId()], type: Router::ABSOLUTE_URL),
default => throw new NotImplementedException(), default => throw new NotImplementedException(),
}; };
$attr['object'] = Type::create('Tombstone', [ $attr['object'] = Type::create('Tombstone', ['id' => $uri]);
'id' => $uri,
]);
}
// If embedded non tombstone Object
if (!\is_string($attr['object']) && $attr['object']->get('type') !== 'Tombstone') {
// Little special case
if ($attr['type'] === 'Create' && ($attr['object']->get('type') === 'Note' || $attr['object']->get('type') === 'Page')) {
$attr['to'] = $attr['object']->get('to') ?? [];
$attr['cc'] = $attr['object']->get('cc') ?? [];
}
} }
if (!\is_string($attr['object'])) { if (!\is_string($attr['object'])) {

View File

@ -94,7 +94,6 @@ class ActivityCreate extends Activity
'modified' => new DateTime(), 'modified' => new DateTime(),
]); ]);
DB::persist($ap_act); DB::persist($ap_act);
$ap_act->setObjectMentionIds($note->_object_mentions_ids);
return $ap_act; return $ap_act;
} }
} }

View File

@ -184,14 +184,24 @@ class Note extends Model
} }
} }
$attention_ids = []; $explorer = new Explorer();
$attention_targets = [];
foreach ($to as $target) { foreach ($to as $target) {
if ($target === 'https://www.w3.org/ns/activitystreams#Public') { if ($target === 'https://www.w3.org/ns/activitystreams#Public') {
continue; continue;
} }
try { try {
$actor = Explorer::getOneFromUri($target); try {
$attention_ids[$actor->getId()] = $target; $actor_targets = $explorer->lookup($target);
foreach ($actor_targets as $actor) {
$attention_targets[$actor->getId()] = $actor;
}
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->Attention->Explorer', [$e]);
}
$actor = Explorer::getOneFromUri($target);
$attention_targets[$actor->getId()] = $actor;
// If $to is a group and note is unlisted, set note's scope as Group // If $to is a group and note is unlisted, set note's scope as Group
if ($actor->isGroup() && $map['scope'] === 'unlisted') { if ($actor->isGroup() && $map['scope'] === 'unlisted') {
$map['scope'] = VisibilityScope::GROUP; $map['scope'] = VisibilityScope::GROUP;
@ -211,29 +221,17 @@ class Note extends Model
continue; continue;
} }
try { try {
$actor = Explorer::getOneFromUri($target); $actor_targets = $explorer->lookup($target);
$attention_ids[$actor->getId()] = $target; foreach ($actor_targets as $actor) {
} catch (Exception $e) { $attention_targets[$actor->getId()] = $actor;
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
}
}
$obj = GSNote::create($map);
DB::persist($obj);
foreach ($attention_ids as $attention_uri) {
$explorer = new Explorer();
try {
$actors = $explorer->lookup($attention_uri);
foreach ($actors as $actor) {
$object_mention_ids[$target_id = $actor->getId()] = $attention_uri;
DB::persist(Attention::create(['note_id' => $obj->getId(), 'target_id' => $target_id]));
} }
} catch (Exception $e) { } catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->Attention->Explorer', [$e]); Log::debug('ActivityPub->Model->Note->fromJson->Attention->Explorer', [$e]);
} }
} }
$attention_ids = array_keys($attention_ids);
$obj = GSNote::create($map);
DB::persist($obj);
// Attachments // Attachments
$processed_attachments = []; $processed_attachments = [];
@ -272,15 +270,15 @@ class Note extends Model
} }
} }
$object_mention_ids = []; $mention_uris = [];
foreach ($type_note->get('tag') ?? [] as $ap_tag) { foreach ($type_note->get('tag') ?? [] as $ap_tag) {
switch ($ap_tag->get('type')) { switch ($ap_tag->get('type')) {
case 'Mention': case 'Mention':
$explorer = new Explorer();
try { try {
$actors = $explorer->lookup($ap_tag->get('href')); $mention_uris[] = $resource = $ap_tag->get('href');
foreach ($actors as $actor) { $actor_targets = $explorer->lookup($resource);
$object_mention_ids[$actor->getId()] = $ap_tag->get('href'); foreach ($actor_targets as $actor) {
$attention_targets[$actor->getId()] = $actor;
} }
} catch (Exception $e) { } catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->Mention->Explorer', [$e]); Log::debug('ActivityPub->Model->Note->fromJson->Mention->Explorer', [$e]);
@ -306,14 +304,15 @@ class Note extends Model
} }
// The content would be non-sanitized text/html // The content would be non-sanitized text/html
Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $object_mention_ids]]); Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $mention_uris]]);
$object_mention_ids = array_keys($object_mention_ids); foreach ($attention_targets as $target) {
$obj->setObjectMentionsIds($object_mention_ids); DB::persist(Attention::create(['object_type' => GSNote::schemaName(), 'object_id' => $obj->getId(), 'target_id' => $target->getId()]));
}
if ($processed_attachments !== []) { if ($processed_attachments !== []) {
foreach ($processed_attachments as [$a, $fname]) { foreach ($processed_attachments as [$a, $fname]) {
if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor_id]) === 0) { if (DB::count(ActorToAttachment::class, $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor_id]) === 0) {
DB::persist(ActorToAttachment::create($args)); DB::persist(ActorToAttachment::create($args));
} }
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $obj->getId(), 'title' => $fname])); DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $obj->getId(), 'title' => $fname]));
@ -351,11 +350,11 @@ class Note extends Model
} }
$attr = [ $attr = [
'@context' => ActivityPub::$activity_streams_two_context, '@context' => ActivityPub::$activity_streams_two_context,
'type' => $object->getScope() === VisibilityScope::MESSAGE ? 'ChatMessage' : (match ($object->getType()) { 'type' => $object->getScope() === VisibilityScope::MESSAGE ? 'ChatMessage' : (match ($object->getType()) {
'note' => 'Note', 'note' => 'Note',
'page' => 'Page', 'page' => 'Page',
default => throw new \Exception('Unsupported note type.') default => throw new Exception('Unsupported note type.')
}), }),
'id' => $object->getUrl(), 'id' => $object->getUrl(),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
@ -370,17 +369,28 @@ class Note extends Model
'inConversation' => $object->getConversationUri(), 'inConversation' => $object->getConversationUri(),
]; ];
$attentions = $object->getAttentionTargets();
// Target scope // Target scope
switch ($object->getScope()) { switch ($object->getScope()) {
case VisibilityScope::EVERYWHERE: case VisibilityScope::EVERYWHERE:
$attr['to'] = ['https://www.w3.org/ns/activitystreams#Public']; $attr['to'] = ['https://www.w3.org/ns/activitystreams#Public'];
$attr['cc'] = [Router::url('actor_subscribers_id', ['id' => $object->getActor()->getId()], Router::ABSOLUTE_URL)]; $attr['cc'] = [];
foreach ($attentions as $target) {
if ($object->getScope() === VisibilityScope::GROUP && $target->isGroup()) {
$attr['to'][] = $target->getUri(Router::ABSOLUTE_URL);
} else {
$attr['cc'][] = $target->getUri(Router::ABSOLUTE_URL);
}
}
break; break;
case VisibilityScope::LOCAL: case VisibilityScope::LOCAL:
throw new ClientException('This note was not federated.', 403); throw new ClientException('This note was not federated.', 403);
case VisibilityScope::ADDRESSEE: case VisibilityScope::ADDRESSEE:
case VisibilityScope::MESSAGE: case VisibilityScope::MESSAGE:
$attr['to'] = []; // Will be filled later $attr['to'] = [];
foreach ($attentions as $target) {
$attr['to'][] = $target->getUri(Router::ABSOLUTE_URL);
}
$attr['cc'] = []; $attr['cc'] = [];
break; break;
case VisibilityScope::GROUP: case VisibilityScope::GROUP:
@ -391,22 +401,19 @@ class Note extends Model
// of posts. In this situation, it's safer to always send answers of type unlisted. // of posts. In this situation, it's safer to always send answers of type unlisted.
$attr['to'] = []; $attr['to'] = [];
$attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public']; $attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public'];
foreach ($attentions as $target) {
if ($object->getScope() === VisibilityScope::GROUP && $target->isGroup()) {
$attr['to'][] = $target->getUri(Router::ABSOLUTE_URL);
} else {
$attr['cc'][] = $target->getUri(Router::ABSOLUTE_URL);
}
}
break; break;
default: default:
Log::error('ActivityPub->Note->toJson: Found an unknown visibility scope.'); Log::error('ActivityPub->Note->toJson: Found an unknown visibility scope.');
throw new ServerException('Found an unknown visibility scope which cannot federate.'); throw new ServerException('Found an unknown visibility scope which cannot federate.');
} }
// Notification Targets without Mentions
$attentions = $object->getNotificationTargets(ids_already_known: ['object' => []]);
foreach ($attentions as $target) {
if ($object->getScope() === VisibilityScope::GROUP && $target->isGroup()) {
$attr['to'][] = $target->getUri(Router::ABSOLUTE_URL);
} else {
$attr['cc'][] = $target->getUri(Router::ABSOLUTE_URL);
}
}
// Mentions // Mentions
foreach ($object->getMentionTargets() as $mention) { foreach ($object->getMentionTargets() as $mention) {
$attr['tag'][] = [ $attr['tag'][] = [

View File

@ -97,7 +97,7 @@ class DeleteNote extends NoteHandlerPlugin
Cache::delete(self::cacheKeys($note)['activity']); Cache::delete(self::cacheKeys($note)['activity']);
// Undertaker successful // Undertaker successful
Event::handle('NewNotification', [$actor, $activity, [], _m('{nickname} deleted note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]); Event::handle('NewNotification', [$actor, $activity, $note->getAttentionTargets(), _m('{actor_id} deleted note {note_id}.', ['actor_id' => $actor->getId(), 'note_id' => $activity->getObjectId()])]);
return $activity; return $activity;
} }
@ -120,7 +120,7 @@ class DeleteNote extends NoteHandlerPlugin
if (\is_null( if (\is_null(
Cache::get( Cache::get(
self::cacheKeys($note)['activity'], self::cacheKeys($note)['activity'],
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true), fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => Note::schemaName(), 'object_id' => $note->getId()], return_null: true),
), ),
)) { )) {
// If none found, then undertaker has a job to do // If none found, then undertaker has a job to do
@ -164,7 +164,7 @@ class DeleteNote extends NoteHandlerPlugin
// Only add action if note wasn't already deleted! // Only add action if note wasn't already deleted!
\is_null(Cache::get( \is_null(Cache::get(
self::cacheKeys($note)['activity'], self::cacheKeys($note)['activity'],
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true), fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => Note::schemaName(), 'object_id' => $note->getId()], return_null: true),
)) ))
// And has permissions // And has permissions
&& $actor->canModerate($note->getActor())) { && $actor->canModerate($note->getActor())) {

View File

@ -30,6 +30,8 @@ use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\InvalidFormException; use App\Util\Exception\InvalidFormException;
@ -55,7 +57,7 @@ class Favourite extends FeedController
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$opts = ['id' => $id]; $opts = ['id' => $id];
$add_favourite_note = DB::find('note', $opts); $add_favourite_note = DB::findOneBy(Note::class, $opts);
if (\is_null($add_favourite_note)) { if (\is_null($add_favourite_note)) {
throw new NoSuchNoteException(); throw new NoSuchNoteException();
} }
@ -76,7 +78,7 @@ class Favourite extends FeedController
if ($form_add_to_favourite->isSubmitted()) { if ($form_add_to_favourite->isSubmitted()) {
if (!\is_null($activity = \Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) { if (!\is_null($activity = \Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) {
DB::flush(); DB::flush();
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{nickname} favoured note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{actor_id} favoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]);
} else { } else {
throw new ClientException(_m('Note already favoured!')); throw new ClientException(_m('Note already favoured!'));
} }
@ -116,7 +118,7 @@ class Favourite extends FeedController
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$opts = ['id' => $id]; $opts = ['id' => $id];
$remove_favourite_note = DB::find('note', $opts); $remove_favourite_note = DB::findOneBy(Note::class, $opts);
if (\is_null($remove_favourite_note)) { if (\is_null($remove_favourite_note)) {
throw new NoSuchNoteException(); throw new NoSuchNoteException();
} }
@ -136,7 +138,7 @@ class Favourite extends FeedController
if ($form_remove_favourite->isSubmitted()) { if ($form_remove_favourite->isSubmitted()) {
if (!\is_null($activity = \Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) { if (!\is_null($activity = \Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) {
DB::flush(); DB::flush();
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{nickname} unfavoured note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{actor_id} unfavoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]);
} else { } else {
throw new ClientException(_m('Note already unfavoured!')); throw new ClientException(_m('Note already unfavoured!'));
} }
@ -157,7 +159,7 @@ class Favourite extends FeedController
} }
} }
$note = DB::find('note', ['id' => $id]); $note = DB::findOneBy(Note::class, ['id' => $id]);
return [ return [
'_template' => 'favourite/remove_from_favourites.html.twig', '_template' => 'favourite/remove_from_favourites.html.twig',
'note' => $note, 'note' => $note,
@ -186,7 +188,7 @@ class Favourite extends FeedController
public function favouritesViewByActorNickname(Request $request, string $nickname) public function favouritesViewByActorNickname(Request $request, string $nickname)
{ {
$user = DB::findOneBy('local_user', ['nickname' => $nickname]); $user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::favouritesViewByActorId($request, $user->getId()); return self::favouritesViewByActorId($request, $user->getId());
} }
@ -219,7 +221,7 @@ class Favourite extends FeedController
public function reverseFavouritesViewByActorNickname(Request $request, string $nickname) public function reverseFavouritesViewByActorNickname(Request $request, string $nickname)
{ {
$user = DB::findOneBy('local_user', ['nickname' => $nickname]); $user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::reverseFavouritesViewByActorId($request, $user->getId()); return self::reverseFavouritesViewByActorId($request, $user->getId());
} }
} }

View File

@ -121,27 +121,6 @@ class NoteFavourite extends Entity
); );
} }
/**
* @see Entity->getNotificationTargetIds
*/
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();
} else {
$target_ids = $ids_already_known['object'];
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
} else {
return $target_ids;
}
return array_unique($target_ids);
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -44,8 +44,9 @@ use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Nickname; use App\Util\Nickname;
use Component\Notification\Entity\Attention;
use DateTime; use DateTime;
use Plugin\Favourite\Entity\NoteFavourite as FavouriteEntity; use Plugin\Favourite\Entity\NoteFavourite;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Favourite extends NoteHandlerPlugin class Favourite extends NoteHandlerPlugin
@ -54,8 +55,6 @@ class Favourite extends NoteHandlerPlugin
* Creates a new Favourite Entity, upon the given Actor performs a Favourite * Creates a new Favourite Entity, upon the given Actor performs a Favourite
* action on the given Note object * action on the given Note object
* *
* A new notification is then handled, informing all interested Actors of this action
*
* @param int $note_id Note id being favoured * @param int $note_id Note id being favoured
* @param int $actor_id Actor performing favourite Activity * @param int $actor_id Actor performing favourite Activity
* *
@ -65,13 +64,13 @@ class Favourite extends NoteHandlerPlugin
{ {
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id]; $opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
$note_already_favoured = Cache::get( $note_already_favoured = Cache::get(
FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite'], NoteFavourite::cacheKeys($note_id, $actor_id)['favourite'],
fn () => DB::findOneBy('note_favourite', $opts, return_null: true), fn () => DB::findOneBy(NoteFavourite::class, $opts, return_null: true),
); );
$activity = null; $activity = null;
if (\is_null($note_already_favoured)) { if (\is_null($note_already_favoured)) {
DB::persist(FavouriteEntity::create($opts)); DB::persist(NoteFavourite::create($opts));
Cache::delete(FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite']); Cache::delete(NoteFavourite::cacheKeys($note_id, $actor_id)['favourite']);
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'favourite', 'verb' => 'favourite',
@ -80,6 +79,7 @@ class Favourite extends NoteHandlerPlugin
'source' => $source, 'source' => $source,
]); ]);
DB::persist($activity); DB::persist($activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => Note::getById($note_id)->getActorId()]));
} }
return $activity; return $activity;
} }
@ -87,8 +87,6 @@ class Favourite extends NoteHandlerPlugin
/** /**
* Removes the Favourite Entity created beforehand, by the same Actor, and on the same Note * Removes the Favourite Entity created beforehand, by the same Actor, and on the same Note
* *
* Informs all interested Actors of this action, handling out the NewNotification event
*
* @param int $note_id Note id being unfavoured * @param int $note_id Note id being unfavoured
* @param int $actor_id Actor undoing favourite Activity * @param int $actor_id Actor undoing favourite Activity
* *
@ -97,15 +95,15 @@ class Favourite extends NoteHandlerPlugin
public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{ {
$note_already_favoured = Cache::get( $note_already_favoured = Cache::get(
FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite'], NoteFavourite::cacheKeys($note_id, $actor_id)['favourite'],
static fn () => DB::findOneBy('note_favourite', ['note_id' => $note_id, 'actor_id' => $actor_id], return_null: true), static fn () => DB::findOneBy(NoteFavourite::class, ['note_id' => $note_id, 'actor_id' => $actor_id], return_null: true),
); );
$activity = null; $activity = null;
if (!\is_null($note_already_favoured)) { if (!\is_null($note_already_favoured)) {
DB::removeBy(FavouriteEntity::class, ['note_id' => $note_id, 'actor_id' => $actor_id]); DB::removeBy(NoteFavourite::class, ['note_id' => $note_id, 'actor_id' => $actor_id]);
Cache::delete(FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite']); Cache::delete(NoteFavourite::cacheKeys($note_id, $actor_id)['favourite']);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'actor_id' => $actor_id, 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0]; $favourite_activity = DB::findBy(Activity::class, ['verb' => 'favourite', 'object_type' => 'note', 'actor_id' => $actor_id, 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'undo', // 'undo_favourite', 'verb' => 'undo', // 'undo_favourite',
@ -114,6 +112,7 @@ class Favourite extends NoteHandlerPlugin
'source' => $source, 'source' => $source,
]); ]);
DB::persist($activity); DB::persist($activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => Note::getById($note_id)->getActorId()]));
} }
return $activity; return $activity;
} }
@ -137,8 +136,8 @@ class Favourite extends NoteHandlerPlugin
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()]; $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
$is_favourite = !\is_null( $is_favourite = !\is_null(
Cache::get( Cache::get(
FavouriteEntity::cacheKeys($note->getId(), $user->getId())['favourite'], NoteFavourite::cacheKeys($note->getId(), $user->getId())['favourite'],
static fn () => DB::findOneBy('note_favourite', $opts, return_null: true), static fn () => DB::findOneBy(NoteFavourite::class, $opts, return_null: true),
), ),
); );
@ -184,7 +183,7 @@ class Favourite extends NoteHandlerPlugin
// Will have actors array, and action string // Will have actors array, and action string
// Actors are the subjects, action is the verb (in the final phrase) // Actors are the subjects, action is the verb (in the final phrase)
$favourite_actors = FavouriteEntity::getNoteFavouriteActors($note); $favourite_actors = NoteFavourite::getNoteFavouriteActors($note);
if (\count($favourite_actors) < 1) { if (\count($favourite_actors) < 1) {
return Event::next; return Event::next;
@ -201,7 +200,7 @@ class Favourite extends NoteHandlerPlugin
*/ */
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
{ {
$note_favourites_list = FavouriteEntity::getNoteFavourites($note); $note_favourites_list = NoteFavourite::getNoteFavourites($note);
foreach ($note_favourites_list as $favourite_entity) { foreach ($note_favourites_list as $favourite_entity) {
DB::remove($favourite_entity); DB::remove($favourite_entity);
} }
@ -331,17 +330,17 @@ class Favourite extends NoteHandlerPlugin
return Event::stop; return Event::stop;
} }
public function onActivityPubNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool public function onActivityPubNewNotification(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
{ {
switch ($activity->getVerb()) { switch ($activity->getVerb()) {
case 'favourite': case 'favourite':
Event::handle('NewNotification', [$sender, $activity, [], _m('{nickname} favoured note {note_id}.', ['{nickname}' => $sender->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} favoured note {note_id}.', ['{nickname}' => $sender->getId(), '{note_id}' => $activity->getObjectId()])]);
return Event::stop; return Event::stop;
case 'undo': case 'undo':
if ($activity->getObjectType() === 'activity') { if ($activity->getObjectType() === 'activity') {
$undone_favourite = $activity->getObject(); $undone_favourite = $activity->getObject();
if ($undone_favourite->getVerb() === 'favourite') { if ($undone_favourite->getVerb() === 'favourite') {
Event::handle('NewNotification', [$sender, $activity, [], _m('{nickname} unfavoured note {note_id}.', ['{nickname}' => $sender->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} unfavoured note {note_id}.', ['{nickname}' => $sender->getId(), '{note_id}' => $activity->getObjectId()])]);
return Event::stop; return Event::stop;
} }
} }

View File

@ -75,7 +75,7 @@ class Repeat extends Controller
if ($form_add_to_repeat->isSubmitted()) { if ($form_add_to_repeat->isSubmitted()) {
$repeat_activity = \Plugin\RepeatNote\RepeatNote::repeatNote(note: $note, actor_id: $actor_id); $repeat_activity = \Plugin\RepeatNote\RepeatNote::repeatNote(note: $note, actor_id: $actor_id);
DB::flush(); DB::flush();
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], _m('{nickname} repeated note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $repeat_activity->getObjectId()])]); 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()])]);
// Redirect user to where they came from // Redirect user to where they came from
// Prevent open redirect // Prevent open redirect
@ -132,7 +132,7 @@ class Repeat extends Controller
if ($form_remove_repeat->isSubmitted()) { if ($form_remove_repeat->isSubmitted()) {
if (!\is_null($undo_repeat_activity = \Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $note_id, actor_id: $actor_id))) { if (!\is_null($undo_repeat_activity = \Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $note_id, actor_id: $actor_id))) {
DB::flush(); DB::flush();
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], _m('{nickname} unrepeated note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $note_id])]); 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])]);
} else { } else {
throw new ClientException(_m('Note wasn\'t repeated!')); throw new ClientException(_m('Note wasn\'t repeated!'));
} }

View File

@ -132,27 +132,6 @@ class NoteRepeat extends Entity
); );
} }
/**
* @see Entity->getNotificationTargetIds
*/
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();
} else {
$target_ids = $ids_already_known['object'];
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
} else {
return $target_ids;
}
return array_unique($target_ids);
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -43,6 +43,7 @@ use App\Util\Exception\ClientException;
use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Component\Language\Entity\Language; use Component\Language\Entity\Language;
use Component\Notification\Entity\Attention;
use Component\Posting\Posting; use Component\Posting\Posting;
use DateTime; use DateTime;
use Plugin\RepeatNote\Entity\NoteRepeat as RepeatEntity; use Plugin\RepeatNote\Entity\NoteRepeat as RepeatEntity;
@ -81,7 +82,7 @@ class RepeatNote extends NoteHandlerPlugin
// Create a new note with the same content as the original // Create a new note with the same content as the original
[, $repeat, ] = Posting::storeLocalNote( [, $repeat, ] = Posting::storeLocalNote(
actor: Actor::getById($actor_id), actor: $actor = Actor::getById($actor_id),
content: $note->getContent(), content: $note->getContent(),
content_type: $note->getContentType(), content_type: $note->getContentType(),
locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(), locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(),
@ -116,6 +117,13 @@ class RepeatNote extends NoteHandlerPlugin
'source' => $source, 'source' => $source,
]); ]);
DB::persist($repeat_activity); DB::persist($repeat_activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $repeat_activity->getId(), 'target_id' => $note->getActorId()]));
foreach ($actor->getSubscribers() as $subscriber) {
$target_id = $subscriber->getId();
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $repeat_activity->getId(), 'target_id' => $target_id]));
$effective_attentions[$target_id] = $subscriber;
}
return $repeat_activity; return $repeat_activity;
} }
@ -166,17 +174,24 @@ class RepeatNote extends NoteHandlerPlugin
'source' => $source, 'source' => $source,
]); ]);
DB::persist($undo_repeat_activity); DB::persist($undo_repeat_activity);
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $undo_repeat_activity->getId(), 'target_id' => Note::getById($note_id)->getActorId()]));
foreach (Actor::getById($actor_id)->getSubscribers() as $subscriber) {
$target_id = $subscriber->getId();
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $undo_repeat_activity->getId(), 'target_id' => $target_id]));
$effective_attentions[$target_id] = $subscriber;
}
return $undo_repeat_activity; return $undo_repeat_activity;
} else { } else {
// Either was undoed already // Either was undoed already
if (!\is_null($already_repeated_activity = DB::findOneBy('activity', [ if (!\is_null($already_repeated_activity = DB::findOneBy(Activity::class, [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'repeat', 'verb' => 'repeat',
'object_type' => 'note', 'object_type' => 'note',
'object_id' => $note_id, 'object_id' => $note_id,
], return_null: true))) { ], return_null: true))) {
return DB::findOneBy('activity', [ return DB::findOneBy(Activity::class, [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'undo', 'verb' => 'undo',
'object_type' => 'activity', 'object_type' => 'activity',
@ -376,7 +391,7 @@ class RepeatNote extends NoteHandlerPlugin
} else { } else {
return Event::next; return Event::next;
} }
$activity = self::repeatNote($note ?? Note::getById($note_id), $actor->getId(), source: 'ActivityPub'); $activity = self::repeatNote($note, $actor->getId(), source: 'ActivityPub');
} elseif ($type_activity->get('type') === 'Undo') { // Undo Repeat } elseif ($type_activity->get('type') === 'Undo') { // Undo Repeat
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) { if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
$ap_prev_repeat_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object); $ap_prev_repeat_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object);
@ -413,17 +428,17 @@ class RepeatNote extends NoteHandlerPlugin
return Event::stop; return Event::stop;
} }
public function onActivityPubNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool public function onActivityPubNewNotification(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
{ {
switch ($activity->getVerb()) { switch ($activity->getVerb()) {
case 'repeat': case 'repeat':
Event::handle('NewNotification', [$sender, $activity, [], _m('{nickname} repeated note {note_id}.', ['{nickname}' => $sender->getNickname(), '{note_id}' => $activity->getObjectId()])]); Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $sender->getId(), '{note_id}' => $activity->getObjectId()])]);
return Event::stop; return Event::stop;
case 'undo': case 'undo':
if ($activity->getObjectType() === 'activity') { if ($activity->getObjectType() === 'activity') {
$undone_repeat = $activity->getObject(); $undone_repeat = $activity->getObject();
if ($undone_repeat->getVerb() === 'repeat') { if ($undone_repeat->getVerb() === 'repeat') {
Event::handle('NewNotification', [$sender, $activity, [], _m('{nickname} unrepeated note {note_id}.', ['{nickname}' => $sender->getNickname(), '{note_id}' => $undone_repeat->getObjectId()])]); Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $sender->getId(), '{note_id}' => $undone_repeat->getObjectId()])]);
return Event::stop; return Event::stop;
} }
} }

View File

@ -75,21 +75,6 @@ class WebMonetization extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
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 = $ids_already_known['object'];
} else {
$target_ids = [$this->getReceiver()];
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
return array_unique($target_ids);
}
return $target_ids;
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -36,9 +36,9 @@ class Activity extends Controller
*/ */
private function activity(int $id, callable $handle) private function activity(int $id, callable $handle)
{ {
$activity = DB::findOneBy('activity', ['id' => $id]); $activity = DB::findOneBy('activity', ['id' => $id], return_null: true);
if (empty($activity)) { if (\is_null($activity)) {
throw new ClientException(_m('No such activity.'), 404); throw new ClientException(_m('No such activity.'), code: 404);
} else { } else {
return $handle($activity); return $handle($activity);
} }

View File

@ -52,23 +52,31 @@ use Functional as F;
* @mixin EntityManager * @mixin EntityManager
* @template T of Entity * @template T of Entity
* *
* // Finds an Entity by its identifier. You probably want to use DB::findBy instead * Finds an Entity by its identifier. You probably want to use DB::findBy instead.
* *
* @method static ?T find(string $class, array<string, mixed> $values) * @method static ?T find(string $class, array<string, mixed> $values)
* // Special cases: It's like find but does not load the object if it has not been loaded yet, it only returns a proxy to the object. (https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/unitofwork.html) *
* It's like find but does not load the object if it has not been loaded yet (can be helpful in special cases),
* it only returns a proxy to the object. (https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/unitofwork.html)
* @method static ?T getReference(string $class, array<string, mixed> $values) * @method static ?T getReference(string $class, array<string, mixed> $values)
* // Removes an entity instance. *
* Removes an entity instance.
* @method static void remove(object $entity) * @method static void remove(object $entity)
* // Refreshes the persistent state of a managed entity, i.e., overriding any local changes *
* Refreshes the persistent state of a managed entity, i.e., overriding any local changes
* @method static void refresh(object $entity) * @method static void refresh(object $entity)
* // Tells the EntityManager to make an instance managed and persistent. *
* Tells the EntityManager to make an instance managed and persistent.
* @method static void persist(object $entity) * @method static void persist(object $entity)
* // Determines whether an entity instance is managed in this EntityManager. *
* Determines whether an entity instance is managed in this EntityManager.
* @method static bool contains(object $entity) * @method static bool contains(object $entity)
* // Flushes the in-memory state of persisted objects to the database. *
* Flushes the in-memory state of persisted objects to the database.
* @method static void flush() * @method static void flush()
* // Executes a function in a transaction. Warning: suppresses exceptions. Returns the result of the callable. *
* @method mixed wrapInTransaction(callable $func) * Executes a function in a transaction. Warning: suppresses exceptions. Returns the result of the callable.
* @method mixed wrapInTransaction(callable $func)
*/ */
class DB class DB
{ {

View File

@ -24,12 +24,15 @@ declare(strict_types = 1);
namespace App\Core; namespace App\Core;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Entity\Actor;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Formatting; use App\Util\Formatting;
use BadMethodCallException; use BadMethodCallException;
use Component\Notification\Entity\Attention;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use Functional as F;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
@ -37,6 +40,14 @@ use InvalidArgumentException;
*/ */
abstract class Entity abstract class Entity
{ {
/**
* @return string Returns the name of this entity's DB table
*/
public static function schemaName(): string
{
return static::schemaDef()['name'];
}
public function __call(string $name, array $arguments): mixed public function __call(string $name, array $arguments): mixed
{ {
if (Formatting::startsWith($name, 'has')) { if (Formatting::startsWith($name, 'has')) {
@ -46,7 +57,7 @@ abstract class Entity
$private_property_accessor = $private_property_accessor->bindTo($this, static::class); $private_property_accessor = $private_property_accessor->bindTo($this, static::class);
return $private_property_accessor($prop); return $private_property_accessor($prop);
} }
throw new BadMethodCallException('Non existent method ' . static::class . "::{$name} called with arguments: " . print_r($arguments, true)); throw new BadMethodCallException('Non existent non-static method ' . static::class . "->{$name} called with arguments: " . var_export($arguments, true));
} }
abstract public static function schemaDef(): array; abstract public static function schemaDef(): array;
@ -55,10 +66,8 @@ abstract class Entity
* Create an instance of the called class or fill in the * Create an instance of the called class or fill in the
* properties of $obj with the associative array $args. Doesn't * properties of $obj with the associative array $args. Doesn't
* persist the result * persist the result
*
* @param null|mixed $obj
*/ */
public static function create(array $args, $obj = null) public static function create(array $args, mixed $obj = null)
{ {
$class = static::class; $class = static::class;
@ -126,7 +135,7 @@ abstract class Entity
* Entity::getByPK([42, 'foo']); * Entity::getByPK([42, 'foo']);
* Entity::getByPK(['key1' => 42, 'key2' => 'foo']) * Entity::getByPK(['key1' => 42, 'key2' => 'foo'])
* *
* @return null|static * @throws \App\Util\Exception\DuplicateFoundException
*/ */
public static function getByPK(mixed $values): ?self public static function getByPK(mixed $values): ?self
{ {
@ -175,27 +184,28 @@ abstract class Entity
} }
/** /**
* Who should be notified about this object? * Ids of the Actors that should be informed about this object.
* BEWARE: If you call this, your object must have a serial integer id!
* *
* @return array of ids of Actors * @return array int[] of Actor's id
*/ */
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array public function getAttentionTargetIds(): array
{ {
// Additional actors that should know about this $attention = DB::findBy(Attention::class, [
if (\array_key_exists('additional', $ids_already_known)) { 'object_type' => static::schemaName(),
return $ids_already_known['additional']; 'object_id' => $this->getId(),
} ]);
return []; return F\map($attention, fn ($cc) => $cc->getTargetId());
} }
/** /**
* Who should be notified about this object? * To whom should this be brought attention to?
* *
* @return array of Actors * @return array Actor[]
*/ */
public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array public function getAttentionTargets(): array
{ {
$target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id, $include_additional); $target_ids = $this->getAttentionTargetIds();
return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); return DB::findBy(Actor::class, ['id' => $target_ids]);
} }
} }

View File

@ -74,10 +74,10 @@ abstract class Log
/** /**
* Simple static wrappers around Monolog's functions * Simple static wrappers around Monolog's functions
*/ */
public static function __callStatic(string $name, array $args) public static function __callStatic(string $name, array $arguments)
{ {
if (isset(self::$logger)) { if (isset(self::$logger)) {
return self::$logger->{$name}(...$args); return self::$logger->{$name}(...$arguments);
} else { } else {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
return; return;

View File

@ -25,6 +25,7 @@ namespace App\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use Component\Notification\Entity\Attention;
use Component\Notification\Entity\Notification; use Component\Notification\Entity\Notification;
use DateTimeInterface; use DateTimeInterface;
use Functional as F; use Functional as F;
@ -143,48 +144,33 @@ class Activity extends Entity
} }
/** /**
* Who should be notified about this object? * Actors that should be informed about this object.
* *
* @return array of ids of Actors * @return array int[] of Actor's id
*/ */
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array public function getAttentionTargetIds(): array
{ {
$target_ids = []; $answer = [];
switch($this->getVerb()) {
// Notifications case 'undo':
if (\array_key_exists('notification_activity', $ids_already_known)) { $answer = $this->getObject()->getAttentionTargetIds();
array_push($target_ids, ...$ids_already_known['notification_activity']); break;
} else { case 'create':
array_push($target_ids, ...Notification::getNotificationTargetIdsByActivity($this->getId())); $object_attention = $this->getObject()->getAttentionTargetIds();
foreach ($object_attention as $att) {
$answer[$att] = $att;
}
// no break
default:
$attention = DB::findBy(Attention::class, [
'object_type' => static::schemaName(),
'object_id' => $this->getId(),
]);
foreach ($attention as $att) {
$answer[$att->getTargetId()] = $att->getTargetId();
}
} }
return $answer;
// 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, 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
if (\array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
}
return array_unique($target_ids);
} }
public static function getAllActivitiesByActor(Actor $actor): array public static function getAllActivitiesByActor(Actor $actor): array

View File

@ -39,7 +39,6 @@ use App\Util\Formatting;
use Component\Avatar\Avatar; use Component\Avatar\Avatar;
use Component\Conversation\Entity\Conversation; use Component\Conversation\Entity\Conversation;
use Component\Language\Entity\Language; use Component\Language\Entity\Language;
use Component\Notification\Entity\Attention;
use const PREG_SPLIT_NO_EMPTY; use const PREG_SPLIT_NO_EMPTY;
use DateTimeInterface; use DateTimeInterface;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
@ -275,7 +274,7 @@ class Note extends Entity
public function getConversation(): Conversation public function getConversation(): Conversation
{ {
return Conversation::getByPK(['id' => $this->getConversationId()]); return DB::findOneBy(Conversation::class, ['id' => $this->getConversationId()]);
} }
public function getConversationUrl(int $type = Router::ABSOLUTE_URL): ?string public function getConversationUrl(int $type = Router::ABSOLUTE_URL): ?string
@ -310,7 +309,7 @@ class Note extends Entity
public static function getById(int $note_id): self public static function getById(int $note_id): self
{ {
return Cache::get(self::cacheKeys($note_id)['note'], fn () => DB::findOneBy('note', ['id' => $note_id])); return Cache::get(self::cacheKeys($note_id)['note'], fn () => DB::findOneBy(self::class, ['id' => $note_id]));
} }
public function getNoteLanguageShortDisplay(): ?string public function getNoteLanguageShortDisplay(): ?string
@ -457,7 +456,7 @@ class Note extends Entity
// Is either the author Or // Is either the author Or
$this->getActorId() == $actor->getId() $this->getActorId() == $actor->getId()
// one of the targets // one of the targets
|| \in_array($actor->getId(), $this->getNotificationTargetIds()) || \in_array($actor->getId(), $this->getAttentionTargetIds())
)); ));
case VisibilityScope::GROUP: case VisibilityScope::GROUP:
@ -482,32 +481,12 @@ class Note extends Entity
default: default:
throw new BugFoundException("Unknown scope found: {$this->getScope()->value}"); throw new BugFoundException("Unknown scope found: {$this->getScope()->value}");
} }
return false; //return false;
}
// @return array of ids of Actors
public array $_object_mentions_ids = [];
public function setObjectMentionsIds(array $mentions): self
{
$this->_object_mentions_ids = $mentions;
return $this;
}
public function getAttentionTargetIds(?int $sender_id = null): array
{
$attentioned = [];
$attention_cc = DB::findBy(Attention::class, ['note_id' => $this->getId()]);
foreach ($attention_cc as $cc) {
$cc_id = $cc->getTargetId();
if ($cc_id === $sender_id) {
continue;
}
$attentioned[] = $cc_id;
}
return $attentioned;
} }
/**
* @return array int[] of Actor's id
*/
public function getMentionTargetIds(): array public function getMentionTargetIds(): array
{ {
$target_ids = []; $target_ids = [];
@ -524,50 +503,10 @@ class Note extends Entity
} }
/** /**
* @see Entity->getNotificationTargetIds * All the actors mentioned in this note.
*
* @return array Actor[]
*/ */
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
{
$target_ids = $this->_object_mentions_ids ?? [];
// Parent
if (!\array_key_exists('object-related', $ids_already_known)) {
if (!\is_null($parent = $this->getReplyToNote())) {
$target_ids[] = $parent->getActorId();
array_push($target_ids, ...$parent->getNotificationTargetIds());
}
} else {
array_push($target_ids, ...$ids_already_known['object-related']);
}
// Mentions
if (!\array_key_exists('object', $ids_already_known)) {
array_push($target_ids, ...$this->getMentionTargetIds());
} else {
array_push($target_ids, ...$ids_already_known['object']);
}
// Attentions
if (!\array_key_exists('note-attention', $ids_already_known)) {
array_push($target_ids, ...$this->getAttentionTargetIds($sender_id));
} else {
array_push($target_ids, ...$ids_already_known['note-attention']);
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
}
return array_unique($target_ids);
}
public function getAttentionTargets(?int $sender_id = null): array
{
$attentioned = $this->getAttentionTargetIds();
return DB::findBy('actor', ['id' => $attentioned]);
}
public function getMentionTargets(): array public function getMentionTargets(): array
{ {
$mentioned = []; $mentioned = [];
@ -580,49 +519,6 @@ class Note extends Entity
return $mentioned; return $mentioned;
} }
/**
* @return array of Actors
*/
public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
{
// Additional (if we have additional, we will just return all the actors from ids)
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::class, ['id' => $target_ids]);
}
$targets = $this->_object_mentions_ids === [] ? [] : DB::findBy(Actor::class, ['id' => $this->_object_mentions_ids]);
// Parent
if (!\array_key_exists('object-related', $ids_already_known)) {
if (!\is_null($parent = $this->getReplyToNote())) {
$targets[] = $parent->getActor();
array_push($targets, ...$parent->getNotificationTargets());
}
} else {
array_push($targets, ...$ids_already_known['object-related']);
}
// Mentions
if (!\array_key_exists('object', $ids_already_known)) {
array_push($targets, ...$this->getMentionTargets());
} elseif ($ids_already_known['object'] !== []) {
array_push($targets, ...DB::findBy('actor', ['id' => $ids_already_known['object']]));
}
// Attentions
if (!\array_key_exists('note-attention', $ids_already_known)) {
array_push($targets, ...$this->getAttentionTargets($sender_id));
} else {
$attentioned = $ids_already_known['note-attention'] ?? [];
if ($attentioned !== []) {
array_push($targets, ...DB::findBy('actor', ['id' => $attentioned]));
}
}
return $targets;
}
public function delete(?Actor $actor = null, string $source = 'web'): Activity public function delete(?Actor $actor = null, string $source = 'web'): Activity
{ {
Event::handle('NoteDeleteRelated', [&$this, $actor]); Event::handle('NoteDeleteRelated', [&$this, $actor]);

View File

@ -153,6 +153,6 @@ abstract class HTML
return self::$sanitizer->{$name}(...$args); return self::$sanitizer->{$name}(...$args);
} }
throw new BadMethodCallException("Method Security::{$name} doesn't exist"); throw new BadMethodCallException("Method HTML::{$name} doesn't exist.");
} }
} }

View File

@ -94,9 +94,9 @@
{% endblock note_info %} {% endblock note_info %}
{% block note_context %} {% block note_context %}
{% set notification_targets = note.getNotificationTargets() %} {% set attentions = note.getAttentionTargets() %}
{% if notification_targets is not empty %} {% if attentions is not empty %}
{% for target in notification_targets %} {% for target in attentions %}
{% if target.isGroup() %} {% if target.isGroup() %}
<em tabindex="0" <em tabindex="0"
class="note-replies-parent" class="note-replies-parent"
@ -107,15 +107,25 @@
{% else %} {% else %}
<em tabindex="0" <em tabindex="0"
class="note-replies-parent" class="note-replies-parent"
title="{% trans %}Whom this is a reply to{% endtrans %}" title="{% trans %}The recipient to whom this note was addressed to{% endtrans %}">
aria-flowto="note-anchor-{{ note.getReplyTo() }}">
{% set replyAnchor %}<a href="{{ note.getConversationUrl() ~ "#note-anchor-" ~ note.getReplyTo() }}">{% endset %}
{% set mentionAnchor %}<a href="{{ target.getUrl() }}">{{ mention(target) | raw }}</a>{% endset %} {% set mentionAnchor %}<a href="{{ target.getUrl() }}">{{ mention(target) | raw }}</a>{% endset %}
{% trans %}%replyAnchor% in reply to %mentionAnchor%{% endtrans %} {% trans %}to %mentionAnchor%{% endtrans %}
</em> </em>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% set reply_to = note.getReplyTo() %}
{% if reply_to is not empty %}
<em tabindex="0"
class="note-replies-parent"
title="{% trans %}Whom this is a reply to{% endtrans %}"
aria-flowto="note-anchor-{{ reply_to }}">
{% set replyAnchor %}<a href="{{ note.getConversationUrl() ~ "#note-anchor-" ~ reply_to }}">{% endset %}
{% set target = note.getActor() %}
{% set mentionAnchor %}<a href="{{ target.getUrl() }}">{{ mention(target) | raw }}</a>{% endset %}
{% trans %}%replyAnchor% in reply to %mentionAnchor%{% endtrans %}
</em>
{% endif %}
{% endblock note_context %} {% endblock note_context %}
{% block note_complementary %} {% block note_complementary %}