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;
|
|
|
|
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-10-27 12:14:01 +09:00
|
|
|
public static function translate(array $object, ?string $source, ?string $actor_uri, ?ActivitypubActivity $act = null): Note
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-10-27 12:14:01 +09:00
|
|
|
if (isset($actor_uri) && $actor_uri === $object['attributedTo']) {
|
|
|
|
$actor_id = $act->getActorId();
|
|
|
|
} else {
|
|
|
|
$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-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) {
|
|
|
|
Event::handle('RenderNoteContent', [
|
|
|
|
$map['content'],
|
|
|
|
$map['content_type'],
|
|
|
|
&$map['rendered'],
|
|
|
|
Actor::getById($actor_id),
|
|
|
|
null, // TODO reply to
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new Note();
|
|
|
|
foreach ($map as $prop => $val) {
|
|
|
|
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
|
|
|
$obj->{$set}($val);
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
2021-10-10 17:26:18 +09:00
|
|
|
}
|