Move data parsing into a dedicated function

Actually posting the notification is now the responsibility of a private function
This commit is contained in:
Loren Burkholder 2021-02-15 18:36:10 -05:00 committed by Nicolas Werner
parent 09303ca49f
commit 648844089c
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
7 changed files with 103 additions and 74 deletions

View file

@ -297,6 +297,9 @@ set(SRC_FILES
src/ui/UserProfile.cpp src/ui/UserProfile.cpp
src/ui/RoomSettings.cpp src/ui/RoomSettings.cpp
# Generic notification stuff
src/notifications/Manager.cpp
src/AvatarProvider.cpp src/AvatarProvider.cpp
src/BlurhashProvider.cpp src/BlurhashProvider.cpp
src/Cache.cpp src/Cache.cpp
@ -557,7 +560,7 @@ set(TRANSLATION_DEPS ${LANG_QRC} ${QRC} ${QM_SRC})
if (APPLE) if (APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa")
set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm src/emoji/MacHelper.mm) set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm src/notifications/ManagerMac.cpp src/emoji/MacHelper.mm)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0") if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
set_source_files_properties( src/notifications/ManagerMac.mm src/emoji/MacHelper.mm PROPERTIES SKIP_PRECOMPILE_HEADERS ON) set_source_files_properties( src/notifications/ManagerMac.mm src/emoji/MacHelper.mm PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
endif() endif()

View file

@ -0,0 +1,27 @@
#include "notifications/Manager.h"
#include "Cache.h"
#include "EventAccessors.h"
#include "Utils.h"
#include <mtx/responses/notifications.hpp>
void
NotificationsManager::postNotification(const mtx::responses::Notification &notification,
const QImage &icon)
{
const auto room_id = QString::fromStdString(notification.room_id);
const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event));
const auto room_name =
QString::fromStdString(cache::singleRoomInfo(notification.room_id).name);
const auto sender = cache::displayName(
room_id, QString::fromStdString(mtx::accessors::sender(notification.event)));
QString text;
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
text =
"* " + sender + " " + formatNotification(utils::event_body(notification.event));
else
text = sender + ":" + formatNotification(utils::event_body(notification.event));
systemPostNotification(room_id, event_id, room_name, sender, text, icon);
}

View file

@ -42,6 +42,16 @@ signals:
public slots: public slots:
void removeNotification(const QString &roomId, const QString &eventId); void removeNotification(const QString &roomId, const QString &eventId);
private:
void systemPostNotification(const QString &room_id,
const QString &event_id,
const QString &roomName,
const QString &sender,
const QString &text,
const QImage &icon);
QString formatNotification(const QString &text);
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_HAIKU) #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_HAIKU)
public: public:
void closeNotifications(QString roomId); void closeNotifications(QString roomId);

View file

