2021-07-24 01:11:33 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "ReadReceiptsModel.h"
|
|
|
|
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
|
|
#include "Cache.h"
|
2021-07-24 22:35:28 +03:00
|
|
|
#include "Cache_p.h"
|
2021-07-24 01:11:33 +03:00
|
|
|
#include "Logging.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
ReadReceiptsModel::ReadReceiptsModel(QString event_id, QString room_id, QObject *parent)
|
|
|
|
: QAbstractListModel{parent}
|
|
|
|
, event_id_{event_id}
|
|
|
|
, room_id_{room_id}
|
|
|
|
{
|
|
|
|
try {
|
2021-07-24 22:35:28 +03:00
|
|
|
addUsers(cache::readReceipts(event_id_, room_id_));
|
2021-07-24 01:11:33 +03:00
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->warn("failed to retrieve read receipts for {} {}",
|
2021-07-24 22:35:28 +03:00
|
|
|
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(),
|
2021-07-24 01:11:33 +03:00
|
|
|
room_id_.toStdString());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
ReadReceiptsModel::roleNames() const
|
|
|
|
{
|
2021-07-29 04:31:37 +03:00
|
|
|
// Note: RawTimestamp is purposely not included here
|
|
|
|
return {
|
|
|
|
{Mxid, "mxid"},
|
|
|
|
{DisplayName, "displayName"},
|
|
|
|
{AvatarUrl, "avatarUrl"},
|
|
|
|
{Timestamp, "timestamp"},
|
|
|
|
};
|
2021-07-24 01:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
ReadReceiptsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid() || index.row() >= (int)readReceipts_.size() || index.row() < 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Mxid:
|
2021-07-24 05:19:48 +03:00
|
|
|
return readReceipts_[index.row()].first;
|
2021-07-24 01:11:33 +03:00
|
|
|
case DisplayName:
|
2021-07-24 05:19:48 +03:00
|
|
|
return cache::displayName(room_id_, readReceipts_[index.row()].first);
|
2021-07-24 01:11:33 +03:00
|
|
|
case AvatarUrl:
|
2021-07-24 05:19:48 +03:00
|
|
|
return cache::avatarUrl(room_id_, readReceipts_[index.row()].first);
|
2021-07-24 01:11:33 +03:00
|
|
|
case Timestamp:
|
2021-07-24 05:19:48 +03:00
|
|
|
return dateFormat(readReceipts_[index.row()].second);
|
2021-07-29 04:31:37 +03:00
|
|
|
case RawTimestamp:
|
|
|
|
return readReceipts_[index.row()].second;
|
2021-07-24 01:11:33 +03:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadReceiptsModel::addUsers(
|
|
|
|
const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users)
|
|
|
|
{
|
2021-07-29 04:31:37 +03:00
|
|
|
auto newReceipts = users.size() - readReceipts_.size();
|
2021-07-24 05:19:48 +03:00
|
|
|
|
2021-07-29 04:31:37 +03:00
|
|
|
if (newReceipts > 0) {
|
|
|
|
beginInsertRows(
|
|
|
|
QModelIndex{}, readReceipts_.size(), readReceipts_.size() + newReceipts - 1);
|
2021-07-24 01:11:33 +03:00
|
|
|
|
2021-07-29 04:31:37 +03:00
|
|
|
for (const auto &user : users) {
|
|
|
|
QPair<QString, QDateTime> item = {
|
|
|
|
QString::fromStdString(user.second),
|
|
|
|
QDateTime::fromMSecsSinceEpoch(user.first)};
|
|
|
|
if (!readReceipts_.contains(item))
|
|
|
|
readReceipts_.push_back(item);
|
|
|
|
}
|
2021-07-24 01:11:33 +03:00
|
|
|
|
2021-07-29 04:31:37 +03:00
|
|
|
endInsertRows();
|
|
|
|
}
|
2021-07-24 01:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2021-07-24 05:19:48 +03:00
|
|
|
ReadReceiptsModel::dateFormat(const QDateTime &then) const
|
2021-07-24 01:11:33 +03:00
|
|
|
{
|
|
|
|
auto now = QDateTime::currentDateTime();
|
|
|
|
auto days = then.daysTo(now);
|
|
|
|
|
|
|
|
if (days == 0)
|
|
|
|
return tr("Today %1")
|
|
|
|
.arg(QLocale::system().toString(then.time(), QLocale::ShortFormat));
|
|
|
|
else if (days < 2)
|
|
|
|
return tr("Yesterday %1")
|
|
|
|
.arg(QLocale::system().toString(then.time(), QLocale::ShortFormat));
|
|
|
|
else if (days < 7)
|
|
|
|
return QString("%1 %2")
|
|
|
|
.arg(then.toString("dddd"))
|
|
|
|
.arg(QLocale::system().toString(then.time(), QLocale::ShortFormat));
|
|
|
|
|
|
|
|
return QLocale::system().toString(then.time(), QLocale::ShortFormat);
|
|
|
|
}
|
2021-07-29 04:31:37 +03:00
|
|
|
|
|
|
|
ReadReceiptsProxy::ReadReceiptsProxy(QString event_id, QString room_id, QObject *parent)
|
|
|
|
: QSortFilterProxyModel{parent}
|
|
|
|
, model_{event_id, room_id, this}
|
|
|
|
{
|
|
|
|
setSourceModel(&model_);
|
|
|
|
setSortRole(ReadReceiptsModel::RawTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ReadReceiptsProxy::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
|
|
|
{
|
|
|
|
// since we are sorting from greatest to least timestamp, return something that looks totally backwards!
|
|
|
|
return source_left.data().toULongLong() > source_right.data().toULongLong();
|
|
|
|
}
|