Simplify read receipt storage

pull/655/head
Loren Burkholder 3 years ago
parent 774a9fdc3a
commit 0d42909e40
  1. 52
      src/ReadReceiptsModel.cpp
  2. 41
      src/ReadReceiptsModel.h

@ -26,12 +26,6 @@ ReadReceiptsModel::ReadReceiptsModel(QString event_id, QString room_id, QObject
} }
} }
ReadReceiptsModel::~ReadReceiptsModel()
{
for (const auto &item : readReceipts_)
item->deleteLater();
}
QHash<int, QByteArray> QHash<int, QByteArray>
ReadReceiptsModel::roleNames() const ReadReceiptsModel::roleNames() const
{ {
@ -49,13 +43,13 @@ ReadReceiptsModel::data(const QModelIndex &index, int role) const
switch (role) { switch (role) {
case Mxid: case Mxid:
return readReceipts_[index.row()]->mxid(); return readReceipts_[index.row()].first;
case DisplayName: case DisplayName:
return readReceipts_[index.row()]->displayName(); return cache::displayName(room_id_, readReceipts_[index.row()].first);
case AvatarUrl: case AvatarUrl:
return readReceipts_[index.row()]->avatarUrl(); return cache::avatarUrl(room_id_, readReceipts_[index.row()].first);
case Timestamp: case Timestamp:
return readReceipts_[index.row()]->timestamp(); return dateFormat(readReceipts_[index.row()].second);
default: default:
return {}; return {};
} }
@ -65,41 +59,25 @@ void
ReadReceiptsModel::addUsers( ReadReceiptsModel::addUsers(
const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users) const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users)
{ {
std::multimap<uint64_t, std::string, std::greater<uint64_t>> unshown; beginInsertRows(QModelIndex{}, readReceipts_.length(), users.size() - 1);
readReceipts_.clear();
for (const auto &user : users) { for (const auto &user : users) {
if (users_.find(user.first) == users_.end()) readReceipts_.push_back({QString::fromStdString(user.second),
unshown.emplace(user); QDateTime::fromMSecsSinceEpoch(user.first)});
} }
beginInsertRows( std::sort(readReceipts_.begin(),
QModelIndex{}, readReceipts_.length(), readReceipts_.length() + unshown.size() - 1); readReceipts_.end(),
[](const QPair<QString, QDateTime> &a, const QPair<QString, QDateTime> &b) {
for (const auto &user : unshown) return a.second > b.second;
readReceipts_.push_back( });
new ReadReceipt{QString::fromStdString(user.second), room_id_, user.first, this});
users_.merge(unshown);
endInsertRows(); 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 QString
ReadReceipt::dateFormat(const QDateTime &then) const ReadReceiptsModel::dateFormat(const QDateTime &then) const
{ {
auto now = QDateTime::currentDateTime(); auto now = QDateTime::currentDateTime();
auto days = then.daysTo(now); auto days = then.daysTo(now);

@ -8,40 +8,7 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QDateTime>
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_;
};
class ReadReceiptsModel : public QAbstractListModel class ReadReceiptsModel : public QAbstractListModel
{ {
@ -60,7 +27,6 @@ public:
}; };
explicit ReadReceiptsModel(QString event_id, QString room_id, QObject *parent = nullptr); explicit ReadReceiptsModel(QString event_id, QString room_id, QObject *parent = nullptr);
~ReadReceiptsModel() override;
QString eventId() const { return event_id_; } QString eventId() const { return event_id_; }
QString roomId() const { return room_id_; } QString roomId() const { return room_id_; }
@ -77,10 +43,11 @@ public slots:
void addUsers(const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users); void addUsers(const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users);
private: private:
QString dateFormat(const QDateTime &then) const;
QString event_id_; QString event_id_;
QString room_id_; QString room_id_;
QVector<ReadReceipt *> readReceipts_; QVector<QPair<QString, QDateTime>> readReceipts_;
std::multimap<uint64_t, std::string, std::greater<uint64_t>> users_;
}; };
#endif // READRECEIPTSMODEL_H #endif // READRECEIPTSMODEL_H

Loading…
Cancel
Save