From 2b581814f334b7a79ef5e7a9fb31fc421c60a57c Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <roeland@famdouma.nl>
Date: Mon, 9 Dec 2019 13:19:45 +0100
Subject: [PATCH] Add interface for notification handler for dimissed events

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
---
 lib/composer/composer/autoload_classmap.php   |  1 +
 lib/composer/composer/autoload_static.php     |  1 +
 lib/private/Notification/Manager.php          | 15 +++++++
 .../Notification/IDismissableNotifier.php     | 45 +++++++++++++++++++
 lib/public/Notification/IManager.php          |  5 +++
 5 files changed, 67 insertions(+)
 create mode 100644 lib/public/Notification/IDismissableNotifier.php

diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 8d261f34952..563d7cf7c72 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -367,6 +367,7 @@ return array(
     'OCP\\Notification\\AlreadyProcessedException' => $baseDir . '/lib/public/Notification/AlreadyProcessedException.php',
     'OCP\\Notification\\IAction' => $baseDir . '/lib/public/Notification/IAction.php',
     'OCP\\Notification\\IApp' => $baseDir . '/lib/public/Notification/IApp.php',
+    'OCP\\Notification\\IDismissableNotifier' => $baseDir . '/lib/public/Notification/IDismissableNotifier.php',
     'OCP\\Notification\\IManager' => $baseDir . '/lib/public/Notification/IManager.php',
     'OCP\\Notification\\INotification' => $baseDir . '/lib/public/Notification/INotification.php',
     'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 806a36e3c6d..140ec461857 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -396,6 +396,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\Notification\\AlreadyProcessedException' => __DIR__ . '/../../..' . '/lib/public/Notification/AlreadyProcessedException.php',
         'OCP\\Notification\\IAction' => __DIR__ . '/../../..' . '/lib/public/Notification/IAction.php',
         'OCP\\Notification\\IApp' => __DIR__ . '/../../..' . '/lib/public/Notification/IApp.php',
+        'OCP\\Notification\\IDismissableNotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/IDismissableNotifier.php',
         'OCP\\Notification\\IManager' => __DIR__ . '/../../..' . '/lib/public/Notification/IManager.php',
         'OCP\\Notification\\INotification' => __DIR__ . '/../../..' . '/lib/public/Notification/INotification.php',
         'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php',
diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php
index 5d640c66be9..7791258d41a 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\IDismissableNotifier;
 use OCP\Notification\IManager;
 use OCP\Notification\INotification;
 use OCP\Notification\INotifier;
@@ -296,4 +297,18 @@ class Manager implements IManager {
 
 		return $count;
 	}
+
+	public function dismissNotification(INotification $notification): void {
+		$notifiers = $this->getNotifiers();
+
+		foreach ($notifiers as $notifier) {
+			if ($notifier instanceof IDismissableNotifier) {
+				try {
+					$notifier->dismissNotification($notification);
+				} catch (\InvalidArgumentException $e) {
+					continue;
+				}
+			}
+		}
+	}
 }
diff --git a/lib/public/Notification/IDismissableNotifier.php b/lib/public/Notification/IDismissableNotifier.php
new file mode 100644
index 00000000000..61088fc72e0
--- /dev/null
+++ b/lib/public/Notification/IDismissableNotifier.php
@@ -0,0 +1,45 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Notification;
+
+/**
+ * Interface INotifier classes should implement if they want to process notifications
+ * that are dismissed by the user.
+ *
+ * This can be useful if dismissing the notification will leave it in an incomplete
+ * state. The handler can chose to for example do some default action.
+ *
+ * @since 18.0.0
+ */
+interface IDismissableNotifier extends INotifier {
+
+	/**
+	 * @param INotification $notification
+	 * @throws \InvalidArgumentException In case the handler can't handle the notification
+	 *
+	 * @since 18.0.0
+	 */
+	public function dismissNotification(INotification $notification): void;
+}
diff --git a/lib/public/Notification/IManager.php b/lib/public/Notification/IManager.php
index c1c4d881619..2a9b8116a55 100644
--- a/lib/public/Notification/IManager.php
+++ b/lib/public/Notification/IManager.php
@@ -85,4 +85,9 @@ interface IManager extends IApp, INotifier {
 	 * @since 14.0.0
 	 */
 	public function isPreparingPushNotification(): bool;
+
+	/**
+	 * @since 18.0.0
+	 */
+	public function dismissNotification(INotification $notification): void;
 }
-- 
GitLab