summary refs log tree commit diff
path: root/src/ChatPage.cpp
diff options
context:
space:
mode:
authorLcsTen <lcs10.twinoid@gmail.com>2023-03-12 10:35:25 +0100
committerLcsTen <lcs10.twinoid@gmail.com>2023-04-14 19:57:33 +0200
commit818e20513433a38d0104cbc13c68bff5394f505c (patch)
tree67f440e4e529d418e91bbe0127cfa1353d86847a /src/ChatPage.cpp
parentOptimize alphabetical sorting a bit (diff)
downloadnheko-818e20513433a38d0104cbc13c68bff5394f505c.tar.xz
Don't send desktop notifications if there are too many of them at once
Diffstat (limited to 'src/ChatPage.cpp')
-rw-r--r--src/ChatPage.cpp58
1 files changed, 40 insertions, 18 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> userSettings, QObject *parent) if (pushrules) { const auto local_user = utils::localUser().toStdString(); + // Desktop notifications to be sent + std::vector<std::tuple<QSharedPointer<TimelineModel>, + mtx::events::collections::TimelineEvents, + std::string, + std::vector<mtx::pushrules::actions::Action>>> + 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> 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<QSharedPointer<TimelineModel>, 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()); + } } });