From 2fe010c04a90bb232f077a513a7ef6e31a97621a Mon Sep 17 00:00:00 2001 From: Loren Burkholder Date: Sat, 24 Jul 2021 15:35:28 -0400 Subject: [PATCH] Dynamically update read receipts --- resources/qml/ReadReceipts.qml | 1 - src/ReadReceiptsModel.cpp | 27 ++++++++++++++++++++++++--- src/ReadReceiptsModel.h | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/resources/qml/ReadReceipts.qml b/resources/qml/ReadReceipts.qml index b3bca9db..0756a2e7 100644 --- a/resources/qml/ReadReceipts.qml +++ b/resources/qml/ReadReceipts.qml @@ -118,7 +118,6 @@ ApplicationWindow { footer: DialogButtonBox { standardButtons: DialogButtonBox.Ok onAccepted: readReceiptsRoot.close() - } } diff --git a/src/ReadReceiptsModel.cpp b/src/ReadReceiptsModel.cpp index 8ee9cf45..8a371922 100644 --- a/src/ReadReceiptsModel.cpp +++ b/src/ReadReceiptsModel.cpp @@ -7,6 +7,7 @@ #include #include "Cache.h" +#include "Cache_p.h" #include "Logging.h" #include "Utils.h" @@ -16,10 +17,26 @@ ReadReceiptsModel::ReadReceiptsModel(QString event_id, QString room_id, QObject , room_id_{room_id} { try { - addUsers(cache::readReceipts(event_id, room_id)); + addUsers(cache::readReceipts(event_id_, room_id_)); } catch (const lmdb::error &) { nhlog::db()->warn("failed to retrieve read receipts for {} {}", - event_id.toStdString(), + event_id_.toStdString(), + room_id_.toStdString()); + + return; + } + + connect(cache::client(), &Cache::newReadReceipts, this, &ReadReceiptsModel::update); +} + +void +ReadReceiptsModel::update() +{ + try { + addUsers(cache::readReceipts(event_id_, room_id_)); + } catch (const lmdb::error &) { + nhlog::db()->warn("failed to retrieve read receipts for {} {}", + event_id_.toStdString(), room_id_.toStdString()); return; @@ -59,7 +76,9 @@ void ReadReceiptsModel::addUsers( const std::multimap> &users) { - beginInsertRows(QModelIndex{}, readReceipts_.length(), users.size() - 1); + auto oldLen = readReceipts_.length(); + + beginInsertRows(QModelIndex{}, oldLen, users.size() - 1); readReceipts_.clear(); for (const auto &user : users) { @@ -74,6 +93,8 @@ ReadReceiptsModel::addUsers( }); endInsertRows(); + + emit dataChanged(index(0), index(oldLen - 1)); } QString diff --git a/src/ReadReceiptsModel.h b/src/ReadReceiptsModel.h index d7ff5fb8..f2e39f88 100644 --- a/src/ReadReceiptsModel.h +++ b/src/ReadReceiptsModel.h @@ -41,6 +41,7 @@ public: public slots: void addUsers(const std::multimap> &users); + void update(); private: QString dateFormat(const QDateTime &then) const;