From 3e8694ff530d28465d0afc7a7a4e43e0707ee604 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 3 Jan 2019 10:36:46 -0100 Subject: [PATCH 1/2] using AP object to generate items so UrlCloud is set by default Signed-off-by: Maxence Lange --- lib/AP.php | 56 ++++++++++++++------- lib/Interfaces/Activity/FollowInterface.php | 4 +- lib/Model/ActivityPub/Actor/Person.php | 9 +++- lib/Service/DocumentService.php | 9 ++-- lib/Service/FollowService.php | 5 +- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/lib/AP.php b/lib/AP.php index c205a2a3..a5f3b50f 100644 --- a/lib/AP.php +++ b/lib/AP.php @@ -32,9 +32,9 @@ namespace OCA\Social; use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Interfaces\Activity\AcceptInterface; use OCA\Social\Interfaces\Activity\AddInterface; use OCA\Social\Interfaces\Activity\BlockInterface; @@ -212,7 +212,6 @@ class AP { */ public function getSimpleItemFromData(array $data): Acore { $item = $this->getItemFromType($this->get('type', $data, '')); - $item->setUrlCloud($this->configService->getCloudAddress()); $item->import($data); $item->setSource(json_encode($data, JSON_UNESCAPED_SLASHES)); @@ -224,57 +223,78 @@ class AP { * * @return ACore * @throws ItemUnknownException + * @throws SocialAppConfigException */ - public function getItemFromType(string $type) { + public function getItemFromType(string $type): ACore { + switch ($type) { case Accept::TYPE: - return new Accept(); + $item = new Accept(); + break; case Add::TYPE: - return new Add(); + $item = new Add(); + break; case Block::TYPE: - return new Block(); + $item = new Block(); + break; case Create::TYPE: - return new Create(); + $item = new Create(); + break; case Delete::TYPE: - return new Delete(); + $item = new Delete(); + break; case Follow::TYPE: - return new Follow(); + $item = new Follow(); + break; case Image::TYPE: - return new Image(); + $item = new Image(); + break; case Like::TYPE: - return new Like(); + $item = new Like(); + break; case Note::TYPE: - return new Note(); + $item = new Note(); + break; case Person::TYPE: - return new Person(); + $item = new Person(); + break; case Reject::TYPE: - return new Reject(); + $item = new Reject(); + break; case Remove::TYPE: - return new Remove(); + $item = new Remove(); + break; case Tombstone::TYPE: - return new Tombstone(); + $item = new Tombstone(); + break; case Undo::TYPE: - return new Undo(); + $item = new Undo(); + break; case Update::TYPE: - return new Update(); + $item = new Update(); + break; default: throw new ItemUnknownException(); } + + $item->setUrlCloud($this->configService->getCloudAddress()); + + return $item; } diff --git a/lib/Interfaces/Activity/FollowInterface.php b/lib/Interfaces/Activity/FollowInterface.php index b88381ff..d73070a0 100644 --- a/lib/Interfaces/Activity/FollowInterface.php +++ b/lib/Interfaces/Activity/FollowInterface.php @@ -33,6 +33,7 @@ namespace OCA\Social\Interfaces\Activity; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use Exception; +use OCA\Social\AP; use OCA\Social\Db\FollowsRequest; use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; @@ -112,8 +113,7 @@ class FollowInterface implements IActivityPubInterface { try { $remoteActor = $this->cacheActorService->getFromId($follow->getActorId()); - $accept = new Accept(); - $accept->setUrlCloud($this->configService->getCloudAddress()); + $accept = AP::$activityPub->getItemFromType(Accept::TYPE); $accept->generateUniqueId('#accept/follows'); $accept->setActorId($follow->getObjectId()); $accept->setObject($follow); diff --git a/lib/Model/ActivityPub/Actor/Person.php b/lib/Model/ActivityPub/Actor/Person.php index 6383ca03..0024eb69 100644 --- a/lib/Model/ActivityPub/Actor/Person.php +++ b/lib/Model/ActivityPub/Actor/Person.php @@ -33,6 +33,9 @@ namespace OCA\Social\Model\ActivityPub\Actor; use DateTime; use JsonSerializable; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemUnknownException; +use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Object\Image; @@ -442,6 +445,8 @@ class Person extends ACore implements JsonSerializable { /** * @param array $data * + * @throws ItemUnknownException + * @throws SocialAppConfigException * @throws UrlCloudException */ public function import(array $data) { @@ -459,8 +464,8 @@ class Person extends ACore implements JsonSerializable { ->setFollowing($this->validate(ACore::AS_URL, 'following', $data, '')) ->setFeatured($this->validate(ACore::AS_URL, 'featured', $data, '')); - $icon = new Image($this); - $icon->setUrlCloud($this->getUrlCloud()); + /** @var Image $icon */ + $icon = AP::$activityPub->getItemFromType(Image::TYPE); $icon->import($this->getArray('icon', $data, [])); if ($icon->getType() === Image::TYPE) { diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 650b8d4f..07b86f6c 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -33,11 +33,13 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use Exception; +use OCA\Social\AP; use OCA\Social\Db\ActorsRequest; use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; +use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; use OCA\Social\Exceptions\RequestResultSizeException; @@ -216,6 +218,7 @@ class DocumentService { * @return string * @throws SocialAppConfigException * @throws UrlCloudException + * @throws ItemUnknownException */ public function cacheLocalAvatarByUsername(Person $actor): string { $url = $this->urlGenerator->linkToRouteAbsolute( @@ -226,10 +229,10 @@ class DocumentService { (int)$this->configService->getUserValue('version', $actor->getUserId(), 'avatar'); $versionCached = $actor->getAvatarVersion(); if ($versionCurrent > $versionCached) { - $icon = new Image(); - $icon->setUrl($url); - $icon->setUrlcloud($this->configService->getCloudAddress()); + /** @var Image $icon */ + $icon = AP::$activityPub->getItemFromType(Image::TYPE); $icon->generateUniqueId('/documents/avatar'); + $icon->setUrl($url); $icon->setMediaType(''); $icon->setLocalCopy('avatar'); diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index 21d48627..c305fa71 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -32,6 +32,7 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\AP; use OCA\Social\Db\FollowsRequest; use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\FollowDoesNotExistException; @@ -139,8 +140,8 @@ class FollowService { throw new FollowSameAccountException("Don't follow yourself, be your own lead"); } - $follow = new Follow(); - $follow->setUrlCloud($this->configService->getCloudAddress()); + /** @var Follow $follow */ + $follow = AP::$activityPub->getItemFromType(Follow::TYPE); $follow->generateUniqueId(); $follow->setActorId($actor->getId()); $follow->setObjectId($remoteActor->getId()); From a1410b7ed83b9bc9c90ffb41d1d2e42577914595 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 3 Jan 2019 10:37:02 -0100 Subject: [PATCH 2/2] generate Id on Undo Signed-off-by: Maxence Lange --- lib/Service/FollowService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index c305fa71..f6a2c45c 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -180,6 +180,7 @@ class FollowService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws UrlCloudException */ public function unfollowAccount(Person $actor, string $account) { $remoteActor = $this->cacheActorService->getFromAccount($account); @@ -188,8 +189,9 @@ class FollowService { $follow = $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId()); $this->followsRequest->delete($follow); - $undo = new Undo(); + $undo = AP::$activityPub->getItemFromType(Undo::TYPE); $follow->setParent($undo); + $undo->generateUniqueId('#undo/follows'); $undo->setObject($follow); $undo->setActorId($actor->getId());