From 0d42909e406821b76c32b37af758a3721ea1238d Mon Sep 17 00:00:00 2001 From: Loren Burkholder Date: Fri, 23 Jul 2021 22:19:48 -0400 Subject: [PATCH] Simplify read receipt storage --- src/ReadReceiptsModel.cpp | 52 +++++++++++---------------------------- src/ReadReceiptsModel.h | 41 +++--------------------------- 2 files changed, 19 insertions(+), 74 deletions(-) diff --git a/src/ReadReceiptsModel.cpp b/src/ReadReceiptsModel.cpp index eadb4e74..8ee9cf45 100644 --- a/src/ReadReceiptsModel.cpp +++ b/src/ReadReceiptsModel.cpp @@ -26,12 +26,6 @@ ReadReceiptsModel::ReadReceiptsModel(QString event_id, QString room_id, QObject } } -ReadReceiptsModel::~ReadReceiptsModel() -{ - for (const auto &item : readReceipts_) - item->deleteLater(); -} - QHash ReadReceiptsModel::roleNames() const { @@ -49,13 +43,13 @@ ReadReceiptsModel::data(const QModelIndex &index, int role) const switch (role) { case Mxid: - return readReceipts_[index.row()]->mxid(); + return readReceipts_[index.row()].first; case DisplayName: - return readReceipts_[index.row()]->displayName(); + return cache::displayName(room_id_, readReceipts_[index.row()].first); case AvatarUrl: - return readReceipts_[index.row()]->avatarUrl(); + return cache::avatarUrl(room_id_, readReceipts_[index.row()].first); case Timestamp: - return readReceipts_[index.row()]->timestamp(); + return dateFormat(readReceipts_[index.row()].second); default: return {}; } @@ -65,41 +59,25 @@ void ReadReceiptsModel::addUsers( const std::multimap> &users) { - std::multimap> unshown; + beginInsertRows(QModelIndex{}, readReceipts_.length(), users.size() - 1); + + readReceipts_.clear(); for (const auto &user : users) { - if (users_.find(user.first) == users_.end()) - unshown.emplace(user); + readReceipts_.push_back({QString::fromStdString(user.second), + QDateTime::fromMSecsSinceEpoch(user.first)}); } - beginInsertRows( - QModelIndex{}, readReceipts_.length(), readReceipts_.length() + unshown.size() - 1); - - for (const auto &user : unshown) - readReceipts_.push_back( - new ReadReceipt{QString::fromStdString(user.second), room_id_, user.first, this}); - - users_.merge(unshown); + std::sort(readReceipts_.begin(), + readReceipts_.end(), + [](const QPair &a, const QPair &b) { + return a.second > b.second; + }); endInsertRows(); } -ReadReceipt::ReadReceipt(QString mxid, QString room_id, uint64_t timestamp, QObject *parent) - : QObject{parent} - , mxid_{mxid} - , room_id_{room_id} - , displayName_{cache::displayName(room_id_, mxid_)} - , avatarUrl_{cache::avatarUrl(room_id_, mxid_)} - , timestamp_{timestamp} -{} - -QString -ReadReceipt::timestamp() const -{ - return dateFormat(QDateTime::fromMSecsSinceEpoch(timestamp_)); -} - QString -ReadReceipt::dateFormat(const QDateTime &then) const +ReadReceiptsModel::dateFormat(const QDateTime &then) const { auto now = QDateTime::currentDateTime(); auto days = then.daysTo(now); diff --git a/src/ReadReceiptsModel.h b/src/ReadReceiptsModel.h index d90bf7c1..98e41f8f 100644 --- a/src/ReadReceiptsModel.h +++ b/src/ReadReceiptsModel.h @@ -8,40 +8,7 @@ #include #include #include - -class ReadReceipt : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QString mxid READ mxid CONSTANT) - Q_PROPERTY(QString displayName READ displayName NOTIFY displayNameChanged) - Q_PROPERTY(QString avatarUrl READ avatarUrl NOTIFY avatarUrlChanged) - Q_PROPERTY(QString timestamp READ timestamp CONSTANT) - -public: - explicit ReadReceipt(QString mxid, - QString room_id, - uint64_t timestamp, - QObject *parent = nullptr); - - QString mxid() const { return mxid_; } - QString displayName() const { return displayName_; } - QString avatarUrl() const { return avatarUrl_; } - QString timestamp() const; - -signals: - void displayNameChanged(); - void avatarUrlChanged(); - -private: - QString dateFormat(const QDateTime &then) const; - - QString mxid_; - QString room_id_; - QString displayName_; - QString avatarUrl_; - uint64_t timestamp_; -}; +#include class ReadReceiptsModel : public QAbstractListModel { @@ -60,7 +27,6 @@ public: }; explicit ReadReceiptsModel(QString event_id, QString room_id, QObject *parent = nullptr); - ~ReadReceiptsModel() override; QString eventId() const { return event_id_; } QString roomId() const { return room_id_; } @@ -77,10 +43,11 @@ public slots: void addUsers(const std::multimap> &users); private: + QString dateFormat(const QDateTime &then) const; + QString event_id_; QString room_id_; - QVector readReceipts_; - std::multimap> users_; + QVector> readReceipts_; }; #endif // READRECEIPTSMODEL_H