[PLUGIN][ActivityPub] Implement Group Inbox POST

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-12 04:34:10 +00:00
parent 3ae8f8213f
commit 10d1a7ed2a
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 87 additions and 6 deletions

View File

@ -63,8 +63,12 @@ class Activity extends Model
* Create an Entity from an ActivityStreams 2.0 JSON string * Create an Entity from an ActivityStreams 2.0 JSON string
* This will persist new GSActivities, GSObjects, and APActivity * This will persist new GSActivities, GSObjects, and APActivity
* *
* @param string|AbstractObject $json
* @param array $options
* @return ActivitypubActivity
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
* @throws NoSuchActorException * @throws NoSuchActorException
* @throws NotImplementedException
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface * @throws ServerExceptionInterface
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
@ -74,10 +78,12 @@ class Activity extends Model
$type_activity = \is_string($json) ? self::jsonToType($json) : $json; $type_activity = \is_string($json) ? self::jsonToType($json) : $json;
// Ditch known activities // Ditch known activities
if ($type_activity->has('id')) { // We can't dereference a transient activity
$ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]); $ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]);
if (!\is_null($ap_act)) { if (!\is_null($ap_act)) {
return $ap_act; return $ap_act;
} }
}
// Find Actor and Object // Find Actor and Object
$actor = ActivityPub::getActorByUri($type_activity->get('actor')); $actor = ActivityPub::getActorByUri($type_activity->get('actor'));
@ -85,6 +91,7 @@ class Activity extends Model
if (\is_string($type_object)) { // Retrieve it if (\is_string($type_object)) { // Retrieve it
$type_object = ActivityPub::getObjectByUri($type_object, try_online: true); $type_object = ActivityPub::getObjectByUri($type_object, try_online: true);
} else { // Encapsulated, if we have it locally, prefer it } else { // Encapsulated, if we have it locally, prefer it
// TODO: Test authority of activity over object
$type_object = ActivityPub::getObjectByUri($type_object->get('id'), try_online: false) ?? $type_object; $type_object = ActivityPub::getObjectByUri($type_object->get('id'), try_online: false) ?? $type_object;
} }
@ -113,9 +120,9 @@ class Activity extends Model
case 'Undo': case 'Undo':
$object_type = $type_object instanceof AbstractObject ? match ($type_object->get('type')) { $object_type = $type_object instanceof AbstractObject ? match ($type_object->get('type')) {
'Note' => \App\Entity\Note::class, 'Note' => \App\Entity\Note::class,
// no break
default => throw new NotImplementedException('Unsupported Undo of Object Activity.'), default => throw new NotImplementedException('Unsupported Undo of Object Activity.'),
} : $type_object::class; } : $type_object::class;
switch ($object_type) { switch ($object_type) {
case GSActivity::class: case GSActivity::class:
switch ($type_object->getVerb()) { switch ($type_object->getVerb()) {
@ -126,6 +133,9 @@ class Activity extends Model
break; break;
} }
break; break;
case 'Announce':
ActivityAnnounce::handle_core_activity($actor, $type_activity, $type_object, $ap_act);
break;
} }
return $ap_act; return $ap_act;
} }

View File

@ -0,0 +1,71 @@
<?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\Model;
use ActivityPhp\Type\AbstractObject;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
/**
* This class handles translation between JSON and ActivityPub Announces
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class ActivityAnnounce extends Activity
{
protected static function handle_core_activity(\App\Entity\Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity
{
// The only core Announce we recognise is for (transitive) activities coming from Group actors
if ($actor->isGroup()) {
if ($type_object instanceof AbstractObject) {
$actual_to = array_flip(is_string($type_object->get('to')) ? [$type_object->get('to')] : $type_object->get('to'));
$actual_cc = array_flip(is_string($type_object->get('cc')) ? [$type_object->get('cc')] : $type_object->get('cc'));
$actual_cc[$type_activity->get('actor')] = true; // Add group to targets
foreach (is_string($type_activity->get('to')) ? [$type_activity->get('to')] : $type_activity->get('to') as $to) {
if ($to !== 'https://www.w3.org/ns/activitystreams#Public') {
$actual_to[$to] = true;
}
}
foreach (is_string($type_activity->get('cc')) ? [$type_activity->get('cc')] : $type_activity->get('cc') as $cc) {
if ($cc !== 'https://www.w3.org/ns/activitystreams#Public') {
$actual_cc[$cc] = true;
}
}
$type_object->set('to', array_keys($actual_to));
$type_object->set('cc', array_keys($actual_cc));
$ap_act = self::fromJson($type_object);
}
}
return $ap_act ?? ($ap_act = $type_object);
}
}

View File

@ -274,7 +274,7 @@ class Note extends Model
'canonical' => $canonical_tag, 'canonical' => $canonical_tag,
'note_id' => $obj->getId(), 'note_id' => $obj->getId(),
'use_canonical' => $ap_tag->get('canonical') ?? false, 'use_canonical' => $ap_tag->get('canonical') ?? false,
'language_id' => $lang_id, 'language_id' => $lang_id ?? null,
])); ]));
Cache::pushList("tag-{$canonical_tag}", $obj); Cache::pushList("tag-{$canonical_tag}", $obj);
foreach (Tag::cacheKeys($canonical_tag) as $key) { foreach (Tag::cacheKeys($canonical_tag) as $key) {

View File

@ -356,7 +356,7 @@ class RepeatNote extends NoteHandlerPlugin
*/ */
private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{ {
if (!\in_array($type_activity->get('type'), ['Announce', 'Undo'])) { if (!\in_array($type_activity->get('type'), ['Announce', 'Undo']) || !$actor->isPerson()) {
return Event::next; return Event::next;
} }
if ($type_activity->get('type') === 'Announce') { // Repeat if ($type_activity->get('type') === 'Announce') { // Repeat