matrixion/src/notifications/ManagerMac.cpp

64 lines
2.5 KiB
C++
Raw Normal View History

#include "Manager.h"
#include <QRegularExpression>
#include <QTextDocumentFragment>
#include "Cache.h"
#include "EventAccessors.h"
#include "Utils.h"
#include <mtx/responses/notifications.hpp>
2021-02-26 23:33:27 +03:00
#include <variant>
QString
NotificationsManager::formatNotification(const mtx::responses::Notification &notification)
{
const auto sender =
cache::displayName(QString::fromStdString(notification.room_id),
QString::fromStdString(mtx::accessors::sender(notification.event)));
return QTextDocumentFragment::fromHtml(
mtx::accessors::formattedBodyWithFallback(notification.event)
2021-02-26 23:38:46 +03:00
.replace(QRegularExpression("<mx-reply>.+</mx-reply>"), ""))
.toPlainText()
.prepend((mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
? "* " + sender + " "
: "");
}
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)));
2021-03-03 04:35:03 +03:00
QImage image;
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image)
2021-03-03 04:35:03 +03:00
image = QImage{cacheImage(notification.event)};
2021-02-26 23:33:27 +03:00
const auto isEncrypted =
std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
&notification.event) != nullptr;
const auto isReply = utils::isReply(notification.event);
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, messageInfo, "", image);
} else {
const QString messageInfo =
(isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender);
objCxxPostNotification(
2021-03-03 03:43:25 +03:00
room_name, messageInfo, formatNotification(notification), image);
2021-02-26 23:33:27 +03:00
}
}