[ActivityPub] Port Explorer

This commit is contained in:
2021-10-27 04:14:01 +01:00
parent 5189269e5b
commit 51c984849f
13 changed files with 1112 additions and 310 deletions

View File

@@ -6,10 +6,12 @@ namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
use App\Core\DB\DB;
use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Exception\ClientException;
use App\Util\Formatting;
use Component\FreeNetwork\Entity\FreenetworkActor;
use DateTime;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
abstract class AS2ToEntity
@@ -35,40 +37,56 @@ abstract class AS2ToEntity
*/
public static function store(array $activity, ?string $source = null): array
{
$map = [
'activity_uri' => $activity['id'],
'actor_id' => FreenetworkActor::getOrCreateByRemoteUri(actor_uri: $activity['actor'])->getActorId(),
'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_uri' => $activity['object']['id'],
'is_local' => false,
'created' => new DateTime($activity['published'] ?? 'now'),
'modified' => new DateTime(),
'source' => $source,
];
$act = ActivitypubActivity::getWithPK(['activity_uri' => $activity['id']]);
if (\is_null($act)) {
$actor = ActivityPub::getActorByUri($activity['actor']);
$map = [
'activity_uri' => $activity['id'],
'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_uri' => $activity['object']['id'],
'is_local' => false,
'created' => new DateTime($activity['published'] ?? 'now'),
'modified' => new DateTime(),
'source' => $source,
];
$act = new ActivitypubActivity();
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$act->{$set}($val);
$act = new ActivitypubActivity();
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$act->{$set}($val);
}
$obj = null;
switch ($activity['object']['type']) {
case 'Note':
$obj = AS2ToNote::translate($activity['object'], $source, $activity['actor'], $act);
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
DB::persist($obj);
$act->setObjectId($obj->getId());
DB::persist($act);
} else {
$actor = Actor::getById($act->getActorId());
switch ($activity['object']['type']) {
case 'Note':
$obj = Note::getWithPK(['id' => $act->getObjectId()]);
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
}
$obj = null;
switch ($activity['object']['type']) {
case 'Note':
$obj = AS2ToNote::translate($activity['object'], $source);
break;
default:
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
throw new ClientException('Unsupported Object type.');
}
break;
}
DB::persist($obj);
$act->setObjectId($obj->getId());
DB::persist($act);
return [$act, $obj];
return [$actor, $act, $obj];
}
}

View File

@@ -1,49 +0,0 @@
<?php
declare(strict_types = 1);
namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Formatting;
use DateTime;
use Exception;
abstract class AS2ToGSActor
{
/**
* @throws Exception
*
* @return Note
*/
public static function translate(array $args, ?string $source = null): Actor
{
$map = [
'isLocal' => false,
'created' => new DateTime($args['published'] ?? 'now'),
'content' => $args['content'] ?? null,
'content_type' => 'text/html',
'rendered' => null,
'modified' => new DateTime(),
'source' => $source,
];
if ($map['content'] !== null) {
Event::handle('RenderNoteContent', [
$map['content'],
$map['content_type'],
&$map['rendered'],
Actor::getById(1), // just for testing
null, // reply to
]);
}
$obj = new Note();
foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$obj->{$set}($val);
}
return $obj;
}
}

View File

@@ -8,19 +8,24 @@ use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Formatting;
use Component\FreeNetwork\Entity\FreenetworkActor;
use DateTime;
use Exception;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
abstract class AS2ToNote
{
/**
*@throws Exception
*/
public static function translate(array $object, ?string $source = null): Note
public static function translate(array $object, ?string $source, ?string $actor_uri, ?ActivitypubActivity $act = null): Note
{
$actor_id = FreenetworkActor::getOrCreateByRemoteUri(actor_uri: $object['attributedTo'])->getActorId();
$map = [
if (isset($actor_uri) && $actor_uri === $object['attributedTo']) {
$actor_id = $act->getActorId();
} else {
$actor_id = ActivityPub::getActorByUri($object['attributedTo'])->getId();
}
$map = [
'is_local' => false,
'created' => new DateTime($object['published'] ?? 'now'),
'content' => $object['content'] ?? null,