Don't send desktop notifications if there are too many of them at once

This commit is contained in:
LcsTen 2023-03-12 10:35:25 +01:00
parent 1af83bb4cc
commit 818e205134
2 changed files with 48 additions and 23 deletions

View file

@ -215,6 +215,12 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent)
if (pushrules) { if (pushrules) {
const auto local_user = utils::localUser().toStdString(); 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) { for (const auto &[room_id, room] : sync.rooms.join) {
// clear old notifications // clear old notifications
for (const auto &e : room.ephemeral.events) { for (const auto &e : room.ephemeral.events) {
@ -334,13 +340,20 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent)
continue; continue;
if (userSettings_->hasDesktopNotifications()) { if (userSettings_->hasDesktopNotifications()) {
auto info = cache::singleRoomInfo(room_id); notifications.emplace_back(roomModel, te, room_id, actions);
}
}
}
}
}
}
if (notifications.size() <= 5) {
for (const auto &[roomModel, te, room_id, actions] : notifications) {
AvatarProvider::resolve( AvatarProvider::resolve(
roomModel->roomAvatarUrl(), roomModel->roomAvatarUrl(),
96, 96,
this, this,
[this, te, room_id = room_id, actions](QPixmap image) { [this, te = te, room_id = room_id, actions = actions](QPixmap image) {
notificationsManager->postNotification( notificationsManager->postNotification(
mtx::responses::Notification{ mtx::responses::Notification{
.actions = actions, .actions = actions,
@ -353,10 +366,19 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent)
image.toImage()); 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());
} }
} }
}); });

View file

@ -187,9 +187,12 @@ NotificationsManager::systemPostNotification(const QString &room_id,
// The list of actions has always the action name and then a localized version of that // 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. // action. Currently we just use an empty string for that.
// TODO(Nico): Look into what to actually put there. // TODO(Nico): Look into what to actually put there.
argumentList << (QStringList(QStringLiteral("default")) QStringList actions;
<< QLatin1String("") << QStringLiteral("inline-reply") actions << QStringList(QStringLiteral("default")) << QLatin1String("");
<< QLatin1String("")); // actions if (!room_id.isEmpty()) {
actions << QStringLiteral("inline-reply") << QLatin1String("");
}
argumentList << actions; // actions
argumentList << hints; // hints argumentList << hints; // hints
argumentList << (int)-1; // timeout in ms argumentList << (int)-1; // timeout in ms