2021-10-05 01:00:58 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
|
|
|
|
|
|
|
|
use App\Core\Event;
|
|
|
|
use App\Entity\Actor;
|
2021-11-28 00:06:46 +09:00
|
|
|
use App\Entity\Language;
|
2021-10-05 01:00:58 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Formatting;
|
|
|
|
use DateTime;
|
2021-10-10 17:26:18 +09:00
|
|
|
use Exception;
|
2021-10-27 12:14:01 +09:00
|
|
|
use Plugin\ActivityPub\ActivityPub;
|
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
2021-10-05 01:00:58 +09:00
|
|
|
|
|
|
|
abstract class AS2ToNote
|
|
|
|
{
|
|
|
|
/**
|
2021-10-10 17:26:18 +09:00
|
|
|
*@throws Exception
|
2021-10-05 01:00:58 +09:00
|
|
|
*/
|
2021-11-28 00:06:46 +09:00
|
|
|
public static function translate(array $object, ?string $source, ?string $actor_uri = null, ?int $actor_id = null): Note
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-11-28 00:06:46 +09:00
|
|
|
if (is_null($actor_uri) || $actor_uri !== $object['attributedTo']) {
|
2021-10-27 12:14:01 +09:00
|
|
|
$actor_id = ActivityPub::getActorByUri($object['attributedTo'])->getId();
|
|
|
|
}
|
|
|
|
$map = [
|
2021-10-05 01:00:58 +09:00
|
|
|
'is_local' => false,
|
|
|
|
'created' => new DateTime($object['published'] ?? 'now'),
|
|
|
|
'content' => $object['content'] ?? null,
|
|
|
|
'content_type' => 'text/html',
|
2021-12-03 00:12:31 +09:00
|
|
|
'language_id' => $object['contentLang'] ?? null,
|
2021-10-10 17:26:18 +09:00
|
|
|
'url' => \array_key_exists('url', $object) ? $object['url'] : $object['id'],
|
2021-10-05 01:00:58 +09:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'modified' => new DateTime(),
|
|
|
|
'source' => $source,
|
|
|
|
];
|
|
|
|
if ($map['content'] !== null) {
|
2021-11-28 00:06:46 +09:00
|
|
|
$mentions = [];
|
2021-10-05 01:00:58 +09:00
|
|
|
Event::handle('RenderNoteContent', [
|
|
|
|
$map['content'],
|
|
|
|
$map['content_type'],
|
|
|
|
&$map['rendered'],
|
|
|
|
Actor::getById($actor_id),
|
2021-11-28 00:06:46 +09:00
|
|
|
$map['language_id'],
|
|
|
|
&$mentions,
|
2021-10-05 01:00:58 +09:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new Note();
|
2021-11-28 00:06:46 +09:00
|
|
|
|
|
|
|
if (!is_null($map['language_id'])) {
|
|
|
|
$map['language_id'] = Language::getFromLocale($map['language_id'])->getId();
|
|
|
|
} else {
|
|
|
|
$map['language_id'] = null;
|
|
|
|
}
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
foreach ($map as $prop => $val) {
|
|
|
|
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
|
|
|
$obj->{$set}($val);
|
|
|
|
}
|
2021-12-03 00:12:31 +09:00
|
|
|
|
|
|
|
Event::handle('NewNoteFromActivityStreamsTwo', [$source, $obj, $actor_id]);
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
return $obj;
|
|
|
|
}
|
2021-10-10 17:26:18 +09:00
|
|
|
}
|