[PLUGIN][ActivityPub][Model][Actor] Fix internal logic for updating

Actors
This commit is contained in:
Diogo Peralta Cordeiro 2022-03-01 21:20:24 +00:00
parent 1daa314c55
commit 626b4263f1
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 55 additions and 33 deletions

View File

@ -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']);
}
}

View File

@ -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

View File

@ -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!');
}

View File

@ -104,9 +104,6 @@ class Actor extends Model
}
// Actor
if (isset($options['objects']['Actor'])) {
$actor = $options['objects']['Actor'];
} else {
$actor_map = [
'nickname' => $object->get('preferredUsername'),
'fullname' => !empty($object->get('name')) ? $object->get('name') : null,
@ -117,32 +114,37 @@ class Actor extends Model
'roles' => $roles,
'modified' => new DateTime(),
];
if (isset($options['objects']['Actor'])) {
$actor = GSActor::create($actor_map, $options['objects']['Actor']);
} else {
$actor = GSActor::create($actor_map);
DB::persist($actor);
}
// ActivityPub Actor
if (isset($options['objects']['ActivitypubActor'])) {
$ap_actor = $options['objects']['ActivitypubActor'];
} else {
$ap_actor = ActivitypubActor::create([
$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,
], $options['objects']['ActivitypubActor'] ?? null);
];
if (isset($options['objects']['ActivitypubActor'])) {
$ap_actor = ActivitypubActor::create($ap_actor_map, $options['objects']['ActivitypubActor']);
} else {
$ap_actor = ActivitypubActor::create($ap_actor_map);
DB::persist($ap_actor);
}
// Public Key
if (isset($options['objects']['ActivitypubRsa'])) {
$apRSA = $options['objects']['ActivitypubRsa'];
} else {
$apRSA = ActivitypubRsa::create([
$ap_rsa_map = [
'actor_id' => $actor->getID(),
'public_key' => ($object->has('publicKey') && isset($object->get('publicKey')['publicKeyPem'])) ? $object->get('publicKey')['publicKeyPem'] : null,
], $options['objects']['ActivitypubRsa'] ?? null);
];
if (isset($options['objects']['ActivitypubRsa'])) {
$apRSA = ActivitypubRsa::create($ap_rsa_map, $options['objects']['ActivitypubRsa']);
} else {
$apRSA = ActivitypubRsa::create($ap_rsa_map);
DB::persist($apRSA);
}