diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 7fb7ec5c..58ac8c7a 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -102,6 +102,45 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { } + /** + * insert cache about an Actor in database. + * + * @param Person $actor + */ + public function update(Person $actor) { + $qb = $this->getCacheActorsUpdateSql(); + $qb->set('account', $qb->createNamedParameter($actor->getAccount())) + ->set('following', $qb->createNamedParameter($actor->getFollowing())) + ->set('followers', $qb->createNamedParameter($actor->getFollowers())) + ->set('inbox', $qb->createNamedParameter($actor->getInbox())) + ->set('shared_inbox', $qb->createNamedParameter($actor->getSharedInbox())) + ->set('outbox', $qb->createNamedParameter($actor->getOutbox())) + ->set('featured', $qb->createNamedParameter($actor->getFeatured())) + ->set('url', $qb->createNamedParameter($actor->getUrl())) + ->set('preferred_username', $qb->createNamedParameter($actor->getPreferredUsername())) + ->set('name', $qb->createNamedParameter($actor->getName())) + ->set('summary', $qb->createNamedParameter($actor->getSummary())) + ->set('public_key', $qb->createNamedParameter($actor->getPublicKey())) + ->set('source', $qb->createNamedParameter($actor->getSource())) + ->set('details', $qb->createNamedParameter(json_encode($actor->getDetails()))) + ->set( + 'creation', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + + if ($actor->gotIcon()) { + $iconId = $actor->getIcon() + ->getId(); + } else { + $iconId = $actor->getIconId(); + } + + $qb->set('icon_id', $qb->createNamedParameter($iconId)); + + $qb->execute(); + } + + /** * get Cached version of an Actor, based on the UriId * diff --git a/lib/Interfaces/Actor/PersonInterface.php b/lib/Interfaces/Actor/PersonInterface.php index 9dfe83cd..acea191f 100644 --- a/lib/Interfaces/Actor/PersonInterface.php +++ b/lib/Interfaces/Actor/PersonInterface.php @@ -110,7 +110,9 @@ class PersonInterface implements IActivityPubInterface { */ public function getItemById(string $id): ACore { try { - return $this->cacheActorsRequest->getFromId($id); + $actor = $this->cacheActorsRequest->getFromId($id); + + return $actor; } catch (CacheActorDoesNotExistException $e) { throw new ItemNotFoundException(); } @@ -122,7 +124,12 @@ class PersonInterface implements IActivityPubInterface { */ public function save(ACore $person) { /** @var Person $person */ - $this->actorService->save($person); + try { + $this->getItemById($person->getId()); + $this->actorService->update($person); + } catch (ItemNotFoundException $e) { + $this->actorService->save($person); + } } diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 6b2438f7..8dea61bd 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -124,6 +124,24 @@ class ActorService { * @param Person $actor */ public function save(Person $actor) { + $this->cacheDocumentIfNeeded($actor); + $this->cacheActorsRequest->save($actor); + } + + + /** + * @param Person $actor + */ + public function update(Person $actor) { + $this->cacheDocumentIfNeeded($actor); + $this->cacheActorsRequest->update($actor); + } + + + /** + * @param Person $actor + */ + private function cacheDocumentIfNeeded(Person $actor) { if ($actor->gotIcon()) { try { $icon = $this->cacheDocumentsRequest->getBySource( @@ -135,8 +153,6 @@ class ActorService { $this->cacheDocumentsRequest->save($actor->getIcon()); } } - - $this->cacheActorsRequest->save($actor); } } diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index 07babd87..d4e4473f 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -130,7 +130,6 @@ class CacheActorService { try { if ($refresh) { - $this->cacheActorsRequest->deleteFromId($id); throw new CacheActorDoesNotExistException(); }