Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ public function __construct(Handler $handler, Push $push) {
public function notify(INotification $notification): void {
$notificationId = $this->handler->add($notification);

$shouldFlush = $this->push->deferPayloads();
try {
$notificationToPush = $this->handler->getById($notificationId, $notification->getUser());
$this->push->pushToDevice($notificationId, $notificationToPush);
} catch (NotificationNotFoundException $e) {
throw new \InvalidArgumentException('Error while preparing push notification');
}

if ($shouldFlush) {
$this->push->flushPayloads();
}
}

/**
Expand All @@ -69,10 +74,23 @@ public function getCount(INotification $notification): int {
public function markProcessed(INotification $notification): void {
$deleted = $this->handler->delete($notification);

$shouldFlush = $this->push->deferPayloads();
foreach ($deleted as $user => $notifications) {
foreach ($notifications as $notificationId) {
$this->push->pushDeleteToDevice($user, $notificationId);
}
}

if ($shouldFlush) {
$this->push->flushPayloads();
}
}

public function defer(): void {
$this->push->deferPayloads();
}

public function flush(): void {
$this->push->flushPayloads();
}
}
20 changes: 20 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
use OCA\Notifications\Capabilities;
use OCA\Notifications\Handler;
use OCA\Notifications\Notifier\AdminNotifications;
use OCA\Notifications\Push;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Notification\IApp;
use OCP\Util;

class Application extends \OCP\AppFramework\App {
Expand All @@ -44,6 +47,7 @@ public function __construct() {

public function register(): void {
$this->registerNotificationApp();
$this->registerTalkDeferPushing();
$this->registerAdminNotifications();
$this->registerUserInterface();
$this->registerUserDeleteHook();
Expand Down Expand Up @@ -77,6 +81,22 @@ protected function registerUserInterface(): void {
}
}

protected function registerTalkDeferPushing(): void {
/** @var IEventDispatcher $dispatcher */
$dispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);

$dispatcher->addListener(IApp::class . '::defer', function() {
/** @var App $app */
$app = $this->getContainer()->query(App::class);
$app->defer();
});
$dispatcher->addListener(IApp::class . '::flush', function() {
/** @var App $app */
$app = $this->getContainer()->query(App::class);
$app->flush();
});
}

protected function registerUserDeleteHook(): void {
Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser');
}
Expand Down
22 changes: 13 additions & 9 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function pushToDevice(int $id, INotification $notification): void {
});
$hasTalkApps = !empty($talkApps);

$pushNotifications = [];
foreach ($devices as $device) {
if (!$isTalkNotification && $device['apptype'] === 'talk') {
// The iOS app can not kill notifications,
Expand All @@ -118,8 +117,8 @@ public function pushToDevice(int $id, INotification $notification): void {
$payload = json_encode($this->encryptAndSign($userKey, $device, $id, $notification, $isTalkNotification));

$proxyServer = rtrim($device['proxyserver'], '/');
if (!isset($pushNotifications[$proxyServer])) {
$pushNotifications[$proxyServer] = [];
if (!isset($this->payloadsToSend[$proxyServer])) {
$this->payloadsToSend[$proxyServer] = [];
}
$pushNotifications[$proxyServer][] = $payload;
} catch (InvalidTokenException $e) {
Expand All @@ -131,7 +130,9 @@ public function pushToDevice(int $id, INotification $notification): void {
}
}

$this->sendNotificationsToProxies($pushNotifications);
if (!$this->deferPayloads) {
$this->sendNotificationsToProxies();
}
}

public function pushDeleteToDevice(string $userId, int $notificationId): void {
Expand All @@ -146,14 +147,13 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}

$userKey = $this->keyManager->getKey($user);
$pushNotifications = [];
foreach ($devices as $device) {
try {
$payload = json_encode($this->encryptAndSignDelete($userKey, $device, $notificationId));

$proxyServer = rtrim($device['proxyserver'], '/');
if (!isset($pushNotifications[$proxyServer])) {
$pushNotifications[$proxyServer] = [];
if (!isset($this->payloadsToSend[$proxyServer])) {
$this->payloadsToSend[$proxyServer] = [];
}
$pushNotifications[$proxyServer][] = $payload;
} catch (InvalidTokenException $e) {
Expand All @@ -165,10 +165,14 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}
}

$this->sendNotificationsToProxies($pushNotifications);
if (!$this->deferPayloads) {
$this->sendNotificationsToProxies();
}
}

protected function sendNotificationsToProxies(array $pushNotifications): void {
protected function sendNotificationsToProxies(): void {
$pushNotifications = $this->payloadsToSend;
$this->payloadsToSend = [];
if (empty($pushNotifications)) {
return;
}
Expand Down