From 7156bf07112786749c4c7371bab1f766406339e6 Mon Sep 17 00:00:00 2001 From: James Graham Date: Mon, 1 May 2023 09:12:16 +0000 Subject: [PATCH] Link Previewer MXC Links - Update link preview to get valid mxc links. - Get the connection from a room. --- src/linkpreviewer.cpp | 41 +++++++++++++++---- src/linkpreviewer.h | 19 +++++++-- .../Timeline/LinkPreviewDelegate.qml | 8 +++- .../Component/Timeline/MessageDelegate.qml | 1 + 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/linkpreviewer.cpp b/src/linkpreviewer.cpp index ace63c11b..61aca7243 100644 --- a/src/linkpreviewer.cpp +++ b/src/linkpreviewer.cpp @@ -3,11 +3,11 @@ #include "linkpreviewer.h" -#include "controller.h" - #include #include +#include "neochatroom.h" + using namespace Quotient; LinkPreviewer::LinkPreviewer(QObject *parent) @@ -16,6 +16,20 @@ LinkPreviewer::LinkPreviewer(QObject *parent) { } +NeoChatRoom *LinkPreviewer::room() const +{ + return m_currentRoom; +} + +void LinkPreviewer::setRoom(NeoChatRoom *room) +{ + if (room == m_currentRoom) { + return; + } + m_currentRoom = room; + Q_EMIT roomChanged(); +} + bool LinkPreviewer::loaded() const { return m_loaded; @@ -31,7 +45,7 @@ QString LinkPreviewer::description() const return m_description; } -QString LinkPreviewer::imageSource() const +QUrl LinkPreviewer::imageSource() const { return m_imageSource; } @@ -50,15 +64,28 @@ void LinkPreviewer::setUrl(QUrl url) m_url = url; Q_EMIT urlChanged(); - auto conn = Controller::instance().activeConnection(); - + auto conn = m_currentRoom->connection(); GetUrlPreviewJob *job = conn->callApi(m_url.toString()); - connect(job, &BaseJob::success, this, [this, job]() { + connect(job, &BaseJob::success, this, [this, job, conn]() { const auto json = job->jsonData(); m_title = json["og:title"].toString().trimmed(); m_description = json["og:description"].toString().trimmed().replace("\n", " "); - m_imageSource = json["og:image"].toString(); + + auto imageUrl = QUrl(json["og:image"].toString()); + if (imageUrl.isValid() && imageUrl.scheme() == QStringLiteral("mxc")) { +#ifdef QUOTIENT_07 + m_imageSource = conn->makeMediaUrl(imageUrl); +#else + QUrlQuery q(imageUrl.query()); + q.addQueryItem(QStringLiteral("user_id"), conn->userId()); + imageUrl.setQuery(q); + m_imageSource = imageUrl; +#endif + } else { + m_imageSource = QUrl(); + } + m_loaded = true; Q_EMIT titleChanged(); Q_EMIT descriptionChanged(); diff --git a/src/linkpreviewer.h b/src/linkpreviewer.h index ca8f74eec..9ae63eec0 100644 --- a/src/linkpreviewer.h +++ b/src/linkpreviewer.h @@ -6,6 +6,8 @@ #include #include +class NeoChatRoom; + /** * @class LinkPreviewer * @@ -18,6 +20,11 @@ class LinkPreviewer : public QObject { Q_OBJECT + /** + * @brief The current room that the URL is from. + */ + Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged) + /** * @brief The URL to get the preview for. */ @@ -41,26 +48,32 @@ class LinkPreviewer : public QObject /** * @brief The image source for the preview. */ - Q_PROPERTY(QString imageSource READ imageSource NOTIFY imageSourceChanged) + Q_PROPERTY(QUrl imageSource READ imageSource NOTIFY imageSourceChanged) public: explicit LinkPreviewer(QObject *parent = nullptr); + [[nodiscard]] NeoChatRoom *room() const; + void setRoom(NeoChatRoom *room); + [[nodiscard]] QUrl url() const; void setUrl(QUrl); [[nodiscard]] bool loaded() const; [[nodiscard]] QString title() const; [[nodiscard]] QString description() const; - [[nodiscard]] QString imageSource() const; + [[nodiscard]] QUrl imageSource() const; private: + NeoChatRoom *m_currentRoom = nullptr; + bool m_loaded; QString m_title; QString m_description; - QString m_imageSource; + QUrl m_imageSource; QUrl m_url; Q_SIGNALS: + void roomChanged(); void loadedChanged(); void titleChanged(); void descriptionChanged(); diff --git a/src/qml/Component/Timeline/LinkPreviewDelegate.qml b/src/qml/Component/Timeline/LinkPreviewDelegate.qml index 86c0595e8..1dfa81a06 100644 --- a/src/qml/Component/Timeline/LinkPreviewDelegate.qml +++ b/src/qml/Component/Timeline/LinkPreviewDelegate.qml @@ -13,6 +13,11 @@ import org.kde.neochat 1.0 Loader { id: root + /** + * @brief The room that the component is created in. + */ + property var room + /** * @brief Get a list of hyperlinks in the text. * @@ -33,6 +38,7 @@ Loader { } LinkPreviewer { id: linkPreviewer + room: root.room url: root.links && root.links.length > 0 ? root.links[0] : "" } @@ -76,7 +82,7 @@ Loader { visible: linkPreviewer.imageSource Layout.maximumHeight: root.defaultHeight Layout.maximumWidth: root.defaultHeight - source: linkPreviewer.imageSource.replace("mxc://", "image://mxc/") + source: linkPreviewer.imageSource fillMode: Image.PreserveAspectFit } ColumnLayout { diff --git a/src/qml/Component/Timeline/MessageDelegate.qml b/src/qml/Component/Timeline/MessageDelegate.qml index 6dd0f1dc0..242ae97c3 100644 --- a/src/qml/Component/Timeline/MessageDelegate.qml +++ b/src/qml/Component/Timeline/MessageDelegate.qml @@ -35,6 +35,7 @@ TimelineContainer { } LinkPreviewDelegate { Layout.fillWidth: true + room: currentRoom indicatorEnabled: messageDelegate.isVisibleInTimeline() } }