Skip to content

Commit fea8749

Browse files
committed
No push notifications when in DND
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 1ae825e commit fea8749

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/Push.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use OCP\L10N\IFactory;
4040
use OCP\Notification\IManager as INotificationManager;
4141
use OCP\Notification\INotification;
42+
use OCP\UserStatus\IManager as IUserStatusManager;
43+
use OCP\UserStatus\IUserStatus;
4244
use Symfony\Component\Console\Output\OutputInterface;
4345

4446
class Push {
@@ -58,6 +60,8 @@ class Push {
5860
protected $clientService;
5961
/** @var ICache */
6062
protected $cache;
63+
/** @var IUserStatusManager */
64+
protected $userStatusManager;
6165
/** @var IFactory */
6266
protected $l10nFactory;
6367
/** @var ILogger */
@@ -77,6 +81,7 @@ public function __construct(IDBConnection $connection,
7781
IUserManager $userManager,
7882
IClientService $clientService,
7983
ICacheFactory $cacheFactory,
84+
IUserStatusManager $userStatusManager,
8085
IFactory $l10nFactory,
8186
ILogger $log) {
8287
$this->db = $connection;
@@ -87,6 +92,7 @@ public function __construct(IDBConnection $connection,
8792
$this->userManager = $userManager;
8893
$this->clientService = $clientService;
8994
$this->cache = $cacheFactory->createDistributed('pushtokens');
95+
$this->userStatusManager = $userStatusManager;
9096
$this->l10nFactory = $l10nFactory;
9197
$this->log = $log;
9298
}
@@ -124,6 +130,18 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
124130
return;
125131
}
126132

133+
$userStatus = $this->userStatusManager->getUserStatuses([
134+
$notification->getUser(),
135+
]);
136+
137+
if (isset($userStatus[$notification->getUser()])) {
138+
$userStatus = $userStatus[$notification->getUser()];
139+
if ($userStatus->getStatus() === IUserStatus::DND) {
140+
$this->printInfo('User status is set to DND');
141+
return;
142+
}
143+
}
144+
127145
$devices = $this->getDevicesForUser($notification->getUser());
128146
if (empty($devices)) {
129147
$this->printInfo('No devices found for user');

tests/Unit/PushTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCP\L10N\IFactory;
4343
use OCP\Notification\IManager as INotificationManager;
4444
use OCP\Notification\INotification;
45+
use OCP\UserStatus\IManager as IUserStatusManager;
4546
use PHPUnit\Framework\MockObject\MockObject;
4647

4748
/**
@@ -69,6 +70,8 @@ class PushTest extends TestCase {
6970
protected $cacheFactory;
7071
/** @var ICache|MockObject */
7172
protected $cache;
73+
/** @var IUserStatusManager|MockObject */
74+
protected $userStatusManager;
7275
/** @var IFactory|MockObject */
7376
protected $l10nFactory;
7477
/** @var ILogger|MockObject */
@@ -86,6 +89,7 @@ protected function setUp(): void {
8689
$this->clientService = $this->createMock(IClientService::class);
8790
$this->cacheFactory = $this->createMock(ICacheFactory::class);
8891
$this->cache = $this->createMock(ICache::class);
92+
$this->userStatusManager = $this->createMock(IUserStatusManager::class);
8993
$this->l10nFactory = $this->createMock(IFactory::class);
9094
$this->logger = $this->createMock(ILogger::class);
9195

@@ -110,6 +114,7 @@ protected function getPush(array $methods = []) {
110114
$this->userManager,
111115
$this->clientService,
112116
$this->cacheFactory,
117+
$this->userStatusManager,
113118
$this->l10nFactory,
114119
$this->logger,
115120
])
@@ -126,6 +131,7 @@ protected function getPush(array $methods = []) {
126131
$this->userManager,
127132
$this->clientService,
128133
$this->cacheFactory,
134+
$this->userStatusManager,
129135
$this->l10nFactory,
130136
$this->logger
131137
);

0 commit comments

Comments
 (0)