From dd628251696a0f9b9bd7aeab97739057ce1152a8 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 15 Mar 2022 17:48:04 +0000 Subject: [PATCH] [PLUGIN][ActivityPub][Model][Note] fromJson: Respect source attribute and @language from context --- plugins/ActivityPub/Util/Model/Note.php | 52 +++++++++++++++++-------- src/Util/Formatting.php | 2 +- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index b510cdd8e7..a4080fe276 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -141,25 +141,33 @@ class Note extends Model $actor = Explorer::getOneFromUri($type_note->get('attributedTo')); $actor_id = $actor->getId(); } + + // Figure the locale of the note when possible + $locale = null; + if (\array_key_exists('@language', $type_note->get('@context'))) { + $locale = $type_note->get('@context')['@language']; + if ($locale === 'und') { + $locale = null; + } + } + $map = [ - 'is_local' => false, - 'created' => new DateTime($type_note->get('published') ?? 'now'), - 'content' => $type_note->get('content') ?? null, - 'rendered' => \is_null($type_note->get('content')) ? null : HTML::sanitize($type_note->get('content')), - 'title' => $type_note->get('name') ?? null, - 'content_type' => 'text/html', - 'language_id' => $type_note->get('contentLang') ?? null, - 'url' => $type_note->get('url') ?? $type_note->get('id'), - 'actor_id' => $actor_id, - 'reply_to' => $reply_to = $handleInReplyTo($type_note), - 'modified' => new DateTime(), - 'type' => match ($type_note->get('type')) { - 'Page' => 'page', - default => 'note' + 'is_local' => false, + 'created' => new DateTime($type_note->get('published') ?? 'now'), + 'title' => $type_note->get('name') ?? null, + 'language_id' => $type_note->get('contentLang') ?? null, + 'url' => $type_note->get('url') ?? $type_note->get('id'), + 'actor_id' => $actor_id, + 'reply_to' => $reply_to = $handleInReplyTo($type_note), + 'modified' => new DateTime(), + 'type' => match ($type_note->get('type')) { + 'Page' => 'page', + default => 'note' }, 'source' => $source, ]; + // Language if (!\is_null($map['language_id'])) { $map['language_id'] = Language::getByLocale($map['language_id'])->getId(); } else { @@ -230,6 +238,19 @@ class Note extends Model } } + // content and rendered + $map['content'] = $type_note->get('content') ?? null; + $map['content_type'] = \is_null($map['content']) ? null : 'text/html'; + $map['rendered'] = \is_null($map['content']) ? null : HTML::sanitize($map['content']); + if ($type_note->has('source')) { + $map['content_type'] = $type_note->get('source')['mediaType'] ?? null; + $map['content'] = $type_note->get('source')['content']; + if (\is_null($map['rendered'])) { + // It doesn't have the content pre-rendered, we can render it ourselves then + Event::handle('RenderNoteContent', [$map['content'], $map['content_type'], &$map['rendered'], $actor, $locale, /*&$mentions = */ []]); + } + } + $obj = GSNote::create($map); DB::persist($obj); @@ -303,8 +324,7 @@ class Note extends Model } } - // The content would be non-sanitized text/html - Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $mention_uris]]); + Event::handle('ProcessNoteContent', [$obj, $obj->getContent(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $mention_uris]]); foreach ($attention_targets as $target) { DB::persist(Attention::create(['object_type' => GSNote::schemaName(), 'object_id' => $obj->getId(), 'target_id' => $target->getId()])); diff --git a/src/Util/Formatting.php b/src/Util/Formatting.php index 9d0b7e98ed..813033e108 100644 --- a/src/Util/Formatting.php +++ b/src/Util/Formatting.php @@ -435,7 +435,7 @@ abstract class Formatting * * @return array [partially-rendered HTML, array of mentions] */ - public static function linkifyMentions(string $text, Actor $author, string $locale): array + public static function linkifyMentions(string $text, Actor $author, ?string $locale): array { $mentions = self::findMentions($text, $author);