Merge pull request #100 from nextcloud-gmbh/fixing-details

set viewer and assign details
pull/99/head
Julius Härtl 2018-12-03 11:15:03 +01:00 zatwierdzone przez GitHub
commit d08ec50d28
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
7 zmienionych plików z 79 dodań i 58 usunięć

Wyświetl plik

@ -75,7 +75,7 @@ return [
['name' => 'Local#actionFollow', 'url' => '/api/v1/current/follow', 'verb' => 'PUT'], ['name' => 'Local#actionFollow', 'url' => '/api/v1/current/follow', 'verb' => 'PUT'],
['name' => 'Local#actionUnfollow', 'url' => '/api/v1/current/follow', 'verb' => 'DELETE'], ['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#currentFollowers', 'url' => '/api/v1/current/followers', 'verb' => 'GET'],
['name' => 'Local#currentFollowing', 'url' => '/api/v1/current/following', 'verb' => 'GET'], ['name' => 'Local#currentFollowing', 'url' => '/api/v1/current/following', 'verb' => 'GET'],

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);
} }

Wyświetl plik

@ -240,9 +240,9 @@ class ActorService {
try { try {
$actor = $this->getActor($username);; $actor = $this->getActor($username);;
$count = [ $count = [
'followers', $this->followsRequest->countFollowers($actor->getId()), 'followers' => $this->followsRequest->countFollowers($actor->getId()),
'following', $this->followsRequest->countFollowing($actor->getId()), 'following' => $this->followsRequest->countFollowing($actor->getId()),
'post', $this->notesRequest->countNotesFromActorId($actor->getId()) 'post' => $this->notesRequest->countNotesFromActorId($actor->getId())
]; ];
$actor->addDetailArray('count', $count); $actor->addDetailArray('count', $count);