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\DB\DB;
|
|
|
|
use App\Core\Event;
|
2021-11-28 00:06:46 +09:00
|
|
|
use App\Entity\Activity;
|
2021-10-27 12:14:01 +09:00
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Entity\Note;
|
2021-10-05 01:00:58 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
|
|
|
use App\Util\Formatting;
|
|
|
|
use DateTime;
|
2021-10-27 12:14:01 +09:00
|
|
|
use Plugin\ActivityPub\ActivityPub;
|
2021-10-05 01:00:58 +09:00
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
|
|
|
|
|
|
|
abstract class AS2ToEntity
|
|
|
|
{
|
|
|
|
public static function activity_stream_two_verb_to_gs_verb($verb)
|
|
|
|
{
|
|
|
|
return match ($verb) {
|
|
|
|
'Create' => 'create',
|
|
|
|
default => throw new ClientException('Invalid verb'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function activity_stream_two_object_type_to_gs_table($verb)
|
|
|
|
{
|
|
|
|
return match ($verb) {
|
|
|
|
'Note' => 'note',
|
|
|
|
default => throw new ClientException('Invalid verb'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ClientException
|
|
|
|
*/
|
2021-11-28 00:06:46 +09:00
|
|
|
public static function store(array $activity, ?string $source = null): ActivitypubActivity
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-11-28 00:06:46 +09:00
|
|
|
$ap_act = ActivitypubActivity::getWithPK(['activity_uri' => $activity['id']]);
|
|
|
|
if (\is_null($ap_act)) {
|
2021-10-27 12:14:01 +09:00
|
|
|
$actor = ActivityPub::getActorByUri($activity['actor']);
|
2021-11-28 00:06:46 +09:00
|
|
|
// Store Object
|
2021-10-27 12:14:01 +09:00
|
|
|
$obj = null;
|
|
|
|
switch ($activity['object']['type']) {
|
|
|
|
case 'Note':
|
2021-11-28 00:06:46 +09:00
|
|
|
$obj = AS2ToNote::translate($activity['object'], $source, $activity['actor'], $actor->getId());
|
2021-10-27 12:14:01 +09:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
|
|
|
|
throw new ClientException('Unsupported Object type.');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DB::persist($obj);
|
2021-11-28 00:06:46 +09:00
|
|
|
// Store Activity
|
|
|
|
$act = Activity::create([
|
|
|
|
'actor_id' => $actor->getId(),
|
|
|
|
'verb' => self::activity_stream_two_verb_to_gs_verb($activity['type']),
|
|
|
|
'object_type' => self::activity_stream_two_object_type_to_gs_table($activity['object']['type']),
|
|
|
|
'object_id' => $obj->getId(),
|
|
|
|
'is_local' => false,
|
|
|
|
'created' => new DateTime($activity['published'] ?? 'now'),
|
|
|
|
'source' => $source,
|
|
|
|
]);
|
2021-10-27 12:14:01 +09:00
|
|
|
DB::persist($act);
|
2021-11-28 00:06:46 +09:00
|
|
|
// Store ActivityPub Activity
|
|
|
|
$ap_act = ActivitypubActivity::create([
|
|
|
|
'activity_id' => $act->getId(),
|
|
|
|
'activity_uri' => $activity['id'],
|
|
|
|
'object_uri' => $activity['object']['id'],
|
|
|
|
'is_local' => false,
|
|
|
|
'created' => new DateTime($activity['published'] ?? 'now'),
|
|
|
|
'modified' => new DateTime(),
|
|
|
|
]);
|
|
|
|
DB::persist($ap_act);
|
2021-10-27 12:14:01 +09:00
|
|
|
}
|
2021-10-05 01:00:58 +09:00
|
|
|
|
2021-11-28 00:06:46 +09:00
|
|
|
return $ap_act;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
2021-10-10 17:26:18 +09:00
|
|
|
}
|