From 802ba78fea363134bd4215dc8ea0124bceb9b38d Mon Sep 17 00:00:00 2001 From: Joseph Donofry Date: Tue, 15 Nov 2022 19:04:38 -0500 Subject: [PATCH] Only play macOS notification sounds when pushrules dictate it --- src/notifications/Manager.h | 3 ++- src/notifications/ManagerMac.cpp | 29 ++++++++++++++++++++++++----- src/notifications/ManagerMac.mm | 7 +++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/notifications/Manager.h b/src/notifications/Manager.h index de678738..9c992767 100644 --- a/src/notifications/Manager.h +++ b/src/notifications/Manager.h @@ -81,7 +81,8 @@ private: const QString &bodyImagePath, const QString &respondStr, const QString &sendStr, - const QString &placeholder); + const QString &placeholder, + const bool playSound); public: static void attachToMacNotifCenter(); diff --git a/src/notifications/ManagerMac.cpp b/src/notifications/ManagerMac.cpp index 75ea838c..630080d4 100644 --- a/src/notifications/ManagerMac.cpp +++ b/src/notifications/ManagerMac.cpp @@ -47,13 +47,29 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if const auto sendStr = QObject::tr("Send"); const auto placeholder = QObject::tr("Write a message..."); + auto playSound = false; + + if (std::find(notification.actions.begin(), + notification.actions.end(), + mtx::pushrules::actions::Action{mtx::pushrules::actions::set_tweak_sound{ + .value = "default"}}) != notification.actions.end()) { + playSound = true; + } if (isEncrypted) { // TODO: decrypt this message if the decryption setting is on in the UserSettings const QString messageInfo = (isReply ? tr("%1 replied with an encrypted message") : tr("%1 sent an encrypted message")) .arg(sender); - objCxxPostNotification( - room_name, room_id, event_id, messageInfo, "", "", respondStr, sendStr, placeholder); + objCxxPostNotification(room_name, + room_id, + event_id, + messageInfo, + "", + "", + respondStr, + sendStr, + placeholder, + playSound); } else { const QString messageInfo = (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender); @@ -69,7 +85,8 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if messageInfo, respondStr, sendStr, - placeholder](QString, QSize, QImage, QString imgPath) { + placeholder, + playSound](QString, QSize, QImage, QString imgPath) { objCxxPostNotification(room_name, room_id, event_id, @@ -78,7 +95,8 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if imgPath, respondStr, sendStr, - placeholder); + placeholder, + playSound); }); else objCxxPostNotification(room_name, @@ -89,6 +107,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if "", respondStr, sendStr, - placeholder); + placeholder, + playSound); } } diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm index 4865e30d..39312465 100644 --- a/src/notifications/ManagerMac.mm +++ b/src/notifications/ManagerMac.mm @@ -75,7 +75,8 @@ void NotificationsManager::objCxxPostNotification( const QString& bodyImagePath, const QString& respondStr, const QString& sendStr, - const QString& placeholder) + const QString& placeholder, + const bool enableSound) { // Request permissions for alerts (the generic type of notification), sound playback, // and badges (which allows the Nheko app icon to show the little red bubble with unread count). @@ -128,7 +129,9 @@ void NotificationsManager::objCxxPostNotification( content.title = title; content.subtitle = sub; content.body = body; - content.sound = [UNNotificationSound defaultSound]; + if (enableSound) { + content.sound = [UNNotificationSound defaultSound]; + } content.threadIdentifier = threadIdentifier; content.categoryIdentifier = @"ReplyCategory";