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>
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<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) {
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<QString, QDateTime> &a, const QPair<QString, QDateTime> &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);

@ -8,40 +8,7 @@
#include <QAbstractListModel>
#include <QObject>
#include <QString>
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 <QDateTime>
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<uint64_t, std::string, std::greater<uint64_t>> &users);
private:
QString dateFormat(const QDateTime &then) const;
QString event_id_;
QString room_id_;
QVector<ReadReceipt *> readReceipts_;
std::multimap<uint64_t, std::string, std::greater<uint64_t>> users_;
QVector<QPair<QString, QDateTime>> readReceipts_;
};
#endif // READRECEIPTSMODEL_H

Loading…
Cancel
Save