summary refs log tree commit diff
path: root/src/timeline2/TimelineModel.cpp
blob: 2882020542ee6451262e2bd69eb316c6c9c1ca7e (plain) (blame)
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
#include "TimelineModel.h"

#include <type_traits>

#include <QRegularExpression>

#include "Logging.h"
#include "Utils.h"

namespace {
template<class T>
QString
eventId(const T &event)
{
        return QString::fromStdString(event.event_id);
}
template<class T>
QString
roomId(const T &event)
{
        return QString::fromStdString(event.room_id);
}
template<class T>
QString
senderId(const T &event)
{
        return QString::fromStdString(event.sender);
}

template<class T>
QDateTime
eventTimestamp(const T &event)
{
        return QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
}

template<class T>
QString
eventFormattedBody(const mtx::events::Event<T> &)
{
        return QString("");
}
template<class T>
auto
eventFormattedBody(const mtx::events::RoomEvent<T> &e)
  -> std::enable_if_t<std::is_same<decltype(e.content.formatted_body), std::string>::value, QString>
{
        auto temp = e.content.formatted_body;
        if (!temp.empty()) {
                auto pos = temp.find("<mx-reply>");
                if (pos != std::string::npos)
                        temp.erase(pos, std::string("<mx-reply>").size());
                pos = temp.find("</mx-reply>");
                if (pos != std::string::npos)
                        temp.erase(pos, std::string("</mx-reply>").size());
                return QString::fromStdString(temp);
        } else
                return QString::fromStdString(e.content.body);
}

template<class T>
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<T> &e)
{
        using mtx::events::EventType;
        switch (e.type) {
        case EventType::RoomKeyRequest:
                return qml_mtx_events::EventType::KeyRequest;
        case EventType::RoomAliases:
                return qml_mtx_events::EventType::Aliases;
        case EventType::RoomAvatar:
                return qml_mtx_events::EventType::Avatar;
        case EventType::RoomCanonicalAlias:
                return qml_mtx_events::EventType::CanonicalAlias;
        case EventType::RoomCreate:
                return qml_mtx_events::EventType::Create;
        case EventType::RoomEncrypted:
                return qml_mtx_events::EventType::Encrypted;
        case EventType::RoomEncryption:
                return qml_mtx_events::EventType::Encryption;
        case EventType::RoomGuestAccess:
                return qml_mtx_events::EventType::GuestAccess;
        case EventType::RoomHistoryVisibility:
                return qml_mtx_events::EventType::HistoryVisibility;
        case EventType::RoomJoinRules:
                return qml_mtx_events::EventType::JoinRules;
        case EventType::RoomMember:
                return qml_mtx_events::EventType::Member;
        case EventType::RoomMessage:
                return qml_mtx_events::EventType::UnknownMessage;
        case EventType::RoomName:
                return qml_mtx_events::EventType::Name;
        case EventType::RoomPowerLevels:
                return qml_mtx_events::EventType::PowerLevels;
        case EventType::RoomTopic:
                return qml_mtx_events::EventType::Topic;
        case EventType::RoomTombstone:
                return qml_mtx_events::EventType::Tombstone;
        case EventType::RoomRedaction:
                return qml_mtx_events::EventType::Redaction;
        case EventType::RoomPinnedEvents:
                return qml_mtx_events::EventType::PinnedEvents;
        case EventType::Sticker:
                return qml_mtx_events::EventType::Sticker;
        case EventType::Tag:
                return qml_mtx_events::EventType::Tag;
        case EventType::Unsupported:
        default:
                return qml_mtx_events::EventType::Unsupported;
        }
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Audio> &)
{
        return qml_mtx_events::EventType::AudioMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Emote> &)
{
        return qml_mtx_events::EventType::EmoteMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::File> &)
{
        return qml_mtx_events::EventType::FileMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Image> &)
{
        return qml_mtx_events::EventType::ImageMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Notice> &)
{
        return qml_mtx_events::EventType::NoticeMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Text> &)
{
        return qml_mtx_events::EventType::TextMessage;
}
qml_mtx_events::EventType
toRoomEventType(const mtx::events::Event<mtx::events::msg::Video> &)
{
        return qml_mtx_events::EventType::VideoMessage;
}
// ::EventType::Type toRoomEventType(const Event<mtx::events::msg::Location> &e) { return
// ::EventType::LocationMessage; }
}

TimelineModel::TimelineModel(QString room_id, QObject *parent)
  : QAbstractListModel(parent)
  , room_id_(room_id)
{
        connect(
          this, &TimelineModel::oldMessagesRetrieved, this, &TimelineModel::addBackwardsEvents);
}

QHash<int, QByteArray>
TimelineModel::roleNames() const
{
        return {
          {Section, "section"},
          {Type, "type"},
          {Body, "body"},
          {FormattedBody, "formattedBody"},
          {UserId, "userId"},
          {UserName, "userName"},
          {Timestamp, "timestamp"},
        };
}
int
TimelineModel::rowCount(const QModelIndex &parent) const
{
        Q_UNUSED(parent);
        return (int)this->eventOrder.size();
}

