set viewer and assign details

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/100/head
Maxence Lange 2018-12-03 08:42:36 -01:00
rodzic 7f33e8d187
commit a6f7faacbc
5 zmienionych plików z 75 dodań i 54 usunięć

Wyświetl plik

@ -407,7 +407,15 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function accountInfo(string $username): DataResponse { public function accountInfo(string $username): DataResponse {
try { try {
$viewer = $this->actorService->getActorFromUserId($this->userId, true);
$this->personService->setViewerId($viewer->getId());
} catch (Exception $e) {
}
try {
$actor = $this->actorService->getActor($username); $actor = $this->actorService->getActor($username);
$actor = $this->personService->getFromLocalAccount($actor->getPreferredUsername()); $actor = $this->personService->getFromLocalAccount($actor->getPreferredUsername());
@ -473,6 +481,12 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function globalAccountInfo(string $account): DataResponse { public function globalAccountInfo(string $account): DataResponse {
try {
$viewer = $this->actorService->getActorFromUserId($this->userId, true);
$this->personService->setViewerId($viewer->getId());
} catch (Exception $e) {
}
try { try {
$actor = $this->personService->getFromAccount($account); $actor = $this->personService->getFromAccount($account);

Wyświetl plik

@ -33,7 +33,6 @@ namespace OCA\Social\Db;
use DateTime; use DateTime;
use Exception; use Exception;
use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Model\ActivityPub\Person;
use OCA\Social\Service\ConfigService; use OCA\Social\Service\ConfigService;
use OCA\Social\Service\MiscService; use OCA\Social\Service\MiscService;
@ -134,20 +133,14 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
* *
* @param string $account * @param string $account
* *
* @param string $viewerId
*
* @return Person * @return Person
* @throws CacheActorDoesNotExistException * @throws CacheActorDoesNotExistException
*/ */
public function getFromAccount(string $account, string $viewerId = ''): Person { public function getFromAccount(string $account): Person {
$qb = $this->getCacheActorsSelectSql(); $qb = $this->getCacheActorsSelectSql();
$this->limitToAccount($qb, $account); $this->limitToAccount($qb, $account);
$this->leftJoinCacheDocuments($qb, 'icon_id'); $this->leftJoinCacheDocuments($qb, 'icon_id');
$this->leftJoinDetails($qb);
if ($viewerId !== '') {
$this->leftJoinFollowAsViewer($qb, 'id', $viewerId, true, 'as_follower');
$this->leftJoinFollowAsViewer($qb, 'id', $viewerId, false, 'as_followed');
}
$cursor = $qb->execute(); $cursor = $qb->execute();
$data = $cursor->fetch(); $data = $cursor->fetch();
@ -157,21 +150,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
throw new CacheActorDoesNotExistException(); throw new CacheActorDoesNotExistException();
} }
$account = $this->parseCacheActorsSelectSql($data); return $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;
} }
@ -188,6 +167,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
$this->limitToPreferredUsername($qb, $account); $this->limitToPreferredUsername($qb, $account);
$this->limitToLocal($qb, true); $this->limitToLocal($qb, true);
$this->leftJoinCacheDocuments($qb, 'icon_id'); $this->leftJoinCacheDocuments($qb, 'icon_id');
$this->leftJoinDetails($qb);
$cursor = $qb->execute(); $cursor = $qb->execute();
$data = $cursor->fetch(); $data = $cursor->fetch();
@ -203,44 +183,19 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
/** /**
* @param string $search * @param string $search
* @param string $viewerId
* *
* @return Person[] * @return Person[]
*/ */
public function searchAccounts(string $search, string $viewerId = ''): array { public function searchAccounts(string $search): array {
$qb = $this->getCacheActorsSelectSql(); $qb = $this->getCacheActorsSelectSql();
$this->searchInAccount($qb, $search); $this->searchInAccount($qb, $search);
$this->leftJoinCacheDocuments($qb, 'icon_id'); $this->leftJoinCacheDocuments($qb, 'icon_id');
$this->leftJoinDetails($qb);
if ($viewerId !== '') {
$this->leftJoinFollowAsViewer($qb, 'id', $viewerId, true, 'as_follower');
$this->leftJoinFollowAsViewer($qb, 'id', $viewerId, false, 'as_followed');
}
$accounts = []; $accounts = [];
$cursor = $qb->execute(); $cursor = $qb->execute();
while ($data = $cursor->fetch()) { while ($data = $cursor->fetch()) {
$account = $this->parseCacheActorsSelectSql($data); $accounts[] = $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;
} }
$cursor->closeCursor(); $cursor->closeCursor();

Wyświetl plik

@ -119,8 +119,41 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
} catch (InvalidResourceException $e) { } catch (InvalidResourceException $e) {
} }
$this->assignDetails($actor, $data);
return $actor; 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);
}
}
} }

Wyświetl plik

@ -77,6 +77,9 @@ class CoreRequestBuilder {
/** @var string */ /** @var string */
protected $defaultSelectAlias; protected $defaultSelectAlias;
/** @var string */
private $viewerId = '';
/** /**
* CoreRequestBuilder constructor. * 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 * Limit the request to the Id
* *

Wyświetl plik

@ -106,6 +106,7 @@ class PersonService implements ICoreService {
*/ */
public function setViewerId(string $viewerId) { public function setViewerId(string $viewerId) {
$this->viewerId = $viewerId; $this->viewerId = $viewerId;
$this->cacheActorsRequest->setViewerId($viewerId);
} }
public function getViewerId(): string { public function getViewerId(): string {
@ -185,7 +186,7 @@ class PersonService implements ICoreService {
public function getFromAccount(string $account, bool $retrieve = true): Person { public function getFromAccount(string $account, bool $retrieve = true): Person {
try { try {
$actor = $this->cacheActorsRequest->getFromAccount($account, $this->getViewerId()); $actor = $this->cacheActorsRequest->getFromAccount($account);
} catch (CacheActorDoesNotExistException $e) { } catch (CacheActorDoesNotExistException $e) {
if (!$retrieve) { if (!$retrieve) {
throw new CacheActorDoesNotExistException(); throw new CacheActorDoesNotExistException();
@ -245,7 +246,7 @@ class PersonService implements ICoreService {
* @return Person[] * @return Person[]
*/ */
public function searchCachedAccounts(string $search): array { public function searchCachedAccounts(string $search): array {
return $this->cacheActorsRequest->searchAccounts($search, $this->getViewerId()); return $this->cacheActorsRequest->searchAccounts($search);
} }