[ActivityPub] Add ActivityToType

Minor bug fixes
This commit is contained in:
2021-11-30 00:46:29 +00:00
parent 4ddd00a091
commit df3fbbc9e7
7 changed files with 151 additions and 7 deletions

View File

@@ -25,9 +25,9 @@ abstract class AS2ToEntity
};
}
public static function activity_stream_two_object_type_to_gs_table($verb)
public static function activity_stream_two_object_type_to_gs_table($object)
{
return match ($verb) {
return match ($object) {
'Note' => 'note',
default => throw new ClientException('Invalid verb'),
};

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Plugin\ActivityPub\Util\Model\EntityToType;
use App\Core\Router\Router;
use App\Entity\Activity;
use App\Util\Exception\ClientException;
use DateTimeInterface;
use Exception;
use Plugin\ActivityPub\Util\Type;
class ActivityToType
{
public static function gs_verb_to_activity_stream_two_verb($verb)
{
return match ($verb) {
'create' => 'Create',
default => throw new ClientException('Invalid verb'),
};
}
/**
* @throws Exception
*/
public static function translate(Activity $activity): Type\Core\Activity
{
$attr = [
'type' => self::gs_verb_to_activity_stream_two_verb($activity->getVerb()),
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('activity_view', ['id' => $activity->getId()], Router::ABSOLUTE_URL),
'published' => $activity->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $activity->getActor()->getUri(Router::ABSOLUTE_URL),
//'to' => $to,
//'cc' => $cc,
'object' => $activity->getObject()->getUrl(),
];
return Type::create($attr);
}
}

View File

@@ -11,12 +11,15 @@ use Plugin\ActivityPub\Util\Type;
abstract class EntityToType
{
/**
* @return Type
* @throws Exception
*/
public static function translate(Entity $entity): Type
public static function translate(Entity $entity): mixed
{
switch ($entity::class) {
case 'Note':
case 'App\Entity\Activity':
return ActivityToType::translate($entity);
case 'App\Entity\Note':
return NoteToType::translate($entity);
default:
$map = [

View File

@@ -17,12 +17,11 @@ class NoteToType
*/
public static function translate(Note $note): Type\Extended\Object\Note
{
$attributedTo = null;
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL),
'published' => $note->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $attributedTo,
'attributedTo' => $note->getActor()->getUri(Router::ABSOLUTE_URL),
//'to' => $to,
//'cc' => $cc,
'content' => json_encode($note->getRendered()),