diff options
author | Joas Schilling <coding@schilljs.com> | 2020-06-04 14:46:49 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-07-03 16:44:43 +0200 |
commit | 3d559159f04017b2b0b91a68b5a2da610b7bb109 (patch) | |
tree | 9b3c544f747b9a4cfa93d75752215bc92e248c75 /lib/private/Notification | |
parent | ebedbf157968e40230a102c8f6f17c22990b0aae (diff) | |
download | nextcloud-server-3d559159f04017b2b0b91a68b5a2da610b7bb109.tar.gz nextcloud-server-3d559159f04017b2b0b91a68b5a2da610b7bb109.zip |
Allow notification apps to defer and flush the sending
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Notification')
-rw-r--r-- | lib/private/Notification/Manager.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php index 3a19b0f49e6..f485b37cae7 100644 --- a/lib/private/Notification/Manager.php +++ b/lib/private/Notification/Manager.php @@ -31,6 +31,7 @@ use OCP\AppFramework\QueryException; use OCP\ILogger; use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IApp; +use OCP\Notification\IDeferrableApp; use OCP\Notification\IDismissableNotifier; use OCP\Notification\IManager; use OCP\Notification\INotification; @@ -55,6 +56,8 @@ class Manager implements IManager { /** @var bool */ protected $preparingPushNotification; + /** @var bool */ + protected $deferPushing; public function __construct(IValidator $validator, ILogger $logger) { @@ -65,6 +68,7 @@ class Manager implements IManager { $this->appClasses = []; $this->notifierClasses = []; $this->preparingPushNotification = false; + $this->deferPushing = false; } /** * @param string $appClass The service must implement IApp, otherwise a @@ -200,6 +204,46 @@ class Manager implements IManager { } /** + * The calling app should only "flush" when it got returned true on the defer call + * @return bool + * @since 20.0.0 + */ + public function defer(): bool { + $alreadyDeferring = $this->deferPushing; + $this->deferPushing = true; + + $apps = $this->getApps(); + + foreach ($apps as $app) { + if ($app instanceof IDeferrableApp) { + $app->defer(); + } + } + + return !$alreadyDeferring; + } + + /** + * @since 20.0.0 + */ + public function flush(): void { + $apps = $this->getApps(); + + foreach ($apps as $app) { + if (!$app instanceof IDeferrableApp) { + continue; + } + + try { + $app->flush(); + } catch (\InvalidArgumentException $e) { + } + } + + $this->deferPushing = false; + } + + /** * @param INotification $notification * @throws \InvalidArgumentException When the notification is not valid * @since 8.2.0 |