summary refs log tree commit diff
path: root/src/notifications/Manager.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-03-17 19:08:17 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2021-03-17 19:18:07 +0100
commite5d75c814b2175dc37beabff3b0421de59a3e93e (patch)
tree97670bfa02e365ef527ad18fad3d66e16c3ef8d2 /src/notifications/Manager.cpp
parentRefactor image download code to be reusable (diff)
downloadnheko-e5d75c814b2175dc37beabff3b0421de59a3e93e.tar.xz
Clean up notification code a bit
Diffstat (limited to 'src/notifications/Manager.cpp')
-rw-r--r--src/notifications/Manager.cpp104
1 files changed, 25 insertions, 79 deletions
diff --git a/src/notifications/Manager.cpp b/src/notifications/Manager.cpp

index 30e74d33..322213dd 100644 --- a/src/notifications/Manager.cpp +++ b/src/notifications/Manager.cpp
@@ -2,89 +2,35 @@ #include "Cache.h" #include "EventAccessors.h" -#include "Logging.h" -#include "MatrixClient.h" #include "Utils.h" -#include <QFile> -#include <QImage> -#include <QStandardPaths> - -#include <mtxclient/crypto/client.hpp> - QString -NotificationsManager::cacheImage(const mtx::events::collections::TimelineEvents &event) +NotificationsManager::getMessageTemplate(const mtx::responses::Notification &notification) { - const auto url = mtx::accessors::url(event); - auto encryptionInfo = mtx::accessors::file(event); - - auto filename = QString::fromStdString(mtx::accessors::body(event)); - QString path{QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + - filename}; - - bool downloadComplete = false; - - http::client()->download( - url, - [&downloadComplete, &path, url, encryptionInfo](const std::string &data, - const std::string &, - const std::string &, - mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn("failed to retrieve image {}: {} {}", - url, - err->matrix_error.error, - static_cast<int>(err->status_code)); - // the image doesn't exist, so delete the path - path.clear(); - downloadComplete = true; - return; - } - - try { - auto temp = data; - if (encryptionInfo) - temp = mtx::crypto::to_string( - mtx::crypto::decrypt_file(temp, encryptionInfo.value())); - - QFile file{path}; - - if (!file.open(QIODevice::WriteOnly)) { - path.clear(); - downloadComplete = true; - return; - } - - // delete any existing file content - file.resize(0); - - // resize the image - QImage img{utils::readImage(QByteArray{temp.data()})}; - - if (img.isNull()) { - path.clear(); - downloadComplete = true; - return; - } - -#ifdef NHEKO_DBUS_SYS // the images in D-Bus notifications are to be 200x100 max - img.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation) - .save(&file); -#else - img.save(&file); -#endif // NHEKO_DBUS_SYS - - file.close(); - - downloadComplete = true; - return; - } catch (const std::exception &e) { - nhlog::ui()->warn("Error while caching file to: {}", e.what()); - } - }); + const auto sender = + cache::displayName(QString::fromStdString(notification.room_id), + QString::fromStdString(mtx::accessors::sender(notification.event))); - while (!downloadComplete) - continue; + // TODO: decrypt this message if the decryption setting is on in the UserSettings + if (auto msg = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>( + &notification.event); + msg != nullptr) { + return tr("%1 sent an encrypted message").arg(sender); + } - return path.toHtmlEscaped(); + if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) { + return tr("* %1 %2", + "Format an emote message in a notification, %1 is the sender, %2 the " + "message") + .arg(sender); + } else if (utils::isReply(notification.event)) { + return tr("%1 replied: %2", + "Format a reply in a notification. %1 is the sender, %2 the message") + .arg(sender); + } else { + return tr("%1: %2", + "Format a normal message in a notification. %1 is the sender, %2 the " + "message") + .arg(sender); + } }