From b69f4a46c5dc7d2853ca31900b92944829184ba7 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Wed, 16 Feb 2022 19:35:27 +0000 Subject: [PATCH] [COMPONENT][Posting] Page should flush with a different notification --- components/Blog/Controller/Post.php | 2 +- components/Posting/Posting.php | 61 ++++++++++++++++------------- plugins/RepeatNote/RepeatNote.php | 7 +++- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/components/Blog/Controller/Post.php b/components/Blog/Controller/Post.php index 2a466c4c6d..df011930c9 100644 --- a/components/Blog/Controller/Post.php +++ b/components/Blog/Controller/Post.php @@ -139,7 +139,7 @@ class Post extends Controller $extra_args = []; Event::handle('AddExtraArgsToNoteContent', [$request, $actor, $data, &$extra_args, $form_params, $form]); - $note = Posting::storeLocalPage( + [,$note,] = Posting::storeLocalPage( actor: $actor, content: $data['content'], content_type: $content_type, diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index b1d67f4e81..dd729faa42 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -216,21 +216,21 @@ class Posting extends Component * @throws ServerException */ public static function storeLocalPage( - Actor $actor, - ?string $content, - string $content_type, - ?string $locale = null, + Actor $actor, + ?string $content, + string $content_type, + ?string $locale = null, ?VisibilityScope $scope = null, - array $targets = [], - null|int|Note $reply_to = null, - array $attachments = [], - array $processed_attachments = [], - array $process_note_content_extra_args = [], - bool $notify = true, - ?string $rendered = null, - string $source = 'web', - ): Note { - $note = self::storeLocalNote( + array $targets = [], + null|int|Note $reply_to = null, + array $attachments = [], + array $processed_attachments = [], + array $process_note_content_extra_args = [], + bool $flush_and_notify = true, + ?string $rendered = null, + string $source = 'web', + ): array { + [$activity, $note, $attention_ids] = self::storeLocalNote( actor: $actor, content: $content, content_type: $content_type, @@ -241,11 +241,19 @@ class Posting extends Component attachments: $attachments, processed_attachments: $processed_attachments, process_note_content_extra_args: $process_note_content_extra_args, - notify: $notify, + flush_and_notify: false, rendered: $rendered, source: $source ); - return $note->setType(NoteType::PAGE); + $note->setType(NoteType::PAGE); + + if ($flush_and_notify) { + // Flush before notification + DB::flush(); + Event::handle('NewNotification', [$actor, $activity, ['object' => $attention_ids], _m('{nickname} created a page {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); + } + + return [$activity, $note, $attention_ids]; } /** @@ -263,10 +271,10 @@ class Posting extends Component * @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 $process_note_content_extra_args Extra arguments for the event ProcessNoteContent - * @param bool $notify True if the newly created Note activity should be passed on as a Notification + * @param bool $flush_and_notify True if the newly created Note activity should be passed on as a Notification * @param null|string $rendered The Note's content post RenderNoteContent event, which sanitizes and processes the raw content sent * @param string $source The source of this Note - * @return Note + * @return array [Activity, Note, int[]] Activity, Note, Attention Ids * @throws ClientException * @throws DuplicateFoundException * @throws ServerException @@ -282,10 +290,10 @@ class Posting extends Component array $attachments = [], array $processed_attachments = [], array $process_note_content_extra_args = [], - bool $notify = true, + bool $flush_and_notify = true, ?string $rendered = null, string $source = 'web', - ): Note { + ): array { $scope ??= VisibilityScope::EVERYWHERE; // TODO: If site is private, default to LOCAL $reply_to_id = is_null($reply_to) ? null : (is_int($reply_to) ? $reply_to : $reply_to->getId()); $mentions = []; @@ -360,16 +368,15 @@ class Posting extends Component ]; } - $mention_ids = F\unique(F\flat_map($mentions, fn (array $m) => F\map($m['mentioned'] ?? [], fn (Actor $a) => $a->getId()))); + $attention_ids = F\unique(F\flat_map($mentions, fn (array $m) => F\map($m['mentioned'] ?? [], fn (Actor $a) => $a->getId()))); - // Flush before notification - DB::flush(); - - if ($notify) { - Event::handle('NewNotification', [$actor, $activity, ['object' => $mention_ids], _m('{nickname} created a note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); + if ($flush_and_notify) { + // Flush before notification + DB::flush(); + Event::handle('NewNotification', [$actor, $activity, ['object' => $attention_ids], _m('{nickname} created a note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]); } - return $note; + return [$activity, $note, $attention_ids]; } public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?string $language = null, array &$mentions = []) diff --git a/plugins/RepeatNote/RepeatNote.php b/plugins/RepeatNote/RepeatNote.php index ace7e8ff26..522677ab1d 100644 --- a/plugins/RepeatNote/RepeatNote.php +++ b/plugins/RepeatNote/RepeatNote.php @@ -80,7 +80,7 @@ class RepeatNote extends NoteHandlerPlugin $original_note_id = $note->getId(); // Create a new note with the same content as the original - $repeat = Posting::storeLocalNote( + [, $repeat, ] = Posting::storeLocalNote( actor: Actor::getById($actor_id), content: $note->getContent(), content_type: $note->getContentType(), @@ -88,7 +88,7 @@ class RepeatNote extends NoteHandlerPlugin // If it's a repeat, the reply_to should be to the original, conversation ought to be the same reply_to: $note->getReplyTo(), processed_attachments: $note->getAttachmentsWithTitle(), - notify: false, + flush_and_notify: false, rendered: $note->getRendered(), ); @@ -110,6 +110,9 @@ class RepeatNote extends NoteHandlerPlugin ]); DB::persist($repeat_activity); + // Flush before notification + 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()])]); return $repeat_activity;