forked from GNUsocial/gnu-social
[ActivityStreamsTwo] Initial Actor support
Various bug fixes
This commit is contained in:
@@ -5,6 +5,8 @@ namespace Plugin\ActivityStreamsTwo;
|
||||
use App\Core\Event;
|
||||
use App\Core\Modules\Plugin;
|
||||
use App\Core\Router\RouteLoader;
|
||||
use Exception;
|
||||
use Plugin\ActivityStreamsTwo\Util\Response\ActorResponse;
|
||||
use Plugin\ActivityStreamsTwo\Util\Response\NoteResponse;
|
||||
use Plugin\ActivityStreamsTwo\Util\Response\TypeResponse;
|
||||
|
||||
@@ -15,7 +17,7 @@ class ActivityStreamsTwo extends Plugin
|
||||
return '0.1.0';
|
||||
}
|
||||
|
||||
public array $accept = [
|
||||
public static array $accept_headers = [
|
||||
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
||||
'application/activity+json',
|
||||
'application/json',
|
||||
@@ -24,23 +26,28 @@ class ActivityStreamsTwo extends Plugin
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array $accept
|
||||
* @param array $accept_header
|
||||
* @param array $vars
|
||||
* @param null|TypeResponse $response
|
||||
*
|
||||
* @throws \Exception
|
||||
*@throws Exception
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
*/
|
||||
public function onRouteInFormat(string $route, array $accept, array $vars, ?TypeResponse &$response = null): bool
|
||||
public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool
|
||||
{
|
||||
if (empty(array_intersect($this->accept, $accept))) {
|
||||
if (count(array_intersect(self::$accept_headers, $accept_header)) === 0) {
|
||||
return Event::next;
|
||||
}
|
||||
switch ($route) {
|
||||
case 'note_show':
|
||||
case 'note_view':
|
||||
$response = NoteResponse::handle($vars['note']);
|
||||
return Event::stop;
|
||||
case 'gsactor_view_id':
|
||||
case 'gsactor_view_nickname':
|
||||
$response = ActorResponse::handle($vars['gsactor']);
|
||||
return Event::stop;
|
||||
default:
|
||||
return Event::next;
|
||||
}
|
||||
@@ -56,11 +63,11 @@ class ActivityStreamsTwo extends Plugin
|
||||
*/
|
||||
public function onAddRoute(RouteLoader $r): bool
|
||||
{
|
||||
$r->connect('note_view_as2',
|
||||
/*$r->connect('note_view_as2',
|
||||
'/note/{id<\d+>}',
|
||||
[NoteResponse::class, 'handle'],
|
||||
options: ['accept' => $this->accept]
|
||||
);
|
||||
options: ['accept' => self::$accept_headers]
|
||||
);*/
|
||||
return Event::next;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Model\EntityToType;
|
||||
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\GSActor;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use Plugin\ActivityStreamsTwo\Util\Type;
|
||||
|
||||
class GSActorToType
|
||||
{
|
||||
/**
|
||||
* @param GSActor $gsactor
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public static function translate(GSActor $gsactor)
|
||||
{
|
||||
$uri = Router::url('gsactor_view_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL);
|
||||
$attr = [
|
||||
'@context' => 'https://www.w3.org/ns/activitystreams',
|
||||
'id' => $uri,
|
||||
//'inbox' =>
|
||||
//'outbox' =>
|
||||
//'following' =>
|
||||
//'followers' =>
|
||||
//'liked' =>
|
||||
//'streams' =>
|
||||
'preferredUsername' => $gsactor->getNickname(),
|
||||
//'publicKey' => [
|
||||
// 'id' => $uri . "#public-key",
|
||||
// 'owner' => $uri,
|
||||
// 'publicKeyPem' => $public_key
|
||||
// ],
|
||||
'name' => $gsactor->getFullname(),
|
||||
//'icon' =>
|
||||
//'location' =>
|
||||
'published' => $gsactor->getCreated()->format(DateTimeInterface::RFC3339),
|
||||
'summary' => $gsactor->getBio(),
|
||||
//'tag' =>
|
||||
'updated' => $gsactor->getModified()->format(DateTimeInterface::RFC3339),
|
||||
'url' => Router::url('gsactor_view_nickname', ['nickname' => $gsactor->getNickname()], Router::ABSOLUTE_URL),
|
||||
];
|
||||
return Type::create(type: 'Person', attributes: $attr);
|
||||
}
|
||||
}
|
24
plugins/ActivityStreamsTwo/Util/Response/ActorResponse.php
Normal file
24
plugins/ActivityStreamsTwo/Util/Response/ActorResponse.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Plugin\ActivityStreamsTwo\Util\Response;
|
||||
|
||||
use App\Entity\GSActor;
|
||||
use Exception;
|
||||
use Plugin\ActivityStreamsTwo\Util\Model\EntityToType\GSActorToType;
|
||||
|
||||
abstract class ActorResponse
|
||||
{
|
||||
/**
|
||||
* @param GSActor $gsactor
|
||||
* @param int $status The response status code
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return TypeResponse
|
||||
*/
|
||||
public static function handle(GSActor $gsactor, int $status = 200): TypeResponse
|
||||
{
|
||||
$gsactor->getLocalUser(); // This throws exception if not a local user, which is intended
|
||||
return new TypeResponse(data: GSActorToType::translate($gsactor), status: $status);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user