From 147e2800fa223874bcbd51353b2ae2afbf15472b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 11 Feb 2019 10:01:06 -0100 Subject: [PATCH] Delete on Actor delete also Note from this Actor Signed-off-by: Maxence Lange --- lib/Db/NotesRequest.php | 11 +++++ lib/Interfaces/Activity/DeleteInterface.php | 4 ++ lib/Interfaces/Actor/PersonInterface.php | 47 +++++++++++++-------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index b3ce7d74..e364dc76 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -375,5 +375,16 @@ class NotesRequest extends NotesRequestBuilder { $qb->execute(); } + + /** + * @param string $actorId + */ + public function deleteByAuthor(string $actorId) { + $qb = $this->getNotesDeleteSql(); + $this->limitToAttributedTo($qb, $actorId); + + $qb->execute(); + } + } diff --git a/lib/Interfaces/Activity/DeleteInterface.php b/lib/Interfaces/Activity/DeleteInterface.php index 5d57ffc9..0860239e 100644 --- a/lib/Interfaces/Activity/DeleteInterface.php +++ b/lib/Interfaces/Activity/DeleteInterface.php @@ -72,11 +72,13 @@ class DeleteInterface implements IActivityPubInterface { $types = ['Note', 'Person']; foreach ($types as $type) { try { + $item->checkOrigin($item->getObjectId()); $interface = AP::$activityPub->getInterfaceForItem($type); $object = $interface->getItemById($item->getObjectId()); $interface->delete($object); return; + } catch (InvalidOriginException $e) { } catch (ItemNotFoundException $e) { } catch (ItemUnknownException $e) { } @@ -88,8 +90,10 @@ class DeleteInterface implements IActivityPubInterface { $object = $item->getObject(); try { + $item->checkOrigin($object->getId()); $interface = AP::$activityPub->getInterfaceForItem($object); $interface->delete($object); + } catch (InvalidOriginException $e) { } catch (ItemUnknownException $e) { } } diff --git a/lib/Interfaces/Actor/PersonInterface.php b/lib/Interfaces/Actor/PersonInterface.php index 6e22a0da..25c0bc60 100644 --- a/lib/Interfaces/Actor/PersonInterface.php +++ b/lib/Interfaces/Actor/PersonInterface.php @@ -33,11 +33,13 @@ namespace OCA\Social\Interfaces\Actor; use daita\MySmallPhpTools\Traits\TArrayTools; use OCA\Social\Db\CacheActorsRequest; +use OCA\Social\Db\NotesRequest; use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Delete; use OCA\Social\Model\ActivityPub\Activity\Update; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Service\ActorService; @@ -59,6 +61,9 @@ class PersonInterface implements IActivityPubInterface { /** @var CacheActorsRequest */ private $cacheActorsRequest; + /** @var NotesRequest */ + private $notesRequest; + /** @var ActorService */ private $actorService; @@ -73,15 +78,17 @@ class PersonInterface implements IActivityPubInterface { * UndoService constructor. * * @param CacheActorsRequest $cacheActorsRequest + * @param NotesRequest $notesRequest * @param ActorService $actorService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - CacheActorsRequest $cacheActorsRequest, ActorService $actorService, - ConfigService $configService, MiscService $miscService + CacheActorsRequest $cacheActorsRequest, NotesRequest $notesRequest, + ActorService $actorService, ConfigService $configService, MiscService $miscService ) { $this->cacheActorsRequest = $cacheActorsRequest; + $this->notesRequest = $notesRequest; $this->actorService = $actorService; $this->configService = $configService; $this->miscService = $miscService; @@ -141,36 +148,40 @@ class PersonInterface implements IActivityPubInterface { */ public function activity(Acore $activity, ACore $item) { /** @var Person $item */ + $activity->checkOrigin($item->getId()); if ($activity->getType() === Update::TYPE) { - $activity->checkOrigin($item->getId()); - $item->setCreation($activity->getOriginCreationTime()); - - try { - $current = $this->cacheActorsRequest->getFromId($item->getId()); - if ($current->getCreation() < $activity->getOriginCreationTime()) { - $this->cacheActorsRequest->update($item); - } - } catch (CacheActorDoesNotExistException $e) { - $this->cacheActorsRequest->save($item); - } - + $this->updateActor($item, $activity); } } /** * @param ACore $item - * - * @throws InvalidOriginException */ public function delete(ACore $item) { - $item->checkOrigin(($item->getId())); - /** @var Person $item */ $this->cacheActorsRequest->deleteFromId($item->getId()); + $this->notesRequest->deleteByAuthor($item->getId()); } + /** + * @param Person $actor + * @param ACore $activity + */ + private function updateActor(Person $actor, ACore $activity) { + $actor->setCreation($activity->getOriginCreationTime()); + + try { + $current = $this->cacheActorsRequest->getFromId($actor->getId()); + if ($current->getCreation() < $activity->getOriginCreationTime()) { + $this->cacheActorsRequest->update($actor); + } + } catch (CacheActorDoesNotExistException $e) { + $this->cacheActorsRequest->save($actor); + } + } + }