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