[PLUGIN][ActivityPub] Correct @context

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-23 17:39:11 +00:00
parent 05b7f2c28b
commit f3c2e49e3f
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
9 changed files with 99 additions and 58 deletions

View File

@ -25,8 +25,6 @@ use App\Core\DB\DB;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Util\Formatting;
use Doctrine\Common\Collections\ExpressionBuilder;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Modules\Component;
@ -46,6 +44,7 @@ use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NoSuchActorException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use App\Util\Nickname;
use Component\FreeNetwork\Controller\Feeds;
use Component\FreeNetwork\Controller\HostMeta;
@ -55,6 +54,7 @@ use Component\FreeNetwork\Util\Discovery;
use Component\FreeNetwork\Util\WebfingerResource;
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceActor;
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceNote;
use Doctrine\Common\Collections\ExpressionBuilder;
use Exception;
use const PREG_SET_ORDER;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -80,9 +80,10 @@ class FreeNetwork extends Component
public const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
private static array $protocols = [];
public function onInitializeComponent()
public function onInitializeComponent(): bool
{
Event::handle('AddFreeNetworkProtocol', [&self::$protocols]);
return Event::next;
}
public function onAddRoute(RouteLoader $m): bool

View File

@ -53,8 +53,6 @@ use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
use Component\FreeNetwork\Util\Discovery;
use Exception;
use InvalidArgumentException;
use Plugin\ActivityPub\Util\Response\ActivityResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use const PHP_URL_HOST;
use Plugin\ActivityPub\Controller\Inbox;
use Plugin\ActivityPub\Controller\Outbox;
@ -64,12 +62,13 @@ use Plugin\ActivityPub\Entity\ActivitypubObject;
use Plugin\ActivityPub\Util\HTTPSignature;
use Plugin\ActivityPub\Util\Model;
use Plugin\ActivityPub\Util\OrderedCollectionController;
use Plugin\ActivityPub\Util\Response\ActivityResponse;
use Plugin\ActivityPub\Util\Response\ActorResponse;
use Plugin\ActivityPub\Util\Response\NoteResponse;
use Plugin\ActivityPub\Util\TypeResponse;
use Plugin\ActivityPub\Util\Validator\contentLangModelValidator;
use Plugin\ActivityPub\Util\Validator\manuallyApprovesFollowersModelValidator;
use const PREG_SET_ORDER;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -110,6 +109,27 @@ class ActivityPub extends Plugin
return '3.0.0';
}
public static array $activity_streams_two_context = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
['gs' => 'https://www.gnu.org/software/social/ns#'],
['litepub' => 'http://litepub.social/ns#'],
['chatMessage' => 'litepub:chatMessage'],
[
'inConversation' => [
'@id' => 'gs:inConversation',
'@type' => '@id',
],
],
];
public function onInitializePlugin(): bool
{
Event::handle('ActivityStreamsTwoContext', [&self::$activity_streams_two_context]);
self::$activity_streams_two_context = array_unique(self::$activity_streams_two_context, \SORT_REGULAR);
return Event::next;
}
/**
* This code executes when GNU social creates the page routing, and we hook
* on this event to add our Inbox and Outbox handler for ActivityPub.
@ -249,6 +269,7 @@ class ActivityPub extends Plugin
* The FreeNetwork component will call this function to pull ActivityPub objects by URI
*
* @param string $uri Query
*
* @return bool true if imported, false otherwise
*/
public static function freeNetworkGrabRemote(string $uri): bool
@ -258,7 +279,7 @@ class ActivityPub extends Plugin
$object = self::getObjectByUri($uri);
if (!\is_null($object)) {
if ($object instanceof Type\AbstractObject) {
if (in_array($object->get('type'), array_keys(Model\Actor::$_as2_actor_type_to_gs_actor_type))) {
if (\in_array($object->get('type'), array_keys(Model\Actor::$_as2_actor_type_to_gs_actor_type))) {
DB::wrapInTransaction(fn () => Model\Actor::fromJson($object));
} else {
DB::wrapInTransaction(fn () => Model\Activity::fromJson($object));
@ -266,7 +287,7 @@ class ActivityPub extends Plugin
}
return true;
}
} catch (\Exception|\Throwable) {
} catch (Exception|Throwable) {
// May be invalid input, we can safely ignore in this case
}
}
@ -467,7 +488,7 @@ class ActivityPub extends Plugin
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return null|mixed|Note|Actor got from URI
* @return null|Actor|mixed|Note got from URI
*/
public static function getObjectByUri(string $resource, bool $try_online = true)
{
@ -485,12 +506,11 @@ class ActivityPub extends Plugin
// Try local Note
if (Common::isValidHttpUrl($resource)) {
// This means $resource is a valid url
$resource_parts = parse_url($resource);
// TODO: Use URLMatcher
if ($resource_parts['host'] === Common::config('site', 'server')) {
$local_note = DB::findOneBy('note', ['url' => $resource], return_null: true);
if ($local_note instanceof Note) {
if (!\is_null($local_note)) {
return $local_note;
}
}
@ -505,14 +525,14 @@ class ActivityPub extends Plugin
// Try remote
if (!$try_online) {
return null;
return;
}
$response = HTTPClient::get($resource, ['headers' => self::HTTP_CLIENT_HEADERS]);
// If it was deleted
if ($response->getStatusCode() == 410) {
//$obj = Type::create('Tombstone', ['id' => $resource]);
return null;
return;
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
throw new Exception('Non Ok Status Code for given Object id.');
} else {

View File

@ -36,10 +36,14 @@ class myValidator extends \Plugin\ActivityPub\Util\ModelValidator
```
**ActivityPubAddActivityStreamsTwoData**: To add attributes to an entity that we are managing to JSON (commonly federating out via ActivityPub)
* `@param string $type_name` When we handle a Type, we will send you the type identifier of the one being handleded
* `@param string $type_name` When we handle a Type, we will send you the type identifier of the one being handled
* `@param \ActivityPhp\Type\AbstractObject &$type_activity` The Activity in the intermediate format between Model and JSON
* `@return` Returns `Event::next`
**ActivityStreamsTwoContext**: To expand our ActivityStreams 2 Context
* `@param array &$activity_streams_two_context` Append to the array your additional context
* `@return` Returns `Event::next`
**ActivityPubActivityStreamsTwoResponse**: To add a route to ActivityPub (the route must already exist in your plugin) (commonly being requested to ActivityPub)
* `@param string $route` Route identifier
* `@param array $vars` From your controller

View File

@ -64,9 +64,6 @@ class Activity extends Model
* Create an Entity from an ActivityStreams 2.0 JSON string
* This will persist new GSActivities, GSObjects, and APActivity
*
* @param string|AbstractObject $json
* @param array $options
* @return ActivitypubActivity
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws NotImplementedException
@ -121,6 +118,7 @@ class Activity extends Model
case 'Undo':
$object_type = $type_object instanceof AbstractObject ? match ($type_object->get('type')) {
'Note' => \App\Entity\Note::class,
// no break
default => throw new NotImplementedException('Unsupported Undo of Object Activity.'),
} : $type_object::class;
@ -164,7 +162,7 @@ class Activity extends Model
$attr = [
'type' => $gs_verb_to_activity_streams_two_verb,
'@context' => ['https://www.w3.org/ns/activitystreams'],
'@context' => ActivityPub::$activity_streams_two_context,
'id' => Router::url('activity_view', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL),

View File

@ -42,7 +42,6 @@ use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Actor as GSActor;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use App\Util\TemporaryFile;
use Component\Avatar\Avatar;
use Component\Group\Entity\LocalGroup;
@ -50,6 +49,7 @@ use DateTime;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActor;
use Plugin\ActivityPub\Entity\ActivitypubRsa;
use Plugin\ActivityPub\Util\Model;
@ -212,7 +212,7 @@ class Actor extends Model
$public_key = $rsa->getPublicKey();
$uri = $object->getUri(Router::ABSOLUTE_URL);
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => ActivityPub::$activity_streams_two_context,
'type' => ($object->getType() === GSActor::GROUP) ? (DB::findOneBy(LocalGroup::class, ['actor_id' => $object->getId()], return_null: true)?->getType() === 'organisation' ? 'Organization' : 'Group') : self::$_gs_actor_type_to_as2_actor_type[$object->getType()],
'id' => $uri,
'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),

View File

@ -39,13 +39,12 @@ use App\Core\DB\DB;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Entity\NoteType;
use Component\Notification\Entity\Attention;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Core\VisibilityScope;
use App\Entity\Note as GSNote;
use App\Entity\NoteType;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\DuplicateFoundException;
@ -59,6 +58,7 @@ use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation;
use Component\FreeNetwork\FreeNetwork;
use Component\Language\Entity\Language;
use Component\Notification\Entity\Attention;
use Component\Tag\Entity\NoteTag;
use Component\Tag\Tag;
use DateTime;
@ -146,7 +146,7 @@ class Note extends Model
'is_local' => false,
'created' => new DateTime($type_note->get('published') ?? 'now'),
'content' => $type_note->get('content') ?? null,
'rendered' => is_null($type_note->get('content')) ? null : HTML::sanitize($type_note->get('content')),
'rendered' => \is_null($type_note->get('content')) ? null : HTML::sanitize($type_note->get('content')),
'title' => $type_note->get('name') ?? null,
'content_type' => 'text/html',
'language_id' => $type_note->get('contentLang') ?? null,
@ -154,7 +154,9 @@ class Note extends Model
'actor_id' => $actor_id,
'reply_to' => $reply_to = $handleInReplyTo($type_note),
'modified' => new DateTime(),
'type' => match ($type_note->get('type')) {'Page' => NoteType::PAGE, default => NoteType::NOTE},
'type' => match ($type_note->get('type')) {
'Page' => NoteType::PAGE, default => NoteType::NOTE
},
'source' => $source,
];
@ -334,8 +336,10 @@ class Note extends Model
}
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => match($object->getType()) {NoteType::NOTE => 'Note', NoteType::PAGE => 'Page'},
'@context' => ActivityPub::$activity_streams_two_context,
'type' => match ($object->getType()) {
NoteType::NOTE => 'Note', NoteType::PAGE => 'Page'
},
'id' => $object->getUrl(),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
@ -365,7 +369,6 @@ class Note extends Model
break;
case VisibilityScope::GROUP:
// Will have the group in the To coming from attentions
// no break
case VisibilityScope::COLLECTION:
// Since we don't support sending unlisted/followers-only
// notices, arriving here means we're instead answering to that type

View File

@ -38,6 +38,7 @@ 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 Plugin\ActivityPub\ActivityPub;
use Symfony\Component\HttpFoundation\Request;
/**
@ -80,7 +81,7 @@ abstract class OrderedCollectionController extends GSOrderedCollection
{
$page = $route_args['page'] ?? 0;
$type = $page === 0 ? new OrderedCollection() : new OrderedCollectionPage();
$type->set('@context', 'https://www.w3.org/ns/activitystreams');
$type->set('@context', ActivityPub::$activity_streams_two_context);
$type->set('items', $ordered_items);
$type->set('orderedItems', $ordered_items);
$type->set('totalItems', \count($ordered_items));

View File

@ -145,19 +145,22 @@ class PinnedNotes extends Plugin
}
// Activity Pub handling stuff
public function onActivityStreamsTwoContext(array &$activity_streams_two_context): bool
{
$activity_streams_two_context[] = ['toot' => 'http://joinmastodon.org/ns#'];
$activity_streams_two_context[] = [
'featured' => [
'@id' => 'toot:featured',
'@type' => '@id',
],
];
return Event::next;
}
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
{
if ($type_name === 'Note') {
$actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->get('attributedTo'));
// Note uri is `https://domain:port/object/note/3`.
// is it always like that? I honestly don't know, but I see
// no other way of getting Note id:
$uri_parts = explode('/', $type->get('id'));
$note_id = end($uri_parts);
$is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, ['actor_id' => $actor->getId(), 'note_id' => $note_id], return_null: true));
$type->set('featured', $is_pinned);
} elseif ($type_name === 'Person') {
if ($type_name === 'Person') {
$actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->get('id'));
$router_args = ['id' => $actor->getId()];
$router_type = Router::ABSOLUTE_URL;

View File

@ -240,6 +240,17 @@ class WebMonetization extends Plugin
return Event::next;
}
public function onActivityStreamsTwoContext(array &$activity_streams_two_context): bool
{
$activity_streams_two_context[] = [
'webmonetizationWallet' => [
'@id' => 'gs:webmonetizationWallet',
'@type' => '@id',
],
];
return Event::next;
}
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
{
if ($type_name === 'Person') {