diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-01-21 10:17:47 -0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-21 10:17:47 -0100 |
commit | 43e68897203a39bb6ba37bef5d0ca8d95d740a4a (patch) | |
tree | c7a39951bb970e556ade8022694401afe78a42f7 | |
parent | 753a04fe90e0d563ba88147b6fa5d69906137c52 (diff) | |
parent | 6208d51a85f1a7d647bd2fedec8f696227b48fcc (diff) |
Merge pull request #354 from nextcloud/backport/340/alpha1
[alpha1] Refresh some Actor details in cache on Follow events
-rw-r--r-- | lib/Command/CacheRefresh.php | 12 | ||||
-rw-r--r-- | lib/Command/NoteCreate.php | 11 | ||||
-rw-r--r-- | lib/Controller/LocalController.php | 10 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequest.php | 7 | ||||
-rw-r--r-- | lib/Interfaces/Activity/FollowInterface.php | 18 | ||||
-rw-r--r-- | lib/Model/Post.php | 19 | ||||
-rw-r--r-- | lib/Service/AccountService.php | 46 | ||||
-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 |
11 files changed, 127 insertions, 77 deletions
diff --git a/lib/Command/CacheRefresh.php b/lib/Command/CacheRefresh.php index c90355a3..1ea3f9fa 100644 --- a/lib/Command/CacheRefresh.php +++ b/lib/Command/CacheRefresh.php @@ -46,7 +46,7 @@ class CacheRefresh extends Base { /** @var AccountService */ - private $actorService; + private $accountService; /** @var CacheActorService */ private $cacheActorService; @@ -64,19 +64,19 @@ class CacheRefresh extends Base { /** * CacheUpdate constructor. * - * @param AccountService $actorService + * @param AccountService $accountService * @param CacheActorService $cacheActorService * @param DocumentService $documentService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - AccountService $actorService, CacheActorService $cacheActorService, + AccountService $accountService, CacheActorService $cacheActorService, DocumentService $documentService, ConfigService $configService, MiscService $miscService ) { parent::__construct(); - $this->actorService = $actorService; + $this->accountService = $accountService; $this->cacheActorService = $cacheActorService; $this->documentService = $documentService; $this->configService = $configService; @@ -102,10 +102,10 @@ class CacheRefresh extends Base { */ protected function execute(InputInterface $input, OutputInterface $output) { - $result = $this->actorService->blindKeyRotation(); + $result = $this->accountService->blindKeyRotation(); $output->writeLn($result . ' key pairs refreshed'); - $result = $this->actorService->manageCacheLocalActors(); + $result = $this->accountService->manageCacheLocalActors(); $output->writeLn($result . ' local accounts regenerated'); $result = $this->cacheActorService->missingCacheRemoteActors(); diff --git a/lib/Command/NoteCreate.php b/lib/Command/NoteCreate.php index 14216ea8..1d16de9d 100644 --- a/lib/Command/NoteCreate.php +++ b/lib/Command/NoteCreate.php @@ -54,7 +54,7 @@ class NoteCreate extends Base { private $activityService; /** @var AccountService */ - private $actorService; + private $accountService; /** @var PostService */ private $postService; @@ -70,21 +70,21 @@ class NoteCreate extends Base { * NoteCreate constructor. * * @param ActivityService $activityService - * @param AccountService $actorService + * @param AccountService $accountService * @param PostService $postService * @param CurlService $curlService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - ActivityService $activityService, AccountService $actorService, + ActivityService $activityService, AccountService $accountService, PostService $postService, CurlService $curlService, ConfigService $configService, MiscService $miscService ) { parent::__construct(); $this->activityService = $activityService; - $this->actorService = $actorService; + $this->accountService = $accountService; $this->postService = $postService; $this->curlService = $curlService; $this->configService = $configService; @@ -128,7 +128,8 @@ class NoteCreate extends Base { $replyTo = $input->getOption('replyTo'); $type = $input->getOption('type'); - $post = new Post($userId); + $actor = $this->accountService->getActorFromUserId($userId); + $post = new Post($actor); $post->setContent($content); $post->setType(($type === null) ? '' : $type); $post->setReplyTo(($replyTo === null) ? '' : $replyTo); diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index cd78c297..a65752b0 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -139,7 +139,9 @@ class LocalController extends Controller { */ public function postCreate(array $data): DataResponse { try { - $post = new Post($this->userId); + $actor = $this->accountService->getActorFromUserId($this->userId); + + $post = new Post($actor); $post->setContent($this->get('content', $data, '')); $post->setReplyTo($this->get('replyTo', $data, '')); $post->setTo($this->getArray('to', $data, [])); @@ -324,6 +326,7 @@ class LocalController extends Controller { try { $actor = $this->accountService->getActorFromUserId($this->userId); $this->followService->followAccount($actor, $account); + $this->accountService->cacheLocalActorDetailCount($actor); return $this->success([]); } catch (Exception $e) { @@ -343,6 +346,7 @@ class LocalController extends Controller { try { $actor = $this->accountService->getActorFromUserId($this->userId); $this->followService->unfollowAccount($actor, $account); + $this->accountService->cacheLocalActorDetailCount($actor); return $this->success([]); } catch (Exception $e) { @@ -397,9 +401,9 @@ class LocalController extends Controller { $this->initViewer(); $actor = $this->accountService->getActorFromUserId($this->userId); - $followers = $this->followService->getFollowing($actor); + $following = $this->followService->getFollowing($actor); - return $this->success($followers); + return $this->success($following); } catch (Exception $e) { return $this->fail($e); } diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 82a2f5de..8ea1438c 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -114,8 +114,10 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { * insert cache about an Actor in database. * * @param Person $actor + * + * @return int */ - public function update(Person $actor) { + public function update(Person $actor): int { if ($actor->getCreation() > 0) { $dTime = new DateTime(); @@ -155,7 +157,8 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { $qb->set('icon_id', $qb->createNamedParameter($iconId)); $this->limitToIdString($qb, $actor->getId()); - $qb->execute(); + + return $qb->execute(); } diff --git a/lib/Interfaces/Activity/FollowInterface.php b/lib/Interfaces/Activity/FollowInterface.php index d73070a0..da6ebd49 100644 --- a/lib/Interfaces/Activity/FollowInterface.php +++ b/lib/Interfaces/Activity/FollowInterface.php @@ -53,6 +53,7 @@ use OCA\Social\Model\ActivityPub\Activity\Follow; use OCA\Social\Model\ActivityPub\Activity\Reject; use OCA\Social\Model\ActivityPub\Activity\Undo; use OCA\Social\Model\InstancePath; +use OCA\Social\Service\AccountService; use OCA\Social\Service\ActivityService; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\ConfigService; @@ -68,6 +69,9 @@ class FollowInterface implements IActivityPubInterface { /** @var CacheActorService */ private $cacheActorService; + /** @var AccountService */ + private $accountService; + /** @var ActivityService */ private $activityService; @@ -83,16 +87,19 @@ class FollowInterface implements IActivityPubInterface { * * @param FollowsRequest $followsRequest * @param CacheActorService $cacheActorService + * @param AccountService $accountService * @param ActivityService $activityService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( FollowsRequest $followsRequest, CacheActorService $cacheActorService, - ActivityService $activityService, ConfigService $configService, MiscService $miscService + AccountService $accountService, ActivityService $activityService, + ConfigService $configService, MiscService $miscService ) { $this->followsRequest = $followsRequest; $this->cacheActorService = $cacheActorService; + $this->accountService = $accountService; $this->activityService = $activityService; $this->configService = $configService; $this->miscService = $miscService; @@ -127,8 +134,12 @@ class FollowInterface implements IActivityPubInterface { $this->activityService->request($accept); $this->followsRequest->accepted($follow); + + $actor = $this->cacheActorService->getFromId($follow->getObjectId()); + $this->accountService->cacheLocalActorDetailCount($actor); } catch (Exception $e) { } + } @@ -153,9 +164,8 @@ class FollowInterface implements IActivityPubInterface { $follow->checkOrigin($follow->getActorId()); try { - $knownFollow = $this->followsRequest->getByPersons( - $follow->getActorId(), $follow->getObjectId() - ); + $knownFollow = + $this->followsRequest->getByPersons($follow->getActorId(), $follow->getObjectId()); if ($knownFollow->getId() === $follow->getId() && !$knownFollow->isAccepted()) { $this->confirmFollowRequest($follow); diff --git a/lib/Model/Post.php b/lib/Model/Post.php index 422729cd..7ce1c6bd 100644 --- a/lib/Model/Post.php +++ b/lib/Model/Post.php @@ -32,6 +32,7 @@ namespace OCA\Social\Model; use daita\MySmallPhpTools\Traits\TArrayTools; use JsonSerializable; +use OCA\Social\Model\ActivityPub\Actor\Person; /** @@ -45,8 +46,8 @@ class Post implements JsonSerializable { use TArrayTools; - /** @var string */ - private $userId = ''; + /** @var Person */ + private $actor; /** @var array */ private $to = []; @@ -64,17 +65,17 @@ class Post implements JsonSerializable { /** * Post constructor. * - * @param string $userId + * @param Person $actor */ - public function __construct(string $userId = '') { - $this->userId = $userId; + public function __construct(Person $actor) { + $this->actor = $actor; } /** - * @return string + * @return Person */ - public function getUserId(): string { - return $this->userId; + public function getActor(): Person { + return $this->actor; } @@ -167,7 +168,7 @@ class Post implements JsonSerializable { */ public function jsonSerialize(): array { return [ - 'userId' => $this->getUserId(), + 'actor' => $this->getActor(), 'to' => $this->getTo(), 'replyTo' => $this->getReplyTo(), 'content' => $this->getContent(), diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index ff868350..dbc2d530 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -38,6 +38,7 @@ use OCA\Social\Db\FollowsRequest; use OCA\Social\Db\NotesRequest; use OCA\Social\Exceptions\AccountAlreadyExistsException; use OCA\Social\Exceptions\ActorDoesNotExistException; +use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -194,6 +195,7 @@ class AccountService { * @throws NoUserException * @throws SocialAppConfigException * @throws UrlCloudException + * @throws ItemUnknownException */ public function createActor(string $userId, string $username) { @@ -224,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); @@ -248,14 +250,8 @@ class AccountService { $iconId = $this->documentService->cacheLocalAvatarByUsername($actor); $actor->setIconId($iconId); - $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) { } } @@ -263,6 +259,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 */ @@ -304,7 +326,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 1e538cdf..5823f648 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -130,7 +130,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 373dab72..db6f5afc 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; @@ -46,7 +43,6 @@ use OCA\Social\Exceptions\RequestNetworkException; 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; @@ -116,22 +112,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; } |