summary refs log tree commit diff
path: root/src/notifications/ManagerMac.cpp
blob: ee5639e40f6c3439dd4bde5983f622e3fceb6371 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "Manager.h"

#include <QCoreApplication>
#include <QRegularExpression>
#include <QTextDocumentFragment>

#include "Cache.h"
#include "EventAccessors.h"
#include "MxcImageProvider.h"
#include "Utils.h"

#include <mtx/responses/notifications.hpp>

#include <variant>

static QString
formatNotification(const mtx::responses::Notification &notification)
{
    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)
  : QObject(parent)
{
    // Putting these here to pass along since I'm not sure how
    // our translate step interacts with .mm files
    respondStr  = QObject::tr("Respond");
    sendStr     = QObject::tr("Send");
    placeholder = QObject::tr("Write a message...");

    connect(
      this,
      &NotificationsManager::systemPostNotificationCb,
      this,
      [this](const QString &room_id,
             const QString &event_id,
             const QString &roomName,
             const QString &text,
             const QImage &) {
          objCxxPostNotification(roomName,
                                 room_id,
                                 event_id,
                                 text,
                                 /*const QString &informativeText*/ "",
                                 "",
                                 true);
      },
      Qt::QueuedConnection);
}

void
NotificationsManager::postNotification(const mtx::responses::Notification &notification,
                                       const QImage &icon)
{
    Q_UNUSED(icon)

    const auto room_name = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name);
    const auto sender =
      cache::displayName(QString::fromStdString(notification.room_id),
                         QString::fromStdString(mtx::accessors::sender(notification.event)));

    const auto room_id  = QString::fromStdString(notification.room_id);
    const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event));

    const auto isEncrypted = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
                               &notification.event) != nullptr;
    const auto isReply = utils::isReply(notification.event);

    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, "", "", playSound);
    } else {
        const QString messageInfo =
          (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender);
        if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image)
            MxcImageProvider::download(
              QString::fromStdString(mtx::accessors::url(notification.event)).remove("mxc://"),
              QSize(200, 80),
              [this, notification, room_name, room_id, event_id, messageInfo, playSound](
                QString, QSize, QImage, QString imgPath) {
                  objCxxPostNotification(room_name,
                                         room_id,
                                         event_id,
                                         messageInfo,
                                         formatNotification(notification),
                                         imgPath,
                                         playSound);
              });
        else
            objCxxPostNotification(room_name,
                                   room_id,
                                   event_id,
                                   messageInfo,
                                   formatNotification(notification),
                                   "",
                                   playSound);
    }
}