[PLUGIN][ActivityPub] Provide ActivityStreams 2.0 responses for every Collection

Implemented ActivityPub Outbox
This commit is contained in:
Diogo Peralta Cordeiro 2022-01-11 20:28:15 +00:00
parent f3a7e8f04d
commit 59b8bdf99b
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
9 changed files with 214 additions and 15 deletions

View File

@ -77,7 +77,7 @@ class Attachment extends Component
/** /**
* Populate $note_expr with the criteria for looking for notes with attachments * Populate $note_expr with the criteria for looking for notes with attachments
*/ */
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): bool public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool
{ {
$include_term = str_contains($term, ':') ? explode(':', $term)[1] : $term; $include_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
if (Formatting::startsWith($term, ['note-types:', 'notes-incude:', 'note-filter:'])) { if (Formatting::startsWith($term, ['note-types:', 'notes-incude:', 'note-filter:'])) {

View File

@ -23,12 +23,12 @@ class Collection extends Component
* Supports a variety of query terms and is used both in feeds and * Supports a variety of query terms and is used both in feeds and
* in search. Uses query builders to allow for extension * in search. Uses query builders to allow for extension
*/ */
public static function query(string $query, int $page, ?string $language = null, ?Actor $actor = null): array public static function query(string $query, int $page, ?string $locale = null, ?Actor $actor = null): array
{ {
$note_criteria = null; $note_criteria = null;
$actor_criteria = null; $actor_criteria = null;
if (!empty($query = trim($query))) { if (!empty($query = trim($query))) {
[$note_criteria, $actor_criteria] = Parser::parse($query, $language, $actor); [$note_criteria, $actor_criteria] = Parser::parse($query, $locale, $actor);
} }
$note_qb = DB::createQueryBuilder(); $note_qb = DB::createQueryBuilder();
$actor_qb = DB::createQueryBuilder(); $actor_qb = DB::createQueryBuilder();
@ -64,7 +64,7 @@ class Collection extends Component
* Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text * Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
* notes, for different types of actors and for the content of text notes * notes, for different types of actors and for the content of text notes
*/ */
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr) public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr)
{ {
if (str_contains($term, ':')) { if (str_contains($term, ':')) {
$term = explode(':', $term); $term = explode(':', $term);

View File

@ -11,7 +11,7 @@ use Component\Collection\Collection as CollectionModule;
class Collection extends Controller class Collection extends Controller
{ {
public function query(string $query, ?string $locale = null, ?Actor $actor = null) public function query(string $query, ?string $locale = null, ?Actor $actor = null): array
{ {
$actor ??= Common::actor(); $actor ??= Common::actor();
$locale ??= Common::currentLanguage()->getLocale(); $locale ??= Common::currentLanguage()->getLocale();

View File

@ -53,7 +53,7 @@ abstract class Parser
* *
* @return Criteria[] * @return Criteria[]
*/ */
public static function parse(string $input, ?string $language = null, ?Actor $actor = null, int $level = 0): array public static function parse(string $input, ?string $locale = null, ?Actor $actor = null, int $level = 0): array
{ {
if ($level === 0) { if ($level === 0) {
$input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&'); $input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&');
@ -78,7 +78,7 @@ abstract class Parser
$term = mb_substr($input, $left, $end ? null : $right - $left); $term = mb_substr($input, $left, $end ? null : $right - $left);
$note_res = null; $note_res = null;
$actor_res = null; $actor_res = null;
Event::handle('CollectionQueryCreateExpression', [$eb, $term, $language, $actor, &$note_res, &$actor_res]); Event::handle('CollectionQueryCreateExpression', [$eb, $term, $locale, $actor, &$note_res, &$actor_res]);
if (\is_null($note_res) && \is_null($actor_res)) { // @phpstan-ignore-line if (\is_null($note_res) && \is_null($actor_res)) { // @phpstan-ignore-line
throw new ServerException("No one claimed responsibility for a match term: {$term}"); throw new ServerException("No one claimed responsibility for a match term: {$term}");
} }

View File

@ -60,7 +60,7 @@ class Language extends Component
/** /**
* Populate $note_expr or $actor_expr with an expression to match a language * Populate $note_expr or $actor_expr with an expression to match a language
*/ */
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): bool public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool
{ {
$search_term = str_contains($term, ':') ? explode(':', $term)[1] : $term; $search_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;

View File

@ -47,17 +47,20 @@ use App\Util\Common;
use App\Util\Exception\BugFoundException; use App\Util\Exception\BugFoundException;
use App\Util\Exception\NoSuchActorException; use App\Util\Exception\NoSuchActorException;
use App\Util\Nickname; use App\Util\Nickname;
use Component\Collection\Util\Controller\OrderedCollection;
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol; use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
use Component\FreeNetwork\Util\Discovery; use Component\FreeNetwork\Util\Discovery;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use const PHP_URL_HOST; use const PHP_URL_HOST;
use Plugin\ActivityPub\Controller\Inbox; use Plugin\ActivityPub\Controller\Inbox;
use Plugin\ActivityPub\Controller\Outbox;
use Plugin\ActivityPub\Entity\ActivitypubActivity; use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Entity\ActivitypubActor; use Plugin\ActivityPub\Entity\ActivitypubActor;
use Plugin\ActivityPub\Entity\ActivitypubObject; use Plugin\ActivityPub\Entity\ActivitypubObject;
use Plugin\ActivityPub\Util\HTTPSignature; use Plugin\ActivityPub\Util\HTTPSignature;
use Plugin\ActivityPub\Util\Model; use Plugin\ActivityPub\Util\Model;
use Plugin\ActivityPub\Util\OrderedCollectionController;
use Plugin\ActivityPub\Util\Response\ActorResponse; use Plugin\ActivityPub\Util\Response\ActorResponse;
use Plugin\ActivityPub\Util\Response\NoteResponse; use Plugin\ActivityPub\Util\Response\NoteResponse;
use Plugin\ActivityPub\Util\TypeResponse; use Plugin\ActivityPub\Util\TypeResponse;
@ -127,7 +130,7 @@ class ActivityPub extends Plugin
$r->connect( $r->connect(
'activitypub_actor_outbox', 'activitypub_actor_outbox',
'/actor/{gsactor_id<\d+>}/outbox.json', '/actor/{gsactor_id<\d+>}/outbox.json',
[Inbox::class, 'handle'], [Outbox::class, 'viewOutboxByActorId'],
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]], options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
); );
return Event::next; return Event::next;
@ -185,16 +188,21 @@ class ActivityPub extends Plugin
case 'actor_view_id': case 'actor_view_id':
case 'actor_view_nickname': case 'actor_view_nickname':
$response = ActorResponse::handle($vars['actor']); $response = ActorResponse::handle($vars['actor']);
return Event::stop; break;
case 'note_view': case 'note_view':
$response = NoteResponse::handle($vars['note']); $response = NoteResponse::handle($vars['note']);
return Event::stop; break;
case 'activitypub_actor_outbox':
$response = new TypeResponse($vars['type']);
break;
default: default:
if (Event::handle('ActivityPubActivityStreamsTwoResponse', [$route, $vars, &$response]) === Event::stop) { if (Event::handle('ActivityPubActivityStreamsTwoResponse', [$route, $vars, &$response]) !== Event::stop) {
return Event::stop; if (is_subclass_of($vars['controller'][0], OrderedCollection::class)) {
$response = new TypeResponse(OrderedCollectionController::fromControllerVars($vars)['type']);
}
} }
return Event::next;
} }
return Event::stop;
} }
/** /**

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types = 1);
// {{{ 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/>.
// }}}
/**
* ActivityPub implementation for GNU social
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Plugin\ActivityPub\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Activity;
use App\Util\Exception\ClientException;
use Exception;
use Plugin\ActivityPub\Util\OrderedCollectionController;
use Symfony\Component\HttpFoundation\Request;
/**
* ActivityPub Outbox Handler
*
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Outbox extends OrderedCollectionController
{
/**
* Create an Inbox Handler to receive something from someone.
*/
public function viewOutboxByActorId(Request $request, int $gsactor_id): array
{
try {
$user = DB::findOneBy('local_user', ['id' => $gsactor_id]);
} catch (Exception $e) {
throw new ClientException(_m('No such actor.'), 404, $e);
}
$this->actor_id = $gsactor_id;
Log::debug('ActivityPub Outbox: Received a GET request.');
$activities = DB::findBy(Activity::class, ['actor_id' => $user->getId()], order_by: ['created' => 'DESC']);
foreach ($activities as $act) {
$this->ordered_items[] = Router::url('activity_view', ['id' => $act->getId()], ROUTER::ABSOLUTE_URL);
}
$this->route = 'activitypub_actor_outbox';
$this->route_args = ['gsactor_id' => $user->getId(), 'page' => $this->int('page') ?? 0];
return $this->handle($request);
}
}

View File

@ -0,0 +1,112 @@
<?php
declare(strict_types = 1);
// {{{ 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/>.
// }}}
/**
* ActivityPub implementation for GNU social
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Plugin\ActivityPub\Util;
use ActivityPhp\Type\Core\OrderedCollection;
use ActivityPhp\Type\Core\OrderedCollectionPage;
use App\Core\Router\Router;
use Component\Collection\Util\Controller\CircleController;
use Component\Collection\Util\Controller\FeedController;
use Component\Collection\Util\Controller\OrderedCollection as GSOrderedCollection;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a response in application/ld+json to GSActivity
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
abstract class OrderedCollectionController extends GSOrderedCollection
{
protected array $ordered_items = [];
protected string $route;
protected array $route_args = [];
protected int $actor_id;
public static function fromControllerVars(array $vars): array
{
$route = $vars['request']->get('_route');
$route_args = array_merge($vars['request']->query->all(), $vars['request']->attributes->get('_route_params'));
unset($route_args['is_system_path'], $route_args['template'], $route_args['_format'], $route_args['accept'], $route_args['p']);
if (is_subclass_of($vars['controller'][0], FeedController::class)) {
$notes = [];
foreach ($vars['notes'] as $note_replies) {
$notes[] = Router::url('note_view', ['id' => $note_replies['note']->getId()], type: Router::ABSOLUTE_URL);
}
$type = self::setupType(route: $route, route_args: $route_args, ordered_items: $notes);
} elseif (is_subclass_of($vars['controller'][0], CircleController::class)) {
$actors = [];
foreach ($vars['actors'] as $actor) {
$actors[] = Router::url('actor_view_id', ['id' => $actor->getId()], type: Router::ABSOLUTE_URL);
}
$type = self::setupType(route: $route, route_args: $route_args, ordered_items: $actors);
} else {
$type = self::setupType(route: $route, route_args: $route_args, ordered_items: []);
}
return ['type' => $type];
}
protected static function setupType(string $route, array $route_args = [], array $ordered_items = []): OrderedCollectionPage|OrderedCollection
{
$page = $route_args['page'] ?? 0;
$type = $page === 0 ? new OrderedCollection() : new OrderedCollectionPage();
$type->set('@context', 'https://www.w3.org/ns/activitystreams');
$type->set('items', $ordered_items);
$type->set('orderedItems', $ordered_items);
$type->set('totalItems', \count($ordered_items));
if ($page === 0) {
$route_args['page'] = 1;
$type->set('first', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
} else {
$type->set('partOf', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
if ($page + 1 < $total_pages = 1) { // TODO: do proper pagination
$route_args['page'] = ($page + 1 == 1 ? 2 : $page + 1);
$type->set('next', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
}
if ($page > 1) {
$route_args['page'] = ($page - 1 <= 0 ? 1 : $page - 1);
$type->set('prev', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
}
}
return $type;
}
public function handle(Request $request): array
{
$type = self::setupType($this->route, $this->route_args, $this->ordered_items, $this->actor_id);
return ['type' => $type];
}
}

View File

@ -49,7 +49,7 @@ class TypeResponse extends JsonResponse
* @param null|AbstractObject|string $json * @param null|AbstractObject|string $json
* @param int $status The response status code * @param int $status The response status code
*/ */
public function __construct(string|AbstractObject|null $json = null, int $status = 202) public function __construct(string|AbstractObject|null $json = null, int $status = 200)
{ {
parent::__construct( parent::__construct(
data: \is_object($json) ? $json->toJson() : $json, data: \is_object($json) ? $json->toJson() : $json,