QVariant
TimelineModel::data(const QModelIndex &index, int role) const
{
        if (index.row() < 0 && index.row() >= (int)eventOrder.size())
                return QVariant();

        QString id = eventOrder[index.row()];

        switch (role) {
        case Section: {
                QDateTime date = boost::apply_visitor(
                  [](const auto &e) -> QDateTime { return eventTimestamp(e); }, events.value(id));
                date.setTime(QTime());

                QString userId = boost::apply_visitor(
                  [](const auto &e) -> QString { return senderId(e); }, events.value(id));

                for (int r = index.row() - 1; r > 0; r--) {
                        QDateTime prevDate = boost::apply_visitor(
                          [](const auto &e) -> QDateTime { return eventTimestamp(e); },
                          events.value(eventOrder[r]));
                        prevDate.setTime(QTime());
                        if (prevDate != date)
                                return QString("%2 %1").arg(date.toMSecsSinceEpoch()).arg(userId);

                        QString prevUserId =
                          boost::apply_visitor([](const auto &e) -> QString { return senderId(e); },
                                               events.value(eventOrder[r]));
                        if (userId != prevUserId)
                                break;
                }

                return QString("%1").arg(userId);
        }
        case UserId:
                return QVariant(boost::apply_visitor(
                  [](const auto &e) -> QString { return senderId(e); }, events.value(id)));
        case UserName:
                return QVariant(displayName(boost::apply_visitor(
                  [](const auto &e) -> QString { return senderId(e); }, events.value(id))));

        case Timestamp:
                return QVariant(boost::apply_visitor(
                  [](const auto &e) -> QDateTime { return eventTimestamp(e); }, events.value(id)));
        case Type:
                return QVariant(boost::apply_visitor(
                  [](const auto &e) -> qml_mtx_events::EventType { return toRoomEventType(e); },
                  events.value(id)));
        case FormattedBody:
                return QVariant(utils::replaceEmoji(boost::apply_visitor(
                  [](const auto &e) -> QString { return eventFormattedBody(e); },
                  events.value(id))));
        default:
                return QVariant();
        }
}

void
TimelineModel::addEvents(const mtx::responses::Timeline &timeline)
{
        if (isInitialSync) {
                prev_batch_token_ = QString::fromStdString(timeline.prev_batch);
                isInitialSync     = false;
        }

        if (timeline.events.empty())
                return;

        std::vector<QString> ids;
        for (const auto &e : timeline.events) {
                QString id =
                  boost::apply_visitor([](const auto &e) -> QString { return eventId(e); }, e);

                this->events.insert(id, e);
                ids.push_back(id);
        }

        beginInsertRows(QModelIndex(),
                        static_cast<int>(this->eventOrder.size()),
                        static_cast<int>(this->eventOrder.size() + ids.size() - 1));
        this->eventOrder.insert(this->eventOrder.end(), ids.begin(), ids.end());
        endInsertRows();
}

void
TimelineModel::fetchHistory()
{
        if (paginationInProgress) {
                nhlog::ui()->warn("Already loading older messages");
                return;
        }

        paginationInProgress = true;
        mtx::http::MessagesOpts opts;
        opts.room_id = room_id_.toStdString();
        opts.from    = prev_batch_token_.toStdString();

        nhlog::ui()->info("Paginationg room {}", opts.room_id);

        http::client()->messages(
          opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
                  if (err) {
                          nhlog::net()->error("failed to call /messages ({}): {} - {}",
                                              opts.room_id,
                                              mtx::errors::to_string(err->matrix_error.errcode),
                                              err->matrix_error.error);
                          return;
                  }

                  emit oldMessagesRetrieved(std::move(res));
          });
}

void
TimelineModel::addBackwardsEvents(const mtx::responses::Messages &msgs)
{
        std::vector<QString> ids;
        for (const auto &e : msgs.chunk) {
                QString id =
                  boost::apply_visitor([](const auto &e) -> QString { return eventId(e); }, e);

                this->events.insert(id, e);
                ids.push_back(id);
        }

        beginInsertRows(QModelIndex(), 0, static_cast<int>(ids.size() - 1));
        this->eventOrder.insert(this->eventOrder.begin(), ids.rbegin(), ids.rend());
        endInsertRows();

        prev_batch_token_ = QString::fromStdString(msgs.end);

        paginationInProgress = false;
}

QColor
TimelineModel::userColor(QString id, QColor background)
{
        if (!userColors.contains(id))
                userColors.insert(
                  id, QColor(utils::generateContrastingHexColor(id, background.name())));
        return userColors.value(id);
}

QString
TimelineModel::displayName(QString id) const
{
        return Cache::displayName(room_id_, id);
}

QString
TimelineModel::formatDateSeparator(QDate date) const
{
        auto now = QDateTime::currentDateTime();

        QString fmt = QLocale::system().dateFormat(QLocale::LongFormat);

        if (now.date().year() == date.year()) {
                QRegularExpression rx("[^a-zA-Z]*y+[^a-zA-Z]*");
                fmt = fmt.remove(rx);
        }

        return date.toString(fmt);
}

QString
TimelineModel::escapeEmoji(QString str) const
{
        return utils::replaceEmoji(str);
}