[ActivityStreamsTwo] Introduce a structure for data representation in ActivityStreams 2.0
Type factory borrowed from landrok/activitypub
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Model\AS2ToEntity;
|
||||
|
||||
use App\Core\Entity;
|
||||
|
||||
abstract class AS2ToEntity
|
||||
{
|
||||
/**
|
||||
* @param array $activity
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public static function translate(array $activity): Entity
|
||||
{
|
||||
return match ($activity['type']) {
|
||||
'Note' => AS2ToNote::translate($activity),
|
||||
default => Entity::create($activity),
|
||||
};
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Model\AS2ToEntity;
|
||||
|
||||
use App\Core\Security;
|
||||
use App\Entity\Note;
|
||||
use DateTime;
|
||||
|
||||
abstract class AS2ToNote
|
||||
{
|
||||
/**
|
||||
* @param array $args
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return Note
|
||||
*/
|
||||
public static function translate(array $args): Note
|
||||
{
|
||||
$map = [
|
||||
'isLocal' => false,
|
||||
'created' => new DateTime($args['published'] ?? 'now'),
|
||||
'rendered' => $args['content'] ?? null,
|
||||
'modified' => new DateTime(),
|
||||
];
|
||||
if (!is_null($map['rendered'])) {
|
||||
$map['content'] = Security::sanitize($map['rendered']);
|
||||
}
|
||||
|
||||
$obj = new Note();
|
||||
foreach ($map as $prop => $val) {
|
||||
$set = "set{$prop}";
|
||||
$obj->{$set}($val);
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Model\EntityToType;
|
||||
|
||||
use Plugin\ActivityStreamsTwo\Util\Type;
|
||||
|
||||
abstract class EntityToType
|
||||
{
|
||||
/**
|
||||
* @param $entity
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public static function translate($entity)
|
||||
{
|
||||
switch ($entity::class) {
|
||||
case 'Note':
|
||||
return NoteToType::translate($entity);
|
||||
default:
|
||||
$map = [
|
||||
'type' => 'Object',
|
||||
];
|
||||
return Type::create($map);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Model\EntityToType;
|
||||
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\Note;
|
||||
use DateTimeInterface;
|
||||
use Plugin\ActivityStreamsTwo\Util\Type;
|
||||
|
||||
class NoteToType
|
||||
{
|
||||
/**
|
||||
* @param $entity
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public static function translate(Note $note)
|
||||
{
|
||||
$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' => Router::url('actor', ['id' => $note->getGSActorId()]),
|
||||
//'to' => $to,
|
||||
//'cc' => $cc,
|
||||
'content' => json_encode($note->getContent()), // change to rendered
|
||||
//'tag' => $tags
|
||||
];
|
||||
return Type::create(type: 'Note', attributes: $attr);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user