Skip to content

Commit

Permalink
fix(Notify): Ungroup dbus notifications
Browse files Browse the repository at this point in the history
Send a new notification for each event instead of reusing one notification.
  • Loading branch information
Pigpog committed Jan 12, 2025
1 parent 4a1cad0 commit 9f4b8a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 108 deletions.
117 changes: 10 additions & 107 deletions src/model/notificationgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,13 @@
#include <QCollator>

namespace {
size_t getNumMessages(const QHash<const Friend*, size_t>& friendNotifications,
const QHash<const Conference*, size_t>& conferenceNotifications)
{
auto numMessages = std::accumulate(friendNotifications.begin(), friendNotifications.end(), 0);
numMessages =
std::accumulate(conferenceNotifications.begin(), conferenceNotifications.end(), numMessages);

return numMessages;
}

size_t getNumChats(const QHash<const Friend*, size_t>& friendNotifications,
const QHash<const Conference*, size_t>& conferenceNotifications)
{
return friendNotifications.size() + conferenceNotifications.size();
}

QString generateMultiChatTitle(size_t numChats, size_t numMessages)
{
//: e.g. 3 messages from 2 chats
return QObject::tr("%1 message(s) from %2 chats").arg(numMessages).arg(numChats);
}

template <typename T>
QString generateSingleChatTitle(const QHash<T, size_t> numNotifications, T contact)
{
if (numNotifications[contact] > 1) {
//: e.g. 2 messages from Bob
return QObject::tr("%1 message(s) from %2")
.arg(numNotifications[contact])
.arg(contact->getDisplayedName());
}
return contact->getDisplayedName();
}

QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
const QHash<const Conference*, size_t>& conferenceNotifications, const Friend* f)
{
auto numChats = getNumChats(friendNotifications, conferenceNotifications);
if (numChats > 1) {
return generateMultiChatTitle(numChats,
getNumMessages(friendNotifications, conferenceNotifications));
}
return generateSingleChatTitle(friendNotifications, f);
}

QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
const QHash<const Conference*, size_t>& conferenceNotifications,
const Conference* c)
{
auto numChats = getNumChats(friendNotifications, conferenceNotifications);
if (numChats > 1) {
return generateMultiChatTitle(numChats,
getNumMessages(friendNotifications, conferenceNotifications));
}
return generateSingleChatTitle(conferenceNotifications, c);
}

QString generateContent(const QHash<const Friend*, size_t>& friendNotifications,
const QHash<const Conference*, size_t>& conferenceNotifications,
QString lastMessage, const ToxPk& sender)
{
assert(!friendNotifications.empty() || !conferenceNotifications.empty());

auto numChats = getNumChats(friendNotifications, conferenceNotifications);
if (numChats > 1) {
// Copy all names into a vector to simplify formatting logic between
// multiple lists
std::vector<QString> displayNames;
displayNames.reserve(numChats);

for (auto it = friendNotifications.begin(); it != friendNotifications.end(); ++it) {
displayNames.push_back(it.key()->getDisplayedName());
}

for (auto it = conferenceNotifications.begin(); it != conferenceNotifications.end(); ++it) {
displayNames.push_back(it.key()->getDisplayedName());
}

assert(!displayNames.empty());

// Lexicographically sort all display names to ensure consistent formatting
QCollator collator;
std::sort(displayNames.begin(), displayNames.end(),
[&](const QString& a, const QString& b) { return collator.compare(a, b) < 1; });

auto it = displayNames.begin();

QString ret = *it;

while (++it != displayNames.end()) {
ret += ", " + *it;
}

return ret;
}
if (conferenceNotifications.size() == 1) {
auto it = conferenceNotifications.begin();
if (it == conferenceNotifications.end()) {
Expand Down Expand Up @@ -141,9 +53,8 @@ NotificationData NotificationGenerator::friendMessageNotification(const Friend*
return ret;
}

ret.title = generateTitle(friendNotifications, conferenceNotifications, f);
ret.message =
generateContent(friendNotifications, conferenceNotifications, message, f->getPublicKey());
ret.title = f->getDisplayedName();
ret.message = message;
ret.category = "im.received";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

Expand All @@ -158,11 +69,12 @@ NotificationData NotificationGenerator::incomingCallNotification(const Friend* f

if (notificationSettings.getNotifyHide()) {
ret.title = tr("Incoming call");
ret.category = "call.incoming";
return ret;
}

ret.title = generateTitle(friendNotifications, conferenceNotifications, f);
ret.message = generateContent(friendNotifications, conferenceNotifications, "", f->getPublicKey());
ret.title = f->getDisplayedName();
ret.message = tr("Incoming call");
ret.category = "call.incoming";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

Expand All @@ -182,8 +94,9 @@ NotificationData NotificationGenerator::conferenceMessageNotification(const Conf
return ret;
}

ret.title = generateTitle(friendNotifications, conferenceNotifications, c);
ret.title = c->getDisplayedName();
ret.message = generateContent(friendNotifications, conferenceNotifications, message, sender);
ret.category = "im.received";
ret.pixmap = getSenderAvatar(profile, sender);

return ret;
Expand All @@ -202,19 +115,9 @@ NotificationData NotificationGenerator::fileTransferNotification(const Friend* f
return ret;
}

auto numChats = getNumChats(friendNotifications, conferenceNotifications);
auto numMessages = getNumMessages(friendNotifications, conferenceNotifications);

if (numChats > 1 || numMessages > 1) {
ret.title = generateTitle(friendNotifications, conferenceNotifications, f);
ret.message = generateContent(friendNotifications, conferenceNotifications,
tr("Incoming file transfer"), f->getPublicKey());
} else {
//: e.g. Bob - file transfer
ret.title = tr("%1 - file transfer").arg(f->getDisplayedName());
ret.message = filename + " (" + getHumanReadableSize(fileSize) + ")";
}

//: e.g. Bob - file transfer
ret.title = tr("%1 - file transfer").arg(f->getDisplayedName());
ret.message = filename + " (" + getHumanReadableSize(fileSize) + ")";
ret.category = "transfer";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ bool DesktopNotifyBackend::showMessage(const QString& title, const QString& mess
// app_name
QApplication::applicationName(),
// replaces_id
d->id,
static_cast<uint32_t>(0),
// app_icon
QStringLiteral("dialog-password"),
// summary
Expand Down

0 comments on commit 9f4b8a0

Please sign in to comment.