Просмотр исходного кода

Add notification cache

The notification cache helps to not display duplicate desktop
notifications to the user.

Signed-off-by: Felix Weilbach <felix.weilbach@nextcloud.com>
Felix Weilbach 4 лет назад
Родитель
Сommit
b736355985

+ 1 - 0
src/gui/CMakeLists.txt

@@ -115,6 +115,7 @@ set(client_SRCS
     tray/ActivityListModel.cpp
     tray/UserModel.cpp
     tray/NotificationHandler.cpp
+    tray/NotificationCache.cpp
     creds/credentialsfactory.cpp
     creds/httpcredentialsgui.cpp
     creds/oauth.cpp

+ 24 - 0
src/gui/tray/NotificationCache.cpp

@@ -0,0 +1,24 @@
+#include "NotificationCache.h"
+
+namespace OCC {
+
+bool NotificationCache::contains(const Notification &notification) const
+{
+    return _notifications.find(calculateKey(notification)) != _notifications.end();
+}
+
+void NotificationCache::insert(const Notification &notification)
+{
+    _notifications.insert(calculateKey(notification));
+}
+
+void NotificationCache::clear()
+{
+    _notifications.clear();
+}
+
+uint NotificationCache::calculateKey(const Notification &notification) const
+{
+    return qHash(notification.title + notification.message);
+}
+}

+ 28 - 0
src/gui/tray/NotificationCache.h

@@ -0,0 +1,28 @@
+#pragma once
+
+#include <QSet>
+
+namespace OCC {
+
+class NotificationCache
+{
+public:
+    struct Notification
+    {
+        QString title;
+        QString message;
+    };
+
+    bool contains(const Notification &notification) const;
+
+    void insert(const Notification &notification);
+
+    void clear();
+
+private:
+    uint calculateKey(const Notification &notification) const;
+
+
+    QSet<uint> _notifications;
+};
+}

+ 1 - 0
test/CMakeLists.txt

@@ -56,6 +56,7 @@ nextcloud_add_test(FolderWatcher)
 nextcloud_add_test(Capabilities)
 nextcloud_add_test(PushNotifications)
 nextcloud_add_test(Theme)
+nextcloud_add_test(NotificationCache)
 
 if( UNIX AND NOT APPLE )
     nextcloud_add_test(InotifyWatcher)

+ 40 - 0
test/testnotificationcache.cpp

@@ -0,0 +1,40 @@
+#include <QTest>
+
+#include "tray/NotificationCache.h"
+
+class TestNotificationCache : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void testContains_doesNotContainNotification_returnsFalse()
+    {
+        OCC::NotificationCache notificationCache;
+
+        QVERIFY(!notificationCache.contains({ "Title", { "Message" } }));
+    }
+
+    void testContains_doesContainNotification_returnTrue()
+    {
+        OCC::NotificationCache notificationCache;
+        const OCC::NotificationCache::Notification notification { "Title", "message" };
+
+        notificationCache.insert(notification);
+
+        QVERIFY(notificationCache.contains(notification));
+    }
+
+    void testClear_doesContainNotification_clearNotifications()
+    {
+        OCC::NotificationCache notificationCache;
+        const OCC::NotificationCache::Notification notification { "Title", "message" };
+
+        notificationCache.insert(notification);
+        notificationCache.clear();
+
+        QVERIFY(!notificationCache.contains(notification));
+    }
+};
+
+QTEST_GUILESS_MAIN(TestNotificationCache)
+#include "testnotificationcache.moc"