kopia lustrzana https://github.com/nextcloud/social
like/unlike post
Signed-off-by: Maxence Lange <maxence@artificial-owl.com> post like Signed-off-by: Maxence Lange <maxence@artificial-owl.com> better managing between save/delete Signed-off-by: Maxence Lange <maxence@artificial-owl.com> delete undoAction Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/614/head
rodzic
85242a82a1
commit
57291f5b1d
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
@ -253,6 +260,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
|
||||
|
|
|
@ -103,6 +103,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();
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
||||
}
|
||||
|
|
@ -80,6 +80,17 @@ class AcceptInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
@ -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
|
||||
|
|
|
@ -80,6 +80,17 @@ class AddInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -80,6 +80,17 @@ class BlockInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -80,6 +80,17 @@ class CreateInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -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) {
|
||||
}
|
||||
|
@ -106,6 +108,17 @@ class DeleteInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -80,6 +80,17 @@ class RejectInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -80,6 +80,17 @@ class RemoveInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -81,6 +81,17 @@ class UndoInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -80,6 +80,17 @@ class UpdateInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -108,6 +108,17 @@ class PersonInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -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 its Id.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return ACore
|
||||
|
@ -69,18 +83,23 @@ interface IActivityPubInterface {
|
|||
|
||||
|
||||
/**
|
||||
* When an activity is triggered by an 'Model\ActivityPub\Activity' model.
|
||||
* get Item when Id is not known.
|
||||
*
|
||||
* @param ACore $activity
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function activity(ACore $activity, ACore $item);
|
||||
public function getItem(ACore $item): ACore;
|
||||
|
||||
|
||||
/**
|
||||
* Save the current item.
|
||||
*
|
||||
* !! Should not be called from an other IActivityPubInterface !!
|
||||
*
|
||||
* @param ACore $item
|
||||
* @throws ItemAlreadyExistsException
|
||||
*/
|
||||
public function save(ACore $item);
|
||||
|
||||
|
@ -88,7 +107,10 @@ interface IActivityPubInterface {
|
|||
/**
|
||||
* Update the current item.
|
||||
*
|
||||
* !! Should not be called from an other IActivityPubInterface !!
|
||||
*
|
||||
* @param ACore $item
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function update(ACore $item);
|
||||
|
||||
|
@ -96,6 +118,8 @@ interface IActivityPubInterface {
|
|||
/**
|
||||
* Event on the current item.
|
||||
*
|
||||
* !! Should not be called from an other IActivityPubInterface !!
|
||||
*
|
||||
* @param ACore $item
|
||||
* @param string $source
|
||||
*/
|
||||
|
@ -105,6 +129,8 @@ interface IActivityPubInterface {
|
|||
/**
|
||||
* Delete the current item.
|
||||
*
|
||||
* !! Should not be called from an other IActivityPubInterface !!
|
||||
*
|
||||
* @param ACore $item
|
||||
*/
|
||||
public function delete(ACore $item);
|
||||
|
|
|
@ -91,6 +91,17 @@ class SocialAppNotificationInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -115,6 +115,33 @@ class AnnounceInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @throws InvalidOriginException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function processIncomingRequest(ACore $item) {
|
||||
/** @var ACore $item */
|
||||
$item->checkOrigin($item->getId());
|
||||
$item->checkOrigin($item->getActorId());
|
||||
|
||||
try {
|
||||
$this->actionsRequest->getActionFromItem($item);
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
$this->actionsRequest->save($item);
|
||||
|
||||
try {
|
||||
$post = $this->streamRequest->getStreamById($item->getObjectId());
|
||||
$this->updateDetails($post);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
$this->save($item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $activity
|
||||
* @param ACore $announce
|
||||
|
@ -144,23 +171,19 @@ class AnnounceInterface implements IActivityPubInterface {
|
|||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @throws InvalidOriginException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function processIncomingRequest(ACore $item) {
|
||||
/** @var Stream $item */
|
||||
$item->checkOrigin($item->getId());
|
||||
|
||||
$this->actionsRequest->save($item);
|
||||
$this->save($item);
|
||||
public function processResult(ACore $item) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function processResult(ACore $item) {
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
|
@ -309,9 +332,7 @@ class AnnounceInterface implements IActivityPubInterface {
|
|||
*/
|
||||
private function undoAnnounceAction(Announce $announce) {
|
||||
try {
|
||||
$this->actionsRequest->getAction(
|
||||
$announce->getActorId(), $announce->getObjectId(), Announce::TYPE
|
||||
);
|
||||
$this->actionsRequest->getActionFromItem($announce);
|
||||
$this->actionsRequest->delete($announce);
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
}
|
||||
|
@ -335,9 +356,9 @@ class AnnounceInterface implements IActivityPubInterface {
|
|||
* @param Stream $post
|
||||
*/
|
||||
private function updateDetails(Stream $post) {
|
||||
if (!$post->isLocal()) {
|
||||
return;
|
||||
}
|
||||
// if (!$post->isLocal()) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
$post->setDetailInt(
|
||||
'boosts', $this->actionsRequest->countActions($post->getId(), Announce::TYPE)
|
||||
|
@ -420,6 +441,5 @@ class AnnounceInterface implements IActivityPubInterface {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -86,6 +86,17 @@ class DocumentInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -199,6 +199,17 @@ class FollowInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -79,6 +79,17 @@ class ImageInterface extends DocumentInterface implements IActivityPubInterface
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -36,6 +36,7 @@ use OCA\Social\AP;
|
|||
use OCA\Social\Db\ActionsRequest;
|
||||
use OCA\Social\Db\StreamRequest;
|
||||
use OCA\Social\Exceptions\InvalidOriginException;
|
||||
use OCA\Social\Exceptions\ItemAlreadyExistsException;
|
||||
use OCA\Social\Exceptions\ItemNotFoundException;
|
||||
use OCA\Social\Exceptions\ItemUnknownException;
|
||||
use OCA\Social\Exceptions\ActionDoesNotExistException;
|
||||
|
@ -99,25 +100,12 @@ class LikeInterface implements IActivityPubInterface {
|
|||
*/
|
||||
public function processIncomingRequest(ACore $like) {
|
||||
/** @var Like $like */
|
||||
$like->checkOrigin($like->getId());
|
||||
$like->checkOrigin($like->getActorId());
|
||||
|
||||
try {
|
||||
$this->actionsRequest->getAction($like->getActorId(), $like->getObjectId(), Like::TYPE);
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
$this->actionsRequest->save($like);
|
||||
|
||||
try {
|
||||
if ($like->hasActor()) {
|
||||
$actor = $like->getActor();
|
||||
} else {
|
||||
$actor = $this->cacheActorService->getFromId($like->getActorId());
|
||||
}
|
||||
|
||||
$post = $this->streamRequest->getStreamById($like->getObjectId());
|
||||
$this->updateDetails($post);
|
||||
$this->generateNotification($post, $actor);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
$this->save($like);
|
||||
} catch (ItemAlreadyExistsException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,7 +122,7 @@ class LikeInterface implements IActivityPubInterface {
|
|||
$activity->checkOrigin($like->getId());
|
||||
$activity->checkOrigin($like->getActorId());
|
||||
|
||||
$this->undoLike($like);
|
||||
$this->delete($like);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,6 +134,24 @@ class LikeInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
try {
|
||||
return $this->actionsRequest->getAction(
|
||||
$item->getActorId(), $item->getObjectId(), Like::TYPE
|
||||
);
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
}
|
||||
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
@ -159,8 +165,29 @@ class LikeInterface implements IActivityPubInterface {
|
|||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @throws ItemAlreadyExistsException
|
||||
*/
|
||||
public function save(ACore $item) {
|
||||
try {
|
||||
$this->actionsRequest->getActionFromItem($item);
|
||||
throw new ItemAlreadyExistsException();
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
}
|
||||
|
||||
$this->actionsRequest->save($item);
|
||||
try {
|
||||
if ($item->hasActor()) {
|
||||
$actor = $item->getActor();
|
||||
} else {
|
||||
$actor = $this->cacheActorService->getFromId($item->getActorId());
|
||||
}
|
||||
|
||||
$post = $this->streamRequest->getStreamById($item->getObjectId());
|
||||
$this->updateDetails($post);
|
||||
$this->generateNotification($post, $actor);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -175,6 +202,8 @@ class LikeInterface implements IActivityPubInterface {
|
|||
* @param ACore $item
|
||||
*/
|
||||
public function delete(ACore $item) {
|
||||
$this->actionsRequest->delete($item);
|
||||
$this->undoLikeAction($item);
|
||||
}
|
||||
|
||||
|
||||
|
@ -187,15 +216,9 @@ class LikeInterface implements IActivityPubInterface {
|
|||
|
||||
|
||||
/**
|
||||
* @param Like $like
|
||||
* @param ACore $like
|
||||
*/
|
||||
private function undoLike(Like $like) {
|
||||
try {
|
||||
$this->actionsRequest->getAction($like->getActorId(), $like->getObjectId(), Like::TYPE);
|
||||
$this->actionsRequest->delete($like);
|
||||
} catch (ActionDoesNotExistException $e) {
|
||||
}
|
||||
|
||||
private function undoLikeAction(ACore $like) {
|
||||
try {
|
||||
if ($like->hasActor()) {
|
||||
$actor = $like->getActor();
|
||||
|
@ -215,9 +238,9 @@ class LikeInterface implements IActivityPubInterface {
|
|||
* @param Stream $post
|
||||
*/
|
||||
private function updateDetails(Stream $post) {
|
||||
if (!$post->isLocal()) {
|
||||
return;
|
||||
}
|
||||
// if (!$post->isLocal()) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
$post->setDetailInt(
|
||||
'likes', $this->actionsRequest->countActions($post->getId(), Like::TYPE)
|
||||
|
|
|
@ -93,6 +93,17 @@ class NoteInterface implements IActivityPubInterface {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
*
|
||||
* @return ACore
|
||||
* @throws ItemNotFoundException
|
||||
*/
|
||||
public function getItem(ACore $item): ACore {
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
|
|
|
@ -158,7 +158,6 @@ class Item {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -305,6 +304,11 @@ class Item {
|
|||
* @return string
|
||||
*/
|
||||
public function getActorId(): string {
|
||||
if ($this->hasActor()) {
|
||||
return $this->getActor()
|
||||
->getId();
|
||||
}
|
||||
|
||||
return $this->actorId;
|
||||
}
|
||||
|
||||
|
@ -687,7 +691,6 @@ class Item {
|
|||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\TStringTools;
|
|||
use Exception;
|
||||
use OCA\Social\AP;
|
||||
use OCA\Social\Db\StreamRequest;
|
||||
use OCA\Social\Exceptions\ItemNotFoundException;
|
||||
use OCA\Social\Exceptions\ItemUnknownException;
|
||||
use OCA\Social\Exceptions\SocialAppConfigException;
|
||||
use OCA\Social\Exceptions\StreamNotFoundException;
|
||||
|
@ -134,6 +135,12 @@ class BoostService {
|
|||
$announce->setRequestToken($this->uuid());
|
||||
|
||||
$interface = AP::$activityPub->getInterfaceFromType(Announce::TYPE);
|
||||
// TODO: check that announce does not exist already ?
|
||||
// try {
|
||||
// return $interface->getItem($announce);
|
||||
// } catch (ItemNotFoundException $e) {
|
||||
// }
|
||||
|
||||
$interface->save($announce);
|
||||
|
||||
$this->streamActionService->setActionBool($actor->getId(), $postId, 'boosted', true);
|
||||
|
|
|
@ -0,0 +1,226 @@
|
|||
<?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\Traits\TStringTools;
|
||||
use Exception;
|
||||
use OCA\Social\AP;
|
||||
use OCA\Social\Db\StreamRequest;
|
||||
use OCA\Social\Exceptions\ItemNotFoundException;
|
||||
use OCA\Social\Exceptions\ItemUnknownException;
|
||||
use OCA\Social\Exceptions\SocialAppConfigException;
|
||||
use OCA\Social\Exceptions\StreamNotFoundException;
|
||||
use OCA\Social\Model\ActivityPub\ACore;
|
||||
use OCA\Social\Model\ActivityPub\Object\Like;
|
||||
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;
|
||||
use OCA\Social\Model\InstancePath;
|
||||
|
||||
|
||||
/**
|
||||
* Class LikeService
|
||||
*
|
||||
* @package OCA\Social\Service
|
||||
*/
|
||||
class LikeService {
|
||||
|
||||
|
||||
use TStringTools;
|
||||
|
||||
|
||||
/** @var StreamRequest */
|
||||
private $streamRequest;
|
||||
|
||||
/** @var NoteService */
|
||||
private $noteService;
|
||||
|
||||
/** @var SignatureService */
|
||||
private $signatureService;
|
||||
|
||||
/** @var ActivityService */
|
||||
private $activityService;
|
||||
|
||||
/** @var StreamActionService */
|
||||
private $streamActionService;
|
||||
|
||||
/** @var StreamQueueService */
|
||||
private $streamQueueService;
|
||||
|
||||
/** @var MiscService */
|
||||
private $miscService;
|
||||
|
||||
|
||||
/**
|
||||
* LikeService constructor.
|
||||
*
|
||||
* @param StreamRequest $streamRequest
|
||||
* @param NoteService $noteService
|
||||
* @param SignatureService $signatureService
|
||||
* @param ActivityService $activityService
|
||||
* @param StreamActionService $streamActionService
|
||||
* @param StreamQueueService $streamQueueService
|
||||
* @param MiscService $miscService
|
||||
*/
|
||||
public function __construct(
|
||||
StreamRequest $streamRequest, NoteService $noteService, SignatureService $signatureService,
|
||||
ActivityService $activityService, StreamActionService $streamActionService,
|
||||
StreamQueueService $streamQueueService, MiscService $miscService
|
||||
) {
|
||||
$this->streamRequest = $streamRequest;
|
||||
$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 StreamNotFoundException
|
||||
* @throws SocialAppConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(Person $actor, string $postId, &$token = ''): ACore {
|
||||
/** @var Like $like */
|
||||
$like = AP::$activityPub->getItemFromType(Like::TYPE);
|
||||
$like->setId($actor->getId() . '#like/' . $this->uuid(8));
|
||||
$like->setActor($actor);
|
||||
|
||||
$note = $this->noteService->getNoteById($postId, true);
|
||||
if ($note->getType() !== Note::TYPE) {
|
||||
throw new StreamNotFoundException('Stream is not a Note');
|
||||
}
|
||||
|
||||
if (!$note->isPublic()) {
|
||||
throw new StreamNotFoundException('Stream is not Public');
|
||||
}
|
||||
|
||||
$like->setObjectId($note->getId());
|
||||
$this->assignInstance($like, $actor, $note);
|
||||
|
||||
$interface = AP::$activityPub->getInterfaceFromType(Like::TYPE);
|
||||
$interface->save($like);
|
||||
|
||||
$this->streamActionService->setActionBool($actor->getId(), $postId, 'liked', true);
|
||||
$token = $this->activityService->request($like);
|
||||
|
||||
return $like;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $postId
|
||||
*
|
||||
* @return Stream
|
||||
* @throws ItemUnknownException
|
||||
* @throws SocialAppConfigException
|
||||
* @throws StreamNotFoundException
|
||||
*/
|
||||
public function get(string $postId): Stream {
|
||||
$stream = $this->streamRequest->getStreamByObjectId($postId, Like::TYPE);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Person $actor
|
||||
* @param string $postId
|
||||
* @param string $token
|
||||
*
|
||||
* @return ACore
|
||||
* @throws SocialAppConfigException
|
||||
* @throws StreamNotFoundException
|
||||
*/
|
||||
public function delete(Person $actor, string $postId, &$token = ''): ACore {
|
||||
$undo = new Undo();
|
||||
$undo->setActor($actor);
|
||||
|
||||
$note = $this->noteService->getNoteById($postId, true);
|
||||
if ($note->getType() !== Note::TYPE) {
|
||||
throw new StreamNotFoundException('Stream is not a Note');
|
||||
}
|
||||
|
||||
$this->assignInstance($undo, $actor, $note);
|
||||
try {
|
||||
$tmp = AP::$activityPub->getItemFromType(Like::TYPE);
|
||||
$tmp->setActor($actor);
|
||||
$tmp->setObjectId($postId);
|
||||
|
||||
$interface = AP::$activityPub->getInterfaceFromType(Like::TYPE);
|
||||
$like = $interface->getItem($tmp);
|
||||
|
||||
$undo->setId($like->getId() . '/undo');
|
||||
$undo->setObject($like);
|
||||
|
||||
$interface->delete($like);
|
||||
|
||||
$token = $this->activityService->request($undo);
|
||||
} catch (ItemUnknownException $e) {
|
||||
} catch (ItemNotFoundException $e) {
|
||||
}
|
||||
|
||||
$this->streamActionService->setActionBool($actor->getId(), $postId, 'boosted', false);
|
||||
|
||||
return $undo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ACore $item
|
||||
* @param Person $actor
|
||||
* @param Stream $note
|
||||
*/
|
||||
private function assignInstance(ACore $item, Person $actor, Stream $note) {
|
||||
// $item->addInstancePath(
|
||||
// new InstancePath(
|
||||
// $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, InstancePath::PRIORITY_LOW
|
||||
// )
|
||||
// );
|
||||
$item->addInstancePath(
|
||||
new InstancePath(
|
||||
$note->getAttributedTo(), InstancePath::TYPE_INBOX, InstancePath::PRIORITY_LOW
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
Ładowanie…
Reference in New Issue