diff --git a/plugins/ActivityPub/Util/Model/Activity.php b/plugins/ActivityPub/Util/Model/Activity.php index ad99d34935..6df72542d2 100644 --- a/plugins/ActivityPub/Util/Model/Activity.php +++ b/plugins/ActivityPub/Util/Model/Activity.php @@ -63,8 +63,12 @@ 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 * @throws RedirectionExceptionInterface * @throws ServerExceptionInterface * @throws TransportExceptionInterface @@ -74,9 +78,11 @@ class Activity extends Model $type_activity = \is_string($json) ? self::jsonToType($json) : $json; // Ditch known activities - $ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]); - if (!\is_null($ap_act)) { - return $ap_act; + if ($type_activity->has('id')) { // We can't dereference a transient activity + $ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]); + if (!\is_null($ap_act)) { + return $ap_act; + } } // Find Actor and Object @@ -85,6 +91,7 @@ class Activity extends Model if (\is_string($type_object)) { // Retrieve it $type_object = ActivityPub::getObjectByUri($type_object, try_online: true); } 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; } @@ -113,9 +120,9 @@ 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; + switch ($object_type) { case GSActivity::class: switch ($type_object->getVerb()) { @@ -126,6 +133,9 @@ class Activity extends Model break; } break; + case 'Announce': + ActivityAnnounce::handle_core_activity($actor, $type_activity, $type_object, $ap_act); + break; } return $ap_act; } diff --git a/plugins/ActivityPub/Util/Model/ActivityAnnounce.php b/plugins/ActivityPub/Util/Model/ActivityAnnounce.php new file mode 100644 index 0000000000..a3cfc87a6a --- /dev/null +++ b/plugins/ActivityPub/Util/Model/ActivityAnnounce.php @@ -0,0 +1,71 @@ +. +// }}} + +/** + * 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); + } +} diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index a294f6c01a..b0c4cd3e98 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -274,7 +274,7 @@ class Note extends Model 'canonical' => $canonical_tag, 'note_id' => $obj->getId(), 'use_canonical' => $ap_tag->get('canonical') ?? false, - 'language_id' => $lang_id, + 'language_id' => $lang_id ?? null, ])); Cache::pushList("tag-{$canonical_tag}", $obj); foreach (Tag::cacheKeys($canonical_tag) as $key) { diff --git a/plugins/RepeatNote/RepeatNote.php b/plugins/RepeatNote/RepeatNote.php index 8e2661fc56..7e223f26c9 100644 --- a/plugins/RepeatNote/RepeatNote.php +++ b/plugins/RepeatNote/RepeatNote.php @@ -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 { - if (!\in_array($type_activity->get('type'), ['Announce', 'Undo'])) { + if (!\in_array($type_activity->get('type'), ['Announce', 'Undo']) || !$actor->isPerson()) { return Event::next; } if ($type_activity->get('type') === 'Announce') { // Repeat