summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2018-12-04 15:45:03 +0100
committerGitHub <noreply@github.com>2018-12-04 15:45:03 +0100
commit3a08bf0c002fb98f236aee682be75669552fbea6 (patch)
treee3167d021a13fe9ec17ef9b2911ac8e7c24af84d /lib
parent236caae52480d91b1bb7cb92afda1048fbefa712 (diff)
parent0ce9d6fa6cf4852ed34bc0f00801b012803167c2 (diff)
Merge pull request #120 from nextcloud-gmbh/details-on-followers
Details also when displaying followers of an account
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/LocalController.php51
-rw-r--r--lib/Db/CacheActorsRequestBuilder.php30
-rw-r--r--lib/Db/CoreRequestBuilder.php52
-rw-r--r--lib/Db/FollowsRequest.php3
-rw-r--r--lib/Db/FollowsRequestBuilder.php3
-rw-r--r--lib/Service/ActivityPub/FollowService.php17
6 files changed, 102 insertions, 54 deletions
diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php
index cadfe338..a1c564b4 100644
--- a/lib/Controller/LocalController.php
+++ b/lib/Controller/LocalController.php
@@ -33,7 +33,10 @@ namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use daita\MySmallPhpTools\Traits\TArrayTools;
use Exception;
+use OC\User\NoUserException;
use OCA\Social\AppInfo\Application;
+use OCA\Social\Exceptions\AccountAlreadyExistsException;
+use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\Post;
@@ -365,6 +368,8 @@ class LocalController extends Controller {
* @return DataResponse
*/
public function currentFollowers(): DataResponse {
+ $this->initViewer();
+
try {
$actor = $this->actorService->getActorFromUserId($this->userId);
$followers = $this->followService->getFollowers($actor);
@@ -384,6 +389,8 @@ class LocalController extends Controller {
* @return DataResponse
*/
public function currentFollowing(): DataResponse {
+ $this->initViewer();
+
try {
$actor = $this->actorService->getActorFromUserId($this->userId);
$followers = $this->followService->getFollowing($actor);
@@ -408,12 +415,7 @@ 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) {
- }
+ $this->initViewer();
try {
@@ -437,6 +439,8 @@ class LocalController extends Controller {
* @return DataResponse
*/
public function accountFollowers(string $username): DataResponse {
+ $this->initViewer();
+
try {
$actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowers($actor);
@@ -458,11 +462,13 @@ class LocalController extends Controller {
* @return DataResponse
*/
public function accountFollowing(string $username): DataResponse {
+ $this->initViewer();
+
try {
$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) {
return $this->fail($e);
}
@@ -482,11 +488,7 @@ 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) {
- }
+ $this->initViewer();
try {
$actor = $this->personService->getFromAccount($account);
@@ -511,6 +513,8 @@ class LocalController extends Controller {
* @return DataResponse
*/
public function globalActorInfo(string $id): DataResponse {
+ $this->initViewer();
+
try {
$actor = $this->personService->getFromId($id);
@@ -563,13 +567,7 @@ class LocalController extends Controller {
* @throws Exception
*/
public function globalAccountsSearch(string $search): DataResponse {
- try {
- $viewer = $this->actorService->getActorFromUserId($this->userId, true);
- } catch (Exception $e) {
- throw new Exception();
- }
-
- $this->personService->setViewerId($viewer->getId());
+ $this->initViewer();
/* Look for an exactly matching account */
$match = null;
@@ -617,5 +615,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) {
+ }
+ }
+
}
diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php
index d00bbd5c..dc037646 100644
--- a/lib/Db/CacheActorsRequestBuilder.php
+++ b/lib/Db/CacheActorsRequestBuilder.php
@@ -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);
- }
- }
-
}
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php
index b021a5a6..e31ec5b5 100644
--- a/lib/Db/CoreRequestBuilder.php
+++ b/lib/Db/CoreRequestBuilder.php
@@ -657,20 +657,27 @@ class CoreRequestBuilder {
/**
* @param IQueryBuilder $qb
* @param string $fieldActorId
- * @param string $viewerId
* @param bool $asFollower
* @param string $prefix
+ * @param string $pf
*/
protected function leftJoinFollowAsViewer(
- IQueryBuilder &$qb, string $fieldActorId, string $viewerId, bool $asFollower = true,
- string $prefix = 'follow'
+ IQueryBuilder &$qb, string $fieldActorId, bool $asFollower = true,
+ string $prefix = 'follow', string $pf = ''
) {
if ($qb->getType() !== QueryBuilder::SELECT) {
return;
}
+ $viewerId = $this->getViewerId();
+ if ($viewerId === '') {
+ return;
+ }
+
$expr = $qb->expr();
- $pf = $this->defaultSelectAlias;
+ if ($pf === '') {
+ $pf = $this->defaultSelectAlias;
+ }
$andX = $expr->andX();
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);
+ }
+ }
}
diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php
index f6f27732..4861b878 100644
--- a/lib/Db/FollowsRequest.php
+++ b/lib/Db/FollowsRequest.php
@@ -34,6 +34,7 @@ namespace OCA\Social\Db;
use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use OCA\Social\Exceptions\FollowDoesNotExistException;
+use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\Follow;
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -173,6 +174,7 @@ class FollowsRequest extends FollowsRequestBuilder {
$qb = $this->getFollowsSelectSql();
$this->limitToOBjectId($qb, $actorId);
$this->leftJoinCacheActors($qb, 'actor_id');
+ $this->leftJoinDetails($qb, 'id', 'ca');
$qb->orderBy('creation', 'desc');
$follows = [];
@@ -195,6 +197,7 @@ class FollowsRequest extends FollowsRequestBuilder {
$qb = $this->getFollowsSelectSql();
$this->limitToActorId($qb, $actorId);
$this->leftJoinCacheActors($qb, 'object_id');
+ $this->leftJoinDetails($qb, 'id', 'ca');
$qb->orderBy('creation', 'desc');
$follows = [];
diff --git a/lib/Db/FollowsRequestBuilder.php b/lib/Db/FollowsRequestBuilder.php
index 0f50b37e..25c0686a 100644
--- a/lib/Db/FollowsRequestBuilder.php
+++ b/lib/Db/FollowsRequestBuilder.php
@@ -132,6 +132,9 @@ class FollowsRequestBuilder extends CoreRequestBuilder {
try {
$actor = $this->parseCacheActorsLeftJoin($data);
+ $actor->setCompleteDetails(true);
+ $this->assignDetails($actor, $data);
+
$follow->setCompleteDetails(true);
$follow->setActor($actor);
} catch (InvalidResourceException $e) {
diff --git a/lib/Service/ActivityPub/FollowService.php b/lib/Service/ActivityPub/FollowService.php
index 8e700bfe..ec669d2f 100644
--- a/lib/Service/ActivityPub/FollowService.php
+++ b/lib/Service/ActivityPub/FollowService.php
@@ -72,6 +72,10 @@ class FollowService implements ICoreService {
private $miscService;
+ /** @var string */
+ private $viewerId = '';
+
+
/**
* NoteService constructor.
*
@@ -95,6 +99,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 string $account
*