Skip to content

Commit

Permalink
No push notifications when in DND
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Aug 26, 2020
1 parent 1ae825e commit fea8749
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use OCP\L10N\IFactory;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\UserStatus\IManager as IUserStatusManager;
use OCP\UserStatus\IUserStatus;
use Symfony\Component\Console\Output\OutputInterface;

class Push {
Expand All @@ -58,6 +60,8 @@ class Push {
protected $clientService;
/** @var ICache */
protected $cache;
/** @var IUserStatusManager */
protected $userStatusManager;
/** @var IFactory */
protected $l10nFactory;
/** @var ILogger */
Expand All @@ -77,6 +81,7 @@ public function __construct(IDBConnection $connection,
IUserManager $userManager,
IClientService $clientService,
ICacheFactory $cacheFactory,
IUserStatusManager $userStatusManager,
IFactory $l10nFactory,
ILogger $log) {
$this->db = $connection;
Expand All @@ -87,6 +92,7 @@ public function __construct(IDBConnection $connection,
$this->userManager = $userManager;
$this->clientService = $clientService;
$this->cache = $cacheFactory->createDistributed('pushtokens');
$this->userStatusManager = $userStatusManager;
$this->l10nFactory = $l10nFactory;
$this->log = $log;
}
Expand Down Expand Up @@ -124,6 +130,18 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
return;
}

$userStatus = $this->userStatusManager->getUserStatuses([
$notification->getUser(),
]);

if (isset($userStatus[$notification->getUser()])) {
$userStatus = $userStatus[$notification->getUser()];
if ($userStatus->getStatus() === IUserStatus::DND) {
$this->printInfo('User status is set to DND');
return;
}
}

$devices = $this->getDevicesForUser($notification->getUser());
if (empty($devices)) {
$this->printInfo('No devices found for user');
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\L10N\IFactory;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\MockObject\MockObject;

/**
Expand Down Expand Up @@ -69,6 +70,8 @@ class PushTest extends TestCase {
protected $cacheFactory;
/** @var ICache|MockObject */
protected $cache;
/** @var IUserStatusManager|MockObject */
protected $userStatusManager;
/** @var IFactory|MockObject */
protected $l10nFactory;
/** @var ILogger|MockObject */
Expand All @@ -86,6 +89,7 @@ protected function setUp(): void {
$this->clientService = $this->createMock(IClientService::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);
$this->userStatusManager = $this->createMock(IUserStatusManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(ILogger::class);

Expand All @@ -110,6 +114,7 @@ protected function getPush(array $methods = []) {
$this->userManager,
$this->clientService,
$this->cacheFactory,
$this->userStatusManager,
$this->l10nFactory,
$this->logger,
])
Expand All @@ -126,6 +131,7 @@ protected function getPush(array $methods = []) {
$this->userManager,
$this->clientService,
$this->cacheFactory,
$this->userStatusManager,
$this->l10nFactory,
$this->logger
);
Expand Down

0 comments on commit fea8749

Please sign in to comment.