diff --git a/lib/AP.php b/lib/AP.php new file mode 100644 index 00000000..705dafe2 --- /dev/null +++ b/lib/AP.php @@ -0,0 +1,363 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Exceptions\RedundancyLimitException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\Activity\AcceptInterface; +use OCA\Social\Interfaces\Activity\AddInterface; +use OCA\Social\Interfaces\Activity\BlockInterface; +use OCA\Social\Interfaces\Activity\CreateInterface; +use OCA\Social\Interfaces\Activity\DeleteInterface; +use OCA\Social\Interfaces\Activity\FollowInterface; +use OCA\Social\Interfaces\Activity\LikeInterface; +use OCA\Social\Interfaces\Activity\RejectInterface; +use OCA\Social\Interfaces\Activity\RemoveInterface; +use OCA\Social\Interfaces\Activity\UndoInterface; +use OCA\Social\Interfaces\Activity\UpdateInterface; +use OCA\Social\Interfaces\Actor\PersonInterface; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Interfaces\Object\NoteInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Accept; +use OCA\Social\Model\ActivityPub\Activity\Add; +use OCA\Social\Model\ActivityPub\Activity\Block; +use OCA\Social\Model\ActivityPub\Activity\Create; +use OCA\Social\Model\ActivityPub\Activity\Delete; +use OCA\Social\Model\ActivityPub\Activity\Follow; +use OCA\Social\Model\ActivityPub\Activity\Like; +use OCA\Social\Model\ActivityPub\Activity\Reject; +use OCA\Social\Model\ActivityPub\Activity\Remove; +use OCA\Social\Model\ActivityPub\Activity\Undo; +use OCA\Social\Model\ActivityPub\Activity\Update; +use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Model\ActivityPub\Object\Document; +use OCA\Social\Model\ActivityPub\Object\Image; +use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Object\Tombstone; +use OCA\Social\Service\ConfigService; +use OCP\AppFramework\QueryException; + + +/** + * Class AP + * + * @package OCA\Social + */ +class AP { + + + use TArrayTools; + + + const REDUNDANCY_LIMIT = 10; + + + /** @var AcceptInterface */ + public $acceptInterface; + + /** @var AddInterface */ + public $addInterface; + + /** @var BlockInterface */ + public $blockInterface; + + /** @var CreateInterface */ + public $createInterface; + + /** @var DeleteInterface */ + public $deleteInterface; + + /** @var FollowInterface */ + public $followInterface; + + /** @var LikeInterface */ + public $likeInterface; + + /** @var PersonInterface */ + public $personInterface; + + /** @var NoteInterface */ + public $noteInterface; + + /** @var RejectInterface */ + public $rejectInterface; + + /** @var RemoveInterface */ + public $removeInterface; + + /** @var UndoInterface */ + public $undoInterface; + + /** @var UpdateInterface */ + public $updateInterface; + + /** @var ConfigService */ + public $configService; + + + /** @var AP */ + public static $activityPub = null; + + + /** + * AP constructor. + */ + public function __construct() { + } + + + /** + * + */ + public static function init() { + $ap = new AP(); + try { + $ap->acceptInterface = \OC::$server->query(AcceptInterface::class); + $ap->addInterface = \OC::$server->query(AddInterface::class); + $ap->blockInterface = \OC::$server->query(BlockInterface::class); + $ap->createInterface = \OC::$server->query(CreateInterface::class); + $ap->deleteInterface = \OC::$server->query(DeleteInterface::class); + $ap->followInterface = \OC::$server->query(FollowInterface::class); + $ap->likeInterface = \OC::$server->query(LikeInterface::class); + $ap->rejectInterface = \OC::$server->query(RejectInterface::class); + $ap->removeInterface = \OC::$server->query(RemoveInterface::class); + $ap->personInterface = \OC::$server->query(PersonInterface::class); + $ap->noteInterface = \OC::$server->query(NoteInterface::class); + $ap->undoInterface = \OC::$server->query(UndoInterface::class); + $ap->updateInterface = \OC::$server->query(UpdateInterface::class); + + $ap->configService = \OC::$server->query(ConfigService::class); + + AP::$activityPub = $ap; + } catch (QueryException $e) { + } + } + + + /** + * @param array $data + * @param ACore $parent + * @param int $level + * + * @return ACore + * @throws RedundancyLimitException + * @throws SocialAppConfigException + * @throws UnknownItemException + */ + public function getItemFromData(array $data, $parent = null, int $level = 0): ACore { + if (++$level > self::REDUNDANCY_LIMIT) { + throw new RedundancyLimitException($level); + } + + $item = $this->getSimpleItemFromData($data); + if ($parent !== null) { + $item->setParent($parent); + } + + try { + $object = $this->getItemFromData($this->getArray('object', $data, []), $item, $level); + $item->setObject($object); + } catch (UnknownItemException $e) { + } + + try { + /** @var Document $icon */ + $icon = $this->getItemFromData($this->getArray('icon', $data, []), $item, $level); + $item->setIcon($icon); + } catch (UnknownItemException $e) { + } + + return $item; + } + + + /** + * @param array $data + * + * @return ACore + * @throws SocialAppConfigException + * @throws UnknownItemException + */ + 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)); + + return $item; + } + + /** + * @param string $type + * + * @return ACore + * @throws UnknownItemException + */ + public function getItemFromType(string $type) { + switch ($type) { + case Accept::TYPE: + return new Accept(); + + case Add::TYPE: + return new Add(); + + case Block::TYPE: + return new Block(); + + case Create::TYPE: + return new Create(); + + case Delete::TYPE: + return new Delete(); + + case Follow::TYPE: + return new Follow(); + + case Image::TYPE: + return new Image(); + + case Like::TYPE: + return new Like(); + + case Note::TYPE: + return new Note(); + + case Person::TYPE: + return new Note(); + + case Reject::TYPE: + return new Reject(); + + case Remove::TYPE: + return new Remove(); + + case Tombstone::TYPE: + return new Tombstone(); + + case Undo::TYPE: + return new Undo(); + + case Update::TYPE: + return new Update(); + + default: + throw new UnknownItemException(); + } + } + + + /** + * @param ACore $activity + * + * @return IActivityPubInterface + * @throws UnknownItemException + */ + public function getInterfaceForItem(Acore $activity): IActivityPubInterface { + return $this->getInterfaceFromType($activity->getType()); + } + + + /** + * @param string $type + * + * @return IActivityPubInterface + * @throws UnknownItemException + */ + public function getInterfaceFromType(string $type): IActivityPubInterface { + switch ($type) { + case Accept::TYPE: + $service = $this->acceptInterface; + break; + + case Add::TYPE: + $service = $this->addInterface; + break; + + case Block::TYPE: + $service = $this->blockInterface; + break; + + case Create::TYPE: + $service = $this->createInterface; + break; + + case Delete::TYPE: + $service = $this->deleteInterface; + break; + + case Follow::TYPE: + $service = $this->followInterface; + break; + + case Like::TYPE: + $service = $this->likeInterface; + break; + + case Note::TYPE: + $service = $this->noteInterface; + break; + + case Person::TYPE: + $service = $this->personInterface; + break; + + case Reject::TYPE: + $service = $this->rejectInterface; + break; + + case Remove::TYPE: + $service = $this->removeInterface; + break; + + case Undo::TYPE: + $service = $this->undoInterface; + break; + + case Update::TYPE: + $service = $this->updateInterface; + break; + + default: + throw new UnknownItemException(); + } + + return $service; + } + +} + + +AP::init(); + diff --git a/lib/Exceptions/ItemNotFoundException.php b/lib/Exceptions/ItemNotFoundException.php new file mode 100644 index 00000000..60b90bf1 --- /dev/null +++ b/lib/Exceptions/ItemNotFoundException.php @@ -0,0 +1,8 @@ +gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,21 @@ class AcceptService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + /** * @param ACore $item @@ -101,5 +110,6 @@ class AcceptService implements ICoreService { */ public function activity(ACore $activity, ACore $item) { } + } diff --git a/lib/Service/ActivityPub/Activity/AddService.php b/lib/Interfaces/Activity/AddInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/AddService.php rename to lib/Interfaces/Activity/AddInterface.php index 166e2b80..efd0adce 100644 --- a/lib/Service/ActivityPub/Activity/AddService.php +++ b/lib/Interfaces/Activity/AddInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class AddService implements ICoreService { +class AddInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class AddService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,20 @@ class AddService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } /** diff --git a/lib/Service/ActivityPub/Activity/BlockService.php b/lib/Interfaces/Activity/BlockInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/BlockService.php rename to lib/Interfaces/Activity/BlockInterface.php index 35c58e3f..cec96640 100644 --- a/lib/Service/ActivityPub/Activity/BlockService.php +++ b/lib/Interfaces/Activity/BlockInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class BlockService implements ICoreService { +class BlockInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class BlockService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,20 @@ class BlockService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } /** diff --git a/lib/Interfaces/Activity/CreateInterface.php b/lib/Interfaces/Activity/CreateInterface.php new file mode 100644 index 00000000..966fa931 --- /dev/null +++ b/lib/Interfaces/Activity/CreateInterface.php @@ -0,0 +1,114 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Interfaces\Activity; + + +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Service\MiscService; + + +class CreateInterface implements IActivityPubInterface { + + + /** @var MiscService */ + private $miscService; + + + /** + * CreateInterface constructor. + * + * @param MiscService $miscService + */ + public function __construct(MiscService $miscService) { + $this->miscService = $miscService; + } + + + /** + * @param ACore $item + */ + public function processIncomingRequest(ACore $item) { + if (!$item->gotObject()) { + return; + } + $object = $item->getObject(); + + try { + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); + $service->activity($item, $object); + } catch (UnknownItemException $e) { + } + } + + + /** + * @param ACore $item + */ + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + + + /** + * @param ACore $item + */ + public function save(ACore $item) { + } + + + /** + * @param ACore $item + */ + public function delete(ACore $item) { + } + + /** + * @param ACore $activity + * @param ACore $item + */ + public function activity(ACore $activity, ACore $item) { + } +} + diff --git a/lib/Interfaces/Activity/DeleteInterface.php b/lib/Interfaces/Activity/DeleteInterface.php new file mode 100644 index 00000000..abe5a815 --- /dev/null +++ b/lib/Interfaces/Activity/DeleteInterface.php @@ -0,0 +1,148 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Interfaces\Activity; + + +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Service\MiscService; + +class DeleteInterface implements IActivityPubInterface { + + + /** @var MiscService */ + private $miscService; + + + /** + * UndoService constructor. + * + * @param MiscService $miscService + */ + public function __construct(MiscService $miscService) { + $this->miscService = $miscService; + } + + + /** + * @param ACore $item + * + */ + public function processIncomingRequest(ACore $item) { + if (!$item->gotObject()) { + return; + } + $object = $item->getObject(); + + try { + $service = AP::$activityPub->getInterfaceForItem($object); + $service->delete($object); + } catch (UnknownItemException $e) { + } + } + +// +// /** +// * @param ACore $delete +// * +// * @throws InvalidOriginException +// */ +// public function processIncomingRequest(ACore $delete) { +// +// if ($delete->gotObject()) { +// $id = $delete->getObject() +// ->getId(); +// } else { +// $id = $delete->getObjectId(); +// } +// +// $delete->checkOrigin($id); +// +// +// /** @var Delete $delete */ +// try { +// $item = $this->activityService->getItem($id); +// $service = AP::$activityPub->getServiceForItem($item); +// +// // we could use ->activity($delete, $item) but the delete() is important enough to +// // be here, and to use it. +//// $service->delete($item); +// } catch (UnknownItemException $e) { +// } catch (InvalidResourceException $e) { +// } +// } + + + /** + * @param ACore $item + */ + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + + + /** + * @param ACore $activity + * @param ACore $item + */ + public function activity(Acore $activity, ACore $item) { + } + + + /** + * @param ACore $item + */ + public function delete(ACore $item) { + } + + + /** + * @param ACore $item + */ + public function save(ACore $item) { + } + + +} + diff --git a/lib/Service/ActivityPub/Activity/FollowService.php b/lib/Interfaces/Activity/FollowInterface.php similarity index 52% rename from lib/Service/ActivityPub/Activity/FollowService.php rename to lib/Interfaces/Activity/FollowInterface.php index 0d90a040..c91da9fc 100644 --- a/lib/Service/ActivityPub/Activity/FollowService.php +++ b/lib/Interfaces/Activity/FollowInterface.php @@ -28,46 +28,42 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use Exception; use OCA\Social\Db\FollowsRequest; -use OCA\Social\Exceptions\ActorDoesNotExistException; -use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\FollowDoesNotExistException; -use OCA\Social\Exceptions\FollowSameAccountException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\Request410Exception; use OCA\Social\Exceptions\RequestException; use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UrlCloudException; +use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Activity\Accept; use OCA\Social\Model\ActivityPub\Activity\Follow; use OCA\Social\Model\ActivityPub\Activity\Reject; use OCA\Social\Model\ActivityPub\Activity\Undo; -use OCA\Social\Model\ActivityPub\OrderedCollection; -use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\InstancePath; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ActivityPub\Actor\PersonService; use OCA\Social\Service\ActivityService; +use OCA\Social\Service\CacheActorService; use OCA\Social\Service\ConfigService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class FollowService implements ICoreService { +class FollowInterface implements IActivityPubInterface { /** @var FollowsRequest */ private $followsRequest; - /** @var PersonService */ - private $personService; + /** @var CacheActorService */ + private $cacheActorService; /** @var ActivityService */ private $activityService; @@ -79,163 +75,31 @@ class FollowService implements ICoreService { private $miscService; - /** @var string */ - private $viewerId = ''; - - /** - * NoteService constructor. + * NoteInterface constructor. * * @param FollowsRequest $followsRequest - * @param PersonService $personService + * @param CacheActorService $cacheActorService * @param ActivityService $activityService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - FollowsRequest $followsRequest, PersonService $personService, - ActivityService $activityService, ConfigService $configService, - MiscService $miscService + FollowsRequest $followsRequest, CacheActorService $cacheActorService, + ActivityService $activityService, ConfigService $configService, MiscService $miscService ) { $this->followsRequest = $followsRequest; - $this->personService = $personService; + $this->cacheActorService = $cacheActorService; $this->activityService = $activityService; $this->configService = $configService; $this->miscService = $miscService; } - /** - * @param string $viewerId - */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->followsRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; - } - - - /** - * @param Person $actor - * @param string $account - * - * @throws ActorDoesNotExistException - * @throws RequestException - * @throws SocialAppConfigException - * @throws CacheActorDoesNotExistException - * @throws InvalidResourceException - * @throws UrlCloudException - * @throws FollowSameAccountException - */ - public function followAccount(Person $actor, string $account) { - $remoteActor = $this->personService->getFromAccount($account); - if ($remoteActor->getId() === $actor->getId()) { - throw new FollowSameAccountException("Don't follow yourself, be your own lead"); - } - - $follow = new Follow(); - $follow->setUrlCloud($this->configService->getCloudAddress()); - $follow->generateUniqueId(); - $follow->setActorId($actor->getId()); - $follow->setObjectId($remoteActor->getId()); - $follow->setFollowId($remoteActor->getFollowers()); - - try { - $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId()); - } catch (FollowDoesNotExistException $e) { - $this->followsRequest->save($follow); - // TODO - Remove this auto-accepted. - $this->followsRequest->accepted($follow); - - $follow->addInstancePath( - new InstancePath( - $remoteActor->getInbox(), InstancePath::TYPE_INBOX, InstancePath::PRIORITY_TOP - ) - ); - $this->activityService->request($follow); - } - } - - - /** - * @param Person $actor - * @param string $account - * - * @throws CacheActorDoesNotExistException - * @throws InvalidResourceException - * @throws RequestException - * @throws SocialAppConfigException - * @throws UrlCloudException - */ - public function unfollowAccount(Person $actor, string $account) { - $remoteActor = $this->personService->getFromAccount($account); - - try { - $follow = $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId()); - $this->followsRequest->delete($follow); - } catch (FollowDoesNotExistException $e) { - } - } - - - /** - * @param Person $actor - * - * @return Person[] - */ - public function getFollowers(Person $actor): array { - return $this->followsRequest->getFollowersByActorId($actor->getId()); - } - - - /** - * @param Person $actor - * - * @return OrderedCollection - */ - public function getFollowersCollection(Person $actor): OrderedCollection { - $collection = new OrderedCollection(); - $collection->setId($actor->getFollowers()); - $collection->setTotalItems(20); - $collection->setFirst('...'); - - return $collection; - } - - - /** - * @param Person $actor - * - * @return Person[] - */ - public function getFollowing(Person $actor): array { - return $this->followsRequest->getFollowingByActorId($actor->getId()); - } - - - /** - * @param Person $actor - * - * @return OrderedCollection - */ - public function getFollowingCollection(Person $actor): OrderedCollection { - $collection = new OrderedCollection(); -// $collection->setId($actor->getFollowers()); -// $collection->setTotalItems(20); -// $collection->setFirst('...'); - - return $collection; - } - - /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } @@ -244,7 +108,7 @@ class FollowService implements ICoreService { */ public function confirmFollowRequest(Follow $follow) { try { - $remoteActor = $this->personService->getFromId($follow->getActorId()); + $remoteActor = $this->cacheActorService->getFromId($follow->getActorId()); $accept = new Accept(); // TODO: improve the generation of the Id @@ -271,17 +135,17 @@ class FollowService implements ICoreService { * This method is called when saving the Follow object * * @param ACore $follow - * @param ImportService $importService * + * @throws InvalidOriginException * @throws InvalidResourceException + * @throws MalformedArrayException + * @throws Request410Exception * @throws RequestException * @throws SocialAppConfigException - * @throws UrlCloudException - * @throws InvalidOriginException - * @throws Request410Exception - * @throws MalformedArrayException + * @throws RedundancyLimitException + * @throws UnknownItemException */ - public function processIncomingRequest(ACore $follow, ImportService $importService) { + public function processIncomingRequest(ACore $follow) { /** @var Follow $follow */ $follow->checkOrigin($follow->getActorId()); @@ -294,7 +158,7 @@ class FollowService implements ICoreService { $this->confirmFollowRequest($follow); } } catch (FollowDoesNotExistException $e) { - $actor = $this->personService->getFromId($follow->getObjectId()); + $actor = $this->cacheActorService->getFromId($follow->getObjectId()); if ($actor->isLocal()) { $follow->setFollowId($actor->getFollowers()); $this->followsRequest->save($follow); @@ -302,7 +166,17 @@ class FollowService implements ICoreService { } } -// } else { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); } diff --git a/lib/Service/ActivityPub/Activity/LikeService.php b/lib/Interfaces/Activity/LikeInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/LikeService.php rename to lib/Interfaces/Activity/LikeInterface.php index a85c82c5..da44e141 100644 --- a/lib/Service/ActivityPub/Activity/LikeService.php +++ b/lib/Interfaces/Activity/LikeInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class LikeService implements ICoreService { +class LikeInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class LikeService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,9 +75,19 @@ class LikeService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); } diff --git a/lib/Service/ActivityPub/Activity/RejectService.php b/lib/Interfaces/Activity/RejectInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/RejectService.php rename to lib/Interfaces/Activity/RejectInterface.php index 859703fe..ff268633 100644 --- a/lib/Service/ActivityPub/Activity/RejectService.php +++ b/lib/Interfaces/Activity/RejectInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class RejectService implements ICoreService { +class RejectInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class RejectService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,21 @@ class RejectService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + /** * @param ACore $item diff --git a/lib/Service/ActivityPub/Activity/RemoveService.php b/lib/Interfaces/Activity/RemoveInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/RemoveService.php rename to lib/Interfaces/Activity/RemoveInterface.php index 525fa5c7..74fc5980 100644 --- a/lib/Service/ActivityPub/Activity/RemoveService.php +++ b/lib/Interfaces/Activity/RemoveInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class RemoveService implements ICoreService { +class RemoveInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class RemoveService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,21 @@ class RemoveService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + /** * @param ACore $item diff --git a/lib/Service/ActivityPub/Activity/UndoService.php b/lib/Interfaces/Activity/UndoInterface.php similarity index 77% rename from lib/Service/ActivityPub/Activity/UndoService.php rename to lib/Interfaces/Activity/UndoInterface.php index 96d7b30e..83e32afb 100644 --- a/lib/Service/ActivityPub/Activity/UndoService.php +++ b/lib/Interfaces/Activity/UndoInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class UndoService implements ICoreService { +class UndoInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class UndoService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,12 +75,20 @@ class UndoService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { } + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } /** diff --git a/lib/Service/ActivityPub/Activity/UpdateService.php b/lib/Interfaces/Activity/UpdateInterface.php similarity index 76% rename from lib/Service/ActivityPub/Activity/UpdateService.php rename to lib/Interfaces/Activity/UpdateInterface.php index 82582600..1fee8bf9 100644 --- a/lib/Service/ActivityPub/Activity/UpdateService.php +++ b/lib/Interfaces/Activity/UpdateInterface.php @@ -28,17 +28,18 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub\Activity; +namespace OCA\Social\Interfaces\Activity; +use OCA\Social\AP; +use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\UnknownItemException; +use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; -class UpdateService implements ICoreService { +class UpdateInterface implements IActivityPubInterface { /** @var MiscService */ @@ -57,16 +58,15 @@ class UpdateService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService) { + public function processIncomingRequest(ACore $item) { if (!$item->gotObject()) { return; } $object = $item->getObject(); try { - $service = $importService->getServiceForItem($item->getObject()); + $service = AP::$activityPub->getInterfaceForItem($item->getObject()); $service->activity($item, $object); } catch (UnknownItemException $e) { } @@ -75,9 +75,19 @@ class UpdateService implements ICoreService { /** * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService) { + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); } diff --git a/lib/Interfaces/Actor/PersonInterface.php b/lib/Interfaces/Actor/PersonInterface.php new file mode 100644 index 00000000..5c84dc9c --- /dev/null +++ b/lib/Interfaces/Actor/PersonInterface.php @@ -0,0 +1,142 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Interfaces\Actor; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Db\CacheActorsRequest; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Update; +use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Service\ActorService; +use OCA\Social\Service\ConfigService; +use OCA\Social\Service\MiscService; + + +/** + * Class PersonService + * + * @package OCA\Social\Service\ActivityPub + */ +class PersonInterface implements IActivityPubInterface { + + + use TArrayTools; + + + /** @var CacheActorsRequest */ + private $cacheActorsRequest; + + /** @var ActorService */ + private $actorService; + + /** @var ConfigService */ + private $configService; + + /** @var MiscService */ + private $miscService; + + + /** + * UndoService constructor. + * + * @param CacheActorsRequest $cacheActorsRequest + * @param ActorService $actorService + * @param ConfigService $configService + * @param MiscService $miscService + */ + public function __construct( + CacheActorsRequest $cacheActorsRequest, ActorService $actorService, + ConfigService $configService, MiscService $miscService + ) { + $this->cacheActorsRequest = $cacheActorsRequest; + $this->actorService = $actorService; + $this->configService = $configService; + $this->miscService = $miscService; + } + + + /** + * @param ACore $person + */ + public function processIncomingRequest(ACore $person) { + } + + + /** + * @param ACore $item + */ + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + + + /** + * @param ACore $person + */ + public function save(ACore $person) { + /** @var Person $person */ + $this->actorService->save($person); + } + + + /** + * @param ACore $activity + * @param ACore $item + */ + public function activity(Acore $activity, ACore $item) { + /** @var Person $item */ + if ($activity->getType() === Update::TYPE) { +// $this->miscService->log('### UPDATE PERSON !' . json_encode($item)); + } + } + + + /** + * @param ACore $item + */ + public function delete(ACore $item) { + } + +} + diff --git a/lib/Service/ActivityPub/ICoreService.php b/lib/Interfaces/IActivityPubInterface.php similarity index 83% rename from lib/Service/ActivityPub/ICoreService.php rename to lib/Interfaces/IActivityPubInterface.php index a0ec09b8..c1599dbd 100644 --- a/lib/Service/ActivityPub/ICoreService.php +++ b/lib/Interfaces/IActivityPubInterface.php @@ -28,11 +28,10 @@ declare(strict_types=1); */ -namespace OCA\Social\Service\ActivityPub; +namespace OCA\Social\Interfaces; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ImportService; /** @@ -40,25 +39,32 @@ use OCA\Social\Service\ImportService; * * @package OCA\Social\Service */ -interface ICoreService { +interface IActivityPubInterface { /** * Freshly imported item can be processed/parsed on incoming Request. * * @param ACore $item - * @param ImportService $importService */ - public function processIncomingRequest(ACore $item, ImportService $importService); + public function processIncomingRequest(ACore $item); /** * Freshly imported item can be processed/parsed on result of outgoing request. * * @param ACore $item - * @param ImportService $importService */ - public function processResult(ACore $item, ImportService $importService); + public function processResult(ACore $item); + + + /** + * @param string $id + * + * @throw ItemNotFoundException + * @return ACore + */ + public function getItemById(string $id): ACore; /** @@ -85,6 +91,5 @@ interface ICoreService { */ public function delete(ACore $item); - } diff --git a/lib/Interfaces/Object/DocumentInterface.php b/lib/Interfaces/Object/DocumentInterface.php new file mode 100644 index 00000000..c7b0711d --- /dev/null +++ b/lib/Interfaces/Object/DocumentInterface.php @@ -0,0 +1,244 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Interfaces\Object; + + +use OCA\Social\Db\CacheDocumentsRequest; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Object\Document; +use OCA\Social\Service\MiscService; + + +class DocumentInterface implements IActivityPubInterface { + + + /** @var CacheDocumentsRequest */ + private $cacheDocumentsRequest; + + /** @var MiscService */ + private $miscService; + + + /** + * DocumentInterface constructor. + * + * @param CacheDocumentsRequest $cacheDocumentsRequest + * @param MiscService $miscService + */ + public function __construct( + CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService + ) { + $this->cacheDocumentsRequest = $cacheDocumentsRequest; + $this->miscService = $miscService; + } + +// +// /** +// * @param string $id +// * @param bool $public +// * +// * @return Document +// * @throws CacheDocumentDoesNotExistException +// * @throws NotPermittedException +// */ +// public function cacheRemoteDocument(string $id, bool $public = false) { +// $document = $this->cacheDocumentsRequest->getById($id, $public); +// if ($document->getError() > 0) { +// throw new CacheDocumentDoesNotExistException(); +// } +// +// if ($document->getLocalCopy() !== '') { +// return $document; +// } +// +// if ($document->getCaching() > (time() - (CacheDocumentsRequest::CACHING_TIMEOUT * 60))) { +// return $document; +// } +// +// $mime = ''; +// $this->cacheDocumentsRequest->initCaching($document); +// +// try { +// $localCopy = $this->cacheService->saveRemoteFileToCache($document->getUrl(), $mime); +// $document->setMimeType($mime); +// $document->setLocalCopy($localCopy); +// $this->cacheDocumentsRequest->endCaching($document); +// +// return $document; +// } catch (CacheContentMimeTypeException $e) { +// $document->setMimeType($mime); +// $document->setError(self::ERROR_MIMETYPE); +// $this->cacheDocumentsRequest->endCaching($document); +// } catch (CacheContentSizeException $e) { +// $document->setError(self::ERROR_SIZE); +// $this->cacheDocumentsRequest->endCaching($document); +// } catch (CacheContentException $e) { +// } +// +// throw new CacheDocumentDoesNotExistException(); +// } +// +// +// /** +// * @param string $id +// * +// * @param bool $public +// * +// * @return ISimpleFile +// * @throws CacheContentException +// * @throws CacheDocumentDoesNotExistException +// * @throws NotPermittedException +// */ +// public function getFromCache(string $id, bool $public = false) { +// $document = $this->cacheRemoteDocument($id, $public); +// +// return $this->cacheService->getContentFromCache($document->getLocalCopy()); +// } +// +// +// /** +// * @return int +// * @throws Exception +// */ +// public function manageCacheDocuments(): int { +// $update = $this->cacheDocumentsRequest->getNotCachedDocuments(); +// +// $count = 0; +// foreach ($update as $item) { +// if ($item->getLocalCopy() === 'avatar') { +// continue; +// } +// +// try { +// $this->cacheRemoteDocument($item->getId()); +// } catch (Exception $e) { +// continue; +// } +// $count++; +// } +// +// return $count; +// } +// +// +// /** +// * @param Person $actor +// * +// * @return string +// * @throws SocialAppConfigException +// * @throws UrlCloudException +// */ +// public function cacheLocalAvatarByUsername(Person $actor): string { +// $url = $this->urlGenerator->linkToRouteAbsolute( +// 'core.avatar.getAvatar', ['userId' => $actor->getUserId(), 'size' => 128] +// ); +// +// $versionCurrent = +// (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()); +// $icon->generateUniqueId('/documents/avatar'); +// $icon->setMediaType(''); +// $icon->setLocalCopy('avatar'); +// +// $this->cacheDocumentsRequest->deleteByUrl($icon->getUrl()); +// $this->cacheDocumentsRequest->save($icon); +// +// $actor->setAvatarVersion($versionCurrent); +// $this->actorRequest->update($actor); +// } else { +// try { +// $icon = $this->cacheDocumentsRequest->getBySource($url); +// } catch (CacheDocumentDoesNotExistException $e) { +// return ''; +// } +// } +// +// return $icon->getId(); +// } + + + /** + * @param ACore $activity + * @param ACore $item + */ + public function activity(Acore $activity, ACore $item) { + } + + + /** + * @param ACore $item + */ + public function processIncomingRequest(ACore $item) { + } + + + /** + * @param ACore $item + */ + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + + /** + * @param ACore $item + */ + public function save(ACore $item) { + /** @var Document $item */ + $this->cacheDocumentsRequest->save($item); + } + + + /** + * @param ACore $item + */ + public function delete(ACore $item) { +// $this->cacheDocumentsRequest->delete($item); + } + + +} + diff --git a/lib/Interfaces/Object/NoteInterface.php b/lib/Interfaces/Object/NoteInterface.php new file mode 100644 index 00000000..12174c6c --- /dev/null +++ b/lib/Interfaces/Object/NoteInterface.php @@ -0,0 +1,147 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Interfaces\Object; + + +use OCA\Social\Db\NotesRequest; +use OCA\Social\Exceptions\InvalidOriginException; +use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Exceptions\NoteNotFoundException; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Create; +use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Service\ConfigService; +use OCA\Social\Service\CurlService; +use OCA\Social\Service\MiscService; + + +class NoteInterface implements IActivityPubInterface { + + + /** @var NotesRequest */ + private $notesRequest; + + /** @var CurlService */ + private $curlService; + + /** @var ConfigService */ + private $configService; + + /** @var MiscService */ + private $miscService; + + + /** + * NoteInterface constructor. + * + * @param NotesRequest $notesRequest + * @param CurlService $curlService + * @param ConfigService $configService + * @param MiscService $miscService + */ + public function __construct( + NotesRequest $notesRequest, CurlService $curlService, ConfigService $configService, + MiscService $miscService + ) { + $this->notesRequest = $notesRequest; + $this->curlService = $curlService; + $this->configService = $configService; + $this->miscService = $miscService; + } + + + /** + * @param ACore $note + */ + public function processIncomingRequest(ACore $note) { + } + + + /** + * @param ACore $item + */ + public function processResult(ACore $item) { + } + + + /** + * @param string $id + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItemById(string $id): ACore { + throw new ItemNotFoundException(); + } + + + /** + * @param ACore $note + */ + public function save(ACore $note) { + /** @var Note $note */ + + try { + $this->notesRequest->getNoteById($note->getId()); + } catch (NoteNotFoundException $e) { + $this->notesRequest->save($note); + } + } + + + /** + * @param ACore $activity + * @param ACore $item + * + * @throws InvalidOriginException + */ + public function activity(Acore $activity, ACore $item) { + /** @var Note $item */ + + if ($activity->getType() === Create::TYPE) { + $activity->checkOrigin($item->getAttributedTo()); + $this->save($item); + } + } + + + /** + * @param ACore $item + */ + public function delete(ACore $item) { + /** @var Note $item */ + $this->notesRequest->deleteNoteById($item->getId()); + } + + +} + diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index c0b8ccf3..7c86a9c7 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -38,7 +38,7 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceEntryException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Object\Document; -use OCA\Social\Service\ActivityPub\ICoreService; +use OCA\Social\Interfaces\IActivityPubInterface; abstract class ACore extends Item implements JsonSerializable { @@ -72,7 +72,7 @@ abstract class ACore extends Item implements JsonSerializable { /** @var Document */ private $icon = null; - /** @var ICoreService */ + /** @var IActivityPubInterface */ private $saveAs; /** @@ -168,20 +168,20 @@ abstract class ACore extends Item implements JsonSerializable { } - /** - * @param ICoreService $class - */ - public function saveAs(ICoreService $class) { - $this->saveAs = $class; - } - - /** - * @return ICoreService - */ - public function savingAs() { - return $this->saveAs; - } - +// /** +// * @param ICoreService $class +// */ +// public function saveAs(ICoreService $class) { +// $this->saveAs = $class; +// } +// +// /** +// * @return ICoreService +// */ +// public function savingAs() { +// return $this->saveAs; +// } +// /** * @param string $base diff --git a/lib/Service/ActorService.php b/lib/Service/AccountService.php similarity index 90% rename from lib/Service/ActorService.php rename to lib/Service/AccountService.php index cb088fab..e505c35f 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/AccountService.php @@ -42,8 +42,8 @@ use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Actor\Person; -use OCA\Social\Service\ActivityPub\Object\DocumentService; -use OCA\Social\Service\ActivityPub\Actor\PersonService; +use OCA\Social\Interfaces\Object\DocumentInterface; +use OCA\Social\Interfaces\Actor\PersonInterface; use OCP\Accounts\IAccountManager; use OCP\IUserManager; @@ -74,10 +74,13 @@ class ActorService { /** @var NotesRequest */ private $notesRequest; - /** @var PersonService */ + /** @var PersonInterface */ private $personService; - /** @var DocumentService */ + /** @var SignatureService */ + private $signatureService; + + /** @var DocumentInterface */ private $documentService; /** @var ConfigService */ @@ -95,15 +98,16 @@ class ActorService { * @param ActorsRequest $actorsRequest * @param FollowsRequest $followsRequest * @param NotesRequest $notesRequest - * @param PersonService $personService - * @param DocumentService $documentService + * @param PersonInterface $personService + * @param DocumentInterface $documentService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( IUserManager $userManager, IAccountManager $accountManager, ActorsRequest $actorsRequest, - FollowsRequest $followsRequest, NotesRequest $notesRequest, PersonService $personService, - DocumentService $documentService, ConfigService $configService, MiscService $miscService + FollowsRequest $followsRequest, NotesRequest $notesRequest, PersonInterface $personService, + DocumentInterface $documentService, SignatureService $signatureService, + ConfigService $configService, MiscService $miscService ) { $this->userManager = $userManager; $this->accountManager = $accountManager; @@ -112,6 +116,7 @@ class ActorService { $this->notesRequest = $notesRequest; $this->personService = $personService; $this->documentService = $documentService; + $this->signatureService = $signatureService; $this->configService = $configService; $this->miscService = $miscService; } @@ -215,7 +220,7 @@ class ActorService { $actor->setUserId($userId); $actor->setPreferredUsername($username); - $this->generateKeys($actor); + $this->signatureService->generateKeys($actor); $this->actorsRequest->create($actor); // generate cache. @@ -321,26 +326,6 @@ class ActorService { } - /** - * @param Person $actor - */ - private function generateKeys(Person &$actor) { - $res = openssl_pkey_new( - [ - "digest_alg" => "rsa", - "private_key_bits" => 2048, - "private_key_type" => OPENSSL_KEYTYPE_RSA, - ] - ); - - openssl_pkey_export($res, $privateKey); - $publicKey = openssl_pkey_get_details($res)['key']; - - $actor->setPublicKey($publicKey); - $actor->setPrivateKey($privateKey); - } - - /** * @throws Exception * @return int diff --git a/lib/Service/ActivityPub/Activity/CreateService.php b/lib/Service/ActivityPub/Activity/CreateService.php deleted file mode 100644 index fe874dd8..00000000 --- a/lib/Service/ActivityPub/Activity/CreateService.php +++ /dev/null @@ -1,104 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Service\ActivityPub\Activity; - - -use OCA\Social\Exceptions\UnknownItemException; -use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ImportService; -use OCA\Social\Service\MiscService; - - -class CreateService implements ICoreService { - - - /** @var MiscService */ - private $miscService; - - - /** - * CreateService constructor. - * - * @param MiscService $miscService - */ - public function __construct(MiscService $miscService) { - $this->miscService = $miscService; - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processIncomingRequest(ACore $item, ImportService $importService) { - if (!$item->gotObject()) { - return; - } - $object = $item->getObject(); - - try { - $service = $importService->getServiceForItem($item->getObject()); - $service->activity($item, $object); - } catch (UnknownItemException $e) { - } - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processResult(ACore $item, ImportService $importService) { - } - - - /** - * @param ACore $item - */ - public function save(ACore $item) { - } - - - /** - * @param ACore $item - */ - public function delete(ACore $item) { - } - - /** - * @param ACore $activity - * @param ACore $item - */ - public function activity(ACore $activity, ACore $item) { - } -} - diff --git a/lib/Service/ActivityPub/Activity/DeleteService.php b/lib/Service/ActivityPub/Activity/DeleteService.php deleted file mode 100644 index fb789d03..00000000 --- a/lib/Service/ActivityPub/Activity/DeleteService.php +++ /dev/null @@ -1,132 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Service\ActivityPub\Activity; - - -use OCA\Social\Exceptions\InvalidOriginException; -use OCA\Social\Exceptions\InvalidResourceException; -use OCA\Social\Exceptions\UnknownItemException; -use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Activity\Delete; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ActivityService; -use OCA\Social\Service\ImportService; -use OCA\Social\Service\MiscService; - - -class DeleteService implements ICoreService { - - - /** @var ActivityService */ - private $activityService; - - - /** @var MiscService */ - private $miscService; - - - /** - * UndoService constructor. - * - * @param ActivityService $activityService - * @param MiscService $miscService - */ - public function __construct(ActivityService $activityService, MiscService $miscService) { - $this->activityService = $activityService; - $this->miscService = $miscService; - } - - - /** - * @param ACore $delete - * @param ImportService $importService - * - * @throws InvalidOriginException - */ - public function processIncomingRequest(ACore $delete, ImportService $importService) { - - if ($delete->gotObject()) { - $id = $delete->getObject() - ->getId(); - } else { - $id = $delete->getObjectId(); - } - - $delete->checkOrigin($id); - - /** @var Delete $delete */ - try { - $item = $this->activityService->getItem($id); - $service = $importService->getServiceForItem($item); - - // we could use ->activity($delete, $item) but the delete() is important enough to - // be here, and to use it. - $service->delete($item); - } catch (UnknownItemException $e) { - } catch (InvalidResourceException $e) { - } - } - - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processResult(ACore $item, ImportService $importService) { - } - - - - /** - * @param ACore $activity - * @param ACore $item - */ - public function activity(Acore $activity, ACore $item) { - } - - - /** - * @param ACore $item - */ - public function delete(ACore $item) { - } - - - /** - * @param ACore $item - */ - public function save(ACore $item) { - } - - -} - diff --git a/lib/Service/ActivityPub/Actor/PersonService.php b/lib/Service/ActivityPub/Actor/PersonService.php deleted file mode 100644 index 914bedb1..00000000 --- a/lib/Service/ActivityPub/Actor/PersonService.php +++ /dev/null @@ -1,357 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Service\ActivityPub\Actor; - - -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Traits\TArrayTools; -use Exception; -use OCA\Social\Db\CacheActorsRequest; -use OCA\Social\Db\CacheDocumentsRequest; -use OCA\Social\Exceptions\CacheActorDoesNotExistException; -use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; -use OCA\Social\Exceptions\InvalidResourceEntryException; -use OCA\Social\Exceptions\InvalidResourceException; -use OCA\Social\Exceptions\Request410Exception; -use OCA\Social\Exceptions\RequestException; -use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UrlCloudException; -use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Activity\Update; -use OCA\Social\Model\ActivityPub\Actor\Person; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ConfigService; -use OCA\Social\Service\ImportService; -use OCA\Social\Service\InstanceService; -use OCA\Social\Service\MiscService; - - -/** - * Class PersonService - * - * @package OCA\Social\Service\ActivityPub - */ -class PersonService implements ICoreService { - - - use TArrayTools; - - - /** @var CacheActorsRequest */ - private $cacheActorsRequest; - - /** @var CacheDocumentsRequest */ - private $cacheDocumentsRequest; - - /** @var InstanceService */ - private $instanceService; - - /** @var ConfigService */ - private $configService; - - /** @var MiscService */ - private $miscService; - - - /** @var string */ - private $viewerId = ''; - - - /** - * UndoService constructor. - * - * @param CacheActorsRequest $cacheActorsRequest - * @param CacheDocumentsRequest $cacheDocumentsRequest - * @param InstanceService $instanceService - * @param ConfigService $configService - * @param MiscService $miscService - */ - public function __construct( - CacheActorsRequest $cacheActorsRequest, CacheDocumentsRequest $cacheDocumentsRequest, - InstanceService $instanceService, ConfigService $configService, MiscService $miscService - ) { - $this->cacheActorsRequest = $cacheActorsRequest; - $this->cacheDocumentsRequest = $cacheDocumentsRequest; - $this->instanceService = $instanceService; - $this->configService = $configService; - $this->miscService = $miscService; - } - - - /** - * @param string $viewerId - */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->cacheActorsRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; - } - - - /** - * @param Person $actor - * @param bool $refresh - */ - public function cacheLocalActor(Person $actor, bool $refresh = false) { - if ($refresh) { - $this->cacheActorsRequest->deleteFromId($actor->getId()); - } - - $actor->setLocal(true); - $actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES)); - - $this->save($actor); - } - - - /** - * @param string $id - * - * @param bool $refresh - * - * @return Person - * @throws InvalidResourceException - * @throws RequestException - * @throws SocialAppConfigException - * @throws UrlCloudException - * @throws Request410Exception - * @throws MalformedArrayException - */ - public function getFromId(string $id, bool $refresh = false): Person { - - $posAnchor = strpos($id, '#'); - if ($posAnchor !== false) { - $id = substr($id, 0, $posAnchor); - } - - try { - if ($refresh) { - $this->cacheActorsRequest->deleteFromId($id); - throw new CacheActorDoesNotExistException(); - } - - $actor = $this->cacheActorsRequest->getFromId($id); - } catch (CacheActorDoesNotExistException $e) { - $object = $this->instanceService->retrieveObject($id); - $actor = $this->generateActorFromObject($object); - $actor->setAccount($actor->getPreferredUsername() . '@' . $this->get('_host', $object)); - try { - $this->save($actor); - } catch (Exception $e) { - throw new InvalidResourceException($e->getMessage()); - } - } - - return $actor; - } - - - /** - * @param string $account - * - * @param bool $retrieve - * - * @return Person - * @throws InvalidResourceException - * @throws RequestException - * @throws CacheActorDoesNotExistException - * @throws SocialAppConfigException - * @throws UrlCloudException - * @throws Request410Exception - * @throws MalformedArrayException - */ - public function getFromAccount(string $account, bool $retrieve = true): Person { - - try { - $actor = $this->cacheActorsRequest->getFromAccount($account); - } catch (CacheActorDoesNotExistException $e) { - if (!$retrieve) { - throw new CacheActorDoesNotExistException(); - } - - $object = $this->instanceService->retrieveAccount($account); - $actor = $this->generateActorFromObject($object); - $actor->setAccount($account); - try { - $this->save($actor); - } catch (Exception $e) { - throw new InvalidResourceException($e->getMessage()); - } - } - - return $actor; - } - - - /** - * @param string $account - * - * @return Person - * @throws CacheActorDoesNotExistException - */ - public function getFromLocalAccount(string $account): Person { - return $this->cacheActorsRequest->getFromLocalAccount($account); - } - - - /** - * @param array $object - * - * @return Person - * @throws InvalidResourceException - * @throws SocialAppConfigException - * @throws UrlCloudException - * @throws InvalidResourceEntryException - */ - private function generateActorFromObject(array $object) { - - $actor = new Person(); - $actor->setUrlCloud($this->configService->getCloudAddress()); - $actor->import($object); - - if ($actor->getType() !== Person::TYPE) { - throw new InvalidResourceException(); - } - - $actor->setSource(json_encode($object, JSON_UNESCAPED_SLASHES)); - - return $actor; - } - - /** - * @param string $search - * - * @return Person[] - */ - public function searchCachedAccounts(string $search): array { - return $this->cacheActorsRequest->searchAccounts($search); - } - - - /** - * @param ACore $person - * @param ImportService $importService - */ - public function processIncomingRequest(ACore $person, ImportService $importService) { - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processResult(ACore $item, ImportService $importService) { - } - - - - - /** - * @param ACore $person - */ - public function save(ACore $person) { - /** @var Person $person */ - if ($person->gotIcon()) { - try { - $icon = $this->cacheDocumentsRequest->getBySource( - $person->getIcon() - ->getUrl() - ); - $person->setIcon($icon); - } catch (CacheDocumentDoesNotExistException $e) { - $this->cacheDocumentsRequest->save($person->getIcon()); - } - } - - $this->cacheActorsRequest->save($person); - } - - - /** - * @param ACore $activity - * @param ACore $item - */ - public function activity(Acore $activity, ACore $item) { - /** @var Person $item */ - if ($activity->getType() === Update::TYPE) { - $this->miscService->log('### UPDATE PERSON !' . json_encode($item)); - } - } - - - /** - * @throws Exception - * @return int - */ - public function missingCacheRemoteActors(): int { - // TODO - looking for missing cache remote actors... - $missing = []; - - foreach ($missing as $item) { - try { - $this->getFromId($item->getId()); - } catch (Exception $e) { - } - } - - return sizeof($missing); - } - - - /** - * @throws Exception - * @return int - */ - public function manageCacheRemoteActors(): int { - $update = $this->cacheActorsRequest->getRemoteActorsToUpdate(); - - foreach ($update as $item) { - try { - $this->getFromId($item->getId(), true); - } catch (Exception $e) { - } - } - - return sizeof($update); - } - - - /** - * @param ACore $item - */ - public function delete(ACore $item) { - } - -} - diff --git a/lib/Service/ActivityPub/Object/DocumentService.php b/lib/Service/ActivityPub/Object/DocumentService.php deleted file mode 100644 index b8a576ff..00000000 --- a/lib/Service/ActivityPub/Object/DocumentService.php +++ /dev/null @@ -1,278 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Service\ActivityPub\Object; - - -use Exception; -use OCA\Social\Db\ActorsRequest; -use OCA\Social\Db\CacheDocumentsRequest; -use OCA\Social\Exceptions\CacheContentException; -use OCA\Social\Exceptions\CacheContentMimeTypeException; -use OCA\Social\Exceptions\CacheContentSizeException; -use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; -use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UrlCloudException; -use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Object\Document; -use OCA\Social\Model\ActivityPub\Object\Image; -use OCA\Social\Model\ActivityPub\Actor\Person; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\CacheService; -use OCA\Social\Service\ConfigService; -use OCA\Social\Service\ImportService; -use OCA\Social\Service\MiscService; -use OCP\Files\NotPermittedException; -use OCP\Files\SimpleFS\ISimpleFile; -use OCP\IURLGenerator; - - -class DocumentService implements ICoreService { - - - const ERROR_SIZE = 1; - const ERROR_MIMETYPE = 2; - - - /** @var IURLGenerator */ - private $urlGenerator; - - /** @var CacheDocumentsRequest */ - private $cacheDocumentsRequest; - - /** @var ActorsRequest */ - private $actorRequest; - - /** @var CacheService */ - private $cacheService; - - /** @var ConfigService */ - private $configService; - - /** @var MiscService */ - private $miscService; - - - /** - * DocumentService constructor. - * - * @param IUrlGenerator $urlGenerator - * @param CacheDocumentsRequest $cacheDocumentsRequest - * @param ActorsRequest $actorRequest - * @param CacheService $cacheService - * @param ConfigService $configService - * @param MiscService $miscService - */ - public function __construct( - IUrlGenerator $urlGenerator, CacheDocumentsRequest $cacheDocumentsRequest, - ActorsRequest $actorRequest, - CacheService $cacheService, - ConfigService $configService, MiscService $miscService - ) { - $this->urlGenerator = $urlGenerator; - $this->cacheDocumentsRequest = $cacheDocumentsRequest; - $this->actorRequest = $actorRequest; - $this->configService = $configService; - $this->cacheService = $cacheService; - $this->miscService = $miscService; - } - - - /** - * @param string $id - * @param bool $public - * - * @return Document - * @throws CacheDocumentDoesNotExistException - * @throws NotPermittedException - */ - public function cacheRemoteDocument(string $id, bool $public = false) { - $document = $this->cacheDocumentsRequest->getById($id, $public); - if ($document->getError() > 0) { - throw new CacheDocumentDoesNotExistException(); - } - - if ($document->getLocalCopy() !== '') { - return $document; - } - - if ($document->getCaching() > (time() - (CacheDocumentsRequest::CACHING_TIMEOUT * 60))) { - return $document; - } - - $mime = ''; - $this->cacheDocumentsRequest->initCaching($document); - - try { - $localCopy = $this->cacheService->saveRemoteFileToCache($document->getUrl(), $mime); - $document->setMimeType($mime); - $document->setLocalCopy($localCopy); - $this->cacheDocumentsRequest->endCaching($document); - - return $document; - } catch (CacheContentMimeTypeException $e) { - $document->setMimeType($mime); - $document->setError(self::ERROR_MIMETYPE); - $this->cacheDocumentsRequest->endCaching($document); - } catch (CacheContentSizeException $e) { - $document->setError(self::ERROR_SIZE); - $this->cacheDocumentsRequest->endCaching($document); - } catch (CacheContentException $e) { - } - - throw new CacheDocumentDoesNotExistException(); - } - - - /** - * @param string $id - * - * @param bool $public - * - * @return ISimpleFile - * @throws CacheContentException - * @throws CacheDocumentDoesNotExistException - * @throws NotPermittedException - */ - public function getFromCache(string $id, bool $public = false) { - $document = $this->cacheRemoteDocument($id, $public); - - return $this->cacheService->getContentFromCache($document->getLocalCopy()); - } - - - /** - * @return int - * @throws Exception - */ - public function manageCacheDocuments(): int { - $update = $this->cacheDocumentsRequest->getNotCachedDocuments(); - - $count = 0; - foreach ($update as $item) { - if ($item->getLocalCopy() === 'avatar') { - continue; - } - - try { - $this->cacheRemoteDocument($item->getId()); - } catch (Exception $e) { - continue; - } - $count++; - } - - return $count; - } - - - /** - * @param Person $actor - * - * @return string - * @throws SocialAppConfigException - * @throws UrlCloudException - */ - public function cacheLocalAvatarByUsername(Person $actor): string { - $url = $this->urlGenerator->linkToRouteAbsolute( - 'core.avatar.getAvatar', ['userId' => $actor->getUserId(), 'size' => 128] - ); - - $versionCurrent = - (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()); - $icon->generateUniqueId('/documents/avatar'); - $icon->setMediaType(''); - $icon->setLocalCopy('avatar'); - - $this->cacheDocumentsRequest->deleteByUrl($icon->getUrl()); - $this->cacheDocumentsRequest->save($icon); - - $actor->setAvatarVersion($versionCurrent); - $this->actorRequest->update($actor); - } else { - try { - $icon = $this->cacheDocumentsRequest->getBySource($url); - } catch (CacheDocumentDoesNotExistException $e) { - return ''; - } - } - - return $icon->getId(); - } - - - /** - * @param ACore $activity - * @param ACore $item - */ - public function activity(Acore $activity, ACore $item) { - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processIncomingRequest(ACore $item, ImportService $importService) { - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processResult(ACore $item, ImportService $importService) { - } - - - /** - * @param ACore $item - */ - public function save(ACore $item) { - /** @var Document $item */ - $this->cacheDocumentsRequest->save($item); - } - - - /** - * @param ACore $item - */ - public function delete(ACore $item) { -// $this->cacheDocumentsRequest->delete($item); - } - - -} - diff --git a/lib/Service/ActivityPub/Object/NoteService.php b/lib/Service/ActivityPub/Object/NoteService.php deleted file mode 100644 index a8b9a968..00000000 --- a/lib/Service/ActivityPub/Object/NoteService.php +++ /dev/null @@ -1,431 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Service\ActivityPub\Object; - - -use Exception; -use OC\User\NoUserException; -use OCA\Social\Db\NotesRequest; -use OCA\Social\Exceptions\ActorDoesNotExistException; -use OCA\Social\Exceptions\InvalidOriginException; -use OCA\Social\Exceptions\NoteNotFoundException; -use OCA\Social\Exceptions\RequestException; -use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Activity\Create; -use OCA\Social\Model\ActivityPub\Object\Note; -use OCA\Social\Model\ActivityPub\Actor\Person; -use OCA\Social\Model\InstancePath; -use OCA\Social\Service\ActivityPub\Actor\PersonService; -use OCA\Social\Service\ActivityPub\ICoreService; -use OCA\Social\Service\ActivityService; -use OCA\Social\Service\ActorService; -use OCA\Social\Service\ConfigService; -use OCA\Social\Service\CurlService; -use OCA\Social\Service\ImportService; -use OCA\Social\Service\MiscService; - - -class NoteService implements ICoreService { - - - const TYPE_PUBLIC = 'public'; - const TYPE_UNLISTED = 'unlisted'; - const TYPE_FOLLOWERS = 'followers'; - const TYPE_DIRECT = 'direct'; - - - /** @var NotesRequest */ - private $notesRequest; - - /** @var ActivityService */ - private $activityService; - - /** @var ActorService */ - private $actorService; - - /** @var PersonService */ - private $personService; - - /** @var CurlService */ - private $curlService; - - /** @var ConfigService */ - private $configService; - - /** @var MiscService */ - private $miscService; - - - /** @var string */ - private $viewerId = ''; - - - /** - * NoteService constructor. - * - * @param NotesRequest $notesRequest - * @param ActivityService $activityService - * @param ActorService $actorService - * @param PersonService $personService - * @param CurlService $curlService - * @param ConfigService $configService - * @param MiscService $miscService - */ - public function __construct( - NotesRequest $notesRequest, ActivityService $activityService, ActorService $actorService, - PersonService $personService, CurlService $curlService, ConfigService $configService, - MiscService $miscService - ) { - $this->notesRequest = $notesRequest; - $this->activityService = $activityService; - $this->actorService = $actorService; - $this->personService = $personService; - $this->curlService = $curlService; - $this->configService = $configService; - $this->miscService = $miscService; - } - - - /** - * @param string $viewerId - */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->notesRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; - } - - - /** - * @param string $userId - * @param string $content - * - * @param string $type - * - * @return Note - * @throws ActorDoesNotExistException - * @throws NoUserException - * @throws SocialAppConfigException - */ - public function generateNote(string $userId, string $content, string $type) { - $note = new Note(); - $actor = $this->actorService->getActorFromUserId($userId); - - $note->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); - $note->setPublished(date("c")); - $note->setAttributedTo( - $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername() - ); - - $this->setRecipient($note, $actor, $type); - $note->setContent($content); - $note->convertPublished(); - $note->setLocal(true); - - $note->saveAs($this); - - return $note; - } - - - /** - * @param Note $note - * @param Person $actor - * @param string $type - */ - private function setRecipient(Note $note, Person $actor, string $type) { - switch ($type) { - case self::TYPE_UNLISTED: - $note->setTo($actor->getFollowers()); - $note->addInstancePath( - new InstancePath( - $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, - InstancePath::PRIORITY_LOW - ) - ); - $note->addCc(ActivityService::TO_PUBLIC); - break; - - case self::TYPE_FOLLOWERS: - $note->setTo($actor->getFollowers()); - $note->addInstancePath( - new InstancePath( - $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, - InstancePath::PRIORITY_LOW - ) - ); - break; - - case self::TYPE_DIRECT: - break; - - default: - $note->setTo(ActivityService::TO_PUBLIC); - $note->addCc($actor->getFollowers()); - $note->addInstancePath( - new InstancePath( - $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, - InstancePath::PRIORITY_LOW - ) - ); - break; - } - } - - - /** - * @param Note $note - * @param string $type - * @param string $account - */ - public function addRecipient(Note $note, string $type, string $account) { - if ($account === '') { - return; - } - - try { - $actor = $this->personService->getFromAccount($account); - } catch (Exception $e) { - return; - } - - $instancePath = new InstancePath( - $actor->getInbox(), InstancePath::TYPE_INBOX, InstancePath::PRIORITY_MEDIUM - ); - if ($type === self::TYPE_DIRECT) { - $instancePath->setPriority(InstancePath::PRIORITY_HIGH); - $note->addToArray($actor->getId()); - } else { - $note->addCc($actor->getId()); - } - - $note->addTag( - [ - 'type' => 'Mention', - 'href' => $actor->getId() - ] - ); - - $note->addInstancePath($instancePath); - } - - - /** - * @param Note $note - * @param string $type - * @param array $accounts - */ - public function addRecipients(Note $note, string $type, array $accounts) { - if ($accounts === []) { - return; - } - - foreach ($accounts as $account) { - $this->addRecipient($note, $type, $account); - } - } - - - /** - * @param Note $note - * @param string $replyTo - */ - public function replyTo(Note $note, string $replyTo) { - if ($replyTo === '') { - return; - } - - $note->setInReplyTo($replyTo); - // TODO - type can be NOT public ! - $note->addInstancePath( - new InstancePath($replyTo, InstancePath::TYPE_PUBLIC, InstancePath::PRIORITY_HIGH) - ); - } - - - /** - * @param ACore $note - * @param ImportService $importService - */ - public function processIncomingRequest(ACore $note, ImportService $importService) { - } - - - /** - * @param ACore $item - * @param ImportService $importService - */ - public function processResult(ACore $item, ImportService $importService) { - } - - - /** - * @param ACore $note - */ - public function save(ACore $note) { - /** @var Note $note */ - - try { - $this->notesRequest->getNoteById($note->getId()); - } catch (NoteNotFoundException $e) { - $this->notesRequest->save($note); - } - } - - - /** - * @param ACore $activity - * @param ACore $item - * - * @throws InvalidOriginException - */ - public function activity(Acore $activity, ACore $item) { - /** @var Note $item */ - - if ($activity->getType() === Create::TYPE) { - $activity->checkOrigin($item->getAttributedTo()); - $this->save($item); - } - } - - - /** - * @param Note $note - * - * @throws ActorDoesNotExistException - * @throws RequestException - * @throws SocialAppConfigException - */ - public function deleteLocalNote(Note $note) { - if (!$note->isLocal()) { - return; - } - - $note->setActorId($note->getAttributedTo()); - $this->activityService->deleteActivity($note); - } - - - /** - * @param ACore $item - */ - public function delete(ACore $item) { - /** @var Note $item */ - $this->notesRequest->deleteNoteById($item->getId()); - } - - - /** - * @param string $id - * - * @return Note - * @throws NoteNotFoundException - */ - public function getNoteById(string $id): Note { - return $this->notesRequest->getNoteById($id); - } - - - /** - * @param Person $actor - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamHome(Person $actor, int $since = 0, int $limit = 5): array { - return $this->notesRequest->getStreamHome($actor, $since, $limit); - } - - - /** - * @param string $actorId - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamAccount(string $actorId, int $since = 0, int $limit = 5): array { - return $this->notesRequest->getStreamAccount($actorId, $since, $limit); - } - - - /** - * @param Person $actor - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamDirect(Person $actor, int $since = 0, int $limit = 5): array { - return $this->notesRequest->getStreamDirect($actor, $since, $limit); - } - - - /** - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamLocalTimeline(int $since = 0, int $limit = 5): array { - return $this->notesRequest->getStreamTimeline($since, $limit, true); - } - - - /** - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamInternalTimeline(int $since = 0, int $limit = 5): array { - // TODO - admin should be able to provide a list of 'friendly/internal' instance of ActivityPub - return []; - } - - - /** - * @param int $since - * @param int $limit - * - * @return Note[] - */ - public function getStreamGlobalTimeline(int $since = 0, int $limit = 5): array { - return $this->notesRequest->getStreamTimeline($since, $limit, false); - } - -} - diff --git a/lib/Service/CacheService.php b/lib/Service/CacheDocumentService.php similarity index 100% rename from lib/Service/CacheService.php rename to lib/Service/CacheDocumentService.php