diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-06-29 19:35:41 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2019-06-29 19:35:41 -0100 |
commit | 882a6f57937ed949ea2bff8ae18c8707b8a31bee (patch) | |
tree | 4a3dd6e0697eae7374a5e1d2ee261883dc9c3fd6 /lib | |
parent | 06a2eb7629aeb56d4f32d544c8753d77fdc10326 (diff) |
likes -> actions
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Db/ActionsRequest.php (renamed from lib/Db/LikesRequest.php) | 36 | ||||
-rw-r--r-- | lib/Db/ActionsRequestBuilder.php (renamed from lib/Db/LikesRequestBuilder.php) | 45 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 2 | ||||
-rw-r--r-- | lib/Interfaces/Object/AnnounceInterface.php | 67 | ||||
-rw-r--r-- | lib/Interfaces/Object/LikeInterface.php | 67 | ||||
-rw-r--r-- | lib/Migration/Version0002Date20190629000001.php (renamed from lib/Migration/Version0002Date20190628000001.php) | 6 |
6 files changed, 135 insertions, 88 deletions
diff --git a/lib/Db/LikesRequest.php b/lib/Db/ActionsRequest.php index a03b448c..b102362d 100644 --- a/lib/Db/LikesRequest.php +++ b/lib/Db/ActionsRequest.php @@ -35,16 +35,17 @@ 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 LikesRequest + * Class ActionsRequest * * @package OCA\Social\Db */ -class LikesRequest extends LikesRequestBuilder { +class ActionsRequest extends ActionsRequestBuilder { use TArrayTools; @@ -53,10 +54,10 @@ class LikesRequest extends LikesRequestBuilder { /** * Insert a new Note in the database. * - * @param Like $like + * @param ACore $like */ - public function save(Like $like) { - $qb = $this->getLikesInsertSql(); + 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())) @@ -80,13 +81,16 @@ class LikesRequest extends LikesRequestBuilder { * @param string $actorId * @param string $objectId * - * @return Like + * @param string $type + * + * @return ACore * @throws LikeDoesNotExistException */ - public function getLike(string $actorId, string $objectId): Like { - $qb = $this->getLikesSelectSql(); + 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(); @@ -95,18 +99,20 @@ class LikesRequest extends LikesRequestBuilder { throw new LikeDoesNotExistException(); } - return $this->parseLikesSelectSql($data); + return $this->parseActionsSelectSql($data); } /** * @param string $objectId + * @param string $type * * @return int */ - public function countLikes(string $objectId): int { - $qb = $this->countLikesSelectSql(); + 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(); @@ -122,14 +128,14 @@ class LikesRequest extends LikesRequestBuilder { * @return Like[] */ public function getByObjectId(string $objectId): array { - $qb = $this->getLikesSelectSql(); + $qb = $this->getActionsSelectSql(); $this->limitToObjectId($qb, $objectId); $this->leftJoinCacheActors($qb, 'actor_id'); $likes = []; $cursor = $qb->execute(); while ($data = $cursor->fetch()) { - $likes[] = $this->parseLikesSelectSql($data); + $likes[] = $this->parseActionsSelectSql($data); } $cursor->closeCursor(); @@ -141,7 +147,7 @@ class LikesRequest extends LikesRequestBuilder { * @param Like $like */ public function delete(Like $like) { - $qb = $this->getLikesDeleteSql(); + $qb = $this->getActionsDeleteSql(); $this->limitToIdString($qb, $like->getId()); $qb->execute(); @@ -152,7 +158,7 @@ class LikesRequest extends LikesRequestBuilder { * @param string $objectId */ public function deleteLikes(string $objectId) { - $qb = $this->getLikesDeleteSql(); + $qb = $this->getActionsDeleteSql(); $this->limitToObjectId($qb, $objectId); $qb->execute(); diff --git a/lib/Db/LikesRequestBuilder.php b/lib/Db/ActionsRequestBuilder.php index 64e83d74..a72ae102 100644 --- a/lib/Db/LikesRequestBuilder.php +++ b/lib/Db/ActionsRequestBuilder.php @@ -33,17 +33,16 @@ 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 OCA\Social\Model\ActivityPub\ACore; use OCP\DB\QueryBuilder\IQueryBuilder; /** - * Class LikesRequestBuilder + * Class ActionsRequestBuilder * * @package OCA\Social\Db */ -class LikesRequestBuilder extends CoreRequestBuilder { +class ActionsRequestBuilder extends CoreRequestBuilder { use TArrayTools; @@ -54,9 +53,9 @@ class LikesRequestBuilder extends CoreRequestBuilder { * * @return IQueryBuilder */ - protected function getLikesInsertSql(): IQueryBuilder { + protected function getActionsInsertSql(): IQueryBuilder { $qb = $this->dbConnection->getQueryBuilder(); - $qb->insert(self::TABLE_LIKES); + $qb->insert(self::TABLE_ACTIONS); return $qb; } @@ -67,9 +66,9 @@ class LikesRequestBuilder extends CoreRequestBuilder { * * @return IQueryBuilder */ - protected function getLikesUpdateSql(): IQueryBuilder { + protected function getActionsUpdateSql(): IQueryBuilder { $qb = $this->dbConnection->getQueryBuilder(); - $qb->update(self::TABLE_LIKES); + $qb->update(self::TABLE_ACTIONS); return $qb; } @@ -80,14 +79,14 @@ class LikesRequestBuilder extends CoreRequestBuilder { * * @return IQueryBuilder */ - protected function getLikesSelectSql(): IQueryBuilder { + protected function getActionsSelectSql(): 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'); + $qb->select('a.id', 'a.type', 'a.actor_id', 'a.object_id', 'a.creation') + ->from(self::TABLE_ACTIONS, 'a'); - $this->defaultSelectAlias = 'l'; + $this->defaultSelectAlias = 'a'; return $qb; } @@ -98,12 +97,12 @@ class LikesRequestBuilder extends CoreRequestBuilder { * * @return IQueryBuilder */ - protected function countLikesSelectSql(): IQueryBuilder { + protected function countActionsSelectSql(): IQueryBuilder { $qb = $this->dbConnection->getQueryBuilder(); $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count') - ->from(self::TABLE_LIKES, 'l'); + ->from(self::TABLE_ACTIONS, 'a'); - $this->defaultSelectAlias = 'l'; + $this->defaultSelectAlias = 'a'; return $qb; } @@ -114,9 +113,9 @@ class LikesRequestBuilder extends CoreRequestBuilder { * * @return IQueryBuilder */ - protected function getLikesDeleteSql(): IQueryBuilder { + protected function getActionsDeleteSql(): IQueryBuilder { $qb = $this->dbConnection->getQueryBuilder(); - $qb->delete(self::TABLE_LIKES); + $qb->delete(self::TABLE_ACTIONS); return $qb; } @@ -125,21 +124,21 @@ class LikesRequestBuilder extends CoreRequestBuilder { /** * @param array $data * - * @return Like + * @return ACore */ - protected function parseLikesSelectSql($data): Like { - $like = new Like(); - $like->importFromDatabase($data); + protected function parseActionsSelectSql($data): ACore { + $item = new ACore(); + $item->importFromDatabase($data); try { $actor = $this->parseCacheActorsLeftJoin($data); $actor->setCompleteDetails(true); - $like->setActor($actor); + $item->setActor($actor); } catch (InvalidResourceException $e) { } - return $like; + 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/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/Version0002Date20190629000001.php index 999bfdbb..dbed67f4 100644 --- a/lib/Migration/Version0002Date20190628000001.php +++ b/lib/Migration/Version0002Date20190629000001.php @@ -46,7 +46,7 @@ use OCP\Migration\SimpleMigrationStep; * * @package OCA\Social\Migration */ -class Version0002Date20190628000001 extends SimpleMigrationStep { +class Version0002Date20190629000001 extends SimpleMigrationStep { /** @var IDBConnection */ @@ -87,8 +87,8 @@ class Version0002Date20190628000001 extends SimpleMigrationStep { } - if (!$schema->hasTable('social_a2_likes')) { - $table = $schema->createTable('social_a2_likes'); + if (!$schema->hasTable('social_a2_actions')) { + $table = $schema->createTable('social_a2_actions'); $table->addColumn( 'id', 'string', |