diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2024-05-16 12:53:34 +0200 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2024-05-16 13:12:37 +0200 |
commit | 61509792706bf39860ee52bd92ca04f74af112bd (patch) | |
tree | 97c11f9e53d6331accd8195a6eb4a943a680765f /src | |
parent | Windows screenshare/video call support, general call improvements (#1725) (diff) | |
download | nheko-61509792706bf39860ee52bd92ca04f74af112bd.tar.xz |
Don't show spoilers in sidebar or notifications if possible
fixes #1247
Diffstat (limited to 'src')
-rw-r--r-- | src/Utils.cpp | 61 | ||||
-rw-r--r-- | src/notifications/Manager.cpp | 31 | ||||
-rw-r--r-- | src/notifications/ManagerLinux.cpp | 5 | ||||
-rw-r--r-- | src/notifications/ManagerMac.cpp | 9 | ||||
-rw-r--r-- | src/notifications/ManagerWin.cpp | 3 |
5 files changed, 84 insertions, 25 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp index b4d45472..8b8a11dc 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -45,9 +45,10 @@ namespace { template<class T> QString -messageDescription(const QString &username = QString(), - const QString &body = QString(), - const bool isLocal = false) +messageDescription(const QString &username, + const QString &body, + const bool isLocal, + bool containsSpoiler) { using Audio = mtx::events::RoomEvent<mtx::events::msg::Audio>; using Emote = mtx::events::RoomEvent<mtx::events::msg::Emote>; @@ -105,6 +106,16 @@ messageDescription(const QString &username = QString(), "%1 sent a notification") .arg(username); } else if (std::is_same<T, Text>::value || std::is_same<T, Unknown>::value) { + if (containsSpoiler) { + if (isLocal) + return QCoreApplication::translate("message-description sent:", + "You sent a spoiler."); + else + return QCoreApplication::translate("message-description sent:", + "%1 sent a spoiler.") + .arg(username); + } + if (isLocal) return QCoreApplication::translate("message-description sent:", "You: %1").arg(body); else @@ -121,6 +132,16 @@ messageDescription(const QString &username = QString(), "%1 sent a chat effect") .arg(username); } else { + if (containsSpoiler) { + if (isLocal) + return QCoreApplication::translate("message-description sent:", + "You sent a spoiler."); + else + return QCoreApplication::translate("message-description sent:", + "%1 sent a spoiler.") + .arg(username); + } + if (isLocal) return QCoreApplication::translate("message-description sent:", "You: %1") .arg(body); @@ -129,6 +150,12 @@ messageDescription(const QString &username = QString(), .arg(username, body); } } else if (std::is_same<T, Emote>::value) { + if (containsSpoiler) { + return QCoreApplication::translate("message-description sent:", + "* %1 spoils something.") + .arg(username); + } + return QStringLiteral("* %1 %2").arg(username, body); } else if (std::is_same<T, Encrypted>::value) { if (isLocal) @@ -178,16 +205,22 @@ createDescriptionInfo(const Event &event, const QString &localUser, const QStrin const auto username = displayName; const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts); auto body = mtx::accessors::body(event); - if (mtx::accessors::relations(event).reply_to()) - body = utils::stripReplyFromBody(body); - - return DescInfo{ - QString::fromStdString(msg.event_id), - sender, - messageDescription<T>(username, QString::fromStdString(body), sender == localUser), - utils::descriptiveTime(ts), - msg.origin_server_ts, - ts}; + auto formatted_body = mtx::accessors::formatted_body(event); + if (mtx::accessors::relations(event).reply_to()) { + body = utils::stripReplyFromBody(body); + formatted_body = utils::stripReplyFromFormattedBody(formatted_body); + } + + // Simplistic heuristic + bool containsSpoiler = formatted_body.find("<span data-mx-spoiler") != formatted_body.npos; + + return DescInfo{QString::fromStdString(msg.event_id), + sender, + messageDescription<T>( + username, QString::fromStdString(body), sender == localUser, containsSpoiler), + utils::descriptiveTime(ts), + msg.origin_server_ts, + ts}; } std::string @@ -402,7 +435,7 @@ utils::getMessageDescription(const mtx::events::collections::TimelineEvents &eve DescInfo info; info.userid = sender; info.body = QStringLiteral(" %1").arg( - messageDescription<Encrypted>(username, QLatin1String(""), sender == localUser)); + messageDescription<Encrypted>(username, QLatin1String(""), sender == localUser, false)); info.timestamp = msg->origin_server_ts; info.descriptiveTime = utils::descriptiveTime(ts); info.event_id = QString::fromStdString(msg->event_id); diff --git a/src/notifications/Manager.cpp b/src/notifications/Manager.cpp index 895beaf7..d15eea51 100644 --- a/src/notifications/Manager.cpp +++ b/src/notifications/Manager.cpp @@ -22,14 +22,31 @@ NotificationsManager::getMessageTemplate(const mtx::responses::Notification ¬ return tr("%1 sent an encrypted message").arg(sender); } - if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) { - return QStringLiteral("* %1 %2").arg(sender); - } else if (utils::isReply(notification.event)) { - return tr("%1 replied: %2", - "Format a reply in a notification. %1 is the sender, %2 the message") - .arg(sender); + bool containsSpoiler = + mtx::accessors::formatted_body(notification.event).find("<span data-mx-spoiler") != + std::string::npos; + + if (containsSpoiler) { + // Because we skip the %2 here, this might cause a warning in some cases. + if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) { + return QStringLiteral("* %1 spoils something.").arg(sender); + } else if (utils::isReply(notification.event)) { + return tr("%1 replied with a spoiler.", + "Format a reply in a notification. %1 is the sender.") + .arg(sender); + } else { + return QStringLiteral("%1 sent a spoiler.").arg(sender); + } } else { - return QStringLiteral("%1: %2").arg(sender); + if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) { + return QStringLiteral("* %1 %2").arg(sender); + } else if (utils::isReply(notification.event)) { + return tr("%1 replied: %2", + "Format a reply in a notification. %1 is the sender, %2 the message") + .arg(sender); + } else { + return QStringLiteral("%1: %2").arg(sender); + } } } diff --git a/src/notifications/ManagerLinux.cpp b/src/notifications/ManagerLinux.cpp index 11a7c1a1..75ba1886 100644 --- a/src/notifications/ManagerLinux.cpp +++ b/src/notifications/ManagerLinux.cpp @@ -107,7 +107,8 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if QString template_ = getMessageTemplate(notification); // TODO: decrypt this message if the decryption setting is on in the UserSettings if (std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>( - notification.event)) { + notification.event) || + !template_.contains("%2")) { postNotif(template_); return; } @@ -115,7 +116,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if if (hasMarkup_) { if (hasImages_ && (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image || - mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image)) { + mtx::accessors::event_type(notification.event) == mtx::events::EventType::Sticker)) { MxcImageProvider::download( QString::fromStdString(mtx::accessors::url(notification.event)) .remove(QStringLiteral("mxc://")), diff --git a/src/notifications/ManagerMac.cpp b/src/notifications/ManagerMac.cpp index 627e9315..ee5639e4 100644 --- a/src/notifications/ManagerMac.cpp +++ b/src/notifications/ManagerMac.cpp @@ -4,6 +4,7 @@ #include "Manager.h" +#include <QCoreApplication> #include <QRegularExpression> #include <QTextDocumentFragment> @@ -19,7 +20,13 @@ static QString formatNotification(const mtx::responses::Notification ¬ification) { - return utils::stripReplyFallbacks(notification.event, {}, {}).quoted_body; + auto fallbacks = utils::stripReplyFallbacks(notification.event, {}, {}); + + bool containsSpoiler = fallbacks.quoted_formatted_body.contains("<span data-mx-spoiler"); + if (containsSpoiler) + return QCoreApplication::translate("macosNotification", "Message contains spoiler."); + else + return fallbacks.quoted_body; } NotificationsManager::NotificationsManager(QObject *parent) diff --git a/src/notifications/ManagerWin.cpp b/src/notifications/ManagerWin.cpp index f1d3797b..a69afa4e 100644 --- a/src/notifications/ManagerWin.cpp +++ b/src/notifications/ManagerWin.cpp @@ -70,7 +70,8 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if auto formatNotification = [this, notification] { const auto template_ = getMessageTemplate(notification); if (std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>( - notification.event)) { + notification.event) || + !template_.contains("%2")) { return template_; } |