diff options
28 files changed, 782 insertions, 89 deletions
diff --git a/appinfo/info.xml b/appinfo/info.xml index 76a3148f..3d15e83a 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -51,6 +51,7 @@ <command>OCA\Social\Command\CacheRefresh</command> <command>OCA\Social\Command\CheckInstall</command> <command>OCA\Social\Command\Fediverse</command> + <command>OCA\Social\Command\NoteLike</command> <command>OCA\Social\Command\NoteCreate</command> <command>OCA\Social\Command\NoteBoost</command> <command>OCA\Social\Command\Reset</command> diff --git a/lib/Command/NoteBoost.php b/lib/Command/NoteBoost.php index ff1b85ae..3df7a515 100644 --- a/lib/Command/NoteBoost.php +++ b/lib/Command/NoteBoost.php @@ -34,13 +34,9 @@ namespace OCA\Social\Command; use Exception; use OC\Core\Command\Base; use OCA\Social\Service\AccountService; -use OCA\Social\Service\ActivityService; use OCA\Social\Service\BoostService; -use OCA\Social\Service\ConfigService; -use OCA\Social\Service\CurlService; use OCA\Social\Service\MiscService; use OCA\Social\Service\NoteService; -use OCA\Social\Service\PostService; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -54,13 +50,6 @@ use Symfony\Component\Console\Output\OutputInterface; */ class NoteBoost extends Base { - - /** @var ConfigService */ - private $configService; - - /** @var ActivityService */ - private $activityService; - /** @var NoteService */ private $noteService; @@ -70,12 +59,6 @@ class NoteBoost extends Base { /** @var BoostService */ private $boostService; - /** @var PostService */ - private $postService; - - /** @var CurlService */ - private $curlService; - /** @var MiscService */ private $miscService; @@ -83,29 +66,20 @@ class NoteBoost extends Base { /** * NoteBoost constructor. * - * @param ActivityService $activityService * @param AccountService $accountService * @param NoteService $noteService * @param BoostService $boostService - * @param PostService $postService - * @param CurlService $curlService - * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - ActivityService $activityService, AccountService $accountService, - NoteService $noteService, BoostService $boostService, PostService $postService, - CurlService $curlService, ConfigService $configService, MiscService $miscService + AccountService $accountService, NoteService $noteService, BoostService $boostService, + MiscService $miscService ) { parent::__construct(); - $this->activityService = $activityService; $this->noteService = $noteService; $this->boostService = $boostService; $this->accountService = $accountService; - $this->postService = $postService; - $this->curlService = $curlService; - $this->configService = $configService; $this->miscService = $miscService; } @@ -116,8 +90,8 @@ class NoteBoost extends Base { protected function configure() { parent::configure(); $this->setName('social:note:boost') - ->addArgument('userid', InputArgument::REQUIRED, 'userId of the author') - ->addArgument('note', InputArgument::REQUIRED, 'Note to boost') + ->addArgument('user_id', InputArgument::REQUIRED, 'userId of the author') + ->addArgument('note_id', InputArgument::REQUIRED, 'Note to boost') ->addOption('unboost', '', InputOption::VALUE_NONE, 'Unboost') ->setDescription('Boost a note'); } @@ -130,8 +104,8 @@ class NoteBoost extends Base { * @throws Exception */ protected function execute(InputInterface $input, OutputInterface $output) { - $userId = $input->getArgument('userid'); - $noteId = $input->getArgument('note'); + $userId = $input->getArgument('user_id'); + $noteId = $input->getArgument('note_id'); $actor = $this->accountService->getActorFromUserId($userId); $this->noteService->setViewer($actor); diff --git a/lib/Command/NoteCreate.php b/lib/Command/NoteCreate.php index b6b8edb5..6a7ae79d 100644 --- a/lib/Command/NoteCreate.php +++ b/lib/Command/NoteCreate.php @@ -132,7 +132,7 @@ class NoteCreate extends Base { */ protected function execute(InputInterface $input, OutputInterface $output) { - $userId = $input->getArgument('userid'); + $userId = $input->getArgument('user_id'); $content = $input->getArgument('content'); $to = $input->getOption('to'); $hashtag = $input->getOption('hashtag'); diff --git a/lib/Command/NoteLike.php b/lib/Command/NoteLike.php new file mode 100644 index 00000000..d2618c0d --- /dev/null +++ b/lib/Command/NoteLike.php @@ -0,0 +1,125 @@ +<?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\Command; + + +use Exception; +use OC\Core\Command\Base; +use OCA\Social\Service\AccountService; +use OCA\Social\Service\LikeService; +use OCA\Social\Service\MiscService; +use OCA\Social\Service\NoteService; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + + +/** + * Class NoteLike + * + * @package OCA\Social\Command + */ +class NoteLike extends Base { + + + /** @var NoteService */ + private $noteService; + + /** @var AccountService */ + private $accountService; + + /** @var LikeService */ + private $likeService; + + /** @var MiscService */ + private $miscService; + + + /** + * NoteBoost constructor. + * + * @param AccountService $accountService + * @param NoteService $noteService + * @param LikeService $likeService + * @param MiscService $miscService + */ + public function __construct( + AccountService $accountService, NoteService $noteService, LikeService $likeService, + MiscService $miscService + ) { + parent::__construct(); + + $this->noteService = $noteService; + $this->likeService = $likeService; + $this->accountService = $accountService; + $this->miscService = $miscService; + } + + + /** + * + */ + protected function configure() { + parent::configure(); + $this->setName('social:note:like') + ->addArgument('user_id', InputArgument::REQUIRED, 'userId of the author') + ->addArgument('note_id', InputArgument::REQUIRED, 'Note to like') + ->addOption('unlike', '', InputOption::VALUE_NONE, 'Unlike') + ->setDescription('Like a note'); + } + + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @throws Exception + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $userId = $input->getArgument('user_id'); + $noteId = $input->getArgument('note_id'); + + $actor = $this->accountService->getActorFromUserId($userId); + $this->noteService->setViewer($actor); + + if (!$input->getOption('unlike')) { + $activity = $this->likeService->create($actor, $noteId, $token); + } else { + $activity = $this->likeService->delete($actor, $noteId, $token); + } + + echo 'object: ' . json_encode($activity, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"; + echo 'token: ' . $token . "\n"; + } + +} + diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 5e7dcd6a..a08d7aa0 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -45,6 +45,7 @@ use OCA\Social\Service\BoostService; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\DocumentService; use OCA\Social\Service\FollowService; +use OCA\Social\Service\LikeService; use OCA\Social\Service\MiscService; use OCA\Social\Service\NoteService; use OCA\Social\Service\PostService; @@ -81,6 +82,9 @@ class LocalController extends Controller { /** @var BoostService */ private $boostService; + /** @var LikeService */ + private $likeService; + /** @var PostService */ private $postService; @@ -116,6 +120,7 @@ class LocalController extends Controller { * @param NoteService $noteService * @param SearchService $searchService * @param BoostService $boostService + * @param LikeService $likeService * @param DocumentService $documentService * @param MiscService $miscService */ @@ -123,7 +128,8 @@ class LocalController extends Controller { IRequest $request, $userId, AccountService $accountService, CacheActorService $cacheActorService, FollowService $followService, PostService $postService, NoteService $noteService, SearchService $searchService, - BoostService $boostService, DocumentService $documentService, MiscService $miscService + BoostService $boostService, LikeService $likeService, DocumentService $documentService, + MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); @@ -135,6 +141,7 @@ class LocalController extends Controller { $this->postService = $postService; $this->followService = $followService; $this->boostService = $boostService; + $this->likeService = $likeService; $this->documentService = $documentService; $this->miscService = $miscService; } @@ -254,6 +261,58 @@ class LocalController extends Controller { /** + * Create a new boost. + * + * @NoAdminRequired + * + * @param string $postId + * + * @return DataResponse + */ + public function postLike(string $postId): DataResponse { + try { + $this->initViewer(true); + $announce = $this->likeService->create($this->viewer, $postId, $token); + + return $this->success( + [ + 'like' => $announce, + 'token' => $token + ] + ); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** + * Delete a boost. + * + * @NoAdminRequired + * + * @param string $postId + * + * @return DataResponse + */ + public function postUnlike(string $postId): DataResponse { + try { + $this->initViewer(true); + $announce = $this->likeService->delete($this->viewer, $postId, $token); + + return $this->success( + [ + 'like' => $announce, + 'token' => $token + ] + ); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** * @NoCSRFRequired * @NoAdminRequired * @@ -275,6 +334,7 @@ class LocalController extends Controller { /** + * @NoCSRFRequired * @NoAdminRequired * * @param int $since diff --git a/lib/Db/ActionsRequest.php b/lib/Db/ActionsRequest.php index cb01c5fc..979164ea 100644 --- a/lib/Db/ActionsRequest.php +++ b/lib/Db/ActionsRequest.php @@ -104,6 +104,29 @@ class ActionsRequest extends ActionsRequestBuilder { /** + * @param ACore $item + * + * @return ACore + * @throws ActionDoesNotExistException + */ + public function getActionFromItem(ACore $item): ACore { + $qb = $this->getActionsSelectSql(); + $this->limitToActorId($qb, $item->getActorId()); + $this->limitToObjectId($qb, $item->getObjectId()); + $this->limitToType($qb, $item->getType()); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + if ($data === false) { + throw new ActionDoesNotExistException(); + } + + return $this->parseActionsSelectSql($data); + } + + + /** * @param string $objectId * @param string $type * @@ -149,6 +172,7 @@ class ActionsRequest extends ActionsRequestBuilder { public function delete(ACore $item) { $qb = $this->getActionsDeleteSql(); $this->limitToIdString($qb, $item->getId()); + $this->limitToType($qb, $item->getType()); $qb->execute(); } diff --git a/lib/Exceptions/ItemAlreadyExistsException.php b/lib/Exceptions/ItemAlreadyExistsException.php new file mode 100644 index 00000000..f048b19c --- /dev/null +++ b/lib/Exceptions/ItemAlreadyExistsException.php @@ -0,0 +1,40 @@ +<?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\Exceptions; + + +use Exception; + + +class ItemAlreadyExistsException extends Exception { + +} + diff --git a/lib/Interfaces/Activity/AcceptInterface.php b/lib/Interfaces/Activity/AcceptInterface.php index fc726028..74038c54 100644 --- a/lib/Interfaces/Activity/AcceptInterface.php +++ b/lib/Interfaces/Activity/AcceptInterface.php @@ -81,6 +81,17 @@ class AcceptInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore @@ -98,7 +109,6 @@ class AcceptInterface implements IActivityPubInterface { } - /** * @param ACore $item */ @@ -113,7 +123,6 @@ class AcceptInterface implements IActivityPubInterface { } - /** * @param ACore $item * @param string $source @@ -122,7 +131,6 @@ class AcceptInterface implements IActivityPubInterface { } - /** * @param ACore $activity * @param ACore $item diff --git a/lib/Interfaces/Activity/AddInterface.php b/lib/Interfaces/Activity/AddInterface.php index 8aa7da89..7d7dfb52 100644 --- a/lib/Interfaces/Activity/AddInterface.php +++ b/lib/Interfaces/Activity/AddInterface.php @@ -81,6 +81,17 @@ class AddInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/BlockInterface.php b/lib/Interfaces/Activity/BlockInterface.php index 3fe46f94..d708cf42 100644 --- a/lib/Interfaces/Activity/BlockInterface.php +++ b/lib/Interfaces/Activity/BlockInterface.php @@ -81,6 +81,17 @@ class BlockInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/CreateInterface.php b/lib/Interfaces/Activity/CreateInterface.php index a625655b..fa00968f 100644 --- a/lib/Interfaces/Activity/CreateInterface.php +++ b/lib/Interfaces/Activity/CreateInterface.php @@ -81,6 +81,17 @@ class CreateInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/DeleteInterface.php b/lib/Interfaces/Activity/DeleteInterface.php index 6f86d8d0..c4533e8e 100644 --- a/lib/Interfaces/Activity/DeleteInterface.php +++ b/lib/Interfaces/Activity/DeleteInterface.php @@ -69,6 +69,7 @@ class DeleteInterface implements IActivityPubInterface { if ($item->getObjectId() !== '') { $item->checkOrigin($item->getObjectId()); + // TODO: migrate to activity() !! $types = ['Note', 'Person']; foreach ($types as $type) { try { @@ -91,8 +92,9 @@ class DeleteInterface implements IActivityPubInterface { $object = $item->getObject(); try { $item->checkOrigin($object->getId()); - $interface = AP::$activityPub->getInterfaceForItem($object); - $interface->delete($object); + // FIXME: needed ? better use activity() +// $interface = AP::$activityPub->getInterfaceForItem($object); +// $interface->delete($object); } catch (InvalidOriginException $e) { } catch (ItemUnknownException $e) { } @@ -107,6 +109,17 @@ class DeleteInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/RejectInterface.php b/lib/Interfaces/Activity/RejectInterface.php index 6f21ba1a..64854226 100644 --- a/lib/Interfaces/Activity/RejectInterface.php +++ b/lib/Interfaces/Activity/RejectInterface.php @@ -81,6 +81,17 @@ class RejectInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/RemoveInterface.php b/lib/Interfaces/Activity/RemoveInterface.php index c284109e..852f8c00 100644 --- a/lib/Interfaces/Activity/RemoveInterface.php +++ b/lib/Interfaces/Activity/RemoveInterface.php @@ -81,6 +81,17 @@ class RemoveInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/UndoInterface.php b/lib/Interfaces/Activity/UndoInterface.php index 5231312b..c74bcac4 100644 --- a/lib/Interfaces/Activity/UndoInterface.php +++ b/lib/Interfaces/Activity/UndoInterface.php @@ -82,6 +82,17 @@ class UndoInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Activity/UpdateInterface.php b/lib/Interfaces/Activity/UpdateInterface.php index 465a6968..e129f4e0 100644 --- a/lib/Interfaces/Activity/UpdateInterface.php +++ b/lib/Interfaces/Activity/UpdateInterface.php @@ -81,6 +81,17 @@ class UpdateInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/Actor/PersonInterface.php b/lib/Interfaces/Actor/PersonInterface.php index 084b277d..ef04c1f3 100644 --- a/lib/Interfaces/Actor/PersonInterface.php +++ b/lib/Interfaces/Actor/PersonInterface.php @@ -109,6 +109,17 @@ class PersonInterface implements IActivityPubInterface { /** + * @param ACore $item + * + * @return ACore + * @throws ItemNotFoundException + */ + public function getItem(ACore $item): ACore { + throw new ItemNotFoundException(); + } + + + /** * @param string $id * * @return ACore diff --git a/lib/Interfaces/IActivityPubInterface.php b/lib/Interfaces/IActivityPubInterface.php index fe7f4d99..2d40f6fe 100644 --- a/lib/Interfaces/IActivityPubInterface.php +++ b/lib/Interfaces/IActivityPubInterface.php @@ -31,6 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Interfaces; +use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Model\ActivityPub\ACore; @@ -60,6 +61,19 @@ interface IActivityPubInterface { /** + * When an activity is triggered by an 'Model\ActivityPub\Activity' model. + * + * !! This should be the only way of interaction between 2 IActivityPubInterface !! + * + * @param ACore $activity + * @param ACore $item + */ + public function activity(ACore $activity, ACore $item); + + + /** + * get Item by it |