[COMPONENT][Posting] Page should flush with a different notification

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-16 19:35:27 +00:00
parent b6ed0b4c6c
commit b69f4a46c5
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 40 additions and 30 deletions

View File

@ -139,7 +139,7 @@ class Post extends Controller
$extra_args = []; $extra_args = [];
Event::handle('AddExtraArgsToNoteContent', [$request, $actor, $data, &$extra_args, $form_params, $form]); Event::handle('AddExtraArgsToNoteContent', [$request, $actor, $data, &$extra_args, $form_params, $form]);
$note = Posting::storeLocalPage( [,$note,] = Posting::storeLocalPage(
actor: $actor, actor: $actor,
content: $data['content'], content: $data['content'],
content_type: $content_type, content_type: $content_type,

View File

@ -226,11 +226,11 @@ class Posting extends Component
array $attachments = [], array $attachments = [],
array $processed_attachments = [], array $processed_attachments = [],
array $process_note_content_extra_args = [], array $process_note_content_extra_args = [],
bool $notify = true, bool $flush_and_notify = true,
?string $rendered = null, ?string $rendered = null,
string $source = 'web', string $source = 'web',
): Note { ): array {
$note = self::storeLocalNote( [$activity, $note, $attention_ids] = self::storeLocalNote(
actor: $actor, actor: $actor,
content: $content, content: $content,
content_type: $content_type, content_type: $content_type,
@ -241,11 +241,19 @@ class Posting extends Component
attachments: $attachments, attachments: $attachments,
processed_attachments: $processed_attachments, processed_attachments: $processed_attachments,
process_note_content_extra_args: $process_note_content_extra_args, process_note_content_extra_args: $process_note_content_extra_args,
notify: $notify, flush_and_notify: false,
rendered: $rendered, rendered: $rendered,
source: $source 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 $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
* @param array $process_note_content_extra_args Extra arguments for the event ProcessNoteContent * @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 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 * @param string $source The source of this Note
* @return Note * @return array [Activity, Note, int[]] Activity, Note, Attention Ids
* @throws ClientException * @throws ClientException
* @throws DuplicateFoundException * @throws DuplicateFoundException
* @throws ServerException * @throws ServerException
@ -282,10 +290,10 @@ class Posting extends Component
array $attachments = [], array $attachments = [],
array $processed_attachments = [], array $processed_attachments = [],
array $process_note_content_extra_args = [], array $process_note_content_extra_args = [],
bool $notify = true, bool $flush_and_notify = true,
?string $rendered = null, ?string $rendered = null,
string $source = 'web', string $source = 'web',
): Note { ): array {
$scope ??= VisibilityScope::EVERYWHERE; // TODO: If site is private, default to LOCAL $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()); $reply_to_id = is_null($reply_to) ? null : (is_int($reply_to) ? $reply_to : $reply_to->getId());
$mentions = []; $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())));
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 note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $activity->getObjectId()])]);
if ($notify) {
Event::handle('NewNotification', [$actor, $activity, ['object' => $mention_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 = []) public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?string $language = null, array &$mentions = [])

View File

@ -80,7 +80,7 @@ class RepeatNote extends NoteHandlerPlugin
$original_note_id = $note->getId(); $original_note_id = $note->getId();
// 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::getById($actor_id),
content: $note->getContent(), content: $note->getContent(),
content_type: $note->getContentType(), 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 // If it's a repeat, the reply_to should be to the original, conversation ought to be the same
reply_to: $note->getReplyTo(), reply_to: $note->getReplyTo(),
processed_attachments: $note->getAttachmentsWithTitle(), processed_attachments: $note->getAttachmentsWithTitle(),
notify: false, flush_and_notify: false,
rendered: $note->getRendered(), rendered: $note->getRendered(),
); );
@ -110,6 +110,9 @@ class RepeatNote extends NoteHandlerPlugin
]); ]);
DB::persist($repeat_activity); 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()])]); 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; return $repeat_activity;