Only try to display images if they exist

This commit is contained in:
Loren Burkholder 2021-03-01 20:07:53 -05:00 committed by Nicolas Werner
parent 82bbdfb929
commit 64dd10a6a0
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
3 changed files with 14 additions and 7 deletions

View File

@ -24,7 +24,7 @@ NotificationsManager::cacheImage(const mtx::events::collections::TimelineEvents
http::client()->download( http::client()->download(
url, url,
[path, url, encryptionInfo](const std::string &data, [&path, url, encryptionInfo](const std::string &data,
const std::string &, const std::string &,
const std::string &, const std::string &,
mtx::http::RequestErr err) { mtx::http::RequestErr err) {
@ -53,10 +53,11 @@ NotificationsManager::cacheImage(const mtx::events::collections::TimelineEvents
// resize the image // resize the image
QImage img{utils::readImage(QByteArray{temp.data()})}; QImage img{utils::readImage(QByteArray{temp.data()})};
// make sure to save as PNG (because Plasma doesn't do JPEG in if (img.isNull())
// notifications) {
// if (!file.fileName().endsWith(".png")) path.clear();
// file.rename(file.fileName() + ".png"); return;
}
#ifdef NHEKO_DBUS_SYS // the images in D-Bus notifications are to be 200x100 max #ifdef NHEKO_DBUS_SYS // the images in D-Bus notifications are to be 200x100 max
img.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation) img.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation)

View File

@ -216,12 +216,18 @@ NotificationsManager::formatNotification(const mtx::responses::Notification &not
if (hasMarkup_) { if (hasMarkup_) {
if (hasImages_ && if (hasImages_ &&
mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image) mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image)
{
QString imgPath = cacheImage(notification.event);
if (imgPath.isNull())
return mtx::accessors::formattedBodyWithFallback(notification.event).prepend(messageLeadIn);
else
return QString( return QString(
"<img src=\"file:///" + cacheImage(notification.event) + "<img src=\"file:///" + imgPath +
"\" alt=\"" + "\" alt=\"" +
mtx::accessors::formattedBodyWithFallback(notification.event) + mtx::accessors::formattedBodyWithFallback(notification.event) +
"\">") "\">")
.prepend(messageLeadIn); .prepend(messageLeadIn);
}
return mtx::accessors::formattedBodyWithFallback(notification.event) return mtx::accessors::formattedBodyWithFallback(notification.event)
.prepend(messageLeadIn) .prepend(messageLeadIn)

View File

@ -58,6 +58,6 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
const QString messageInfo = const QString messageInfo =
(isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender); (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender);
objCxxPostNotification( objCxxPostNotification(
room_name, messageInfo, formatNotification(notification), image); room_name, messageInfo, formatNotification(notification), (image != nullptr && !image->isNull()) ? image : nullptr);
} }
} }