l10n = $l10n; $this->urlGenerator = $urlGenerator; $this->streamService = $streamService; $this->followService = $followService; $this->cacheActorService = $cacheActorService; $this->accountService = $accountService; $this->searchService = $searchService; $this->configService = $configService; $this->logger = $logger; } /** * return unique id of the provider */ public function getId(): string { return self::PROVIDER_ID; } /** * @return string */ public function getName(): string { return $this->l10n->t('Social'); } /** * @param string $route * @param array $routeParameters * * @return int */ public function getOrder(string $route, array $routeParameters): int { return self::ORDER; } /** * @param IUser $user * @param ISearchQuery $query * * @return SearchResult * @throws AccountDoesNotExistException */ public function search(IUser $user, ISearchQuery $query): SearchResult { $this->initViewer(); $search = trim($query->getTerm()); $result = array_merge( $this->convertAccounts($this->searchService->searchUri($search)), $this->convertAccounts($this->searchService->searchAccounts($search)), $this->convertHashtags($this->searchService->searchHashtags($search)) ); // $this->searchService->searchStreamContent($search) return SearchResult::paginated( $this->l10n->t('Social'), $result, ($query->getCursor() ?? 0) + $query->getLimit() ); } /** * TODO: switch to SessionService * * @param bool $exception * * @throws AccountDoesNotExistException */ private function initViewer(bool $exception = false) { if (!isset($this->userId)) { if ($exception) { throw new AccountDoesNotExistException('userId not defined'); } return; } try { $this->viewer = $this->accountService->getActorFromUserId($this->userId, true); $this->streamService->setViewer($this->viewer); $this->followService->setViewer($this->viewer); $this->cacheActorService->setViewer($this->viewer); } catch (Exception $e) { if ($exception) { throw new AccountDoesNotExistException( 'unable to initViewer - ' . get_class($e) . ' - ' . $e->getMessage() ); } } } /** * @param Person[] $accounts * * @return UnifiedSearchResult[] */ private function convertAccounts(array $accounts): array { $result = []; foreach ($accounts as $account) { $icon = ($account->hasIcon()) ? $account->getIcon() ->getUrl() : ''; $result[] = new UnifiedSearchResult( $icon, $account->getPreferredUsername(), '@' . $account->getAccount(), $this->urlGenerator->linkToRoute('social.ActivityPub.actorAlias', ['username' => $account->getAccount()]), $icon ); } return $result; } /** * @param array $hashtags * * @return UnifiedSearchResult[] */ private function convertHashtags(array $hashtags): array { $result = []; foreach ($hashtags as $hashtag) { $tag = $hashtag['hashtag']; $result[] = new UnifiedSearchResult( '', $hashtag['trend']['10d'] . ' posts related to \'' . $tag . '\'', '#' . $tag, $this->urlGenerator->linkToRouteAbsolute( 'social.Navigation.timeline', ['path' => 'tags/' . $tag] ), '' ); } return $result; } }