From 3343f6e9e4b04b4475baab2cfbb2ebd7cd7f4b89 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 28 Jun 2019 09:47:44 -0100 Subject: [PATCH] notification on boost Signed-off-by: Maxence Lange --- lib/Db/StreamRequest.php | 1 - .../SocialAppNotificationInterface.php | 9 +- lib/Interfaces/Object/AnnounceInterface.php | 107 +++++++++++++++++- lib/Interfaces/Object/FollowInterface.php | 18 +-- lib/Traits/TDetails.php | 10 ++ 5 files changed, 133 insertions(+), 12 deletions(-) diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index 9eda3647..a32ff935 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -40,7 +40,6 @@ use OCA\Social\Exceptions\DateTimeException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; -use OCA\Social\Interfaces\Internal\SocialAppNotificationInterface; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification; diff --git a/lib/Interfaces/Internal/SocialAppNotificationInterface.php b/lib/Interfaces/Internal/SocialAppNotificationInterface.php index ed4f5028..72e99069 100644 --- a/lib/Interfaces/Internal/SocialAppNotificationInterface.php +++ b/lib/Interfaces/Internal/SocialAppNotificationInterface.php @@ -129,9 +129,14 @@ class SocialAppNotificationInterface implements IActivityPubInterface { /** - * @param ACore $item + * @param ACore $notification */ - public function update(ACore $item) { + public function update(ACore $notification) { + /** @var SocialAppNotification $notification */ + $this->miscService->log( + 'Updating notification: ' . json_encode($notification, JSON_UNESCAPED_SLASHES), 1 + ); + $this->streamRequest->update($notification); } diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index 4cbb095f..d45d4864 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -32,17 +32,30 @@ namespace OCA\Social\Interfaces\Object; use daita\MySmallPhpTools\Exceptions\CacheItemNotFoundException; +use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; +use OCA\Social\AP; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; +use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; +use OCA\Social\Exceptions\RedundancyLimitException; +use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; +use OCA\Social\Exceptions\RequestResultSizeException; +use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; +use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Interfaces\Internal\SocialAppNotificationInterface; use OCA\Social\Model\ActivityPub\ACore; 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\Announce; use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Model\StreamQueue; @@ -99,6 +112,15 @@ class AnnounceInterface implements IActivityPubInterface { * @param ACore $item * * @throws InvalidOriginException + * @throws InvalidResourceException + * @throws MalformedArrayException + * @throws RedundancyLimitException + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultNotJsonException + * @throws RequestResultSizeException + * @throws RequestServerException + * @throws UnauthorizedFediverseException */ public function activity(Acore $activity, ACore $item) { $item->checkOrigin($activity->getId()); @@ -148,6 +170,7 @@ class AnnounceInterface implements IActivityPubInterface { * @throws Exception */ public function save(ACore $item) { + /** @var Announce $item */ try { $knownItem = @@ -159,11 +182,12 @@ 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); } - + } catch (StreamNotFoundException $e) { $objectId = $item->getObjectId(); $item->addCacheItem($objectId); @@ -173,6 +197,7 @@ class AnnounceInterface implements IActivityPubInterface { $item->getRequestToken(), StreamQueue::TYPE_CACHE, $item->getId() ); } + } @@ -185,13 +210,29 @@ class AnnounceInterface implements IActivityPubInterface { /** * @param ACore $item + * + * @throws InvalidOriginException + * @throws InvalidResourceException + * @throws RedundancyLimitException + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultNotJsonException + * @throws RequestResultSizeException + * @throws RequestServerException + * @throws UnauthorizedFediverseException + * @throws MalformedArrayException */ public function delete(ACore $item) { try { $knownItem = $this->streamRequest->getStreamByObjectId($item->getObjectId(), Announce::TYPE); - $actor = $item->getActor(); + if ($item->hasActor()) { + $actor = $item->getActor(); + } else { + $actor = $this->cacheActorService->getFromId($item->getActorId()); + } + $knownItem->removeCc($actor->getFollowers()); if (empty($knownItem->getCcArray())) { @@ -227,9 +268,71 @@ class AnnounceInterface implements IActivityPubInterface { $this->streamRequest->updateAttributedTo($item->getId(), $to); } + try { + if ($item->hasActor()) { + $actor = $item->getActor(); + } else { + $actor = $this->cacheActorService->getFromId($item->getActorId()); + } + + $this->updateNotification($item, $actor); + } catch (Exception $e) { + } + break; } } + + /** + * @param Stream $item + * + * @param Person $author + * + * @throws ItemUnknownException + * @throws SocialAppConfigException + */ + private function updateNotification(Stream $item, Person $author) { + + /** @var SocialAppNotificationInterface $notificationInterface */ + $notificationInterface = + AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); + + try { + $notification = $this->streamRequest->getStreamByObjectId( + $item->getId(), SocialAppNotification::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', ''); + $notification->setDetailItem('post', $post); + $notification->addDetail('accounts', $author->getAccount()); + $notification->setAttributedTo($author->getId()) + ->setId($item->getId() . '/notification') + ->setSummary('{accounts} boosted your post') + ->setObjectId($item->getId()) + ->setTo($item->getAttributedTo()) + ->setLocal(true); + + $notificationInterface->save($notification); + } + } + } diff --git a/lib/Interfaces/Object/FollowInterface.php b/lib/Interfaces/Object/FollowInterface.php index 1123e2e8..d7f216ea 100644 --- a/lib/Interfaces/Object/FollowInterface.php +++ b/lib/Interfaces/Object/FollowInterface.php @@ -53,7 +53,6 @@ use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Activity\Accept; use OCA\Social\Model\ActivityPub\Activity\Reject; 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\Follow; use OCA\Social\Model\InstancePath; @@ -147,7 +146,7 @@ class FollowInterface implements IActivityPubInterface { $actor = $this->cacheActorService->getFromId($follow->getObjectId()); $this->accountService->cacheLocalActorDetailCount($actor); - $this->generateNotification($actor, $follow); + $this->generateNotification($follow); } catch (Exception $e) { $this->miscService->log( 'exception while confirmFollowRequest: ' . get_class($e) . ' - ' . $e->getMessage(), @@ -267,24 +266,29 @@ class FollowInterface implements IActivityPubInterface { /** - * @param Person $actor * @param Follow $follow * * @throws ItemUnknownException * @throws SocialAppConfigException */ - private function generateNotification(Person $actor, Follow $follow) { + private function generateNotification(Follow $follow) { /** @var SocialAppNotificationInterface $notificationInterface */ $notificationInterface = AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); + try { + $follower = $this->cacheActorService->getFromId($follow->getActorId()); + } catch (Exception $e) { + return; + } + /** @var SocialAppNotification $notification */ $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); - $notification->setDetail('account', $actor->getAccount()); - $notification->setDetail('url', $actor->getId()); + $notification->setDetail('url', $follower->getId()); + $notification->setDetail('account', $follower->getAccount()); $notification->setAttributedTo($follow->getActorId()) ->setId($follow->getId() . '/notification') - ->setActorId($actor->getId()) + ->setActorId($follower->getId()) ->setSummary('{account} is following you') ->setTo($follow->getObjectId()) ->setLocal(true); diff --git a/lib/Traits/TDetails.php b/lib/Traits/TDetails.php index d26a360e..00a9602e 100644 --- a/lib/Traits/TDetails.php +++ b/lib/Traits/TDetails.php @@ -31,6 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Traits; +use OCA\Social\Model\ActivityPub\Item; + /** * Trait TDetails * @@ -89,6 +91,14 @@ trait TDetails { $this->details[$detail] = $value; } + /** + * @param string $detail + * @param Item $value + */ + public function setDetailItem(string $detail, Item $value) { + $this->details[$detail] = $value; + } + /** * @param string $detail