diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-04-08 15:52:03 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2019-05-03 16:31:15 -0100 |
commit | b4d773940c9d6c601a2dc9a2ba39c0f6348e9545 (patch) | |
tree | 4b0ae964c51e8e8c8fc2f5411a180c177bc71858 /lib/Service | |
parent | 36f7b7290bfab599610b779d83b12806fddacfce (diff) |
Returns if a post is boosted, and allow unboost
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/BoostService.php | 192 | ||||
-rw-r--r-- | lib/Service/NoteService.php | 58 | ||||
-rw-r--r-- | lib/Service/PostService.php | 18 | ||||
-rw-r--r-- | lib/Service/StreamActionService.php | 117 |
4 files changed, 338 insertions, 47 deletions
diff --git a/lib/Service/BoostService.php b/lib/Service/BoostService.php new file mode 100644 index 00000000..b0e22aaa --- /dev/null +++ b/lib/Service/BoostService.php @@ -0,0 +1,192 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.com> + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Service; + + +use Exception; +use OCA\Social\Db\NotesRequest; +use OCA\Social\Exceptions\NoteNotFoundException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Undo; +use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Model\ActivityPub\Object\Announce; +use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Stream; + +/** + * Class BoostService + * + * @package OCA\Social\Service + */ +class BoostService { + + + /** @var NotesRequest */ + private $notesRequest; + + /** @var NoteService */ + private $noteService; + + /** @var SignatureService */ + private $signatureService; + + /** @var ActivityService */ + private $activityService; + + /** @var StreamActionService */ + private $streamActionService; + + /** @var StreamQueueService */ + private $streamQueueService; + + /** @var MiscService */ + private $miscService; + + + /** + * BoostService constructor. + * + * @param NotesRequest $notesRequest + * @param NoteService $noteService + * @param SignatureService $signatureService + * @param ActivityService $activityService + * @param StreamActionService $streamActionService + * @param StreamQueueService $streamQueueService + * @param MiscService $miscService + */ + public function __construct( + NotesRequest $notesRequest, NoteService $noteService, SignatureService $signatureService, + ActivityService $activityService, StreamActionService $streamActionService, + StreamQueueService $streamQueueService, MiscService $miscService + ) { + $this->notesRequest = $notesRequest; + $this->noteService = $noteService; + $this->signatureService = $signatureService; + $this->activityService = $activityService; + $this->streamActionService = $streamActionService; + $this->streamQueueService = $streamQueueService; + $this->miscService = $miscService; + } + + + /** + * @param Person $actor + * @param string $postId + * @param string $token + * + * @return ACore + * @throws NoteNotFoundException + * @throws SocialAppConfigException + */ + public function create(Person $actor, string $postId, string &$token = ''): ACore { + + try { + return $this->get($actor, $postId); + } catch (NoteNotFoundException $e) { + } + + $announce = new Announce(); + $this->noteService->assignItem($announce, $actor, Stream::TYPE_PUBLIC); + $announce->setActor($actor); + + $note = $this->noteService->getNoteById($postId, true); + if ($note->getType() !== Note::TYPE) { + throw new NoteNotFoundException('Stream is not a Note'); + } + + $announce->addCc($note->getAttributedTo()); + if ($note->isLocal()) { + $announce->setObject($note); + } else { + $announce->setObjectId($note->getId()); + $announce->addCacheItem($note->getId()); + } + + $this->notesRequest->save($announce); + + $this->streamActionService->setActionBool($actor->getActorId(), $postId, 'boosted', true); + $this->signatureService->signObject($actor, $announce); + $token = $this->activityService->request($announce); + + $this->streamQueueService->cacheStreamByToken($token); + + return $announce; + } + + + /** + * @param Person $actor + * @param string $postId + * + * @return Stream + * @throws NoteNotFoundException + */ + public function get(Person $actor, string $postId): Stream { + $stream = $this->notesRequest->getNoteByObjectId($actor, Announce::TYPE, $postId); + + return $stream; + } + + + /** + * @param Person $actor + * @param string $postId + * @param ACore $undo + * + * @return string + * @throws SocialAppConfigException + * @throws NoteNotFoundException + */ + public function delete(Person $actor, string $postId, ACore &$undo = null): string { + $undo = new Undo(); + $this->noteService->assignItem($undo, $actor, Stream::TYPE_PUBLIC); + $undo->setActor($actor); + + $note = $this->noteService->getNoteById($postId, true); + if ($note->getType() !== Note::TYPE) { + throw new NoteNotFoundException('Stream is not a Note'); + } + + $announce = $this->notesRequest->getNoteByObjectId($actor, Announce::TYPE, $postId); + $undo->setObject($announce); + $undo->setCcArray($announce->getCcArray()); + + $this->notesRequest->deleteNoteById($announce->getId()); + $this->streamActionService->setActionBool($actor->getActorId(), $postId, 'boosted', false); + $this->signatureService->signObject($actor, $undo); + $token = $this->activityService->request($undo); + + return $token; + } + + +} + diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 5788bebc..5b9d8069 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -46,7 +46,6 @@ use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; -use OCA\Social\Model\ActivityPub\Object\Announce; use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Model\InstancePath; @@ -122,66 +121,42 @@ class NoteService { /** + * @param ACore $stream * @param Person $actor - * @param string $postId - * @param ACore|null $announce + * @param string $type * - * @return string - * @throws NoteNotFoundException * @throws SocialAppConfigException * @throws Exception */ - public function createBoost(Person $actor, string $postId, ACore &$announce = null): string { + public function assignItem(Acore &$stream, Person $actor, string $type) { + $stream->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); + $stream->setPublished(date("c")); - $announce = new Announce(); - $this->assignStream($announce, $actor, Stream::TYPE_PUBLIC); - $announce->setActor($actor); + $this->setRecipient($stream, $actor, $type); + $stream->setLocal(true); - $note = $this->getNoteById($postId, true); - if ($note->getType() !== Note::TYPE) { - throw new NoteNotFoundException('Stream is not a Note'); + if ($stream instanceof Stream) { + $this->assignStream($stream); } - - $announce->addCc($note->getAttributedTo()); - if ($note->isLocal()) { - $announce->setObject($note); - } else { - $announce->setObjectId($note->getId()); - $announce->addCacheItem($note->getId()); - } - - $this->signatureService->signObject($actor, $announce); - $token = $this->activityService->request($announce); - - $this->streamQueueService->cacheStreamByToken($token); - - return $token; } /** * @param Stream $stream - * @param Person $actor - * @param string $type * - * @throws SocialAppConfigException + * @throws Exception */ - public function assignStream(Stream &$stream, Person $actor, string $type) { - $stream->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); - $stream->setPublished(date("c")); - - $this->setRecipient($stream, $actor, $type); + public function assignStream(Stream &$stream) { $stream->convertPublished(); - $stream->setLocal(true); } /** - * @param Stream $stream + * @param ACore $stream * @param Person $actor * @param string $type */ - private function setRecipient(Stream $stream, Person $actor, string $type) { + private function setRecipient(ACore $stream, Person $actor, string $type) { switch ($type) { case Note::TYPE_UNLISTED: $stream->setTo($actor->getFollowers()); @@ -370,6 +345,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamHome(Person $actor, int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamHome($actor, $since, $limit); @@ -382,6 +358,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamNotifications(Person $actor, int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamNotifications($actor, $since, $limit); @@ -394,6 +371,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamAccount(string $actorId, int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamAccount($actorId, $since, $limit); @@ -406,6 +384,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamDirect(Person $actor, int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamDirect($actor, $since, $limit); @@ -417,6 +396,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamLocalTimeline(int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamTimeline($since, $limit, true); @@ -430,6 +410,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamLocalTag(Person $actor, string $hashtag, int $since = 0, int $limit = 5 ): array { @@ -455,6 +436,7 @@ class NoteService { * @param int $limit * * @return Note[] + * @throws Exception */ public function getStreamGlobalTimeline(int $since = 0, int $limit = 5): array { return $this->notesRequest->getStreamTimeline($since, $limit, false); diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index e8803402..3e6dba3a 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -88,26 +88,26 @@ class PostService { /** * @param Post $post - * @param ACore $activity + * @param string $token * - * @return string - * @throws SocialAppConfigException + * @return ACore * @throws InvalidOriginException * @throws InvalidResourceException * @throws ItemUnknownException + * @throws MalformedArrayException * @throws NoteNotFoundException * @throws RedundancyLimitException * @throws RequestContentException * @throws RequestNetworkException + * @throws RequestResultNotJsonException * @throws RequestResultSizeException * @throws RequestServerException - * @throws MalformedArrayException - * @throws RequestResultNotJsonException + * @throws SocialAppConfigException */ - public function createPost(Post $post, ACore &$activity = null): string { + public function createPost(Post $post, string &$token = ''): ACore { $note = new Note(); $actor = $post->getActor(); - $this->noteService->assignStream($note, $actor, $post->getType()); + $this->noteService->assignItem($note, $actor, $post->getType()); $note->setAttributedTo( $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername() @@ -119,10 +119,10 @@ class PostService { $this->noteService->addRecipients($note, $post->getType(), $post->getTo()); $this->noteService->addHashtags($note, $post->getHashtags()); - $result = $this->activityService->createActivity($actor, $note, $activity); + $token = $this->activityService->createActivity($actor, $note, $activity); $this->accountService->cacheLocalActorDetailCount($actor); - return $result; + return $activity; } diff --git a/lib/Service/StreamActionService.php b/lib/Service/StreamActionService.php new file mode 100644 index 00000000..978b9952 --- /dev/null +++ b/lib/Service/StreamActionService.php @@ -0,0 +1,117 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.com> + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Service; + + +use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use Exception; +use OCA\Social\Db\NotesRequest; +use OCA\Social\Db\StreamActionsRequest; +use OCA\Social\Exceptions\InvalidOriginException; +use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Exceptions\ItemUnknownException; +use OCA\Social\Exceptions\NoteNotFoundException; +use OCA\Social\Exceptions\RedundancyLimitException; +use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; +use OCA\Social\Exceptions\RequestResultSizeException; +use OCA\Social\Exceptions\RequestServerException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Model\ActivityPub\Object\Announce; +use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Stream; +use OCA\Social\Model\InstancePath; + + +/** + * Class StreamActionService + * + * @package OCA\Social\Service + */ +class StreamActionService { + + + /** @var StreamActionsRequest */ + private $streamActionsRequest; + + /** @var MiscService */ + private $miscService; + + + /** + * StreamActionService constructor. + * + * @param StreamActionsRequest $streamActionsRequest + * @param MiscService $miscService + */ + public function __construct(StreamActionsRequest $streamActionsRequest, MiscService $miscService + ) { + $this->streamActionsRequest = $streamActionsRequest; + $this->miscService = $miscService; + } + + + /** + * @param string $actorId + * @param string $streamId + * @param string $key + * @param string $value + */ + public function setAction(string $actorId, string $streamId, string $key, string $value) { + + } + + + /** + * @param string $actorId + * @param string $streamId + * @param string $key + * @param int $value + */ + public function setActionInt(string $actorId, string $streamId, string $key, int $value) { + + } + + + /** + * @param string $actorId + * @param string $streamId + * @param string $key + * @param bool $value + */ + public function setActionBool(string $actorId, string $streamId, string $key, bool $value) { + + } + +} + |