diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-01-20 21:35:24 -0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-20 21:35:24 -0100 |
commit | 1486e6454a13790c33c4dfea6946b795fbc00c69 (patch) | |
tree | 6d52ca8d417caccc36b7553c37340e43f712272b /lib/Service | |
parent | 4fdacb38dc692e5d8bced4967322eb4613652f37 (diff) | |
parent | 03bfa7f974e9960865e5396b2edf38ed35890cc5 (diff) |
Merge pull request #340 from nextcloud/feature/333/refresh-detail-cache-on-new-follows
Refresh some Actor details in cache on Follow events
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/AccountService.php | 45 | ||||
-rw-r--r-- | lib/Service/ActivityService.php | 2 | ||||
-rw-r--r-- | lib/Service/ActorService.php | 22 | ||||
-rw-r--r-- | lib/Service/NoteService.php | 14 | ||||
-rw-r--r-- | lib/Service/PostService.php | 43 |
5 files changed, 78 insertions, 48 deletions
diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index f94bd965..497b732b 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -195,6 +195,7 @@ class AccountService { * @throws NoUserException * @throws SocialAppConfigException * @throws UrlCloudException + * @throws ItemUnknownException */ public function createActor(string $userId, string $username) { @@ -225,18 +226,18 @@ class AccountService { $this->actorsRequest->create($actor); // generate cache. - $this->cacheLocalActorByUsername($username, true); + $this->cacheLocalActorByUsername($username); } /** * @param string $username - * @param bool $refresh * * @throws SocialAppConfigException * @throws UrlCloudException + * @throws ItemUnknownException */ - public function cacheLocalActorByUsername(string $username, bool $refresh = false) { + public function cacheLocalActorByUsername(string $username) { try { $actor = $this->getActor($username); @@ -252,14 +253,8 @@ class AccountService { } catch (ItemUnknownException $e) { } - $count = [ - 'followers' => $this->followsRequest->countFollowers($actor->getId()), - 'following' => $this->followsRequest->countFollowing($actor->getId()), - 'post' => $this->notesRequest->countNotesFromActorId($actor->getId()) - ]; - $actor->addDetailArray('count', $count); - - $this->actorService->cacheLocalActor($actor, $refresh); + $this->addLocalActorDetailCount($actor); + $this->actorService->cacheLocalActor($actor); } catch (ActorDoesNotExistException $e) { } } @@ -267,6 +262,32 @@ class AccountService { /** * @param Person $actor + */ + public function cacheLocalActorDetailCount(Person $actor) { + if (!$actor->isLocal()) { + return; + } + + $this->addLocalActorDetailCount($actor); + $this->actorService->cacheLocalActor($actor); + } + + + /** + * @param Person $actor + */ + public function addLocalActorDetailCount(Person &$actor) { + $count = [ + 'followers' => $this->followsRequest->countFollowers($actor->getId()), + 'following' => $this->followsRequest->countFollowing($actor->getId()), + 'post' => $this->notesRequest->countNotesFromActorId($actor->getId()) + ]; + $actor->addDetailArray('count', $count); + } + + + /** + * @param Person $actor * * @throws NoUserException */ @@ -308,7 +329,7 @@ class AccountService { $update = $this->actorsRequest->getAll(); foreach ($update as $item) { try { - $this->cacheLocalActorByUsername($item->getPreferredUsername(), true); + $this->cacheLocalActorByUsername($item->getPreferredUsername()); } catch (Exception $e) { } } diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 904807cf..d89c5195 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -131,7 +131,7 @@ class ActivityService { * @param ACore $activity * * @return string - * @throws Exception + * @throws SocialAppConfigException */ public function createActivity(Person $actor, ACore $item, ACore &$activity = null): string { diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 8dea61bd..e15dbae5 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -33,6 +33,7 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Traits\TArrayTools; use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Db\CacheDocumentsRequest; +use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -106,17 +107,17 @@ class ActorService { /** * @param Person $actor - * @param bool $refresh */ - public function cacheLocalActor(Person $actor, bool $refresh = false) { - if ($refresh) { - $this->cacheActorsRequest->deleteFromId($actor->getId()); - } - + public function cacheLocalActor(Person $actor) { $actor->setLocal(true); $actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES)); - $this->save($actor); + try { + $this->cacheActorsRequest->getFromId($actor->getId()); + $this->update($actor); + } catch (CacheActorDoesNotExistException $e) { + $this->save($actor); + } } @@ -131,10 +132,13 @@ class ActorService { /** * @param Person $actor + * + * @return int */ - public function update(Person $actor) { + public function update(Person $actor): int { $this->cacheDocumentIfNeeded($actor); - $this->cacheActorsRequest->update($actor); + + return $this->cacheActorsRequest->update($actor); } diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index c832659c..8e444523 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -32,10 +32,7 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use Exception; -use OC\User\NoUserException; use OCA\Social\Db\NotesRequest; -use OCA\Social\Exceptions\AccountAlreadyExistsException; -use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; @@ -47,7 +44,6 @@ use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Note; @@ -117,22 +113,16 @@ class NoteService { /** - * @param string $userId + * @param Person $actor * @param string $content * * @param string $type * * @return Note - * @throws ActorDoesNotExistException - * @throws NoUserException * @throws SocialAppConfigException - * @throws AccountAlreadyExistsException - * @throws UrlCloudException */ - public function generateNote(string $userId, string $content, string $type) { + public function generateNote(Person $actor, string $content, string $type) { $note = new Note(); - $actor = $this->accountService->getActorFromUserId($userId); - $note->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); $note->setPublished(date("c")); $note->setAttributedTo( diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index b2f39ac8..89c0f603 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -30,9 +30,16 @@ declare(strict_types=1); namespace OCA\Social\Service; -use Exception; -use OC\User\NoUserException; -use OCA\Social\Exceptions\ActorDoesNotExistException; +use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use OCA\Social\Exceptions\InvalidOriginException; +use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Exceptions\ItemUnknownException; +use OCA\Social\Exceptions\NoteNotFoundException; +use OCA\Social\Exceptions\RedundancyLimitException; +use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultSizeException; +use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\Post; @@ -44,7 +51,7 @@ class PostService { private $noteService; /** @var AccountService */ - private $actorService; + private $accountService; /** @var ActivityService */ private $activityService; @@ -57,16 +64,16 @@ class PostService { * PostService constructor. * * @param NoteService $noteService - * @param AccountService $actorService + * @param AccountService $accountService * @param ActivityService $activityService * @param MiscService $miscService */ public function __construct( - NoteService $noteService, AccountService $actorService, ActivityService $activityService, + NoteService $noteService, AccountService $accountService, ActivityService $activityService, MiscService $miscService ) { $this->noteService = $noteService; - $this->actorService = $actorService; + $this->accountService = $accountService; $this->activityService = $activityService; $this->miscService = $miscService; } @@ -77,23 +84,31 @@ class PostService { * @param ACore $activity * * @return string - * @throws ActorDoesNotExistException - * @throws NoUserException * @throws SocialAppConfigException - * @throws Exception + * @throws InvalidOriginException + * @throws InvalidResourceException + * @throws ItemUnknownException + * @throws NoteNotFoundException + * @throws RedundancyLimitException + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultSizeException + * @throws RequestServerException + * @throws MalformedArrayException */ public function createPost(Post $post, ACore &$activity = null): string { $note = $this->noteService->generateNote( - $post->getUserId(), htmlentities($post->getContent(), ENT_QUOTES), $post->getType() + $post->getActor(), htmlentities($post->getContent(), ENT_QUOTES), $post->getType() ); - + $this->noteService->replyTo($note, $post->getReplyTo()); $this->noteService->addRecipients($note, $post->getType(), $post->getTo()); - $actor = $this->actorService->getActorFromUserId($post->getUserId()); + $result = $this->activityService->createActivity($post->getActor(), $note, $activity); + $this->accountService->cacheLocalActorDetailCount($post->getActor()); - return $this->activityService->createActivity($actor, $note, $activity); + return $result; } |