[ActivityStreamsTwo] Initial Actor support

Various bug fixes
This commit is contained in:
Diogo Peralta Cordeiro 2021-09-14 17:15:37 +01:00
parent 1f3a6fe6ac
commit 365edbaff0
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
10 changed files with 217 additions and 34 deletions

View File

@ -7,6 +7,7 @@ use App\Core\Modules\Plugin;
use App\Core\Router\RouteLoader;
use Exception;
use Plugin\ActivityPub\Controller\Inbox;
use Plugin\ActivityStreamsTwo\ActivityStreamsTwo;
class ActivityPub extends Plugin
{
@ -15,13 +16,6 @@ class ActivityPub extends Plugin
return '3.0.0';
}
public static array $accept_headers = [
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'application/activity+json',
'application/json',
'application/ld+json',
];
/**
* This code executes when GNU social creates the page routing, and we hook
* on this event to add our action handler for Embed.
@ -36,7 +30,7 @@ class ActivityPub extends Plugin
'activitypub_inbox',
'{gsactor_id<\d+>}/inbox',
[Inbox::class, 'handle'],
options: ['accept' => self::$accept_headers]
options: ['accept' => ActivityStreamsTwo::$accept_headers]
);
return Event::next;
}
@ -60,7 +54,7 @@ class ActivityPub extends Plugin
} elseif (is_array($accept)
&& count(
array_intersect($accept, self::$accept_headers)
)
) > 0
) {
return true;
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View 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);
}
}

View File

@ -0,0 +1,69 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Util\Exception\ClientException;
use Symfony\Component\HttpFoundation\Request;
class GSActor extends Controller
{
/**
* Generic function that handles getting a representation for an actor from id
*/
private function GSActorById(int $id, callable $handle)
{
$gsactor = DB::findOneBy('gsactor', ['id' => $id]);
if (empty($gsactor)) {
throw new ClientException(_m('No such actor.'), 404);
} else {
return $handle($gsactor);
}
}
/**
* Generic function that handles getting a representation for an actor from nickname
*/
private function GSActorByNickname(string $nickname, callable $handle)
{
$user = DB::findOneBy('local_user', ['nickname' => $nickname]);
$gsactor = DB::findOneBy('gsactor', ['id' => $user->getId()]);
if (empty($gsactor)) {
throw new ClientException(_m('No such actor.'), 404);
} else {
return $handle($gsactor);
}
}
/**
* The page where the note and it's info is shown
*/
public function GSActorShowId(Request $request, int $id)
{
return $this->GSActorById($id, fn ($gsactor) => ['_template' => 'actor/view.html.twig', 'gsactor' => $gsactor]);
}
public function GSActorShowNickname(Request $request, string $nickname)
{
return $this->GSActorByNickname($nickname, fn ($gsactor) => ['_template' => 'actor/view.html.twig', 'gsactor' => $gsactor]);
}
}

View File

@ -36,7 +36,7 @@ class Note extends Controller
{
$note = DB::findOneBy('note', ['id' => $id]);
if (empty($note)) {
throw new ClientException(_m('No such note'), 404);
throw new ClientException(_m('No such note.'), 404);
} else {
return $handle($note);
}
@ -45,7 +45,7 @@ class Note extends Controller
/**
* The page where the note and it's info is shown
*/
public function note_show(Request $request, int $id)
public function NoteShow(Request $request, int $id)
{
return $this->note($id, fn ($note) => ['_template' => 'note/view.html.twig', 'note' => $note]);
}

View File

@ -126,7 +126,7 @@ class Controller extends AbstractController implements EventSubscriberInterface
'accept_header' => $accept,
'vars' => $this->vars,
'response' => &$potential_response,
]) === Event::next) {
]) !== Event::stop) {
switch ($format) {
case 'html':
$event->setResponse($this->render($template, $this->vars));

View File

@ -49,7 +49,6 @@ class GSActor extends Entity
// @codeCoverageIgnoreStart
private int $id;
private string $nickname;
private string $normalized_nickname;
private ?string $fullname;
private int $roles = 4;
private ?string $homepage;
@ -84,17 +83,6 @@ class GSActor extends Entity
return $this->nickname;
}
public function setNormalizedNickname(string $normalized_nickname): self
{
$this->normalized_nickname = $normalized_nickname;
return $this;
}
public function getNormalizedNickname(): string
{
return $this->normalized_nickname;
}
public function setFullname(?string $fullname): self
{
$this->fullname = $fullname;
@ -219,6 +207,11 @@ class GSActor extends Entity
// @codeCoverageIgnoreEnd
// }}} Autocode
public function getLocalUser()
{
return DB::findOneBy('local_user', ['id' => $this->getId()]);
}
public function getAvatarUrl()
{
$url = null;

47
src/Routes/GSActor.php Normal file
View File

@ -0,0 +1,47 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Define social's attachment routes
*
* @package GNUsocial
* @category Router
*
* @author Diogo Cordeiro <mail@diogo.site>
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace App\Routes;
use App\Controller as C;
use App\Core\Router\RouteLoader;
use App\Util\Nickname;
abstract class GSActor
{
public static function load(RouteLoader $r): void
{
$r->connect(id: 'gsactor_view_id', uri_path: '/actor/{id<\d+>}', target: [C\GSActor::class, 'GSActorShowId']);
$r->connect(id: 'gsactor_view_nickname', uri_path: '/{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\GSActor::class, 'GSActorShowNickname']);
}
}

View File

@ -40,6 +40,6 @@ abstract class Note
{
public static function load(RouteLoader $r): void
{
$r->connect('note_view', '/note/{id<\d+>}', [C\Note::class, 'note_show']);
$r->connect('note_view', '/note/{id<\d+>}', [C\Note::class, 'NoteShow']);
}
}