@ -9,12 +9,7 @@
#include <QImage> #include <QImage>
#include <QTextDocumentFragment> #include <QTextDocumentFragment>
#include "Cache.h"
#include "EventAccessors.h"
#include "MatrixClient.h"
#include "Utils.h" #include "Utils.h"
#include <mtx/responses/notifications.hpp>
#include <cmark.h>
NotificationsManager::NotificationsManager(QObject *parent) NotificationsManager::NotificationsManager(QObject *parent)
: QObject(parent) : QObject(parent)
@ -52,35 +47,24 @@ NotificationsManager::NotificationsManager(QObject *parent)
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
void void
NotificationsManager::postNotification(const mtx::responses::Notification &notification, NotificationsManager::systemPostNotification(const QString &room_id,
const QImage &icon) const QString &event_id,
const QString &roomName,
const QString &sender,
const QString &text,
const QImage &icon)
{ {
const auto room_id = QString::fromStdString(notification.room_id); Q_UNUSED(sender)
const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event));
const auto sender = cache::displayName(
room_id, QString::fromStdString(mtx::accessors::sender(notification.event)));
const auto text = utils::event_body(notification.event);
auto formattedText = utils::markdownToHtml(text);
auto capabilites = dbus.call("GetCapabilites");
if (!capabilites.arguments().contains("body-markup"))
formattedText = QTextDocumentFragment::fromHtml(formattedText).toPlainText();
QVariantMap hints; QVariantMap hints;
hints["image-data"] = icon; hints["image-data"] = icon;
hints["sound-name"] = "message-new-instant"; hints["sound-name"] = "message-new-instant";
QList<QVariant> argumentList; QList<QVariant> argumentList;
argumentList << "nheko"; // app_name argumentList << "nheko"; // app_name
argumentList << (uint)0; // replace_id argumentList << (uint)0; // replace_id
argumentList << ""; // app_icon argumentList << ""; // app_icon
argumentList << QString::fromStdString( argumentList << roomName; // summary
cache::singleRoomInfo(notification.room_id).name); // summary argumentList << text; // body
// body
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
argumentList << "* " + sender + " " + formattedText;
else
argumentList << sender + ": " + formattedText;
// The list of actions has always the action name and then a localized version of that // The list of actions has always the action name and then a localized version of that
// action. Currently we just use an empty string for that. // action. Currently we just use an empty string for that.
@ -167,6 +151,16 @@ NotificationsManager::notificationClosed(uint id, uint reason)
notificationIds.remove(id); notificationIds.remove(id);
} }
QString
NotificationsManager::formatNotification(const QString &text)
{
static auto capabilites = dbus.call("GetCapabilites");
if (capabilites.arguments().contains("body-markup"))
return utils::markdownToHtml(text);
else
return QTextDocumentFragment::fromHtml(utils::markdownToHtml(text)).toPlainText();
}
/** /**
* Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify * Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify
* *

View file

@ -0,0 +1,11 @@
#include "Manager.h"
#include <QTextDocumentFragment>
#include "Utils.h"
QString
NotificationsManager::formatNotification(const QString &text)
{
return utils::markdownToHtml(text);
}

View file

@ -3,13 +3,6 @@
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
#include <QtMac> #include <QtMac>
#include "Cache.h"
#include "EventAccessors.h"
#include "MatrixClient.h"
#include "Utils.h"
#include <mtx/responses/notifications.hpp>
#include <cmark.h>
@interface NSUserNotification (CFIPrivate) @interface NSUserNotification (CFIPrivate)
- (void)set_identityImage:(NSImage *)image; - (void)set_identityImage:(NSImage *)image;
@end @end
@ -20,23 +13,22 @@ NotificationsManager::NotificationsManager(QObject *parent): QObject(parent)
} }
void void
NotificationsManager::postNotification(const mtx::responses::Notification &notification, NotificationsManager::systemPostNotification(const QString &room_id,
const QImage &icon) const QString &event_id,
const QString &roomName,
const QString &sender,
const QString &text,
const QImage &icon)
{ {
Q_UNUSED(icon); Q_UNUSED(room_id)
Q_UNUSED(event_id)
const auto sender = cache::displayName(QString::fromStdString(notification.room_id), QString::fromStdString(mtx::accessors::sender(notification.event))); Q_UNUSED(icon)
const auto text = utils::event_body(notification.event);
const auto formattedText = cmark_markdown_to_html(text.toStdString().c_str(), text.length(), CMARK_OPT_UNSAFE);
NSUserNotification * notif = [[NSUserNotification alloc] init]; NSUserNotification * notif = [[NSUserNotification alloc] init];
notif.title = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name).toNSString(); notif.title = roomName.toNSString();
notif.subtitle = QString("%1 sent a message").arg(sender).toNSString(); notif.subtitle = QString("%1 sent a message").arg(sender).toNSString();
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) notif.informativeText = text.toNSString();
notif.informativeText = QString("* ").append(sender).append(" ").append(formattedText).toNSString();
else
notif.informativeText = formattedText.toNSString();
notif.soundName = NSUserNotificationDefaultSoundName; notif.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notif]; [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notif];

View file

@ -7,12 +7,7 @@
#include <QTextDocumentFragment> #include <QTextDocumentFragment>
#include "Cache.h"
#include "EventAccessors.h"
#include "MatrixClient.h"
#include "Utils.h" #include "Utils.h"
#include <mtx/responses/notifications.hpp>
#include <cmark.h>
using namespace WinToastLib; using namespace WinToastLib;
@ -45,36 +40,27 @@ NotificationsManager::NotificationsManager(QObject *parent)
{} {}
void void
NotificationsManager::postNotification(const mtx::responses::Notification &notification, NotificationsManager::systemPostNotification(const QString &room_id,
const QImage &icon) const QString &event_id,
const QString &roomName,
const QString &sender,
const QString &text,
const QImage &icon)
{ {
Q_UNUSED(room_id)
Q_UNUSED(event_id)
Q_UNUSED(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 text = utils::event_body(notification.event);
const auto formattedText = QTextDocumentFragment::fromHtml(utils::markdownToHtml(text)).toPlainText();
if (!isInitialized) if (!isInitialized)
init(); init();
auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02); auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
if (room_name != sender) if (roomName != sender)
templ.setTextField(QString("%1 - %2").arg(sender).arg(room_name).toStdWString(), templ.setTextField(QString("%1 - %2").arg(sender).arg(roomName).toStdWString(),
WinToastTemplate::FirstLine); WinToastTemplate::FirstLine);
else else
templ.setTextField(QString("%1").arg(sender).toStdWString(), templ.setTextField(sender.toStdWString(), WinToastTemplate::FirstLine);
WinToastTemplate::FirstLine); templ.setTextField(text.toStdWString(), WinToastTemplate::SecondLine);
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
templ.setTextField(
QString("* ").append(sender).append(" ").append(formattedText).toStdWString(),
WinToastTemplate::SecondLine);
else
templ.setTextField(QString("%1").arg(formattedText).toStdWString(),
WinToastTemplate::SecondLine);
// TODO: implement room or user avatar // TODO: implement room or user avatar
// templ.setImagePath(L"C:/example.png"); // templ.setImagePath(L"C:/example.png");
@ -89,3 +75,9 @@ void NotificationsManager::notificationClosed(uint, uint) {}
void void
NotificationsManager::removeNotification(const QString &, const QString &) NotificationsManager::removeNotification(const QString &, const QString &)
{} {}
QString
NotificationsManager::formatNotification(const QString &text)
{
return QTextDocumentFragment::fromHtml(utils::markdownToHtml(text)).toPlainText();
}