Debian Codegraph @

src/ReadReceiptsModel.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "ReadReceiptsModel.h"
6
7 #include <QLocale>
8
9 #include "Cache.h"
10 #include "Cache_p.h"
11 #include "Logging.h"
12 #include "Utils.h"
13
14 ReadReceiptsModel::ReadReceiptsModel(QString event_id, QString room_id, QObject *parent)
15 : QAbstractListModel{parent}
16 , event_id_{event_id}
17 , room_id_{room_id}
18 {
19 try {
20 addUsers(cache::readReceipts(event_id_, room_id_));
21 } catch (const lmdb::error &) {
22 nhlog::db()->warn("failed to retrieve read receipts for {} {}",
23 event_id_.toStdString(),
24 room_id_.toStdString());
25
26 return;
27 }
28
29 connect(cache::client(), &Cache::newReadReceipts, this, &ReadReceiptsModel::update);
30 }
31
32 void
33 ReadReceiptsModel::update()
34 {
35 try {
36 addUsers(cache::readReceipts(event_id_, room_id_));
37 } catch (const lmdb::error &) {
38 nhlog::db()->warn("failed to retrieve read receipts for {} {}",
39 event_id_.toStdString(),
40 room_id_.toStdString());
41
42 return;
43 }
44 }
45
46 QHash<int, QByteArray>
47 ReadReceiptsModel::roleNames() const
48 {
49 // Note: RawTimestamp is purposely not included here
50 return {
51 {Mxid, "mxid"},
52 {DisplayName, "displayName"},
53 {AvatarUrl, "avatarUrl"},
54 {Timestamp, "timestamp"},
55 };
56 }
57
58 QVariant
59 ReadReceiptsModel::data(const QModelIndex &index, int role) const
60 {
61 if (!index.isValid() || index.row() >= (int)readReceipts_.size() || index.row() < 0)
62 return {};
63
64 switch (role) {
65 case Mxid:
66 return readReceipts_[index.row()].first;
67 case DisplayName:
68 return cache::displayName(room_id_, readReceipts_[index.row()].first);
69 case AvatarUrl:
70 return cache::avatarUrl(room_id_, readReceipts_[index.row()].first);
71 case Timestamp:
72 return dateFormat(readReceipts_[index.row()].second);
73 case RawTimestamp:
74 return readReceipts_[index.row()].second;
75 default:
76 return {};
77 }
78 }
79
80 void
81 ReadReceiptsModel::addUsers(
82 const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users)
83 {
84 auto newReceipts = users.size() - readReceipts_.size();
85
86 if (newReceipts > 0) {
87 beginInsertRows(QModelIndex{},
88 static_cast<int>(readReceipts_.size()),
89 static_cast<int>(readReceipts_.size() + newReceipts - 1));
90
91 for (const auto &user : users) {
92 QPair<QString, QDateTime> item = {QString::fromStdString(user.second),
93 QDateTime::fromMSecsSinceEpoch(user.first)};
94 if (!readReceipts_.contains(item))
95 readReceipts_.push_back(item);
96 }
97
98 endInsertRows();
99 }
100 }
101
102 QString
103 ReadReceiptsModel::dateFormat(const QDateTime &then) const
104 {
105 auto now = QDateTime::currentDateTime();
106 auto days = then.daysTo(now);
107
108 if (days == 0)
109 return QLocale::system().toString(then.time(), QLocale::ShortFormat);
110 else if (days < 2)
111 return tr("Yesterday, %1")
112 .arg(QLocale::system().toString(then.time(), QLocale::ShortFormat));
113 else if (days < 7)
114 //: %1 is the name of the current day, %2 is the time the read receipt was read. The
115 //: result may look like this: Monday, 7:15
116 return QStringLiteral("%1, %2").arg(
117 then.toString(QStringLiteral("dddd")),
118 QLocale::system().toString(then.time(), QLocale::ShortFormat));
119
120 return QLocale::system().toString(then.time(), QLocale::ShortFormat);
121 }
122
123 ReadReceiptsProxy::ReadReceiptsProxy(QString event_id, QString room_id, QObject *parent)
124 : QSortFilterProxyModel{parent}
125 , model_{event_id, room_id, this}
126 {
127 setSourceModel(&model_);
128 setSortRole(ReadReceiptsModel::RawTimestamp);
129 sort(0, Qt::DescendingOrder);
130 setDynamicSortFilter(true);
131 }
132
133 #include "moc_ReadReceiptsModel.cpp"