1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "RoomlistModel.h"
#include "Cache_p.h"
#include "ChatPage.h"
#include "MatrixClient.h"
#include "MxcImageProvider.h"
#include "TimelineModel.h"
#include "TimelineViewManager.h"
#include "UserSettingsPage.h"
RoomlistModel::RoomlistModel(TimelineViewManager *parent)
: manager(parent)
{
connect(ChatPage::instance(), &ChatPage::decryptSidebarChanged, this, [this]() {
auto decrypt = ChatPage::instance()->userSettings()->decryptSidebar();
QHash<QString, QSharedPointer<TimelineModel>>::iterator i;
for (i = models.begin(); i != models.end(); ++i) {
auto ptr = i.value();
if (!ptr.isNull()) {
ptr->setDecryptDescription(decrypt);
ptr->updateLastMessage();
}
}
});
connect(this,
&RoomlistModel::totalUnreadMessageCountUpdated,
ChatPage::instance(),
&ChatPage::unreadMessages);
}
QHash<int, QByteArray>
RoomlistModel::roleNames() const
{
return {
{AvatarUrl, "avatarUrl"},
{RoomName, "roomName"},
{RoomId, "roomId"},
{LastMessage, "lastMessage"},
{Time, "time"},
{Timestamp, "timestamp"},
{HasUnreadMessages, "hasUnreadMessages"},
{HasLoudNotification, "hasLoudNotification"},
{NotificationCount, "notificationCount"},
{IsInvite, "isInvite"},
{IsSpace, "isSpace"},
};
}
QVariant
RoomlistModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= 0 && static_cast<size_t>(index.row()) < roomids.size()) {
auto roomid = roomids.at(index.row());
if (models.contains(roomid)) {
auto room = models.value(roomid);
switch (role) {
case Roles::AvatarUrl:
return room->roomAvatarUrl();
case Roles::RoomName:
return room->plainRoomName();
case Roles::RoomId:
return room->roomId();
case Roles::LastMessage:
return room->lastMessage().body;
case Roles::Time:
return room->lastMessage().descriptiveTime;
case Roles::Timestamp:
return QVariant(
static_cast<quint64>(room->lastMessage().timestamp));
case Roles::HasUnreadMessages:
return this->roomReadStatus.count(roomid) &&
this->roomReadStatus.at(roomid);
case Roles::HasLoudNotification:
return room->hasMentions();
case Roles::NotificationCount:
return room->notificationCount();
case Roles::IsInvite:
case Roles::IsSpace:
return false;
default:
return {};
}
} else if (invites.contains(roomid)) {
auto room = invites.value(roomid);
switch (role) {
case Roles::AvatarUrl:
return QString::fromStdString(room.avatar_url);
case Roles::RoomName:
return QString::fromStdString(room.name);
case Roles::RoomId:
return roomid;
case Roles::LastMessage:
return room.msgInfo.body;
case Roles::Time:
return room.msgInfo.descriptiveTime;
case Roles::Timestamp:
return QVariant(static_cast<quint64>(room.msgInfo.timestamp));
case Roles::HasUnreadMessages:
case Roles::HasLoudNotification:
return false;
case Roles::NotificationCount:
return 0;
case Roles::IsInvite:
return true;
case Roles::IsSpace:
return false;
default:
return {};
}
} else {
return {};
}
} else {
return {};
}
}
void
RoomlistModel::updateReadStatus(const std::map<QString, bool> roomReadStatus_)
{
std::vector<int> roomsToUpdate;
roomsToUpdate.resize(roomReadStatus_.size());
for (const auto &[roomid, roomUnread] : roomReadStatus_) {
if (roomUnread != roomReadStatus[roomid]) {
roomsToUpdate.push_back(this->roomidToIndex(roomid));
}
this->roomReadStatus[roomid] = roomUnread;
}
for (auto idx : roomsToUpdate) {
emit dataChanged(index(idx),
index(idx),
{
Roles::HasUnreadMessages,
});
}
}
void
RoomlistModel::addRoom(const QString &room_id, bool suppressInsertNotification)
{
if (!models.contains(room_id)) {
// ensure we get read status updates and are only connected once
connect(cache::client(),
&Cache::roomReadStatus,
this,
&RoomlistModel::updateReadStatus,
Qt::UniqueConnection);
QSharedPointer<TimelineModel> newRoom(new TimelineModel(manager, room_id));
newRoom->setDecryptDescription(
ChatPage::instance()->userSettings()->decryptSidebar());
connect(newRoom.data(),
&TimelineModel::newEncryptedImage,
manager->imageProvider(),
&MxcImageProvider::addEncryptionInfo);
connect(newRoom.data(),
&TimelineModel::forwardToRoom,
manager,
&TimelineViewManager::forwardMessageToRoom);
connect(
newRoom.data(), &TimelineModel::lastMessageChanged, this, [room_id, this]() {
auto idx = this->roomidToIndex(room_id);
emit dataChanged(index(idx),
index(idx),
{
Roles::HasLoudNotification,
Roles::LastMessage,
Roles::Timestamp,
Roles::NotificationCount,
Qt::DisplayRole,
});
});
connect(
newRoom.data(), &TimelineModel::roomAvatarUrlChanged, this, [room_id, this]() {
auto idx = this->roomidToIndex(room_id);
emit dataChanged(index(idx),
index(idx),
{
Roles::AvatarUrl,
});
});
connect(newRoom.data(), &TimelineModel::roomNameChanged, this, [room_id, this]() {
auto idx = this->roomidToIndex(room_id);
emit dataChanged(index(idx),
index(idx),
{
Roles::RoomName,
});
});
connect(
newRoom.data(), &TimelineModel::notificationsChanged, this, [room_id, this]() {
auto idx = this->roomidToIndex(room_id);
emit dataChanged(index(idx),
index(idx),
{
Roles::HasLoudNotification,
Roles::NotificationCount,
Qt::DisplayRole,
});
int total_unread_msgs = 0;
for (const auto &room : models) {
if (!room.isNull())
total_unread_msgs += room->notificationCount();
}
emit totalUnreadMessageCountUpdated(total_unread_msgs);
});
newRoom->updateLastMessage();
bool wasInvite = invites.contains(room_id);
if (!suppressInsertNotification && !wasInvite)
beginInsertRows(QModelIndex(), (int)roomids.size(), (int)roomids.size());
models.insert(room_id, std::move(newRoom));
if (wasInvite) {
auto idx = roomidToIndex(room_id);
invites.remove(room_id);
emit dataChanged(index(idx), index(idx));
} else {
roomids.push_back(room_id);
}
if (!suppressInsertNotification && !wasInvite)
endInsertRows();
}
}
void
RoomlistModel::sync(const mtx::responses::Rooms &rooms)
{
for (const auto &[room_id, room] : rooms.join) {
// addRoom will only add the room, if it doesn't exist
addRoom(QString::fromStdString(room_id));
const auto &room_model = models.value(QString::fromStdString(room_id));
room_model->sync(room);
// room_model->addEvents(room.timeline);
connect(room_model.data(),
&TimelineModel::newCallEvent,
manager->callManager(),
&CallManager::syncEvent,
Qt::UniqueConnection);
if (ChatPage::instance()->userSettings()->typingNotifications()) {
for (const auto &ev : room.ephemeral.events) {
if (auto t = std::get_if<
mtx::events::EphemeralEvent<mtx::events::ephemeral::Typing>>(
&ev)) {
std::vector<QString> typing;
typing.reserve(t->content.user_ids.size());
for (const auto &user : t->content.user_ids) {
if (user != http::client()->user_id().to_string())
typing.push_back(
QString::fromStdString(user));
}
room_model->updateTypingUsers(typing);
}
}
}
}
for (const auto &[room_id, room] : rooms.leave) {
(void)room;
auto idx = this->roomidToIndex(QString::fromStdString(room_id));
if (idx != -1) {
beginRemoveRows(QModelIndex(), idx, idx);
roomids.erase(roomids.begin() + idx);
if (models.contains(QString::fromStdString(room_id)))
models.remove(QString::fromStdString(room_id));
else if (invites.contains(QString::fromStdString(room_id)))
invites.remove(QString::fromStdString(room_id));
endRemoveRows();
}
}
for (const auto &[room_id, room] : rooms.invite) {
(void)room_id;
auto qroomid = QString::fromStdString(room_id);
auto invite = cache::client()->invite(room_id);
if (!invite)
continue;
if (invites.contains(qroomid)) {
invites[qroomid] = *invite;
auto idx = roomidToIndex(qroomid);
emit dataChanged(index(idx), index(idx));
} else {
beginInsertRows(QModelIndex(), (int)roomids.size(), (int)roomids.size());
invites.insert(qroomid, *invite);
roomids.push_back(std::move(qroomid));
endInsertRows();
}
}
}
void
RoomlistModel::initializeRooms()
{
beginResetModel();
models.clear();
roomids.clear();
invites.clear();
invites = cache::client()->invites();
for (const auto &id : invites.keys())
roomids.push_back(id);
for (const auto &id : cache::client()->roomIds())
addRoom(id, true);
endResetModel();
}
void
RoomlistModel::clear()
{
beginResetModel();
models.clear();
invites.clear();
roomids.clear();
endResetModel();
}
void
RoomlistModel::acceptInvite(QString roomid)
{
if (invites.contains(roomid)) {
auto idx = roomidToIndex(roomid);
if (idx != -1) {
beginRemoveRows(QModelIndex(), idx, idx);
roomids.erase(roomids.begin() + idx);
invites.remove(roomid);
endRemoveRows();
ChatPage::instance()->joinRoom(roomid);
}
}
}
void
RoomlistModel::declineInvite(QString roomid)
{
if (invites.contains(roomid)) {
auto idx = roomidToIndex(roomid);
if (idx != -1) {
beginRemoveRows(QModelIndex(), idx, idx);
roomids.erase(roomids.begin() + idx);
invites.remove(roomid);
endRemoveRows();
ChatPage::instance()->leaveRoom(roomid);
}
}
}
namespace {
enum NotificationImportance : short
{
ImportanceDisabled = -1,
AllEventsRead = 0,
NewMessage = 1,
NewMentions = 2,
Invite = 3
};
}
short int
FilteredRoomlistModel::calculateImportance(const QModelIndex &idx) const
{
// Returns the degree of importance of the unread messages in the room.
// If sorting by importance is disabled in settings, this only ever
// returns ImportanceDisabled or Invite
if (sourceModel()->data(idx, RoomlistModel::IsInvite).toBool()) {
return Invite;
} else if (!this->sortByImportance) {
return ImportanceDisabled;
} else if (sourceModel()->data(idx, RoomlistModel::HasLoudNotification).toBool()) {
return NewMentions;
} else if (sourceModel()->data(idx, RoomlistModel::NotificationCount).toInt() > 0) {
return NewMessage;
} else {
return AllEventsRead;
}
}
bool
FilteredRoomlistModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QModelIndex const left_idx = sourceModel()->index(left.row(), 0, QModelIndex());
QModelIndex const right_idx = sourceModel()->index(right.row(), 0, QModelIndex());
// Sort by "importance" (i.e. invites before mentions before
// notifs before new events before old events), then secondly
// by recency.
// Checking importance first
const auto a_importance = calculateImportance(left_idx);
const auto b_importance = calculateImportance(right_idx);
if (a_importance != b_importance) {
return a_importance > b_importance;
}
// Now sort by recency
// Zero if empty, otherwise the time that the event occured
uint64_t a_recency = sourceModel()->data(left_idx, RoomlistModel::Timestamp).toULongLong();
uint64_t b_recency = sourceModel()->data(right_idx, RoomlistModel::Timestamp).toULongLong();
if (a_recency != b_recency)
return a_recency > b_recency;
else
return left.row() < right.row();
}
FilteredRoomlistModel::FilteredRoomlistModel(RoomlistModel *model, QObject *parent)
: QSortFilterProxyModel(parent)
, roomlistmodel(model)
{
this->sortByImportance = UserSettings::instance()->sortByImportance();
setSourceModel(model);
setDynamicSortFilter(true);
QObject::connect(UserSettings::instance().get(),
&UserSettings::roomSortingChanged,
this,
[this](bool sortByImportance_) {
this->sortByImportance = sortByImportance_;
invalidate();
});
sort(0);
}
|