Rewrite notification posting logic

This does away with the nice abstraction layers in order to easily get the best-looking notifications for each platform.
This commit is contained in:
Nicolas Werner 2021-03-17 19:17:57 +01:00
parent 37acdad928
commit f578272a0d
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
6 changed files with 261 additions and 83 deletions

View file

@ -6,8 +6,10 @@
#include "wintoastlib.h"
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextDocumentFragment>
#include "Cache.h"
#include "EventAccessors.h"
#include "Utils.h"
@ -42,17 +44,25 @@ NotificationsManager::NotificationsManager(QObject *parent)
{}
void
NotificationsManager::systemPostNotification(const QString &room_id,
const QString &event_id,
const QString &roomName,
NotificationsManager::postNotification(const mtx::responses::Notification &notification,
const QImage &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 text = formatNotification(notification);
systemPostNotification(room_name, sender, text, icon);
}
void
NotificationsManager::systemPostNotification(const QString &roomName,
const QString &sender,
const QString &text,
const QImage &icon)
{
Q_UNUSED(room_id)
Q_UNUSED(event_id)
Q_UNUSED(icon)
if (!isInitialized)
init();
@ -63,8 +73,11 @@ NotificationsManager::systemPostNotification(const QString &room_id,
else
templ.setTextField(sender.toStdWString(), WinToastTemplate::FirstLine);
templ.setTextField(text.toStdWString(), WinToastTemplate::SecondLine);
// TODO: implement room or user avatar
// templ.setImagePath(L"C:/example.png");
auto iconPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + roomName +
"-room-avatar.png";
if (icon.save(iconPath))
templ.setImagePath(iconPath.toStdWString());
WinToast::instance()->showToast(templ, new CustomHandler());
}
@ -79,10 +92,17 @@ NotificationsManager::removeNotification(const QString &, const QString &)
{}
QString
NotificationsManager::formatNotification(const mtx::events::collections::TimelineEvents &e)
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(e).replace(
QRegularExpression("(<mx-reply>.+\\<\\/mx-reply\\>)"), ""))
.toPlainText();
mtx::accessors::formattedBodyWithFallback(notification.event)
.replace(QRegularExpression("(<mx-reply>.+\\<\\/mx-reply\\>)"), ""))
.toPlainText()
.prepend((mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
? "* " + sender + " "
: "");
}