Details also when displaying followers of an account

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/120/head
Maxence Lange 2018-12-04 07:27:56 -01:00
rodzic c45cfe6b49
commit 0ce9d6fa6c
7 zmienionych plików z 103 dodań i 99 usunięć

Wyświetl plik

@ -33,7 +33,10 @@ namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TArrayTools;
use Exception; use Exception;
use OC\User\NoUserException;
use OCA\Social\AppInfo\Application; use OCA\Social\AppInfo\Application;
use OCA\Social\Exceptions\AccountAlreadyExistsException;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\Post; use OCA\Social\Model\Post;
@ -364,6 +367,8 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function currentFollowers(): DataResponse { public function currentFollowers(): DataResponse {
$this->initViewer();
try { try {
$actor = $this->actorService->getActorFromUserId($this->userId); $actor = $this->actorService->getActorFromUserId($this->userId);
$followers = $this->followService->getFollowers($actor); $followers = $this->followService->getFollowers($actor);
@ -383,6 +388,8 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function currentFollowing(): DataResponse { public function currentFollowing(): DataResponse {
$this->initViewer();
try { try {
$actor = $this->actorService->getActorFromUserId($this->userId); $actor = $this->actorService->getActorFromUserId($this->userId);
$followers = $this->followService->getFollowing($actor); $followers = $this->followService->getFollowing($actor);
@ -407,12 +414,7 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function accountInfo(string $username): DataResponse { public function accountInfo(string $username): DataResponse {
$this->initViewer();
try {
$viewer = $this->actorService->getActorFromUserId($this->userId, true);
$this->personService->setViewerId($viewer->getId());
} catch (Exception $e) {
}
try { try {
@ -436,6 +438,8 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function accountFollowers(string $username): DataResponse { public function accountFollowers(string $username): DataResponse {
$this->initViewer();
try { try {
$actor = $this->actorService->getActor($username); $actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowers($actor); $followers = $this->followService->getFollowers($actor);
@ -457,11 +461,13 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function accountFollowing(string $username): DataResponse { public function accountFollowing(string $username): DataResponse {
$this->initViewer();
try { try {
$actor = $this->actorService->getActor($username); $actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowing($actor); $following = $this->followService->getFollowing($actor);
return $this->success($followers); return $this->success($following);
} catch (Exception $e) { } catch (Exception $e) {
return $this->fail($e); return $this->fail($e);
} }
@ -481,11 +487,7 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function globalAccountInfo(string $account): DataResponse { public function globalAccountInfo(string $account): DataResponse {
try { $this->initViewer();
$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);
@ -510,6 +512,8 @@ class LocalController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function globalActorInfo(string $id): DataResponse { public function globalActorInfo(string $id): DataResponse {
$this->initViewer();
try { try {
$actor = $this->personService->getFromId($id); $actor = $this->personService->getFromId($id);
@ -537,6 +541,7 @@ class LocalController extends Controller {
$response = new FileDisplayResponse($document); $response = new FileDisplayResponse($document);
$response->cacheFor(86400); $response->cacheFor(86400);
return $response; return $response;
} }
@ -561,13 +566,7 @@ class LocalController extends Controller {
* @throws Exception * @throws Exception
*/ */
public function globalAccountsSearch(string $search): DataResponse { public function globalAccountsSearch(string $search): DataResponse {
try { $this->initViewer();
$viewer = $this->actorService->getActorFromUserId($this->userId, true);
} catch (Exception $e) {
throw new Exception();
}
$this->personService->setViewerId($viewer->getId());
/* Look for an exactly matching account */ /* Look for an exactly matching account */
$match = null; $match = null;
@ -615,5 +614,18 @@ class LocalController extends Controller {
} }
} }
/**
* @throws Exception
*/
private function initViewer() {
try {
$viewer = $this->actorService->getActorFromUserId($this->userId, true);
$this->followService->setViewerId($viewer->getId());
$this->personService->setViewerId($viewer->getId());
} catch (Exception $e) {
}
}
} }

Wyświetl plik

@ -125,35 +125,5 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
} }
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

@ -657,20 +657,27 @@ class CoreRequestBuilder {
/** /**
* @param IQueryBuilder $qb * @param IQueryBuilder $qb
* @param string $fieldActorId * @param string $fieldActorId
* @param string $viewerId
* @param bool $asFollower * @param bool $asFollower
* @param string $prefix * @param string $prefix
* @param string $pf
*/ */
protected function leftJoinFollowAsViewer( protected function leftJoinFollowAsViewer(
IQueryBuilder &$qb, string $fieldActorId, string $viewerId, bool $asFollower = true, IQueryBuilder &$qb, string $fieldActorId, bool $asFollower = true,
string $prefix = 'follow' string $prefix = 'follow', string $pf = ''
) { ) {
if ($qb->getType() !== QueryBuilder::SELECT) { if ($qb->getType() !== QueryBuilder::SELECT) {
return; return;
} }
$viewerId = $this->getViewerId();
if ($viewerId === '') {
return;
}
$expr = $qb->expr(); $expr = $qb->expr();
$pf = $this->defaultSelectAlias; if ($pf === '') {
$pf = $this->defaultSelectAlias;
}
$andX = $expr->andX(); $andX = $expr->andX();
if ($asFollower === true) { if ($asFollower === true) {
@ -722,6 +729,43 @@ class CoreRequestBuilder {
} }
/**
* @param IQueryBuilder $qb
* @param string $fieldActorId
* @param string $pf
*/
protected function leftJoinDetails(
IQueryBuilder $qb, string $fieldActorId = 'id', string $pf = ''
) {
$this->leftJoinFollowAsViewer($qb, $fieldActorId, true, 'as_follower', $pf);
$this->leftJoinFollowAsViewer($qb, $fieldActorId, false, 'as_followed', $pf);
}
/**
* @param Person $actor
* @param array $data
*/
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

@ -34,6 +34,7 @@ namespace OCA\Social\Db;
use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime; use DateTime;
use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\Follow; use OCA\Social\Model\ActivityPub\Follow;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
@ -173,6 +174,7 @@ class FollowsRequest extends FollowsRequestBuilder {
$qb = $this->getFollowsSelectSql(); $qb = $this->getFollowsSelectSql();
$this->limitToOBjectId($qb, $actorId); $this->limitToOBjectId($qb, $actorId);
$this->leftJoinCacheActors($qb, 'actor_id'); $this->leftJoinCacheActors($qb, 'actor_id');
$this->leftJoinDetails($qb, 'id', 'ca');
$qb->orderBy('creation', 'desc'); $qb->orderBy('creation', 'desc');
$follows = []; $follows = [];
@ -195,6 +197,7 @@ class FollowsRequest extends FollowsRequestBuilder {
$qb = $this->getFollowsSelectSql(); $qb = $this->getFollowsSelectSql();
$this->limitToActorId($qb, $actorId); $this->limitToActorId($qb, $actorId);
$this->leftJoinCacheActors($qb, 'object_id'); $this->leftJoinCacheActors($qb, 'object_id');
$this->leftJoinDetails($qb, 'id', 'ca');
$qb->orderBy('creation', 'desc'); $qb->orderBy('creation', 'desc');
$follows = []; $follows = [];

Wyświetl plik

@ -132,6 +132,9 @@ class FollowsRequestBuilder extends CoreRequestBuilder {
try { try {
$actor = $this->parseCacheActorsLeftJoin($data); $actor = $this->parseCacheActorsLeftJoin($data);
$actor->setCompleteDetails(true);
$this->assignDetails($actor, $data);
$follow->setCompleteDetails(true); $follow->setCompleteDetails(true);
$follow->setActor($actor); $follow->setActor($actor);
} catch (InvalidResourceException $e) { } catch (InvalidResourceException $e) {

Wyświetl plik

@ -72,6 +72,10 @@ class FollowService implements ICoreService {
private $miscService; private $miscService;
/** @var string */
private $viewerId = '';
/** /**
* NoteService constructor. * NoteService constructor.
* *
@ -94,6 +98,19 @@ class FollowService implements ICoreService {
} }
/**
* @param string $viewerId
*/
public function setViewerId(string $viewerId) {
$this->viewerId = $viewerId;
$this->followsRequest->setViewerId($viewerId);
}
public function getViewerId(): string {
return $this->viewerId;
}
/** /**
* @param Person $actor * @param Person $actor
* @param string $account * @param string $account

Wyświetl plik

@ -1,45 +0,0 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"sensitive": "as:sensitive",
"movedTo": {
"@id": "as:movedTo",
"@type": "@id"
},
"Hashtag": "as:Hashtag",
"ostatus": "http://ostatus.org#",
"atomUri": "ostatus:atomUri",
"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
"conversation": "ostatus:conversation",
"toot": "http://joinmastodon.org/ns#",
"Emoji": "toot:Emoji",
"focalPoint": {
"@container": "@list",
"@id": "toot:focalPoint"
},
"featured": {
"@id": "toot:featured",
"@type": "@id"
},
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value"
}
],
"id": "https://mastodon.social/users/twospirit#delete",
"type": "Delete",
"actor": "https://mastodon.social/users/twospirit",
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"object": "https://mastodon.social/users/twospirit",
"signature": {
"type": "RsaSignature2017",
"creator": "https://mastodon.social/users/twospirit#main-key",
"created": "2018-11-26T14:33:30Z",
"signatureValue": "e20O0AYwAGLgHyb/xD1fO+v7gnE8aONFR3iMb/4ULd+Qjz4fV1/ay0IRFdatWGRP4uG/XzjdW6hJTR69wjQUag9k0JOvHkOssUHIXkmBEsKZURyEVWrjT1+Dyx1ZFwsbhjSXCkvbz70mq+1JPx2zDK+fTsG8TPIKBpvj9LYmQbF3Z4n7wRRxibCCL8oGlrHlwWwABYkZKoruLYJya9a6eVvbCD1P5TO+M1CUdMHhqez8k3ll50ZsGRlHqnojR0V9AjMixuMNMx4qvcC+wwJb4QBorfPJDh843Pw4lybwbs5/bXwErxR+Infc61w/dkyw1MvZdPYD2dy+uOHdndRRdQ=="
}
}