From 626b4263f1f037763c757f2c3c0156d4f6894317 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 1 Mar 2022 21:20:24 +0000 Subject: [PATCH] [PLUGIN][ActivityPub][Model][Actor] Fix internal logic for updating Actors --- components/Language/Controller/Language.php | 2 +- .../ActivityPub/Entity/ActivitypubActor.php | 14 ++++-- plugins/ActivityPub/Entity/ActivitypubRsa.php | 22 ++++++-- plugins/ActivityPub/Util/Model/Actor.php | 50 ++++++++++--------- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/components/Language/Controller/Language.php b/components/Language/Controller/Language.php index c2d747ac4b..7cb430aca8 100644 --- a/components/Language/Controller/Language.php +++ b/components/Language/Controller/Language.php @@ -134,7 +134,7 @@ class Language extends Controller // Stay on same page, but force update and prevent resubmission throw new RedirectException('settings_sort_languages'); } else { - throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']); + throw new RedirectException('person_actor_settings', ['id' => $user->getId(), 'open' => 'settings-language-details', '_fragment' => 'settings-language-details']); } } diff --git a/plugins/ActivityPub/Entity/ActivitypubActor.php b/plugins/ActivityPub/Entity/ActivitypubActor.php index 929dd01813..35cf7baec4 100644 --- a/plugins/ActivityPub/Entity/ActivitypubActor.php +++ b/plugins/ActivityPub/Entity/ActivitypubActor.php @@ -233,9 +233,17 @@ class ActivitypubActor extends Entity * * @throws Exception */ - public static function update_profile(self &$ap_actor, Actor &$actor, ActivitypubRsa &$activitypub_rsa, string $res): void - { - \Plugin\ActivityPub\Util\Model\Actor::fromJson($res, ['objects' => ['ActivitypubActor' => &$ap_actor, 'Actor' => &$actor, 'ActivitypubRsa' => &$activitypub_rsa]]); + public static function update_profile( + self &$ap_actor, + Actor &$actor, + ActivitypubRsa &$activitypub_rsa, + string $res, + ): void { + \Plugin\ActivityPub\Util\Model\Actor::fromJson($res, ['objects' => [ + 'ActivitypubActor' => &$ap_actor, + 'Actor' => &$actor, + 'ActivitypubRsa' => &$activitypub_rsa, + ]]); } public static function schemaDef(): array diff --git a/plugins/ActivityPub/Entity/ActivitypubRsa.php b/plugins/ActivityPub/Entity/ActivitypubRsa.php index 3cda1297ba..5214451edf 100644 --- a/plugins/ActivityPub/Entity/ActivitypubRsa.php +++ b/plugins/ActivityPub/Entity/ActivitypubRsa.php @@ -38,6 +38,8 @@ use App\Core\Log; use App\Entity\Actor; use App\Util\Exception\ServerException; use DateTimeInterface; +use Exception; +use Plugin\ActivityPub\Util\Explorer; /** * ActivityPub Keys System @@ -157,14 +159,24 @@ class ActivitypubRsa extends Entity 'private_key' => $private_key, 'public_key' => $public_key, ]); - DB::persist($apRSA); + DB::wrapInTransaction(fn () => DB::persist($apRSA)); } else { // ASSERT: This should never happen, but try to recover! - Log::error('Activitypub_rsa: An impossible thing has happened... Please let the devs know.'); + Log::error('Activitypub_rsa: It seems that the RSA key for this remote actor was somehow lost. That should have never happened!'); if ($fetch) { - //$res = Activitypub_explorer::get_remote_user_activity($profile->getUri()); - //Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']); - //return self::ensure_public_key($profile, false); + try { + $res = Explorer::getRemoteActorActivity($gsactor->getUri()); + if (\is_null($res)) { + throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened! Invalid remote actor (null response).'); + } + DB::wrapInTransaction(fn () => DB::persist(self::create([ + 'actor_id' => $gsactor->getId(), + 'public_key' => json_decode($res, associative: true)['publicKey']['publicKeyPem'], + ]))); + return self::getByActor($gsactor, false); + } catch (Exception $e) { + throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!', previous: $e); + } } else { throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!'); } diff --git a/plugins/ActivityPub/Util/Model/Actor.php b/plugins/ActivityPub/Util/Model/Actor.php index 6eba6c9fe9..172f9ca5e9 100644 --- a/plugins/ActivityPub/Util/Model/Actor.php +++ b/plugins/ActivityPub/Util/Model/Actor.php @@ -104,45 +104,47 @@ class Actor extends Model } // Actor + $actor_map = [ + 'nickname' => $object->get('preferredUsername'), + 'fullname' => !empty($object->get('name')) ? $object->get('name') : null, + 'created' => new DateTime($object->get('published') ?? 'now'), + 'bio' => $object->get('summary'), + 'is_local' => false, // duh! + 'type' => self::$_as2_actor_type_to_gs_actor_type[$object->get('type')], + 'roles' => $roles, + 'modified' => new DateTime(), + ]; if (isset($options['objects']['Actor'])) { - $actor = $options['objects']['Actor']; + $actor = GSActor::create($actor_map, $options['objects']['Actor']); } else { - $actor_map = [ - 'nickname' => $object->get('preferredUsername'), - 'fullname' => !empty($object->get('name')) ? $object->get('name') : null, - 'created' => new DateTime($object->get('published') ?? 'now'), - 'bio' => $object->get('summary'), - 'is_local' => false, // duh! - 'type' => self::$_as2_actor_type_to_gs_actor_type[$object->get('type')], - 'roles' => $roles, - 'modified' => new DateTime(), - ]; $actor = GSActor::create($actor_map); DB::persist($actor); } // ActivityPub Actor + $ap_actor_map = [ + 'inbox_uri' => $object->get('inbox'), + 'inbox_shared_uri' => ($object->has('endpoints') && isset($object->get('endpoints')['sharedInbox'])) ? $object->get('endpoints')['sharedInbox'] : null, + 'uri' => $object->get('id'), + 'actor_id' => $actor->getId(), + 'url' => $object->get('url') ?? null, + ]; if (isset($options['objects']['ActivitypubActor'])) { - $ap_actor = $options['objects']['ActivitypubActor']; + $ap_actor = ActivitypubActor::create($ap_actor_map, $options['objects']['ActivitypubActor']); } else { - $ap_actor = ActivitypubActor::create([ - 'inbox_uri' => $object->get('inbox'), - 'inbox_shared_uri' => ($object->has('endpoints') && isset($object->get('endpoints')['sharedInbox'])) ? $object->get('endpoints')['sharedInbox'] : null, - 'uri' => $object->get('id'), - 'actor_id' => $actor->getId(), - 'url' => $object->get('url') ?? null, - ], $options['objects']['ActivitypubActor'] ?? null); + $ap_actor = ActivitypubActor::create($ap_actor_map); DB::persist($ap_actor); } // Public Key + $ap_rsa_map = [ + 'actor_id' => $actor->getID(), + 'public_key' => ($object->has('publicKey') && isset($object->get('publicKey')['publicKeyPem'])) ? $object->get('publicKey')['publicKeyPem'] : null, + ]; if (isset($options['objects']['ActivitypubRsa'])) { - $apRSA = $options['objects']['ActivitypubRsa']; + $apRSA = ActivitypubRsa::create($ap_rsa_map, $options['objects']['ActivitypubRsa']); } else { - $apRSA = ActivitypubRsa::create([ - 'actor_id' => $actor->getID(), - 'public_key' => ($object->has('publicKey') && isset($object->get('publicKey')['publicKeyPem'])) ? $object->get('publicKey')['publicKeyPem'] : null, - ], $options['objects']['ActivitypubRsa'] ?? null); + $apRSA = ActivitypubRsa::create($ap_rsa_map); DB::persist($apRSA); }