[PLUGIN][ActivityPub][Model][Note] fromJson: Respect source attribute and @language from context

This commit is contained in:
Diogo Peralta Cordeiro 2022-03-15 17:48:04 +00:00
parent 27706d63f4
commit dd62825169
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 37 additions and 17 deletions

View File

@ -141,25 +141,33 @@ class Note extends Model
$actor = Explorer::getOneFromUri($type_note->get('attributedTo')); $actor = Explorer::getOneFromUri($type_note->get('attributedTo'));
$actor_id = $actor->getId(); $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 = [ $map = [
'is_local' => false, 'is_local' => false,
'created' => new DateTime($type_note->get('published') ?? 'now'), 'created' => new DateTime($type_note->get('published') ?? 'now'),
'content' => $type_note->get('content') ?? null, 'title' => $type_note->get('name') ?? null,
'rendered' => \is_null($type_note->get('content')) ? null : HTML::sanitize($type_note->get('content')), 'language_id' => $type_note->get('contentLang') ?? null,
'title' => $type_note->get('name') ?? null, 'url' => $type_note->get('url') ?? $type_note->get('id'),
'content_type' => 'text/html', 'actor_id' => $actor_id,
'language_id' => $type_note->get('contentLang') ?? null, 'reply_to' => $reply_to = $handleInReplyTo($type_note),
'url' => $type_note->get('url') ?? $type_note->get('id'), 'modified' => new DateTime(),
'actor_id' => $actor_id, 'type' => match ($type_note->get('type')) {
'reply_to' => $reply_to = $handleInReplyTo($type_note), 'Page' => 'page',
'modified' => new DateTime(), default => 'note'
'type' => match ($type_note->get('type')) {
'Page' => 'page',
default => 'note'
}, },
'source' => $source, 'source' => $source,
]; ];
// Language
if (!\is_null($map['language_id'])) { if (!\is_null($map['language_id'])) {
$map['language_id'] = Language::getByLocale($map['language_id'])->getId(); $map['language_id'] = Language::getByLocale($map['language_id'])->getId();
} else { } 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); $obj = GSNote::create($map);
DB::persist($obj); DB::persist($obj);
@ -303,8 +324,7 @@ class Note extends Model
} }
} }
// The content would be non-sanitized text/html Event::handle('ProcessNoteContent', [$obj, $obj->getContent(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $mention_uris]]);
Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $mention_uris]]);
foreach ($attention_targets as $target) { foreach ($attention_targets as $target) {
DB::persist(Attention::create(['object_type' => GSNote::schemaName(), 'object_id' => $obj->getId(), 'target_id' => $target->getId()])); DB::persist(Attention::create(['object_type' => GSNote::schemaName(), 'object_id' => $obj->getId(), 'target_id' => $target->getId()]));

View File

@ -435,7 +435,7 @@ abstract class Formatting
* *
* @return array [partially-rendered HTML, array of mentions] * @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); $mentions = self::findMentions($text, $author);