| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "Manager.h" |
| 6 | |
| 7 | #include <QCoreApplication> |
| 8 | #include <QRegularExpression> |
| 9 | #include <QTextDocumentFragment> |
| 10 | |
| 11 | #include "Cache.h" |
| 12 | #include "EventAccessors.h" |
| 13 | #include "MxcImageProvider.h" |
| 14 | #include "Utils.h" |
| 15 | |
| 16 | #include <mtx/responses/notifications.hpp> |
| 17 | |
| 18 | #include <variant> |
| 19 | |
| 20 | static QString |
| 21 | formatNotification(const mtx::responses::Notification ¬ification) |
| 22 | { |
| 23 | auto fallbacks = utils::stripReplyFallbacks(notification.event, {}, {}); |
| 24 | |
| 25 | bool containsSpoiler = fallbacks.quoted_formatted_body.contains("<span data-mx-spoiler"); |
| 26 | if (containsSpoiler) |
| 27 | return QCoreApplication::translate("macosNotification", "Message contains spoiler."); |
| 28 | else |
| 29 | return fallbacks.quoted_body; |
| 30 | } |
| 31 | |
| 32 | NotificationsManager::NotificationsManager(QObject *parent) |
| 33 | : QObject(parent) |
| 34 | { |
| 35 | // Putting these here to pass along since I'm not sure how |
| 36 | // our translate step interacts with .mm files |
| 37 | respondStr = QObject::tr("Respond"); |
| 38 | sendStr = QObject::tr("Send"); |
| 39 | placeholder = QObject::tr("Write a message..."); |
| 40 | |
| 41 | connect( |
| 42 | this, |
| 43 | &NotificationsManager::systemPostNotificationCb, |
| 44 | this, |
| 45 | [this](const QString &room_id, |
| 46 | const QString &event_id, |
| 47 | const QString &roomName, |
| 48 | const QString &text, |
| 49 | const QImage &) { |
| 50 | objCxxPostNotification(roomName, |
| 51 | room_id, |
| 52 | event_id, |
| 53 | text, |
| 54 | /*const QString &informativeText*/ "", |
| 55 | "", |
| 56 | true); |
| 57 | }, |
| 58 | Qt::QueuedConnection); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | NotificationsManager::postNotification(const mtx::responses::Notification ¬ification, |
| 63 | const QImage &icon) |
| 64 | { |
| 65 | Q_UNUSED(icon) |
| 66 | |
| 67 | const auto room_name = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name); |
| 68 | const auto sender = |
| 69 | cache::displayName(QString::fromStdString(notification.room_id), |
| 70 | QString::fromStdString(mtx::accessors::sender(notification.event))); |
| 71 | |
| 72 | const auto room_id = QString::fromStdString(notification.room_id); |
| 73 | const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event)); |
| 74 | |
| 75 | const auto isEncrypted = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>( |
| 76 | ¬ification.event) != nullptr; |
| 77 | const auto isReply = utils::isReply(notification.event); |
| 78 | |
| 79 | auto playSound = false; |
| 80 | |
| 81 | if (std::find(notification.actions.begin(), |
| 82 | notification.actions.end(), |
| 83 | mtx::pushrules::actions::Action{mtx::pushrules::actions::set_tweak_sound{ |
| 84 | .value = "default"}}) != notification.actions.end()) { |
| 85 | playSound = true; |
| 86 | } |
| 87 | if (isEncrypted) { |
| 88 | // TODO: decrypt this message if the decryption setting is on in the UserSettings |
| 89 | const QString messageInfo = (isReply ? tr("%1 replied with an encrypted message") |
| 90 | : tr("%1 sent an encrypted message")) |
| 91 | .arg(sender); |
| 92 | objCxxPostNotification(room_name, room_id, event_id, messageInfo, "", "", playSound); |
| 93 | } else { |
| 94 | const QString messageInfo = |
| 95 | (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender); |
| 96 | if (allowShowingImages(notification) && |
| 97 | (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image || |
| 98 | mtx::accessors::event_type(notification.event) == mtx::events::EventType::Sticker)) |
| 99 | MxcImageProvider::download( |
| 100 | QString::fromStdString(mtx::accessors::url(notification.event)).remove("mxc://"), |
| 101 | QSize(200, 80), |
| 102 | [this, notification, room_name, room_id, event_id, messageInfo, playSound]( |
| 103 | QString, QSize, QImage, QString imgPath) { |
| 104 | objCxxPostNotification(room_name, |
| 105 | room_id, |
| 106 | event_id, |
| 107 | messageInfo, |
| 108 | formatNotification(notification), |
| 109 | imgPath, |
| 110 | playSound); |
| 111 | }); |
| 112 | else |
| 113 | objCxxPostNotification(room_name, |
| 114 | room_id, |
| 115 | event_id, |
| 116 | messageInfo, |
| 117 | formatNotification(notification), |
| 118 | "", |
| 119 | playSound); |
| 120 | } |
| 121 | } |