Update for the latest event content changes to libquotient

This commit is contained in:
James Graham
2024-10-15 23:27:24 +01:00
parent b42fd3fc51
commit a2ec6d97b1
4 changed files with 140 additions and 40 deletions

View File

@@ -10,6 +10,7 @@
#include <Quotient/events/encryptionevent.h>
#include <Quotient/events/event.h>
#include <Quotient/events/eventcontent.h>
#include <Quotient/events/reactionevent.h>
#include <Quotient/events/redactionevent.h>
#include <Quotient/events/roomavatarevent.h>
@@ -227,7 +228,11 @@ QString EventHandler::rawMessageBody(const Quotient::RoomMessageEvent &event)
if (event.hasFileContent()) {
// if filename is given or body is equal to filename,
// then body is a caption
#if Quotient_VERSION_MINOR > 8
QString filename = event.fileContent()->originalName;
#else
QString filename = event.content()->fileInfo()->originalName;
#endif
QString body = event.plainBody();
if (filename.isEmpty() || filename == body) {
return QString();
@@ -237,7 +242,7 @@ QString EventHandler::rawMessageBody(const Quotient::RoomMessageEvent &event)
if (event.hasTextContent() && event.content()) {
#if Quotient_VERSION_MINOR > 8
body = static_cast<const EventContent::TextContent *>(event.content().get())->body;
body = event.richTextContent()->body;
#else
body = static_cast<const EventContent::TextContent *>(event.content())->body;
#endif
@@ -468,10 +473,14 @@ QString EventHandler::getMessageBody(const NeoChatRoom *room, const RoomMessageE
TextHandler textHandler;
if (event.hasFileContent()) {
auto fileCaption = event.content()->fileInfo()->originalName;
#if Quotient_VERSION_MINOR > 8
QString fileCaption = event.fileContent()->originalName;
#else
QString fileCaption = event.content()->fileInfo()->originalName;
#endif
if (fileCaption.isEmpty()) {
fileCaption = event.plainBody();
} else if (event.content()->fileInfo()->originalName != event.plainBody()) {
} else if (fileCaption != event.plainBody()) {
fileCaption = event.plainBody() + " | "_ls + fileCaption;
}
textHandler.setData(fileCaption);
@@ -481,7 +490,7 @@ QString EventHandler::getMessageBody(const NeoChatRoom *room, const RoomMessageE
QString body;
if (event.hasTextContent() && event.content()) {
#if Quotient_VERSION_MINOR > 8
body = static_cast<const EventContent::TextContent *>(event.content().get())->body;
body = event.richTextContent()->body;
#else
body = static_cast<const EventContent::TextContent *>(event.content())->body;
#endif
@@ -703,29 +712,39 @@ QVariantMap EventHandler::getMediaInfoForEvent(const NeoChatRoom *room, const Qu
return {};
}
const EventContent::FileInfo *fileInfo;
#if Quotient_VERSION_MINOR > 8
const auto content = roomMessageEvent->content();
fileInfo = content->fileInfo();
QVariantMap mediaInfo = getMediaInfoFromFileInfo(room, fileInfo, eventId, false, false);
QVariantMap mediaInfo = getMediaInfoFromFileInfo(room, static_cast<EventContent::FileContent *>(content.get()), eventId, false, false);
#else
const auto content = static_cast<const EventContent::FileContent *>(roomMessageEvent->content());
QVariantMap mediaInfo = getMediaInfoFromFileInfo(room, content, eventId, false, false);
#endif
// if filename isn't specifically given, it is in body
// https://spec.matrix.org/latest/client-server-api/#mfile
mediaInfo["filename"_ls] = (fileInfo->originalName.isEmpty()) ? roomMessageEvent->plainBody() : fileInfo->originalName;
#if Quotient_VERSION_MINOR > 8
mediaInfo["filename"_ls] =
(roomMessageEvent->fileContent()->originalName.isEmpty()) ? roomMessageEvent->plainBody() : roomMessageEvent->fileContent()->originalName;
#else
mediaInfo["filename"_ls] = (content->fileInfo()->originalName.isEmpty()) ? roomMessageEvent->plainBody() : content->fileInfo()->originalName;
#endif
return mediaInfo;
} else if (event->is<StickerEvent>()) {
const EventContent::FileInfo *fileInfo;
auto stickerEvent = eventCast<const StickerEvent>(event);
fileInfo = &stickerEvent->image();
auto content = &stickerEvent->image();
return getMediaInfoFromFileInfo(room, fileInfo, eventId, false, true);
return getMediaInfoFromFileInfo(room, content, eventId, false, true);
} else {
return {};
}
}
QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
const EventContent::FileInfo *fileInfo,
#if Quotient_VERSION_MINOR > 8
const Quotient::EventContent::FileContentBase *fileContent,
#else
const Quotient::EventContent::TypedBase *fileContent,
#endif
const QString &eventId,
bool isThumbnail,
bool isSticker)
@@ -733,10 +752,18 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
QVariantMap mediaInfo;
// Get the mxc URL for the media.
if (!fileInfo->url().isValid() || fileInfo->url().scheme() != QStringLiteral("mxc") || eventId.isEmpty()) {
#if Quotient_VERSION_MINOR > 8
if (!fileContent->url().isValid() || fileContent->url().scheme() != QStringLiteral("mxc") || eventId.isEmpty()) {
#else
if (!fileContent->fileInfo()->url().isValid() || fileContent->fileInfo()->url().scheme() != QStringLiteral("mxc") || eventId.isEmpty()) {
#endif
mediaInfo["source"_ls] = QUrl();
} else {
QUrl source = room->makeMediaUrl(eventId, fileInfo->url());
#if Quotient_VERSION_MINOR > 8
QUrl source = room->makeMediaUrl(eventId, fileContent->url());
#else
QUrl source = room->makeMediaUrl(eventId, fileContent->fileInfo()->url());
#endif
if (source.isValid()) {
mediaInfo["source"_ls] = source;
@@ -745,7 +772,7 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
}
}
auto mimeType = fileInfo->mimeType;
auto mimeType = fileContent->type();
// Add the MIME type for the media if available.
mediaInfo["mimeType"_ls] = mimeType.name();
@@ -753,45 +780,53 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
mediaInfo["mimeIcon"_ls] = mimeType.iconName();
// Add media size if available.
mediaInfo["size"_ls] = fileInfo->payloadSize;
#if Quotient_VERSION_MINOR > 8
mediaInfo["size"_ls] = static_cast<const EventContent::FileContent *>(fileContent)->payloadSize;
#else
mediaInfo["size"_ls] = static_cast<const EventContent::FileContent *>(fileContent)->fileInfo()->payloadSize;
#endif
mediaInfo["isSticker"_ls] = isSticker;
// Add parameter depending on media type.
if (mimeType.name().contains(QStringLiteral("image"))) {
if (auto castInfo = static_cast<const EventContent::ImageContent *>(fileInfo)) {
if (auto castInfo = static_cast<const EventContent::ImageContent *>(fileContent)) {
#if Quotient_VERSION_MINOR > 8
mediaInfo["width"_ls] = castInfo->imageSize.width();
mediaInfo["height"_ls] = castInfo->imageSize.height();
#else
const auto imageInfo = static_cast<const EventContent::ImageInfo *>(castInfo->fileInfo());
mediaInfo["width"_ls] = imageInfo->imageSize.width();
mediaInfo["height"_ls] = imageInfo->imageSize.height();
#endif
// TODO: Images in certain formats (e.g. WebP) will be erroneously marked as animated, even if they are static.
mediaInfo["animated"_ls] = QMovie::supportedFormats().contains(mimeType.preferredSuffix().toUtf8());
if (!isThumbnail) {
QVariantMap tempInfo;
auto thumbnailInfo = getMediaInfoFromFileInfo(room, castInfo->thumbnailInfo(), eventId, true);
if (thumbnailInfo["source"_ls].toUrl().scheme() == "mxc"_ls) {
tempInfo = thumbnailInfo;
QVariantMap tempInfo;
auto thumbnailInfo = getMediaInfoFromTumbnail(room, castInfo->thumbnail, eventId);
if (thumbnailInfo["source"_ls].toUrl().scheme() == "mxc"_ls) {
tempInfo = thumbnailInfo;
} else {
QString blurhash = castInfo->originalInfoJson["xyz.amorgan.blurhash"_ls].toString();
if (blurhash.isEmpty()) {
tempInfo["source"_ls] = QUrl();
} else {
QString blurhash = castInfo->originalInfoJson["xyz.amorgan.blurhash"_ls].toString();
if (blurhash.isEmpty()) {
tempInfo["source"_ls] = QUrl();
} else {
tempInfo["source"_ls] = QUrl("image://blurhash/"_ls + blurhash);
}
tempInfo["source"_ls] = QUrl("image://blurhash/"_ls + blurhash);
}
mediaInfo["tempInfo"_ls] = tempInfo;
}
mediaInfo["tempInfo"_ls] = tempInfo;
}
}
if (mimeType.name().contains(QStringLiteral("video"))) {
if (auto castInfo = static_cast<const EventContent::VideoContent *>(fileInfo)) {
if (auto castInfo = static_cast<const EventContent::VideoContent *>(fileContent)) {
mediaInfo["width"_ls] = castInfo->imageSize.width();
mediaInfo["height"_ls] = castInfo->imageSize.height();
mediaInfo["duration"_ls] = castInfo->duration;
if (!isThumbnail) {
QVariantMap tempInfo;
auto thumbnailInfo = getMediaInfoFromFileInfo(room, castInfo->thumbnailInfo(), eventId, true);
auto thumbnailInfo = getMediaInfoFromTumbnail(room, castInfo->thumbnail, eventId);
if (thumbnailInfo["source"_ls].toUrl().scheme() == "mxc"_ls) {
tempInfo = thumbnailInfo;
} else {
@@ -807,7 +842,7 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
}
}
if (mimeType.name().contains(QStringLiteral("audio"))) {
if (auto castInfo = static_cast<const EventContent::AudioContent *>(fileInfo)) {
if (auto castInfo = static_cast<const EventContent::AudioContent *>(fileContent)) {
mediaInfo["duration"_ls] = castInfo->duration;
}
}
@@ -815,6 +850,38 @@ QVariantMap EventHandler::getMediaInfoFromFileInfo(const NeoChatRoom *room,
return mediaInfo;
}
QVariantMap EventHandler::getMediaInfoFromTumbnail(const NeoChatRoom *room, const Quotient::EventContent::Thumbnail &thumbnail, const QString &eventId)
{
QVariantMap thumbnailInfo;
if (!thumbnail.url().isValid() || thumbnail.url().scheme() != QStringLiteral("mxc") || eventId.isEmpty()) {
thumbnailInfo["source"_ls] = QUrl();
} else {
QUrl source = room->makeMediaUrl(eventId, thumbnail.url());
if (source.isValid()) {
thumbnailInfo["source"_ls] = source;
} else {
thumbnailInfo["source"_ls] = QUrl();
}
}
auto mimeType = thumbnail.mimeType;
// Add the MIME type for the media if available.
thumbnailInfo["mimeType"_ls] = mimeType.name();
// Add the MIME type icon if available.
thumbnailInfo["mimeIcon"_ls] = mimeType.iconName();
// Add media size if available.
thumbnailInfo["size"_ls] = thumbnail.payloadSize;
thumbnailInfo["width"_ls] = thumbnail.imageSize.width();
thumbnailInfo["height"_ls] = thumbnail.imageSize.height();
return thumbnailInfo;
}
bool EventHandler::hasReply(const Quotient::RoomEvent *event, bool showFallbacks)
{
if (event == nullptr) {