diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-29 16:41:55 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-11-29 16:41:55 -0100 |
commit | c005273e1f2a5226a3099a698f789b1de6502c2a (patch) | |
tree | 749fa00c67ad1e525fed3e8b851a38dcf1f283c0 | |
parent | aaafa82b4b8e1e3ec7e58abb2246e71de3e855b8 (diff) |
generate details on account info
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | lib/Controller/ActivityPubController.php | 2 | ||||
-rw-r--r-- | lib/Controller/SocialPubController.php | 64 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequest.php | 26 | ||||
-rw-r--r-- | lib/Service/ActivityPub/PersonService.php | 11 | ||||
-rw-r--r-- | lib/Service/ActorService.php | 35 |
5 files changed, 123 insertions, 15 deletions
diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index acd79fa3..a958f7bc 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -128,7 +128,7 @@ class ActivityPubController extends Controller { */ public function actor(string $username): Response { if (!$this->checkSourceActivityStreams()) { - return $this->navigationController->public($username); + return $this->socialPubController->actor($username); } try { diff --git a/lib/Controller/SocialPubController.php b/lib/Controller/SocialPubController.php index 4783c90e..36ab15be 100644 --- a/lib/Controller/SocialPubController.php +++ b/lib/Controller/SocialPubController.php @@ -31,13 +31,18 @@ namespace OCA\Social\Controller; use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use Exception; use OCA\Social\AppInfo\Application; -use OCA\Social\Service\ActivityService; +use OCA\Social\Exceptions\CacheActorDoesNotExistException; +use OCA\Social\Service\ActivityPub\PersonService; use OCA\Social\Service\ActorService; use OCA\Social\Service\MiscService; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\IL10N; use OCP\IRequest; class SocialPubController extends Controller { @@ -45,12 +50,18 @@ class SocialPubController extends Controller { use TNCDataResponse; - /** @var ActivityService */ - private $activityService; + /** @var string */ + private $userId; + + /** @var IL10N */ + private $l10n; /** @var ActorService */ private $actorService; + /** @var PersonService */ + private $personService; + /** @var MiscService */ private $miscService; @@ -58,19 +69,23 @@ class SocialPubController extends Controller { /** * SocialPubController constructor. * - * @param ActivityService $activityService - * @param ActorService $actorService + * @param $userId * @param IRequest $request + * @param IL10N $l10n + * @param ActorService $actorService + * @param PersonService $personService * @param MiscService $miscService */ public function __construct( - ActivityService $activityService, ActorService $actorService, IRequest $request, - MiscService $miscService + $userId, IRequest $request, IL10N $l10n, ActorService $actorService, + PersonService $personService, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); - $this->activityService = $activityService; + $this->userId = $userId; + $this->l10n = $l10n; $this->actorService = $actorService; + $this->personService = $personService; $this->miscService = $miscService; } @@ -81,14 +96,39 @@ class SocialPubController extends Controller { * * @NoCSRFRequired * @PublicPage - * e* * * @param string $username * - * @return TemplateResponse + * @return Response */ - public function actor(string $username): TemplateResponse { - return new TemplateResponse(Application::APP_NAME, 'actor', [], 'blank'); + public function actor(string $username): Response { + + try { + $actor = $this->personService->getFromLocalAccount($username); + + if ($this->userId !== null) { + $local = $this->actorService->getActorFromUserId($this->userId, true); + $this->actorService->acquaintLinksBetweenPersons($actor, $local); + $actor->setCompleteDetails(true); + } + + $data = [ + 'serverData' => [ + 'public' => true, + ], + 'actor' => $actor + ]; + $page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data); + $page->setHeaderTitle($this->l10n->t('Social') . ' ' . $username); + + $this->miscService->log(json_encode($actor)); + + return $page; + } catch (CacheActorDoesNotExistException $e) { + return new NotFoundResponse(); + } catch (Exception $e) { + $this->fail($e); + } } diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index d247396b..7887ad12 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -154,6 +154,32 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { /** + * get Cached version of a local Actor, based on the preferred username + * + * @param string $account + * + * @return Person + * @throws CacheActorDoesNotExistException + */ + public function getFromLocalAccount(string $account): Person { + $qb = $this->getCacheActorsSelectSql(); + $this->limitToPreferredUsername($qb, $account); + $this->limitToLocal($qb, true); + $this->leftJoinCacheDocuments($qb, 'icon_id'); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new CacheActorDoesNotExistException(); + } + + return $this->parseCacheActorsSelectSql($data); + } + + + /** * @param string $search * * @return Person[] diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 1d3c169a..77575dca 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -190,6 +190,17 @@ class PersonService implements ICoreService { /** + * @param string $account + * + * @return Person + * @throws CacheActorDoesNotExistException + */ + public function getFromLocalAccount(string $account): Person { + return $this->cacheActorsRequest->getFromLocalAccount($account); + } + + + /** * @param array $object * * @return Person diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 70fb824e..dea05590 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -36,6 +36,7 @@ use OC\User\NoUserException; use OCA\Social\Db\ActorsRequest; use OCA\Social\Exceptions\AccountAlreadyExistsException; use OCA\Social\Exceptions\ActorDoesNotExistException; +use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Service\ActivityPub\PersonService; @@ -114,15 +115,26 @@ class ActorService { /** * @param string $userId + * @param bool $create * * @return Person + * @throws AccountAlreadyExistsException * @throws ActorDoesNotExistException * @throws NoUserException * @throws SocialAppConfigException */ - public function getActorFromUserId(string $userId): Person { + public function getActorFromUserId(string $userId, bool $create = false): Person { $this->miscService->confirmUserId($userId); - $actor = $this->actorsRequest->getFromUserId($userId); + try { + $actor = $this->actorsRequest->getFromUserId($userId); + } catch (ActorDoesNotExistException $e) { + if ($create) { + $this->createActor($userId, $userId); + $actor = $this->actorsRequest->getFromUserId($userId); + } else { + throw new ActorDoesNotExistException(); + } + } return $actor; } @@ -178,6 +190,21 @@ class ActorService { /** + * @param Person $local + * @param Person $actor + */ + public function acquaintLinksBetweenPersons(Person $actor, Person $local) { + $actor->addDetailArray( + 'links', + [ + 'follower' => true, + 'following' => true + ] + ); + } + + + /** * @param string $username * @param bool $refresh * @@ -186,6 +213,10 @@ class ActorService { public function cacheLocalActorByUsername(string $username, bool $refresh = false) { try { $actor = $this->getActor($username); + $actor->addDetailInt('followers', 10); + $actor->addDetailInt('following', 10); + $actor->addDetailInt('post', 100); + $this->personService->cacheLocalActor($actor, $refresh); } catch (ActorDoesNotExistException $e) { } |