From d34067a25792b5d69b2ce3192486189f0db12abb Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 18 Sep 2019 23:37:30 +0200 Subject: [PATCH] Enable read receipts action and sync read receipts from cache --- resources/qml/TimelineView.qml | 1 + src/timeline2/TimelineModel.cpp | 25 ++++++++++++++++++++++++- src/timeline2/TimelineModel.h | 5 +++-- src/timeline2/TimelineViewManager.cpp | 10 ++++++++++ src/timeline2/TimelineViewManager.h | 2 +- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/resources/qml/TimelineView.qml b/resources/qml/TimelineView.qml index 5eb00b06..91b3f173 100644 --- a/resources/qml/TimelineView.qml +++ b/resources/qml/TimelineView.qml @@ -180,6 +180,7 @@ Rectangle { MenuItem { text: "Read receipts" + onTriggered: chat.model.readReceiptsAction(model.id) } MenuItem { text: "Mark as read" diff --git a/src/timeline2/TimelineModel.cpp b/src/timeline2/TimelineModel.cpp index 13429c3e..d0daae25 100644 --- a/src/timeline2/TimelineModel.cpp +++ b/src/timeline2/TimelineModel.cpp @@ -7,6 +7,7 @@ #include "ChatPage.h" #include "Logging.h" +#include "MainWindow.h" #include "Olm.h" #include "Utils.h" #include "dialogs/RawMessage.h" @@ -376,6 +377,8 @@ TimelineModel::data(const QModelIndex &index, int role) const return qml_mtx_events::Failed; else if (pending.contains(id)) return qml_mtx_events::Sent; + else if (read.contains(id)) + return qml_mtx_events::Read; else return qml_mtx_events::Received; default: @@ -664,7 +667,13 @@ TimelineModel::replyAction(QString id) if (related.quoted_body.isEmpty()) return; - emit ChatPage::instance()->messageReply(related); + ChatPage::instance()->messageReply(related); +} + +void +TimelineModel::readReceiptsAction(QString id) const +{ + MainWindow::instance()->openReadReceiptsDialog(id); } int @@ -685,3 +694,17 @@ TimelineModel::indexToId(int index) const return ""; return eventOrder[index]; } + +void +TimelineModel::markEventsAsRead(const std::vector &event_ids) +{ + for (const auto &id : event_ids) { + read.insert(id); + int idx = idToIndex(id); + if (idx < 0) { + nhlog::ui()->warn("Read index out of range"); + return; + } + emit dataChanged(index(idx, 0), index(idx, 0)); + } +} diff --git a/src/timeline2/TimelineModel.h b/src/timeline2/TimelineModel.h index b651708d..2cd22661 100644 --- a/src/timeline2/TimelineModel.h +++ b/src/timeline2/TimelineModel.h @@ -131,6 +131,7 @@ public: Q_INVOKABLE QString escapeEmoji(QString str) const; Q_INVOKABLE void viewRawMessage(QString id) const; Q_INVOKABLE void replyAction(QString id); + Q_INVOKABLE void readReceiptsAction(QString id) const; Q_INVOKABLE int idToIndex(QString id) const; Q_INVOKABLE QString indexToId(int index) const; @@ -146,10 +147,10 @@ public slots: emit currentIndexChanged(index); } int currentIndex() const { return idToIndex(currentId); } + void markEventsAsRead(const std::vector &event_ids); private slots: // Add old events at the top of the timeline. - void addBackwardsEvents(const mtx::responses::Messages &msgs); signals: @@ -165,7 +166,7 @@ private: const std::vector &timeline); QHash events; - QSet pending, failed; + QSet pending, failed, read; std::vector eventOrder; QString room_id_; diff --git a/src/timeline2/TimelineViewManager.cpp b/src/timeline2/TimelineViewManager.cpp index 8233d33e..18297370 100644 --- a/src/timeline2/TimelineViewManager.cpp +++ b/src/timeline2/TimelineViewManager.cpp @@ -52,6 +52,16 @@ TimelineViewManager::setHistoryView(const QString &room_id) } } +void +TimelineViewManager::updateReadReceipts(const QString &room_id, + const std::vector &event_ids) +{ + auto room = models.find(room_id); + if (room != models.end()) { + room.value()->markEventsAsRead(event_ids); + } +} + void TimelineViewManager::initWithMessages(const std::map &msgs) { diff --git a/src/timeline2/TimelineViewManager.h b/src/timeline2/TimelineViewManager.h index c2d1a8c0..52070b97 100644 --- a/src/timeline2/TimelineViewManager.h +++ b/src/timeline2/TimelineViewManager.h @@ -40,7 +40,7 @@ signals: void activeTimelineChanged(TimelineModel *timeline); public slots: - void updateReadReceipts(const QString &room_id, const std::vector &event_ids) {} + void updateReadReceipts(const QString &room_id, const std::vector &event_ids); void removeTimelineEvent(const QString &room_id, const QString &event_id) {} void initWithMessages(const std::map &msgs);