kopia lustrzana https://github.com/nextcloud/social
				
				
				
			Details also when displaying followers of an account
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/120/head
							rodzic
							
								
									c45cfe6b49
								
							
						
					
					
						commit
						0ce9d6fa6c
					
				|  | @ -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; | ||||
|  | @ -364,6 +367,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); | ||||
|  | @ -383,6 +388,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); | ||||
|  | @ -407,12 +414,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 { | ||||
| 
 | ||||
|  | @ -436,6 +438,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); | ||||
|  | @ -457,11 +461,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); | ||||
| 		} | ||||
|  | @ -481,11 +487,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); | ||||
|  | @ -510,6 +512,8 @@ class LocalController extends Controller { | |||
| 	 * @return DataResponse | ||||
| 	 */ | ||||
| 	public function globalActorInfo(string $id): DataResponse { | ||||
| 		$this->initViewer(); | ||||
| 
 | ||||
| 		try { | ||||
| 			$actor = $this->personService->getFromId($id); | ||||
| 
 | ||||
|  | @ -537,6 +541,7 @@ class LocalController extends Controller { | |||
| 
 | ||||
| 				$response = new FileDisplayResponse($document); | ||||
| 				$response->cacheFor(86400); | ||||
| 
 | ||||
| 				return $response; | ||||
| 			} | ||||
| 
 | ||||
|  | @ -561,13 +566,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; | ||||
|  | @ -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) { | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 = []; | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -72,6 +72,10 @@ class FollowService implements ICoreService { | |||
| 	private $miscService; | ||||
| 
 | ||||
| 
 | ||||
| 	/** @var string */ | ||||
| 	private $viewerId = ''; | ||||
| 
 | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 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 string $account | ||||
|  |  | |||
|  | @ -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==" | ||||
|   } | ||||
| } | ||||
		Ładowanie…
	
		Reference in New Issue
	
	 Maxence Lange
						Maxence Lange