From 1ca4120fd997fc01b6fd9f51ff825a78e173a685 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 22 Mar 2025 11:16:50 +0000 Subject: [PATCH] Reply to messages with images Reply to messages with images (and technically any other attachment file type) --- src/chatbar/ChatBar.qml | 19 +++++++++++++++---- src/chatbarcache.cpp | 28 ++++++++++++++++------------ src/neochatroom.cpp | 11 ++++++++--- src/neochatroom.h | 4 ++-- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/chatbar/ChatBar.qml b/src/chatbar/ChatBar.qml index 3dfd841a4..02915e218 100644 --- a/src/chatbar/ChatBar.qml +++ b/src/chatbar/ChatBar.qml @@ -163,15 +163,26 @@ QQC2.Control { Layout.fillWidth: true } Loader { - id: paneLoader + id: replyLoader Layout.fillWidth: true Layout.margins: Kirigami.Units.largeSpacing Layout.preferredHeight: active ? item.implicitHeight : 0 active: visible - visible: root.currentRoom.mainCache.replyId.length > 0 || root.currentRoom.mainCache.attachmentPath.length > 0 - sourceComponent: root.currentRoom.mainCache.replyId.length > 0 ? replyPane : attachmentPane + visible: root.currentRoom.mainCache.replyId.length > 0 + sourceComponent: replyPane + } + Loader { + id: attachLoader + + Layout.fillWidth: true + Layout.margins: Kirigami.Units.largeSpacing + Layout.preferredHeight: active ? item.implicitHeight : 0 + + active: visible + visible: root.currentRoom.mainCache.attachmentPath.length > 0 + sourceComponent: attachmentPane } RowLayout { QQC2.ScrollView { @@ -349,7 +360,7 @@ QQC2.Control { replyEventId: _private.chatBarCache.replyId replyAuthor: _private.chatBarCache.relationAuthor replyContentModel: _private.chatBarCache.relationEventContentModel - Message.maxContentWidth: paneLoader.item.width + Message.maxContentWidth: replyLoader.item.width } QQC2.Button { id: cancelButton diff --git a/src/chatbarcache.cpp b/src/chatbarcache.cpp index c1f90a53f..2b227abaf 100644 --- a/src/chatbarcache.cpp +++ b/src/chatbarcache.cpp @@ -213,11 +213,14 @@ void ChatBarCache::setAttachmentPath(const QString &attachmentPath) return; } m_attachmentPath = attachmentPath; + Q_EMIT attachmentPathChanged(); + +#if (Quotient_VERSION_MINOR < 10 && Quotient_VERSION_PATCH < 3) || Quotient_VERSION_MINOR < 9 m_relationType = None; const auto oldEventId = std::exchange(m_relationId, QString()); delete m_relationContentModel; - Q_EMIT attachmentPathChanged(); Q_EMIT relationIdChanged(oldEventId, m_relationId); +#endif } void ChatBarCache::clearRelations() @@ -302,8 +305,19 @@ void ChatBarCache::postMessage() return; } + bool isReply = !replyId().isEmpty(); + std::optional relatesTo = std::nullopt; + + if (!threadId().isEmpty()) { + relatesTo = Quotient::EventRelation::replyInThread(threadId(), !isReply, isReply ? replyId() : threadId()); + } else if (!editId().isEmpty()) { + relatesTo = Quotient::EventRelation::replace(editId()); + } else if (isReply) { + relatesTo = Quotient::EventRelation::replyTo(replyId()); + } + if (!attachmentPath().isEmpty()) { - room->uploadFile(QUrl(attachmentPath()), sendText()); + room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo); clearCache(); return; } @@ -321,22 +335,12 @@ void ChatBarCache::postMessage() return; } - bool isReply = !replyId().isEmpty(); const auto replyIt = room->findInTimeline(replyId()); if (replyIt == room->historyEdge()) { isReply = false; } auto content = std::make_unique(sendText, u"text/html"_s); - std::optional relatesTo = std::nullopt; - - if (!threadId().isEmpty()) { - relatesTo = Quotient::EventRelation::replyInThread(threadId(), !isReply, isReply ? replyId() : threadId()); - } else if (!editId().isEmpty()) { - relatesTo = Quotient::EventRelation::replace(editId()); - } else if (isReply) { - relatesTo = Quotient::EventRelation::replyTo(replyId()); - } room->post(text(), *std::get>(result), std::move(content), relatesTo); clearCache(); diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index c2ed653d0..058d4ad8a 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -221,12 +221,12 @@ void NeoChatRoom::setFileUploadingProgress(int value) Q_EMIT fileUploadingProgressChanged(); } -void NeoChatRoom::uploadFile(const QUrl &url, const QString &body) +void NeoChatRoom::uploadFile(const QUrl &url, const QString &body, std::optional relatesTo) { - doUploadFile(url, body); + doUploadFile(url, body, relatesTo); } -QCoro::Task NeoChatRoom::doUploadFile(QUrl url, QString body) +QCoro::Task NeoChatRoom::doUploadFile(QUrl url, QString body, std::optional relatesTo) { if (url.isEmpty()) { co_return; @@ -250,7 +250,12 @@ QCoro::Task NeoChatRoom::doUploadFile(QUrl url, QString body) } else { content = new EventContent::FileContent(url, fileInfo.size(), mime, fileInfo.fileName()); } + +#if Quotient_VERSION_MINOR > 9 || (Quotient_VERSION_MINOR == 9 && Quotient_VERSION_PATCH > 2) + QString txnId = postFile(body.isEmpty() ? url.fileName() : body, std::unique_ptr(content), relatesTo); +#else QString txnId = postFile(body.isEmpty() ? url.fileName() : body, std::unique_ptr(content)); +#endif setHasFileUploading(true); connect(this, &Room::fileTransferCompleted, [this, txnId](const QString &id, FileSourceInfo) { if (id == txnId) { diff --git a/src/neochatroom.h b/src/neochatroom.h index 20a1e3769..1ffa8aca1 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -636,7 +636,7 @@ private: void onRedaction(const Quotient::RoomEvent &prevEvent, const Quotient::RoomEvent &after) override; QCoro::Task doDeleteMessagesByUser(const QString &user, QString reason); - QCoro::Task doUploadFile(QUrl url, QString body = QString()); + QCoro::Task doUploadFile(QUrl url, QString body = QString(), std::optional relatesTo = std::nullopt); std::unique_ptr m_cachedEvent; @@ -694,7 +694,7 @@ public Q_SLOTS: * @param url the location of the file to be uploaded. * @param body the caption that is to be given to the file. */ - void uploadFile(const QUrl &url, const QString &body = QString()); + void uploadFile(const QUrl &url, const QString &body = QString(), std::optional relatesTo = std::nullopt); /** * @brief Accept an invitation for the local user to join the room.