From 818e20513433a38d0104cbc13c68bff5394f505c Mon Sep 17 00:00:00 2001 From: LcsTen Date: Sun, 12 Mar 2023 10:35:25 +0100 Subject: [PATCH 1/2] Don't send desktop notifications if there are too many of them at once --- src/ChatPage.cpp | 58 ++++++++++++++++++++---------- src/notifications/ManagerLinux.cpp | 13 ++++--- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 4e0d373c..495aa3c3 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -215,6 +215,12 @@ ChatPage::ChatPage(QSharedPointer userSettings, QObject *parent) if (pushrules) { const auto local_user = utils::localUser().toStdString(); + // Desktop notifications to be sent + std::vector, + mtx::events::collections::TimelineEvents, + std::string, + std::vector>> + notifications; for (const auto &[room_id, room] : sync.rooms.join) { // clear old notifications for (const auto &e : room.ephemeral.events) { @@ -334,30 +340,46 @@ ChatPage::ChatPage(QSharedPointer userSettings, QObject *parent) continue; if (userSettings_->hasDesktopNotifications()) { - auto info = cache::singleRoomInfo(room_id); - - AvatarProvider::resolve( - roomModel->roomAvatarUrl(), - 96, - this, - [this, te, room_id = room_id, actions](QPixmap image) { - notificationsManager->postNotification( - mtx::responses::Notification{ - .actions = actions, - .event = std::move(te), - .read = false, - .profile_tag = "", - .room_id = room_id, - .ts = 0, - }, - image.toImage()); - }); + notifications.emplace_back(roomModel, te, room_id, actions); } } } } } } + if (notifications.size() <= 5) { + for (const auto &[roomModel, te, room_id, actions] : notifications) { + AvatarProvider::resolve( + roomModel->roomAvatarUrl(), + 96, + this, + [this, te = te, room_id = room_id, actions = actions](QPixmap image) { + notificationsManager->postNotification( + mtx::responses::Notification{ + .actions = actions, + .event = std::move(te), + .read = false, + .profile_tag = "", + .room_id = room_id, + .ts = 0, + }, + image.toImage()); + }); + } + } else if (!notifications.empty()) { + std::map, std::size_t> missedEvents; + for (const auto &[roomModel, te, room_id, actions] : notifications) { + missedEvents[roomModel]++; + } + QString body; + for (const auto &[roomModel, nbNotifs] : missedEvents) { + body += tr("%1 unread messages in room %2\n") + .arg(nbNotifs) + .arg(roomModel->roomName()); + } + emit notificationsManager->systemPostNotificationCb( + "", "", "New messages while away", body, QImage()); + } } }); diff --git a/src/notifications/ManagerLinux.cpp b/src/notifications/ManagerLinux.cpp index 6babb447..fc92c9ae 100644 --- a/src/notifications/ManagerLinux.cpp +++ b/src/notifications/ManagerLinux.cpp @@ -187,11 +187,14 @@ NotificationsManager::systemPostNotification(const QString &room_id, // The list of actions has always the action name and then a localized version of that // action. Currently we just use an empty string for that. // TODO(Nico): Look into what to actually put there. - argumentList << (QStringList(QStringLiteral("default")) - << QLatin1String("") << QStringLiteral("inline-reply") - << QLatin1String("")); // actions - argumentList << hints; // hints - argumentList << (int)-1; // timeout in ms + QStringList actions; + actions << QStringList(QStringLiteral("default")) << QLatin1String(""); + if (!room_id.isEmpty()) { + actions << QStringLiteral("inline-reply") << QLatin1String(""); + } + argumentList << actions; // actions + argumentList << hints; // hints + argumentList << (int)-1; // timeout in ms QDBusPendingCall call = dbus.asyncCallWithArgumentList(QStringLiteral("Notify"), argumentList); auto watcher = new QDBusPendingCallWatcher{call, this}; From 7e85e9b12b9cc3360da7e03d4d70f1fdf3d8cab5 Mon Sep 17 00:00:00 2001 From: LcsTen Date: Mon, 27 Mar 2023 15:44:53 +0200 Subject: [PATCH 2/2] Do not display reply action in the recap notif on Mac --- src/notifications/ManagerMac.mm | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm index 39312465..6da5d006 100644 --- a/src/notifications/ManagerMac.mm +++ b/src/notifications/ManagerMac.mm @@ -106,10 +106,18 @@ void NotificationsManager::objCxxPostNotification( textInputButtonTitle:sendStr.toNSString() textInputPlaceholder:placeholder.toNSString()]; - UNNotificationCategory* category = [UNNotificationCategory categoryWithIdentifier:@"ReplyCategory" - actions:@[ replyAction ] - intentIdentifiers:@[] - options:UNNotificationCategoryOptionNone]; + UNNotificationCategory* category; + if(!room_id.isEmpty()){ + category = [UNNotificationCategory categoryWithIdentifier:@"ReplyCategory" + actions:@[ replyAction ] + intentIdentifiers:@[] + options:UNNotificationCategoryOptionNone]; + }else{ + category = [UNNotificationCategory categoryWithIdentifier:@"ReplyCategory" + actions:@[] + intentIdentifiers:@[] + options:UNNotificationCategoryOptionNone]; + } NSString* title = room_name.toNSString(); NSString* sub = subtitle.toNSString();