feat(notifications): Expose an attribute whether there is a desktop client handling it
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Родитель
9afd6705d6
Коммит
8a3464fea1
|
@ -77,6 +77,7 @@ The user needs to be identified/logged in by the server. Then you can just run a
|
||||||
"messageRichParameters": [],
|
"messageRichParameters": [],
|
||||||
"link": "http://localhost/index.php/apps/files_sharing/pending",
|
"link": "http://localhost/index.php/apps/files_sharing/pending",
|
||||||
"icon": "http://localhost/img/icon.svg",
|
"icon": "http://localhost/img/icon.svg",
|
||||||
|
"shouldNotify": true,
|
||||||
"actions": [
|
"actions": [
|
||||||
{
|
{
|
||||||
"label": "Accept",
|
"label": "Accept",
|
||||||
|
|
|
@ -27,9 +27,11 @@ namespace OCA\Notifications\Controller;
|
||||||
use OCA\Notifications\Exceptions\NotificationNotFoundException;
|
use OCA\Notifications\Exceptions\NotificationNotFoundException;
|
||||||
use OCA\Notifications\Handler;
|
use OCA\Notifications\Handler;
|
||||||
use OCA\Notifications\Push;
|
use OCA\Notifications\Push;
|
||||||
|
use OCA\Notifications\Service\ClientService;
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\AppFramework\OCSController;
|
use OCP\AppFramework\OCSController;
|
||||||
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
use OCP\IUserSession;
|
use OCP\IUserSession;
|
||||||
|
@ -41,35 +43,19 @@ use OCP\UserStatus\IManager as IUserStatusManager;
|
||||||
use OCP\UserStatus\IUserStatus;
|
use OCP\UserStatus\IUserStatus;
|
||||||
|
|
||||||
class EndpointController extends OCSController {
|
class EndpointController extends OCSController {
|
||||||
/** @var Handler */
|
public function __construct(
|
||||||
private $handler;
|
string $appName,
|
||||||
/** @var IManager */
|
|
||||||
private $manager;
|
|
||||||
/** @var IFactory */
|
|
||||||
private $l10nFactory;
|
|
||||||
/** @var IUserSession */
|
|
||||||
private $session;
|
|
||||||
/** @var IUserStatusManager */
|
|
||||||
private $userStatusManager;
|
|
||||||
/** @var Push */
|
|
||||||
private $push;
|
|
||||||
|
|
||||||
public function __construct(string $appName,
|
|
||||||
IRequest $request,
|
IRequest $request,
|
||||||
Handler $handler,
|
protected Handler $handler,
|
||||||
IManager $manager,
|
protected IManager $manager,
|
||||||
IFactory $l10nFactory,
|
protected IFactory $l10nFactory,
|
||||||
IUserSession $session,
|
protected IUserSession $session,
|
||||||
IUserStatusManager $userStatusManager,
|
protected ITimeFactory $timeFactory,
|
||||||
Push $push) {
|
protected IUserStatusManager $userStatusManager,
|
||||||
|
protected ClientService $clientService,
|
||||||
|
protected Push $push,
|
||||||
|
) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
|
|
||||||
$this->handler = $handler;
|
|
||||||
$this->manager = $manager;
|
|
||||||
$this->l10nFactory = $l10nFactory;
|
|
||||||
$this->session = $session;
|
|
||||||
$this->userStatusManager = $userStatusManager;
|
|
||||||
$this->push = $push;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,13 +82,22 @@ class EndpointController extends OCSController {
|
||||||
return new DataResponse(null, Http::STATUS_NO_CONTENT, $headers);
|
return new DataResponse(null, Http::STATUS_NO_CONTENT, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$user = $this->session->getUser();
|
||||||
$filter = $this->manager->createNotification();
|
$filter = $this->manager->createNotification();
|
||||||
$filter->setUser($this->getCurrentUser());
|
$filter->setUser($this->getCurrentUser());
|
||||||
$language = $this->l10nFactory->getUserLanguage($this->session->getUser());
|
$language = $this->l10nFactory->getUserLanguage($user);
|
||||||
$notifications = $this->handler->get($filter);
|
$notifications = $this->handler->get($filter);
|
||||||
|
|
||||||
$shouldFlush = $this->manager->defer();
|
$shouldFlush = $this->manager->defer();
|
||||||
|
|
||||||
|
$hasActiveTalkDesktop = false;
|
||||||
|
if ($user instanceof IUser) {
|
||||||
|
$hasActiveTalkDesktop = $this->clientService->hasTalkDesktop(
|
||||||
|
$user->getUID(),
|
||||||
|
$this->timeFactory->getTime() - ClientService::DESKTOP_CLIENT_TIMEOUT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
$notificationIds = [];
|
$notificationIds = [];
|
||||||
foreach ($notifications as $notificationId => $notification) {
|
foreach ($notifications as $notificationId => $notification) {
|
||||||
|
@ -115,7 +110,7 @@ class EndpointController extends OCSController {
|
||||||
}
|
}
|
||||||
|
|
||||||
$notificationIds[] = $notificationId;
|
$notificationIds[] = $notificationId;
|
||||||
$data[] = $this->notificationToArray($notificationId, $notification, $apiVersion);
|
$data[] = $this->notificationToArray($notificationId, $notification, $apiVersion, $hasActiveTalkDesktop);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($shouldFlush) {
|
if ($shouldFlush) {
|
||||||
|
@ -153,7 +148,8 @@ class EndpointController extends OCSController {
|
||||||
return new DataResponse(null, Http::STATUS_NOT_FOUND);
|
return new DataResponse(null, Http::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
$language = $this->l10nFactory->getUserLanguage($this->session->getUser());
|
$user = $this->session->getUser();
|
||||||
|
$language = $this->l10nFactory->getUserLanguage($user);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$notification = $this->manager->prepare($notification, $language);
|
$notification = $this->manager->prepare($notification, $language);
|
||||||
|
@ -162,7 +158,15 @@ class EndpointController extends OCSController {
|
||||||
return new DataResponse(null, Http::STATUS_NOT_FOUND);
|
return new DataResponse(null, Http::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DataResponse($this->notificationToArray($id, $notification, $apiVersion));
|
$hasActiveTalkDesktop = false;
|
||||||
|
if ($user instanceof IUser) {
|
||||||
|
$hasActiveTalkDesktop = $this->clientService->hasTalkDesktop(
|
||||||
|
$user->getUID(),
|
||||||
|
$this->timeFactory->getTime() - ClientService::DESKTOP_CLIENT_TIMEOUT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DataResponse($this->notificationToArray($id, $notification, $apiVersion, $hasActiveTalkDesktop));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,9 +235,10 @@ class EndpointController extends OCSController {
|
||||||
* @param int $notificationId
|
* @param int $notificationId
|
||||||
* @param INotification $notification
|
* @param INotification $notification
|
||||||
* @param string $apiVersion
|
* @param string $apiVersion
|
||||||
|
* @param bool $hasActiveTalkDesktop
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function notificationToArray(int $notificationId, INotification $notification, string $apiVersion): array {
|
protected function notificationToArray(int $notificationId, INotification $notification, string $apiVersion, bool $hasActiveTalkDesktop = false): array {
|
||||||
$data = [
|
$data = [
|
||||||
'notification_id' => $notificationId,
|
'notification_id' => $notificationId,
|
||||||
'app' => $notification->getApp(),
|
'app' => $notification->getApp(),
|
||||||
|
@ -247,12 +252,19 @@ class EndpointController extends OCSController {
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($apiVersion !== 'v1') {
|
if ($apiVersion !== 'v1') {
|
||||||
|
if ($this->request->isUserAgent([IRequest::USER_AGENT_TALK_DESKTOP])) {
|
||||||
|
$shouldNotify = $notification->getApp() === 'spreed';
|
||||||
|
} else {
|
||||||
|
$shouldNotify = !$hasActiveTalkDesktop || $notification->getApp() !== 'spreed';
|
||||||
|
}
|
||||||
|
|
||||||
$data = array_merge($data, [
|
$data = array_merge($data, [
|
||||||
'subjectRich' => $notification->getRichSubject(),
|
'subjectRich' => $notification->getRichSubject(),
|
||||||
'subjectRichParameters' => $notification->getRichSubjectParameters(),
|
'subjectRichParameters' => $notification->getRichSubjectParameters(),
|
||||||
'messageRich' => $notification->getRichMessage(),
|
'messageRich' => $notification->getRichMessage(),
|
||||||
'messageRichParameters' => $notification->getRichMessageParameters(),
|
'messageRichParameters' => $notification->getRichMessageParameters(),
|
||||||
'icon' => $notification->getIcon(),
|
'icon' => $notification->getIcon(),
|
||||||
|
'shouldNotify' => $shouldNotify,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Notifications\Service;
|
||||||
|
|
||||||
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||||
|
use OCP\Diagnostics\IQuery;
|
||||||
|
use OCP\IDBConnection;
|
||||||
|
use OCP\IRequest;
|
||||||
|
|
||||||
|
class ClientService {
|
||||||
|
public const DESKTOP_CLIENT_TIMEOUT = 120;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected IDBConnection $connection,
|
||||||
|
protected IRequest $request,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasTalkDesktop(string $userId, int $maxAge = 0): bool {
|
||||||
|
$query = $this->connection->getQueryBuilder();
|
||||||
|
$query->select('name')
|
||||||
|
->from('authtoken')
|
||||||
|
->where($query->expr()->eq('uid', $query->createNamedParameter($userId)));
|
||||||
|
|
||||||
|
if ($maxAge !== 0) {
|
||||||
|
$query->andWhere($query->expr()->gte(
|
||||||
|
'last_activity',
|
||||||
|
$query->createNamedParameter($maxAge, IQueryBuilder::PARAM_INT)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $query->executeQuery();
|
||||||
|
while ($row = $result->fetch()) {
|
||||||
|
\OC::$server->getLogger()->error(json_encode($row['name']));
|
||||||
|
if (preg_match('/ \(Talk Desktop Client - [A-Za-z ]+\)$/', $row['name'])) {
|
||||||
|
$result->closeCursor();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$result->closeCursor();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче