Link Previewer MXC Links

- Update link preview to get valid mxc links.
- Get the connection from a room.
This commit is contained in:
James Graham
2023-05-01 09:12:16 +00:00
parent bc317514e6
commit 7156bf0711
4 changed files with 58 additions and 11 deletions

View File

@@ -3,11 +3,11 @@
#include "linkpreviewer.h" #include "linkpreviewer.h"
#include "controller.h"
#include <connection.h> #include <connection.h>
#include <csapi/content-repo.h> #include <csapi/content-repo.h>
#include "neochatroom.h"
using namespace Quotient; using namespace Quotient;
LinkPreviewer::LinkPreviewer(QObject *parent) 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 bool LinkPreviewer::loaded() const
{ {
return m_loaded; return m_loaded;
@@ -31,7 +45,7 @@ QString LinkPreviewer::description() const
return m_description; return m_description;
} }
QString LinkPreviewer::imageSource() const QUrl LinkPreviewer::imageSource() const
{ {
return m_imageSource; return m_imageSource;
} }
@@ -50,15 +64,28 @@ void LinkPreviewer::setUrl(QUrl url)
m_url = url; m_url = url;
Q_EMIT urlChanged(); Q_EMIT urlChanged();
auto conn = Controller::instance().activeConnection(); auto conn = m_currentRoom->connection();
GetUrlPreviewJob *job = conn->callApi<GetUrlPreviewJob>(m_url.toString()); GetUrlPreviewJob *job = conn->callApi<GetUrlPreviewJob>(m_url.toString());
connect(job, &BaseJob::success, this, [this, job]() { connect(job, &BaseJob::success, this, [this, job, conn]() {
const auto json = job->jsonData(); const auto json = job->jsonData();
m_title = json["og:title"].toString().trimmed(); m_title = json["og:title"].toString().trimmed();
m_description = json["og:description"].toString().trimmed().replace("\n", " "); 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; m_loaded = true;
Q_EMIT titleChanged(); Q_EMIT titleChanged();
Q_EMIT descriptionChanged(); Q_EMIT descriptionChanged();

View File

@@ -6,6 +6,8 @@
#include <QObject> #include <QObject>
#include <QUrl> #include <QUrl>
class NeoChatRoom;
/** /**
* @class LinkPreviewer * @class LinkPreviewer
* *
@@ -18,6 +20,11 @@ class LinkPreviewer : public QObject
{ {
Q_OBJECT 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. * @brief The URL to get the preview for.
*/ */
@@ -41,26 +48,32 @@ class LinkPreviewer : public QObject
/** /**
* @brief The image source for the preview. * @brief The image source for the preview.
*/ */
Q_PROPERTY(QString imageSource READ imageSource NOTIFY imageSourceChanged) Q_PROPERTY(QUrl imageSource READ imageSource NOTIFY imageSourceChanged)
public: public:
explicit LinkPreviewer(QObject *parent = nullptr); explicit LinkPreviewer(QObject *parent = nullptr);
[[nodiscard]] NeoChatRoom *room() const;
void setRoom(NeoChatRoom *room);
[[nodiscard]] QUrl url() const; [[nodiscard]] QUrl url() const;
void setUrl(QUrl); void setUrl(QUrl);
[[nodiscard]] bool loaded() const; [[nodiscard]] bool loaded() const;
[[nodiscard]] QString title() const; [[nodiscard]] QString title() const;
[[nodiscard]] QString description() const; [[nodiscard]] QString description() const;
[[nodiscard]] QString imageSource() const; [[nodiscard]] QUrl imageSource() const;
private: private:
NeoChatRoom *m_currentRoom = nullptr;
bool m_loaded; bool m_loaded;
QString m_title; QString m_title;
QString m_description; QString m_description;
QString m_imageSource; QUrl m_imageSource;
QUrl m_url; QUrl m_url;
Q_SIGNALS: Q_SIGNALS:
void roomChanged();
void loadedChanged(); void loadedChanged();
void titleChanged(); void titleChanged();
void descriptionChanged(); void descriptionChanged();

View File

@@ -13,6 +13,11 @@ import org.kde.neochat 1.0
Loader { Loader {
id: root id: root
/**
* @brief The room that the component is created in.
*/
property var room
/** /**
* @brief Get a list of hyperlinks in the text. * @brief Get a list of hyperlinks in the text.
* *
@@ -33,6 +38,7 @@ Loader {
} }
LinkPreviewer { LinkPreviewer {
id: linkPreviewer id: linkPreviewer
room: root.room
url: root.links && root.links.length > 0 ? root.links[0] : "" url: root.links && root.links.length > 0 ? root.links[0] : ""
} }
@@ -76,7 +82,7 @@ Loader {
visible: linkPreviewer.imageSource visible: linkPreviewer.imageSource
Layout.maximumHeight: root.defaultHeight Layout.maximumHeight: root.defaultHeight
Layout.maximumWidth: root.defaultHeight Layout.maximumWidth: root.defaultHeight
source: linkPreviewer.imageSource.replace("mxc://", "image://mxc/") source: linkPreviewer.imageSource
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
} }
ColumnLayout { ColumnLayout {

View File

@@ -35,6 +35,7 @@ TimelineContainer {
} }
LinkPreviewDelegate { LinkPreviewDelegate {
Layout.fillWidth: true Layout.fillWidth: true
room: currentRoom
indicatorEnabled: messageDelegate.isVisibleInTimeline() indicatorEnabled: messageDelegate.isVisibleInTimeline()
} }
} }