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/Interfaces/Object/AnnounceInterface.php | 67 +++++++++++++++++++---------- lib/Interfaces/Object/LikeInterface.php | 67 ++++++++++++++++++----------- 2 files changed, 88 insertions(+), 46 deletions(-) (limited to 'lib/Interfaces') 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); -- cgit v1.2.3