diff --git a/lib/Db/ActorsRequest.php b/lib/Db/ActorsRequest.php index d041d06f..e33c43c6 100644 --- a/lib/Db/ActorsRequest.php +++ b/lib/Db/ActorsRequest.php @@ -58,10 +58,10 @@ class ActorsRequest extends ActorsRequestBuilder { * * @param Person $actor * - * @return int + * @return string * @throws \Exception */ - public function create(Person $actor) { + public function create(Person $actor): string { $id = $this->configService->getRoot() . '@' . $actor->getPreferredUsername(); @@ -80,6 +80,8 @@ class ActorsRequest extends ActorsRequestBuilder { ->setValue('private_key', $qb->createNamedParameter($actor->getPrivateKey())); $qb->execute(); + + return $id; } catch (\Exception $e) { throw $e; } @@ -110,7 +112,8 @@ class ActorsRequest extends ActorsRequestBuilder { } /** - * @param string $username + * @param string $id + * * @return Person * @throws ActorDoesNotExistException */ diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index a19cf3b9..4e0ef561 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -183,5 +183,18 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { } + /** + * delete cached version of an Actor, based on the UriId + * + * @param string $id + */ + public function deleteFromId(string $id) { + $qb = $this->getCacheActorsDeleteSql(); + $this->limitToIdString($qb, $id); + + $qb->execute(); + } + + } diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 6c84718a..2c8f2803 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -85,11 +85,13 @@ class PersonService implements ICoreService { /** * @param string $id * + * @param bool $refresh + * * @return Person - * @throws RequestException * @throws InvalidResourceException + * @throws RequestException */ - public function getFromId(string $id): Person { + public function getFromId(string $id, bool $refresh = false): Person { $posAnchor = strpos($id, '#'); if ($posAnchor !== false) { @@ -97,24 +99,30 @@ class PersonService implements ICoreService { } try { + if ($refresh) { + $this->cacheActorsRequest->deleteFromId($id); + throw new CacheActorDoesNotExistException(); + } + $actor = $this->cacheActorsRequest->getFromId($id); } catch (CacheActorDoesNotExistException $e) { $object = $this->instanceService->retrieveObject($id); $actor = new Person(); $actor->import($object); - if ($actor->getType() !== 'Person') { - throw new InvalidResourceException(); - } - $actor->setPreferredUsername($this->get('preferredUsername', $object, '')); $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object)); $actor->setSharedInbox($this->get('endpoints.sharedInbox', $object)); $actor->setAccount($actor->getPreferredUsername() . '@' . $this->get('_host', $object)); + + if ($actor->getType() !== 'Person') { + throw new InvalidResourceException(); + } + try { $this->save($actor); } catch (Exception $e) { - throw new InvalidResourceException(); + throw new InvalidResourceException($e->getMessage()); } } @@ -138,15 +146,20 @@ class PersonService implements ICoreService { $actor = new Person(); $actor->import($object); - if ($actor->getType() !== 'Person') { - throw new InvalidResourceException(); - } - $actor->setAccount($account); $actor->setPreferredUsername($this->get('preferredUsername', $object, '')); $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object)); $actor->setSharedInbox($this->get('endpoints.sharedInbox', $object)); - $this->save($actor); + + if ($actor->getType() !== 'Person') { + throw new InvalidResourceException(); + } + + try { + $this->save($actor); + } catch (Exception $e) { + throw new InvalidResourceException($e->getMessage()); + } } return $actor; diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index f7730c1c..74f06e7a 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -38,6 +38,7 @@ use OCA\Social\Exceptions\AccountAlreadyExistsException; use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Model\InstancePath; +use OCA\Social\Service\ActivityPub\PersonService; /** @@ -51,12 +52,15 @@ class ActorService { use TArrayTools; - /** @var ConfigService */ - private $configService; - /** @var ActorsRequest */ private $actorsRequest; + /** @var PersonService */ + private $personService; + + /** @var ConfigService */ + private $configService; + /** @var MiscService */ private $miscService; @@ -65,14 +69,17 @@ class ActorService { * ActorService constructor. * * @param ActorsRequest $actorsRequest + * @param PersonService $personService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - ActorsRequest $actorsRequest, ConfigService $configService, MiscService $miscService + ActorsRequest $actorsRequest, PersonService $personService, ConfigService $configService, + MiscService $miscService ) { - $this->configService = $configService; $this->actorsRequest = $actorsRequest; + $this->personService = $personService; + $this->configService = $configService; $this->miscService = $miscService; } @@ -90,6 +97,12 @@ class ActorService { return $actor; } + /** + * @param string $id + * + * @return Person + * @throws ActorDoesNotExistException + */ public function getActorById(string $id): Person { $actor = $this->actorsRequest->getFromId($id); @@ -122,8 +135,6 @@ class ActorService { } - - /** * Method should be called by the frontend and will generate a fresh Social account for * the user, using the userId and the username. @@ -166,7 +177,10 @@ class ActorService { $actor->setPreferredUsername($username); $this->generateKeys($actor); - $this->actorsRequest->create($actor); + $id = $this->actorsRequest->create($actor); + + // generate cache. + $this->personService->getFromId($id, true); }