diff --git a/appinfo/routes.php b/appinfo/routes.php index 3d371336..7fc4b1fb 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -75,7 +75,7 @@ return [ ['name' => 'Local#actionFollow', 'url' => '/api/v1/current/follow', 'verb' => 'PUT'], ['name' => 'Local#actionUnfollow', 'url' => '/api/v1/current/follow', 'verb' => 'DELETE'], - ['name' => 'Local#currentInfo', 'url' => '/api/v1/current/info', 'verb' => 'PUT'], + ['name' => 'Local#currentInfo', 'url' => '/api/v1/current/info', 'verb' => 'GET'], ['name' => 'Local#currentFollowers', 'url' => '/api/v1/current/followers', 'verb' => 'GET'], ['name' => 'Local#currentFollowing', 'url' => '/api/v1/current/following', 'verb' => 'GET'], diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 69bd259e..f6c02b64 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -407,7 +407,15 @@ class LocalController extends Controller { * @return DataResponse */ public function accountInfo(string $username): DataResponse { + try { + $viewer = $this->actorService->getActorFromUserId($this->userId, true); + $this->personService->setViewerId($viewer->getId()); + } catch (Exception $e) { + } + + try { + $actor = $this->actorService->getActor($username); $actor = $this->personService->getFromLocalAccount($actor->getPreferredUsername()); @@ -473,6 +481,12 @@ class LocalController extends Controller { * @return DataResponse */ public function globalAccountInfo(string $account): DataResponse { + try { + $viewer = $this->actorService->getActorFromUserId($this->userId, true); + $this->personService->setViewerId($viewer->getId()); + } catch (Exception $e) { + } + try { $actor = $this->personService->getFromAccount($account); diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index cc258235..b024392a 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -33,7 +33,6 @@ namespace OCA\Social\Db; use DateTime; use Exception; use OCA\Social\Exceptions\CacheActorDoesNotExistException; -use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; @@ -134,20 +133,14 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { * * @param string $account * - * @param string $viewerId - * * @return Person * @throws CacheActorDoesNotExistException */ - public function getFromAccount(string $account, string $viewerId = ''): Person { + public function getFromAccount(string $account): Person { $qb = $this->getCacheActorsSelectSql(); $this->limitToAccount($qb, $account); $this->leftJoinCacheDocuments($qb, 'icon_id'); - - if ($viewerId !== '') { - $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, true, 'as_follower'); - $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, false, 'as_followed'); - } + $this->leftJoinDetails($qb); $cursor = $qb->execute(); $data = $cursor->fetch(); @@ -157,21 +150,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { throw new CacheActorDoesNotExistException(); } - $account = $this->parseCacheActorsSelectSql($data); - - try { - $this->parseFollowLeftJoin($data, 'as_follower'); - $account->addDetailBool('following', true); - } catch (InvalidResourceException $e) { - } - - try { - $this->parseFollowLeftJoin($data, 'as_followed'); - $account->addDetailBool('followed', true); - } catch (InvalidResourceException $e) { - } - - return $account; + return $this->parseCacheActorsSelectSql($data); } @@ -188,6 +167,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { $this->limitToPreferredUsername($qb, $account); $this->limitToLocal($qb, true); $this->leftJoinCacheDocuments($qb, 'icon_id'); + $this->leftJoinDetails($qb); $cursor = $qb->execute(); $data = $cursor->fetch(); @@ -203,44 +183,19 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { /** * @param string $search - * @param string $viewerId * * @return Person[] */ - public function searchAccounts(string $search, string $viewerId = ''): array { + public function searchAccounts(string $search): array { $qb = $this->getCacheActorsSelectSql(); $this->searchInAccount($qb, $search); $this->leftJoinCacheDocuments($qb, 'icon_id'); - - if ($viewerId !== '') { - $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, true, 'as_follower'); - $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, false, 'as_followed'); - } + $this->leftJoinDetails($qb); $accounts = []; $cursor = $qb->execute(); while ($data = $cursor->fetch()) { - $account = $this->parseCacheActorsSelectSql($data); - - if ($viewerId !== '') { - try { - $this->parseFollowLeftJoin($data, 'as_follower'); - $account->addDetailBool('following', true); - } catch (InvalidResourceException $e) { - $account->addDetailBool('following', false); - } - - try { - $this->parseFollowLeftJoin($data, 'as_followed'); - $account->addDetailBool('followed', true); - } catch (InvalidResourceException $e) { - $account->addDetailBool('followed', false); - } - - $account->setCompleteDetails(true); - } - - $accounts[] = $account; + $accounts[] = $this->parseCacheActorsSelectSql($data); } $cursor->closeCursor(); diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php index cb9ce58a..d00bbd5c 100644 --- a/lib/Db/CacheActorsRequestBuilder.php +++ b/lib/Db/CacheActorsRequestBuilder.php @@ -119,8 +119,41 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder { } catch (InvalidResourceException $e) { } + $this->assignDetails($actor, $data); + return $actor; } + + protected function leftJoinDetails(IQueryBuilder $qb) { + $viewerId = $this->getViewerId(); + if ($viewerId !== '') { + $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, true, 'as_follower'); + $this->leftJoinFollowAsViewer($qb, 'id', $viewerId, false, 'as_followed'); + } + } + + + protected function assignDetails(Person $actor, array $data) { + if ($this->getViewerId() !== '') { + + try { + $this->parseFollowLeftJoin($data, 'as_follower'); + $actor->addDetailBool('following', true); + } catch (InvalidResourceException $e) { + $actor->addDetailBool('following', false); + } + + try { + $this->parseFollowLeftJoin($data, 'as_followed'); + $actor->addDetailBool('followed', true); + } catch (InvalidResourceException $e) { + $actor->addDetailBool('followed', false); + } + + $actor->setCompleteDetails(true); + } + } + } diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 114dc671..72fd8450 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -77,6 +77,9 @@ class CoreRequestBuilder { /** @var string */ protected $defaultSelectAlias; + /** @var string */ + private $viewerId = ''; + /** * CoreRequestBuilder constructor. @@ -94,6 +97,21 @@ class CoreRequestBuilder { } + /** + * @return string + */ + public function getViewerId(): string { + return $this->viewerId; + } + + /** + * @param string $viewerId + */ + public function setViewerId(string $viewerId) { + $this->viewerId = $viewerId; + } + + /** * Limit the request to the Id * diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index c6d9a074..59613c38 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -106,6 +106,7 @@ class PersonService implements ICoreService { */ public function setViewerId(string $viewerId) { $this->viewerId = $viewerId; + $this->cacheActorsRequest->setViewerId($viewerId); } public function getViewerId(): string { @@ -185,7 +186,7 @@ class PersonService implements ICoreService { public function getFromAccount(string $account, bool $retrieve = true): Person { try { - $actor = $this->cacheActorsRequest->getFromAccount($account, $this->getViewerId()); + $actor = $this->cacheActorsRequest->getFromAccount($account); } catch (CacheActorDoesNotExistException $e) { if (!$retrieve) { throw new CacheActorDoesNotExistException(); @@ -245,7 +246,7 @@ class PersonService implements ICoreService { * @return Person[] */ public function searchCachedAccounts(string $search): array { - return $this->cacheActorsRequest->searchAccounts($search, $this->getViewerId()); + return $this->cacheActorsRequest->searchAccounts($search); } diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 33ee2cfd..e92e59da 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -240,9 +240,9 @@ class ActorService { try { $actor = $this->getActor($username);; $count = [ - 'followers', $this->followsRequest->countFollowers($actor->getId()), - 'following', $this->followsRequest->countFollowing($actor->getId()), - 'post', $this->notesRequest->countNotesFromActorId($actor->getId()) + 'followers' => $this->followsRequest->countFollowers($actor->getId()), + 'following' => $this->followsRequest->countFollowing($actor->getId()), + 'post' => $this->notesRequest->countNotesFromActorId($actor->getId()) ]; $actor->addDetailArray('count', $count);