From 882a6f57937ed949ea2bff8ae18c8707b8a31bee Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 29 Jun 2019 19:35:41 -0100 Subject: likes -> actions Signed-off-by: Maxence Lange --- lib/Db/ActionsRequest.php | 168 ++++++++++++++++++++++++ lib/Db/ActionsRequestBuilder.php | 145 ++++++++++++++++++++ lib/Db/CoreRequestBuilder.php | 2 +- lib/Db/LikesRequest.php | 162 ----------------------- lib/Db/LikesRequestBuilder.php | 146 -------------------- lib/Interfaces/Object/AnnounceInterface.php | 67 ++++++---- lib/Interfaces/Object/LikeInterface.php | 67 ++++++---- lib/Migration/Version0002Date20190628000001.php | 159 ---------------------- lib/Migration/Version0002Date20190629000001.php | 159 ++++++++++++++++++++++ 9 files changed, 561 insertions(+), 514 deletions(-) create mode 100644 lib/Db/ActionsRequest.php create mode 100644 lib/Db/ActionsRequestBuilder.php delete mode 100644 lib/Db/LikesRequest.php delete mode 100644 lib/Db/LikesRequestBuilder.php delete mode 100644 lib/Migration/Version0002Date20190628000001.php create mode 100644 lib/Migration/Version0002Date20190629000001.php (limited to 'lib') diff --git a/lib/Db/ActionsRequest.php b/lib/Db/ActionsRequest.php new file mode 100644 index 00000000..b102362d --- /dev/null +++ b/lib/Db/ActionsRequest.php @@ -0,0 +1,168 @@ + + * @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\Db; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use DateTime; +use Exception; +use OCA\Social\Exceptions\LikeDoesNotExistException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Object\Like; +use OCP\DB\QueryBuilder\IQueryBuilder; + + +/** + * Class ActionsRequest + * + * @package OCA\Social\Db + */ +class ActionsRequest extends ActionsRequestBuilder { + + + use TArrayTools; + + + /** + * Insert a new Note in the database. + * + * @param ACore $like + */ + public function save(ACore $like) { + $qb = $this->getActionsInsertSql(); + $qb->setValue('id', $qb->createNamedParameter($like->getId())) + ->setValue('actor_id', $qb->createNamedParameter($like->getActorId())) + ->setValue('type', $qb->createNamedParameter($like->getType())) + ->setValue('object_id', $qb->createNamedParameter($like->getObjectId())); + + try { + $qb->setValue( + 'creation', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + } catch (Exception $e) { + } + + $this->generatePrimaryKey($qb, $like->getId()); + + $qb->execute(); + } + + + /** + * @param string $actorId + * @param string $objectId + * + * @param string $type + * + * @return ACore + * @throws LikeDoesNotExistException + */ + public function getAction(string $actorId, string $objectId, string $type): ACore { + $qb = $this->getActionsSelectSql(); + $this->limitToActorId($qb, $actorId); + $this->limitToObjectId($qb, $objectId); + $this->limitToType($qb, $type); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + if ($data === false) { + throw new LikeDoesNotExistException(); + } + + return $this->parseActionsSelectSql($data); + } + + + /** + * @param string $objectId + * @param string $type + * + * @return int + */ + public function countActions(string $objectId, string $type): int { + $qb = $this->countActionsSelectSql(); + $this->limitToObjectId($qb, $objectId); + $this->limitToType($qb, $type); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + return $this->getInt('count', $data, 0); + } + + + /** + * @param string $objectId + * + * @return Like[] + */ + public function getByObjectId(string $objectId): array { + $qb = $this->getActionsSelectSql(); + $this->limitToObjectId($qb, $objectId); + $this->leftJoinCacheActors($qb, 'actor_id'); + + $likes = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $likes[] = $this->parseActionsSelectSql($data); + } + $cursor->closeCursor(); + + return $likes; + } + + + /** + * @param Like $like + */ + public function delete(Like $like) { + $qb = $this->getActionsDeleteSql(); + $this->limitToIdString($qb, $like->getId()); + + $qb->execute(); + } + + + /** + * @param string $objectId + */ + public function deleteLikes(string $objectId) { + $qb = $this->getActionsDeleteSql(); + $this->limitToObjectId($qb, $objectId); + + $qb->execute(); + } + +} + diff --git a/lib/Db/ActionsRequestBuilder.php b/lib/Db/ActionsRequestBuilder.php new file mode 100644 index 00000000..a72ae102 --- /dev/null +++ b/lib/Db/ActionsRequestBuilder.php @@ -0,0 +1,145 @@ + + * @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\Db; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Model\ActivityPub\ACore; +use OCP\DB\QueryBuilder\IQueryBuilder; + + +/** + * Class ActionsRequestBuilder + * + * @package OCA\Social\Db + */ +class ActionsRequestBuilder extends CoreRequestBuilder { + + + use TArrayTools; + + + /** + * Base of the Sql Insert request + * + * @return IQueryBuilder + */ + protected function getActionsInsertSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->insert(self::TABLE_ACTIONS); + + return $qb; + } + + + /** + * Base of the Sql Update request + * + * @return IQueryBuilder + */ + protected function getActionsUpdateSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->update(self::TABLE_ACTIONS); + + return $qb; + } + + + /** + * Base of the Sql Select request for Shares + * + * @return IQueryBuilder + */ + protected function getActionsSelectSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $qb->select('a.id', 'a.type', 'a.actor_id', 'a.object_id', 'a.creation') + ->from(self::TABLE_ACTIONS, 'a'); + + $this->defaultSelectAlias = 'a'; + + return $qb; + } + + + /** + * Base of the Sql Select request for Shares + * + * @return IQueryBuilder + */ + protected function countActionsSelectSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count') + ->from(self::TABLE_ACTIONS, 'a'); + + $this->defaultSelectAlias = 'a'; + + return $qb; + } + + + /** + * Base of the Sql Delete request + * + * @return IQueryBuilder + */ + protected function getActionsDeleteSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->delete(self::TABLE_ACTIONS); + + return $qb; + } + + + /** + * @param array $data + * + * @return ACore + */ + protected function parseActionsSelectSql($data): ACore { + $item = new ACore(); + $item->importFromDatabase($data); + + try { + $actor = $this->parseCacheActorsLeftJoin($data); + $actor->setCompleteDetails(true); + + $item->setActor($actor); + } catch (InvalidResourceException $e) { + } + + return $item; + } + +} + diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 132e0cf3..1be163d4 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -76,7 +76,7 @@ class CoreRequestBuilder { const TABLE_STREAMS = 'social_a2_stream'; const TABLE_HASHTAGS = 'social_a2_hashtags'; const TABLE_FOLLOWS = 'social_a2_follows'; - const TABLE_LIKES = 'social_a2_likes'; + const TABLE_ACTIONS = 'social_a2_actions'; const TABLE_CACHE_ACTORS = 'social_a2_cache_actors'; const TABLE_CACHE_DOCUMENTS = 'social_a2_cache_documts'; diff --git a/lib/Db/LikesRequest.php b/lib/Db/LikesRequest.php deleted file mode 100644 index a03b448c..00000000 --- a/lib/Db/LikesRequest.php +++ /dev/null @@ -1,162 +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\Db; - - -use daita\MySmallPhpTools\Traits\TArrayTools; -use DateTime; -use Exception; -use OCA\Social\Exceptions\LikeDoesNotExistException; -use OCA\Social\Model\ActivityPub\Object\Like; -use OCP\DB\QueryBuilder\IQueryBuilder; - - -/** - * Class LikesRequest - * - * @package OCA\Social\Db - */ -class LikesRequest extends LikesRequestBuilder { - - - use TArrayTools; - - - /** - * Insert a new Note in the database. - * - * @param Like $like - */ - public function save(Like $like) { - $qb = $this->getLikesInsertSql(); - $qb->setValue('id', $qb->createNamedParameter($like->getId())) - ->setValue('actor_id', $qb->createNamedParameter($like->getActorId())) - ->setValue('type', $qb->createNamedParameter($like->getType())) - ->setValue('object_id', $qb->createNamedParameter($like->getObjectId())); - - try { - $qb->setValue( - 'creation', - $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) - ); - } catch (Exception $e) { - } - - $this->generatePrimaryKey($qb, $like->getId()); - - $qb->execute(); - } - - - /** - * @param string $actorId - * @param string $objectId - * - * @return Like - * @throws LikeDoesNotExistException - */ - public function getLike(string $actorId, string $objectId): Like { - $qb = $this->getLikesSelectSql(); - $this->limitToActorId($qb, $actorId); - $this->limitToObjectId($qb, $objectId); - - $cursor = $qb->execute(); - $data = $cursor->fetch(); - $cursor->closeCursor(); - if ($data === false) { - throw new LikeDoesNotExistException(); - } - - return $this->parseLikesSelectSql($data); - } - - - /** - * @param string $objectId - * - * @return int - */ - public function countLikes(string $objectId): int { - $qb = $this->countLikesSelectSql(); - $this->limitToObjectId($qb, $objectId); - - $cursor = $qb->execute(); - $data = $cursor->fetch(); - $cursor->closeCursor(); - - return $this->getInt('count', $data, 0); - } - - - /** - * @param string $objectId - * - * @return Like[] - */ - public function getByObjectId(string $objectId): array { - $qb = $this->getLikesSelectSql(); - $this->limitToObjectId($qb, $objectId); - $this->leftJoinCacheActors($qb, 'actor_id'); - - $likes = []; - $cursor = $qb->execute(); - while ($data = $cursor->fetch()) { - $likes[] = $this->parseLikesSelectSql($data); - } - $cursor->closeCursor(); - - return $likes; - } - - - /** - * @param Like $like - */ - public function delete(Like $like) { - $qb = $this->getLikesDeleteSql(); - $this->limitToIdString($qb, $like->getId()); - - $qb->execute(); - } - - - /** - * @param string $objectId - */ - public function deleteLikes(string $objectId) { - $qb = $this->getLikesDeleteSql(); - $this->limitToObjectId($qb, $objectId); - - $qb->execute(); - } - -} - diff --git a/lib/Db/LikesRequestBuilder.php b/lib/Db/LikesRequestBuilder.php deleted file mode 100644 index 64e83d74..00000000 --- a/lib/Db/LikesRequestBuilder.php +++ /dev/null @@ -1,146 +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\Db; - - -use daita\MySmallPhpTools\Traits\TArrayTools; -use OCA\Social\Exceptions\InvalidResourceException; -use OCA\Social\Model\ActivityPub\Object\Follow; -use OCA\Social\Model\ActivityPub\Object\Like; -use OCP\DB\QueryBuilder\IQueryBuilder; - - -/** - * Class LikesRequestBuilder - * - * @package OCA\Social\Db - */ -class LikesRequestBuilder extends CoreRequestBuilder { - - - use TArrayTools; - - - /** - * Base of the Sql Insert request - * - * @return IQueryBuilder - */ - protected function getLikesInsertSql(): IQueryBuilder { - $qb = $this->dbConnection->getQueryBuilder(); - $qb->insert(self::TABLE_LIKES); - - return $qb; - } - - - /** - * Base of the Sql Update request - * - * @return IQueryBuilder - */ - protected function getLikesUpdateSql(): IQueryBuilder { - $qb = $this->dbConnection->getQueryBuilder(); - $qb->update(self::TABLE_LIKES); - - return $qb; - } - - - /** - * Base of the Sql Select request for Shares - * - * @return IQueryBuilder - */ - protected function getLikesSelectSql(): IQueryBuilder { - $qb = $this->dbConnection->getQueryBuilder(); - - /** @noinspection PhpMethodParametersCountMismatchInspection */ - $qb->select('l.id', 'l.type', 'l.actor_id', 'l.object_id', 'l.creation') - ->from(self::TABLE_LIKES, 'l'); - - $this->defaultSelectAlias = 'l'; - - return $qb; - } - - - /** - * Base of the Sql Select request for Shares - * - * @return IQueryBuilder - */ - protected function countLikesSelectSql(): IQueryBuilder { - $qb = $this->dbConnection->getQueryBuilder(); - $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count') - ->from(self::TABLE_LIKES, 'l'); - - $this->defaultSelectAlias = 'l'; - - return $qb; - } - - - /** - * Base of the Sql Delete request - * - * @return IQueryBuilder - */ - protected function getLikesDeleteSql(): IQueryBuilder { - $qb = $this->dbConnection->getQueryBuilder(); - $qb->delete(self::TABLE_LIKES); - - return $qb; - } - - - /** - * @param array $data - * - * @return Like - */ - protected function parseLikesSelectSql($data): Like { - $like = new Like(); - $like->importFromDatabase($data); - - try { - $actor = $this->parseCacheActorsLeftJoin($data); - $actor->setCompleteDetails(true); - - $like->setActor($actor); - } catch (InvalidResourceException $e) { - } - - return $like; - } - -} - diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index 8c37f042..ad261abe 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -36,6 +36,7 @@ use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\AP; +use OCA\Social\Db\ActionsRequest; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; @@ -78,6 +79,9 @@ class AnnounceInterface implements IActivityPubInterface { /** @var StreamRequest */ private $streamRequest; + /** @var ActionsRequest */ + private $actionsRequest; + /** @var StreamQueueService */ private $streamQueueService; @@ -92,15 +96,18 @@ class AnnounceInterface implements IActivityPubInterface { * AnnounceInterface constructor. * * @param StreamRequest $streamRequest + * @param ActionsRequest $actionsRequest * @param StreamQueueService $streamQueueService * @param CacheActorService $cacheActorService * @param MiscService $miscService */ public function __construct( - StreamRequest $streamRequest, StreamQueueService $streamQueueService, - CacheActorService $cacheActorService, MiscService $miscService + StreamRequest $streamRequest, ActionsRequest $actionsRequest, + StreamQueueService $streamQueueService, CacheActorService $cacheActorService, + MiscService $miscService ) { $this->streamRequest = $streamRequest; + $this->actionsRequest = $actionsRequest; $this->streamQueueService = $streamQueueService; $this->cacheActorService = $cacheActorService; $this->miscService = $miscService; @@ -142,6 +149,7 @@ class AnnounceInterface implements IActivityPubInterface { /** @var Stream $item */ $item->checkOrigin($item->getId()); + $this->actionsRequest->save($item); $this->save($item); } @@ -182,12 +190,19 @@ class AnnounceInterface implements IActivityPubInterface { $actor = $this->cacheActorService->getFromId($item->getActorId()); } - $this->updateNotification($knownItem, $actor); if (!$knownItem->hasCc($actor->getFollowers())) { $knownItem->addCc($actor->getFollowers()); $this->streamRequest->update($knownItem); } + try { + $post = $this->streamRequest->getStreamById($item->getObjectId()); + } catch (StreamNotFoundException $e) { + return; // should not happens. + } + + $this->updateDetails($post); + $this->updateNotification($post, $actor); } catch (StreamNotFoundException $e) { $objectId = $item->getObjectId(); $item->addCacheItem($objectId); @@ -275,7 +290,9 @@ class AnnounceInterface implements IActivityPubInterface { $actor = $this->cacheActorService->getFromId($item->getActorId()); } - $this->updateNotification($item, $actor); + $post = $this->streamRequest->getStreamById($item->getObjectId()); + $this->updateDetails($post); + $this->updateNotification($post, $actor); } catch (Exception $e) { } @@ -285,14 +302,31 @@ class AnnounceInterface implements IActivityPubInterface { /** - * @param Stream $item - * + * @param Stream $post + */ + private function updateDetails(Stream $post) { + if (!$post->isLocal()) { + return; + } + + $post->setDetailInt( + 'boosts', $this->actionsRequest->countActions($post->getId(), Announce::TYPE) + ); + + $this->streamRequest->update($post); + } + + /** + * @param Stream $post * @param Person $author * * @throws ItemUnknownException * @throws SocialAppConfigException */ - private function updateNotification(Stream $item, Person $author) { + private function updateNotification(Stream $post, Person $author) { + if (!$post->isLocal()) { + return; + } /** @var SocialAppNotificationInterface $notificationInterface */ $notificationInterface = @@ -300,24 +334,13 @@ class AnnounceInterface implements IActivityPubInterface { try { $notification = $this->streamRequest->getStreamByObjectId( - $item->getId(), SocialAppNotification::TYPE + $post->getId(), SocialAppNotification::TYPE, Announce::TYPE ); $notification->addDetail('accounts', $author->getAccount()); $notificationInterface->update($notification); - } catch (StreamNotFoundException $e) { - try { - $post = $this->streamRequest->getStreamById($item->getObjectId()); - } catch (StreamNotFoundException $e) { - return; // should not happens. - } - - if (!$post->isLocal()) { - return; - } - /** @var SocialAppNotification $notification */ $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); // $notification->setDetail('url', ''); @@ -325,10 +348,10 @@ class AnnounceInterface implements IActivityPubInterface { $notification->addDetail('accounts', $author->getAccount()); $notification->setAttributedTo($author->getId()) ->setSubType(Announce::TYPE) - ->setId($item->getId() . '/notification') + ->setId($post->getId() . '/notification+boost') ->setSummary('{accounts} boosted your post') - ->setObjectId($item->getId()) - ->setTo($item->getAttributedTo()) + ->setObjectId($post->getId()) + ->setTo($post->getAttributedTo()) ->setLocal(true); $notificationInterface->save($notification); diff --git a/lib/Interfaces/Object/LikeInterface.php b/lib/Interfaces/Object/LikeInterface.php index 658955f2..65579e87 100644 --- a/lib/Interfaces/Object/LikeInterface.php +++ b/lib/Interfaces/Object/LikeInterface.php @@ -33,7 +33,7 @@ namespace OCA\Social\Interfaces\Object; use Exception; use OCA\Social\AP; -use OCA\Social\Db\LikesRequest; +use OCA\Social\Db\ActionsRequest; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\ItemNotFoundException; @@ -48,6 +48,7 @@ use OCA\Social\Model\ActivityPub\Activity\Undo; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification; use OCA\Social\Model\ActivityPub\Object\Like; +use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\MiscService; @@ -59,8 +60,8 @@ use OCA\Social\Service\MiscService; */ class LikeInterface implements IActivityPubInterface { - /** @var LikesRequest */ - private $likesRequest; + /** @var ActionsRequest */ + private $actionsRequest; /** @var StreamRequest */ private $streamRequest; @@ -75,16 +76,16 @@ class LikeInterface implements IActivityPubInterface { /** * LikeService constructor. * - * @param LikesRequest $likesRequest + * @param ActionsRequest $actionsRequest * @param StreamRequest $streamRequest * @param CacheActorService $cacheActorService * @param MiscService $miscService */ public function __construct( - LikesRequest $likesRequest, StreamRequest $streamRequest, + ActionsRequest $actionsRequest, StreamRequest $streamRequest, CacheActorService $cacheActorService, MiscService $miscService ) { - $this->likesRequest = $likesRequest; + $this->actionsRequest = $actionsRequest; $this->streamRequest = $streamRequest; $this->cacheActorService = $cacheActorService; $this->miscService = $miscService; @@ -101,9 +102,9 @@ class LikeInterface implements IActivityPubInterface { $like->checkOrigin($like->getActorId()); try { - $this->likesRequest->getLike($like->getActorId(), $like->getObjectId()); + $this->actionsRequest->getAction($like->getActorId(), $like->getObjectId(), Like::TYPE); } catch (LikeDoesNotExistException $e) { - $this->likesRequest->save($like); + $this->actionsRequest->save($like); try { if ($like->hasActor()) { @@ -112,7 +113,14 @@ class LikeInterface implements IActivityPubInterface { $actor = $this->cacheActorService->getFromId($like->getActorId()); } - $this->generateNotification($like, $actor); + try { + $post = $this->streamRequest->getStreamById($like->getObjectId()); + $this->updateDetails($post); + $this->generateNotification($post, $actor); + } catch (StreamNotFoundException $e) { + return; // should not happens. + } + } catch (Exception $e) { } } @@ -177,40 +185,51 @@ class LikeInterface implements IActivityPubInterface { if ($activity->getType() === Undo::TYPE) { $activity->checkOrigin($item->getId()); $activity->checkOrigin($item->getActorId()); - $this->likesRequest->delete($item); + $this->actionsRequest->delete($item); + } + } + + + /** + * @param Stream $post + */ + private function updateDetails(Stream $post) { + if (!$post->isLocal()) { + return; } + + $post->setDetailInt( + 'likes', $this->actionsRequest->countActions($post->getId(), Like::TYPE) + ); + + $this->streamRequest->update($post); } /** - * @param Like $like + * @param Stream $post * @param Person $author * * @throws ItemUnknownException * @throws SocialAppConfigException */ - private function generateNotification(Like $like, Person $author) { + private function generateNotification(Stream $post, Person $author) { + if (!$post->isLocal()) { + return; + } + /** @var SocialAppNotificationInterface $notificationInterface */ $notificationInterface = AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); try { $notification = $this->streamRequest->getStreamByObjectId( - $like->getObjectId(), SocialAppNotification::TYPE, Like::TYPE + $post->getId(), SocialAppNotification::TYPE, Like::TYPE ); $notification->addDetail('accounts', $author->getAccount()); $notificationInterface->update($notification); } catch (StreamNotFoundException $e) { - try { - $post = $this->streamRequest->getStreamById($like->getObjectId()); - } catch (StreamNotFoundException $e) { - return; // should not happens. - } - - if (!$post->isLocal()) { - return; - } /** @var SocialAppNotification $notification */ $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); @@ -219,9 +238,9 @@ class LikeInterface implements IActivityPubInterface { $notification->addDetail('accounts', $author->getAccount()); $notification->setAttributedTo($author->getId()) ->setSubType(Like::TYPE) - ->setId($like->getObjectId() . '/like') + ->setId($post->getId() . '/notification+like') ->setSummary('{accounts} liked your post') - ->setObjectId($like->getObjectId()) + ->setObjectId($post->getId()) ->setTo($post->getAttributedTo()) ->setLocal(true); diff --git a/lib/Migration/Version0002Date20190628000001.php b/lib/Migration/Version0002Date20190628000001.php deleted file mode 100644 index 999bfdbb..00000000 --- a/lib/Migration/Version0002Date20190628000001.php +++ /dev/null @@ -1,159 +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\Migration; - - -use Closure; -use Doctrine\DBAL\Schema\SchemaException; -use Doctrine\DBAL\Types\Type; -use Exception; -use OCP\DB\ISchemaWrapper; -use OCP\IDBConnection; -use OCP\Migration\IOutput; -use OCP\Migration\SimpleMigrationStep; - - -/** - * Class Version0002Date20190226000001 - * - * @package OCA\Social\Migration - */ -class Version0002Date20190628000001 extends SimpleMigrationStep { - - - /** @var IDBConnection */ - private $connection; - - - /** - * @param IDBConnection $connection - */ - public function __construct(IDBConnection $connection) { - $this->connection = $connection; - } - - - /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options - * - * @return ISchemaWrapper - * @throws SchemaException - */ - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options - ): ISchemaWrapper { - /** @var ISchemaWrapper $schema */ - $schema = $schemaClosure(); - if ($schema->hasTable('social_a2_stream')) { - $table = $schema->getTable('social_a2_stream'); - if (!$table->hasColumn('subtype')) { - $table->addColumn( - 'subtype', Type::STRING, - [ - 'notnull' => false, - 'length' => 31, - ] - ); - } - } - - - if (!$schema->hasTable('social_a2_likes')) { - $table = $schema->createTable('social_a2_likes'); - - $table->addColumn( - 'id', 'string', - [ - 'notnull' => false, - 'length' => 1000 - ] - ); - $table->addColumn( - 'id_prim', 'string', - [ - 'notnull' => false, - 'length' => 128 - ] - ); - $table->addColumn( - 'type', 'string', - [ - 'notnull' => false, - 'length' => 31, - ] - ); - $table->addColumn( - 'actor_id', 'string', - [ - 'notnull' => true, - 'length' => 1000, - ] - ); - $table->addColumn( - 'object_id', 'string', - [ - 'notnull' => true, - 'length' => 1000, - ] - ); - $table->addColumn( - 'creation', 'datetime', - [ - 'notnull' => false, - ] - ); - - $table->setPrimaryKey(['id_prim']); - } - - return $schema; - } - - - /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options - * - * @throws Exception - */ - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { - $qb = $this->connection->getQueryBuilder(); - $qb->delete('social_a2_stream'); - $expr = $qb->expr(); - $qb->where($expr->eq('type', $qb->createNamedParameter('SocialAppNotification'))); - - $qb->execute(); - } - -} - diff --git a/lib/Migration/Version0002Date20190629000001.php b/lib/Migration/Version0002Date20190629000001.php new file mode 100644 index 00000000..dbed67f4 --- /dev/null +++ b/lib/Migration/Version0002Date20190629000001.php @@ -0,0 +1,159 @@ + + * @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\Migration; + + +use Closure; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Types\Type; +use Exception; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + + +/** + * Class Version0002Date20190226000001 + * + * @package OCA\Social\Migration + */ +class Version0002Date20190629000001 extends SimpleMigrationStep { + + + /** @var IDBConnection */ + private $connection; + + + /** + * @param IDBConnection $connection + */ + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @return ISchemaWrapper + * @throws SchemaException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options + ): ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + if ($schema->hasTable('social_a2_stream')) { + $table = $schema->getTable('social_a2_stream'); + if (!$table->hasColumn('subtype')) { + $table->addColumn( + 'subtype', Type::STRING, + [ + 'notnull' => false, + 'length' => 31, + ] + ); + } + } + + + if (!$schema->hasTable('social_a2_actions')) { + $table = $schema->createTable('social_a2_actions'); + + $table->addColumn( + 'id', 'string', + [ + 'notnull' => false, + 'length' => 1000 + ] + ); + $table->addColumn( + 'id_prim', 'string', + [ + 'notnull' => false, + 'length' => 128 + ] + ); + $table->addColumn( + 'type', 'string', + [ + 'notnull' => false, + 'length' => 31, + ] + ); + $table->addColumn( + 'actor_id', 'string', + [ + 'notnull' => true, + 'length' => 1000, + ] + ); + $table->addColumn( + 'object_id', 'string', + [ + 'notnull' => true, + 'length' => 1000, + ] + ); + $table->addColumn( + 'creation', 'datetime', + [ + 'notnull' => false, + ] + ); + + $table->setPrimaryKey(['id_prim']); + } + + return $schema; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @throws Exception + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + $qb = $this->connection->getQueryBuilder(); + $qb->delete('social_a2_stream'); + $expr = $qb->expr(); + $qb->where($expr->eq('type', $qb->createNamedParameter('SocialAppNotification'))); + + $qb->execute(); + } + +} + -- cgit v1.2.3