| Server IP : 93.86.61.54 / Your IP : 216.73.216.60 Web Server : Apache/2.4.62 (Ubuntu) System : Linux rasin.ddns.net 6.8.0-124-generic #124~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 21:05:19 UTC x86_64 User : www-data ( 33) PHP Version : 8.4.22 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/projects/nextcloud/apps/integration_github/lib/Search/ |
Upload File : |
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Julien Veyssier
*
* @author Julien Veyssier <eneiluj@posteo.net>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Github\Search;
use OCA\Github\Service\GithubAPIService;
use OCA\Github\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\IL10N;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
class GithubSearchReposProvider implements IProvider {
/** @var IAppManager */
private $appManager;
/** @var IL10N */
private $l10n;
/** @var IURLGenerator */
private $urlGenerator;
/**
* CospendSearchProvider constructor.
*
* @param IAppManager $appManager
* @param IL10N $l10n
* @param IURLGenerator $urlGenerator
* @param GithubAPIService $service
*/
public function __construct(IAppManager $appManager,
IL10N $l10n,
IConfig $config,
IURLGenerator $urlGenerator,
GithubAPIService $service) {
$this->appManager = $appManager;
$this->l10n = $l10n;
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->service = $service;
}
/**
* @inheritDoc
*/
public function getId(): string {
return 'github-search-repos';
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->l10n->t('GitHub repositories');
}
/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, Application::APP_ID . '.') === 0) {
// Active app, prefer Github results
return -1;
}
return 20;
}
/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
if (!$this->appManager->isEnabledForUser(Application::APP_ID, $user)) {
return SearchResult::complete($this->getName(), []);
}
$limit = $query->getLimit();
$term = $query->getTerm();
$offset = $query->getCursor();
$offset = $offset ? intval($offset) : 0;
$theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', '');
$accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token', '');
$searchReposEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_repos_enabled', '0') === '1';
if ($accessToken === '' || !$searchReposEnabled) {
return SearchResult::paginated($this->getName(), [], 0);
}
$searchResult = $this->service->searchRepositories($accessToken, $term, $offset, $limit);
if (isset($searchResult['error'])) {
$repos = [];
} else {
$repos = $searchResult['items'];
}
$formattedResults = \array_map(function (array $entry): GithubSearchResultEntry {
return new GithubSearchResultEntry(
$this->getThumbnailUrl($entry),
$this->getMainText($entry),
$this->getSubline($entry),
$this->getLinkToGithub($entry),
'',
true
);
}, $repos);
return SearchResult::paginated(
$this->getName(),
$formattedResults,
$offset + $limit
);
}
/**
* @param array $entry
* @return string
*/
protected function getMainText(array $entry): string {
return $entry['full_name'] . ' [' . ($entry['stargazers_count'] ?? 0) . '⭐]';
}
/**
* @param array $entry
* @return string
*/
protected function getSubline(array $entry): string {
return $entry['description'] ?? '';
}
/**
* @param array $entry
* @return string
*/
protected function getLinkToGithub(array $entry): string {
return $entry['html_url'] ?? '';
}
/**
* @param array $entry
* @return string
*/
protected function getThumbnailUrl(array $entry): string {
$userName = $entry['owner']['login'] ?? '';
return $this->urlGenerator->linkToRoute('integration_github.githubAPI.getAvatar', []) . '?githubUserName=' . urlencode($userName);
}
}