[ActivityStreamsTwo] Further work on routes and use render event on note's content

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-20 17:04:24 +01:00
parent 7813723ca1
commit 9e4c43e8fd
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
7 changed files with 101 additions and 32 deletions

View File

@ -47,13 +47,16 @@ class ActivityStreamsTwo extends Plugin
case 'note_view': case 'note_view':
$response = NoteResponse::handle($vars['note']); $response = NoteResponse::handle($vars['note']);
return Event::stop; return Event::stop;
case 'actor_favourites': case 'actor_favourites_id':
case 'actor_favourites_nickname':
$response = LikeResponse::handle($vars['actor']); $response = LikeResponse::handle($vars['actor']);
return Event::stop; return Event::stop;
case 'actor_subscriptions': case 'actor_subscriptions_id':
case 'actor_subscriptions_nickname':
$response = FollowingResponse::handle($vars['actor']); $response = FollowingResponse::handle($vars['actor']);
return Event::stop; return Event::stop;
case 'actor_subscribers': case 'actor_subscribers_id':
case 'actor_subscribers_nickname':
$response = FollowersResponse::handle($vars['actor']); $response = FollowersResponse::handle($vars['actor']);
return Event::stop; return Event::stop;
default: default:

View File

@ -11,10 +11,10 @@ abstract class AS2ToEntity
* *
* @return Entity * @return Entity
*/ */
public static function translate(array $activity): Entity public static function translate(array $activity, ?string $source = null): Entity
{ {
return match ($activity['type']) { return match ($activity['type']) {
'Note' => AS2ToNote::translate($activity), 'Note' => AS2ToNote::translate($activity, $source),
default => Entity::create($activity), default => Entity::create($activity),
}; };
} }

View File

@ -0,0 +1,48 @@
<?php
namespace Plugin\ActivityStreamsTwo\Util\Model\AS2ToEntity;
use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Formatting;
use DateTime;
abstract class AS2ToGSActor
{
/**
* @param array $args
*
* @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::getFromId(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

@ -2,8 +2,10 @@
namespace Plugin\ActivityStreamsTwo\Util\Model\AS2ToEntity; namespace Plugin\ActivityStreamsTwo\Util\Model\AS2ToEntity;
use App\Core\Security; use App\Core\Event;
use App\Entity\Actor;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Formatting;
use DateTime; use DateTime;
abstract class AS2ToNote abstract class AS2ToNote
@ -15,21 +17,30 @@ abstract class AS2ToNote
* *
* @return Note * @return Note
*/ */
public static function translate(array $args): Note public static function translate(array $args, ?string $source = null): Note
{ {
$map = [ $map = [
'isLocal' => false, 'isLocal' => false,
'created' => new DateTime($args['published'] ?? 'now'), 'created' => new DateTime($args['published'] ?? 'now'),
'rendered' => $args['content'] ?? null, 'content' => $args['content'] ?? null,
'content_type' => 'text/html',
'rendered' => null,
'modified' => new DateTime(), 'modified' => new DateTime(),
'source' => $source,
]; ];
if (!is_null($map['rendered'])) { if ($map['content'] !== null) {
$map['content'] = Security::sanitize($map['rendered']); Event::handle('RenderNoteContent', [
$map['content'],
$map['content_type'],
&$map['rendered'],
Actor::getFromId(1), // just for testing
null, // reply to
]);
} }
$obj = new Note(); $obj = new Note();
foreach ($map as $prop => $val) { foreach ($map as $prop => $val) {
$set = "set{$prop}"; $set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$obj->{$set}($val); $obj->{$set}($val);
} }
return $obj; return $obj;

View File

@ -4,6 +4,8 @@ namespace Plugin\ActivityStreamsTwo\Util\Model\EntityToType;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor; use App\Entity\Actor;
use Component\Avatar\Avatar;
use Component\Avatar\Exception\NoAvatarException;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use Plugin\ActivityStreamsTwo\Util\Type; use Plugin\ActivityStreamsTwo\Util\Type;
@ -24,11 +26,11 @@ class GSActorToType
$attr = [ $attr = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $uri, 'id' => $uri,
//'inbox' => 'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'outbox' => 'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'following' => 'following' => Router::url('actor_subscriptions_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'followers' => 'followers' => Router::url('actor_subscribers_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'liked' => 'liked' => Router::url('actor_favourites_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'streams' => //'streams' =>
'preferredUsername' => $gsactor->getNickname(), 'preferredUsername' => $gsactor->getNickname(),
//'publicKey' => [ //'publicKey' => [
@ -37,14 +39,19 @@ class GSActorToType
// 'publicKeyPem' => $public_key // 'publicKeyPem' => $public_key
// ], // ],
'name' => $gsactor->getFullname(), 'name' => $gsactor->getFullname(),
//'icon' => 'location' => $gsactor->getLocation(),
//'location' =>
'published' => $gsactor->getCreated()->format(DateTimeInterface::RFC3339), 'published' => $gsactor->getCreated()->format(DateTimeInterface::RFC3339),
'summary' => $gsactor->getBio(), 'summary' => $gsactor->getBio(),
//'tag' => //'tag' => $gsactor->getSelfTags(),
'updated' => $gsactor->getModified()->format(DateTimeInterface::RFC3339), 'updated' => $gsactor->getModified()->format(DateTimeInterface::RFC3339),
'url' => Router::url('actor_view_nickname', ['nickname' => $gsactor->getNickname()], Router::ABSOLUTE_URL), 'url' => Router::url('actor_view_nickname', ['nickname' => $gsactor->getNickname()], Router::ABSOLUTE_URL),
]; ];
try {
$attr['icon'] = Avatar::getAvatar($gsactor->getId())->getUrl(type: Router::ABSOLUTE_URL);
} catch (NoAvatarException) {
// No icon for this actor
}
return Type::create(type: 'Person', attributes: $attr); return Type::create(type: 'Person', attributes: $attr);
} }
} }

View File

@ -22,10 +22,10 @@ class NoteToType
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL), 'id' => Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL),
'published' => $note->getCreated()->format(DateTimeInterface::RFC3339), 'published' => $note->getCreated()->format(DateTimeInterface::RFC3339),
//'attributedTo' => Router::url('actor', ['id' => $note->getGSActorId()]), 'attributedTo' => Router::url('actor_view_id', ['id' => $note->getActorId()], Router::ABSOLUTE_URL),
//'to' => $to, //'to' => $to,
//'cc' => $cc, //'cc' => $cc,
'content' => json_encode($note->getContent()), // change to rendered 'content' => json_encode($note->getRendered()),
//'tag' => $tags //'tag' => $tags
]; ];
return Type::create(type: 'Note', attributes: $attr); return Type::create(type: 'Note', attributes: $attr);

View File

@ -125,8 +125,8 @@ class Favourite extends NoteHandlerPlugin
{ {
$r->connect(id: 'actor_favourites_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorId']); $r->connect(id: 'actor_favourites_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorId']);
$r->connect(id: 'actor_reverse_favourites_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorId']); $r->connect(id: 'actor_reverse_favourites_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorId']);
$r->connect('actor_favourites_nickname', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', [Controller\Favourite::class, 'favouritesByActorNickname']); $r->connect(id:'actor_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorNickname']);
$r->connect('actor_reverse_favourites_nickname', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', [Controller\Favourite::class, 'reverseFavouritesByActorNickname']); $r->connect(id: 'actor_reverse_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']);
return Event::next; return Event::next;
} }
} }