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 "controller.h"
#include <connection.h>
#include <csapi/content-repo.h>
#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<GetUrlPreviewJob>(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();

View File

@@ -6,6 +6,8 @@
#include <QObject>
#include <QUrl>
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();

View File

@@ -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 {

View File

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