diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-19 09:37:12 -0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-19 09:37:12 -0100 |
commit | 596669104e13fd1057120efe95ebbfe460e697ae (patch) | |
tree | a5aaab4ab78ebdf5c8f85d2e0d8de1d49a0bfbc4 /lib/Service/ActivityPub/FollowService.php | |
parent | 31867cfbb74123ce59f9eb0b474d03f93dd8d296 (diff) | |
parent | de9559ab87aac00615976035bd21b3950b906e04 (diff) |
Merge pull request #35 from nextcloud-gmbh/follow-unfollow
Following accounts
Diffstat (limited to 'lib/Service/ActivityPub/FollowService.php')
-rw-r--r-- | lib/Service/ActivityPub/FollowService.php | 118 |
1 files changed, 114 insertions, 4 deletions
diff --git a/lib/Service/ActivityPub/FollowService.php b/lib/Service/ActivityPub/FollowService.php index 644ea74b..347e9104 100644 --- a/lib/Service/ActivityPub/FollowService.php +++ b/lib/Service/ActivityPub/FollowService.php @@ -33,10 +33,19 @@ namespace OCA\Social\Service\ActivityPub; use Exception; use OCA\Social\Db\FollowsRequest; +use OCA\Social\Exceptions\FollowDoesNotExistException; +use OCA\Social\Exceptions\RequestException; +use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Person; +use OCA\Social\Model\ActivityPub\Activity\Accept; +use OCA\Social\Model\ActivityPub\Activity\Reject; +use OCA\Social\Model\ActivityPub\Activity\Undo; use OCA\Social\Model\ActivityPub\Follow; use OCA\Social\Model\ActivityPub\OrderedCollection; +use OCA\Social\Model\ActivityPub\Person; +use OCA\Social\Model\InstancePath; +use OCA\Social\Service\ActivityService; +use OCA\Social\Service\ConfigService; use OCA\Social\Service\ICoreService; use OCA\Social\Service\MiscService; @@ -47,6 +56,15 @@ class FollowService implements ICoreService { /** @var FollowsRequest */ private $followsRequest; + /** @var PersonService */ + private $personService; + + /** @var ActivityService */ + private $activityService; + + /** @var ConfigService */ + private $configService; + /** @var MiscService */ private $miscService; @@ -55,16 +73,68 @@ class FollowService implements ICoreService { * NoteService constructor. * * @param FollowsRequest $followsRequest + * @param PersonService $personService + * @param ActivityService $activityService + * @param ConfigService $configService * @param MiscService $miscService */ - public function __construct(FollowsRequest $followsRequest, MiscService $miscService) { + public function __construct( + FollowsRequest $followsRequest, PersonService $personService, + ActivityService $activityService, ConfigService $configService, + MiscService $miscService + ) { $this->followsRequest = $followsRequest; + $this->personService = $personService; + $this->activityService = $activityService; + $this->configService = $configService; $this->miscService = $miscService; } /** * @param Person $actor + * @param string $account + * + * @throws RequestException + * @throws SocialAppConfigException + */ + public function followAccount(Person $actor, string $account) { + $remoteActor = $this->personService->getFromAccount($account); + $follow = new Follow(); + $follow->generateUniqueId($this->configService->getCloudAddress()); + $follow->setActorId($actor->getId()); + $follow->setObjectId($remoteActor->getId()); + + try { + $this->followsRequest->getByPersons($actor, $remoteActor); + } catch (FollowDoesNotExistException $e) { + $this->followsRequest->save($follow); + + $follow->addInstancePath(new InstancePath($remoteActor->getInbox())); + $this->activityService->manageRequest($follow, ActivityService::REQUEST_INBOX); + } + } + + + /** + * @param Person $actor + * @param string $account + * + * @throws RequestException + */ + public function unfollowAccount(Person $actor, string $account) { + $remoteActor = $this->personService->getFromAccount($account); + + try { + $follow = $this->followsRequest->getByPersons($actor, $remoteActor); + $this->followsRequest->delete($follow); + } catch (FollowDoesNotExistException $e) { + } + } + + + /** + * @param Person $actor * * @return OrderedCollection */ @@ -79,6 +149,29 @@ class FollowService implements ICoreService { /** + * @param Follow $follow + */ + public function confirmFollowRequest(Follow $follow) { + try { + $remoteActor = $this->personService->getFromId($follow->getActorId()); + + $accept = new Accept(); + $accept->setId($follow->getObjectId() . '#accepts/follows/1234'); + $accept->setActorId($follow->getObjectId()); + $accept->setObject($follow); + + $accept->addInstancePath(new InstancePath($remoteActor->getInbox())); + + $follow->setParent($accept); + + $this->activityService->manageRequest($accept, ActivityService::REQUEST_INBOX); + $this->followsRequest->accepted($follow); + } catch (Exception $e) { + } + } + + + /** * This method is called when saving the Follow object * * @param ACore $follow @@ -88,10 +181,27 @@ class FollowService implements ICoreService { public function save(ACore $follow) { /** @var Follow $follow */ - if ($follow->getMetaBool('Undo') === false) { + if ($follow->isRoot()) { $this->followsRequest->save($follow); + $this->confirmFollowRequest($follow); } else { - $this->followsRequest->delete($follow); + $parent = $follow->getParent(); + if ($parent->isRoot() === false) { + return; + } + + if ($parent->getType() === Undo::TYPE && $parent->verify($follow->getActorId())) { + $this->followsRequest->delete($follow); + } + + if ($parent->getType() === Reject::TYPE && $parent->verify($follow->getObjectId())) { + $this->followsRequest->delete($follow); + } + + if ($parent->getType() === Accept::TYPE && $parent->verify($follow->getObjectId())) { + $this->followsRequest->accepted($follow); + } + } } |