use IAPIWidget instead of IWidget to implement dashboard api for clients
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
Родитель
8df814162d
Коммит
e888d8f750
|
@ -21,7 +21,7 @@
|
|||
<database min-version="9.4">pgsql</database>
|
||||
<database>sqlite</database>
|
||||
<database min-version="5.5">mysql</database>
|
||||
<nextcloud min-version="20" max-version="22"/>
|
||||
<nextcloud min-version="22" max-version="22"/>
|
||||
</dependencies>
|
||||
<settings>
|
||||
<admin>OCA\Github\Settings\Admin</admin>
|
||||
|
|
|
@ -23,18 +23,21 @@
|
|||
|
||||
namespace OCA\Github\Dashboard;
|
||||
|
||||
use OCP\Dashboard\IWidget;
|
||||
use OCP\Dashboard\IAPIWidget;
|
||||
use OCP\IL10N;
|
||||
use OCA\Github\AppInfo\Application;
|
||||
use OCA\Github\Service\GithubAPIService;
|
||||
|
||||
class GithubWidget implements IWidget {
|
||||
class GithubWidget implements IAPIWidget {
|
||||
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
|
||||
public function __construct(
|
||||
GithubAPIService $githubAPIService,
|
||||
IL10N $l10n
|
||||
) {
|
||||
$this->githubAPIService = $githubAPIService;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
|
@ -80,4 +83,11 @@ class GithubWidget implements IWidget {
|
|||
\OC_Util::addScript(Application::APP_ID, 'integration_github-dashboard');
|
||||
\OC_Util::addStyle(Application::APP_ID, 'dashboard');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
|
||||
return $this->githubAPIService->getWidgetItems($userId, $since, $limit);
|
||||
}
|
||||
}
|
|
@ -12,9 +12,14 @@
|
|||
namespace OCA\Github\Service;
|
||||
|
||||
use OCP\IL10N;
|
||||
use OCP\IConfig;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use OCP\Http\Client\IClientService;
|
||||
|
||||
use OCP\Dashboard\Model\WidgetItem;
|
||||
|
||||
use OCA\Github\AppInfo\Application;
|
||||
|
||||
class GithubAPIService {
|
||||
|
||||
private $l10n;
|
||||
|
@ -23,19 +28,53 @@ class GithubAPIService {
|
|||
/**
|
||||
* Service to make requests to GitHub v3 (JSON) API
|
||||
*/
|
||||
public function __construct (
|
||||
string $appName,
|
||||
LoggerInterface $logger,
|
||||
IL10N $l10n,
|
||||
IClientService $clientService
|
||||
) {
|
||||
public function __construct (string $appName,
|
||||
LoggerInterface $logger,
|
||||
IL10N $l10n,
|
||||
IConfig $config,
|
||||
IClientService $clientService) {
|
||||
$this->appName = $appName;
|
||||
$this->l10n = $l10n;
|
||||
$this->config = $config;
|
||||
$this->logger = $logger;
|
||||
$this->clientService = $clientService;
|
||||
$this->client = $clientService->newClient();
|
||||
}
|
||||
|
||||
public function getWidgetItems(string $userId, ?string $since = null, int $limit = 7): ?array {
|
||||
$token = $this->config->getUserValue($userId, Application::APP_ID, 'token', '');
|
||||
$notifications = $this->getNotifications($token, $since, false);
|
||||
if (isset($notifications['error'])) {
|
||||
return [];
|
||||
} else {
|
||||
$notifications = array_slice($notifications, 0, $limit);
|
||||
return array_map(function (array $notification) use ($token) {
|
||||
$title = $notification['subject']['title'] ?? '';
|
||||
|
||||
$subtitle = $notification['repository']['name'] ?? '';
|
||||
if (in_array($notification['subject']['type'] ?? '', ['PullRequest', 'Issue']) && $notification['subject']['url']) {
|
||||
$parts = explode('/', $notification['subject']['url']);
|
||||
$subtitle .= ' #' . $parts[count($parts) - 1];
|
||||
}
|
||||
|
||||
$iconUrl = ($notification['repository']['owner']['login'] ?? false)
|
||||
? $this->getAvatarUrl($token, $notification['repository']['owner']['login'])
|
||||
: '';
|
||||
|
||||
$sinceId = $notification['updated_at'] ?? '';
|
||||
return new WidgetItem($title, $subtitle, $iconUrl, $sinceId);
|
||||
}, $notifications);
|
||||
}
|
||||
}
|
||||
|
||||
private function getAvatarUrl(string $accessToken, string $githubUserName): ?string {
|
||||
$userInfo = $this->request($accessToken, 'users/' . $githubUserName);
|
||||
if (!isset($userInfo['error']) && isset($userInfo['avatar_url'])) {
|
||||
return $userInfo['avatar_url'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request an avatar image
|
||||
* @param string $accessToken
|
||||
|
@ -43,9 +82,9 @@ class GithubAPIService {
|
|||
* @return ?string Avatar image data
|
||||
*/
|
||||
public function getAvatar(string $accessToken, string $githubUserName): ?string {
|
||||
$userInfo = $this->request($accessToken, 'users/' . $githubUserName);
|
||||
if (!isset($userInfo['error']) && isset($userInfo['avatar_url'])) {
|
||||
return $this->client->get($userInfo['avatar_url'])->getBody();
|
||||
$avatarUrl = $this->getAvatarUrl($accessToken, $githubUserName);
|
||||
if ($avatarUrl) {
|
||||
return $this->client->get($avatarUrl)->getBody();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче