2019-08-31 00:20:53 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QColor>
|
2019-09-01 23:58:26 +03:00
|
|
|
#include <QDate>
|
2019-08-31 23:43:31 +03:00
|
|
|
#include <QHash>
|
2019-09-18 23:58:25 +03:00
|
|
|
#include <QSet>
|
2019-08-31 00:20:53 +03:00
|
|
|
|
2019-10-06 13:59:51 +03:00
|
|
|
#include <mtx/responses.hpp>
|
|
|
|
|
2019-09-19 23:44:25 +03:00
|
|
|
#include "Cache.h"
|
2019-09-11 01:00:04 +03:00
|
|
|
#include "Logging.h"
|
|
|
|
#include "MatrixClient.h"
|
|
|
|
|
2019-09-03 00:28:05 +03:00
|
|
|
namespace qml_mtx_events {
|
|
|
|
Q_NAMESPACE
|
|
|
|
|
|
|
|
enum EventType
|
|
|
|
{
|
|
|
|
// Unsupported event
|
|
|
|
Unsupported,
|
|
|
|
/// m.room_key_request
|
|
|
|
KeyRequest,
|
|
|
|
/// m.room.aliases
|
|
|
|
Aliases,
|
|
|
|
/// m.room.avatar
|
|
|
|
Avatar,
|
|
|
|
/// m.room.canonical_alias
|
|
|
|
CanonicalAlias,
|
|
|
|
/// m.room.create
|
|
|
|
Create,
|
|
|
|
/// m.room.encrypted.
|
|
|
|
Encrypted,
|
|
|
|
/// m.room.encryption.
|
|
|
|
Encryption,
|
|
|
|
/// m.room.guest_access
|
|
|
|
GuestAccess,
|
|
|
|
/// m.room.history_visibility
|
|
|
|
HistoryVisibility,
|
|
|
|
/// m.room.join_rules
|
|
|
|
JoinRules,
|
|
|
|
/// m.room.member
|
|
|
|
Member,
|
|
|
|
/// m.room.name
|
|
|
|
Name,
|
|
|
|
/// m.room.power_levels
|
|
|
|
PowerLevels,
|
|
|
|
/// m.room.tombstone
|
|
|
|
Tombstone,
|
|
|
|
/// m.room.topic
|
|
|
|
Topic,
|
|
|
|
/// m.room.redaction
|
|
|
|
Redaction,
|
|
|
|
/// m.room.pinned_events
|
|
|
|
PinnedEvents,
|
|
|
|
// m.sticker
|
|
|
|
Sticker,
|
|
|
|
// m.tag
|
|
|
|
Tag,
|
|
|
|
/// m.room.message
|
|
|
|
AudioMessage,
|
|
|
|
EmoteMessage,
|
|
|
|
FileMessage,
|
|
|
|
ImageMessage,
|
|
|
|
LocationMessage,
|
|
|
|
NoticeMessage,
|
|
|
|
TextMessage,
|
|
|
|
VideoMessage,
|
2019-09-09 22:42:33 +03:00
|
|
|
Redacted,
|
2019-09-03 00:28:05 +03:00
|
|
|
UnknownMessage,
|
|
|
|
};
|
|
|
|
Q_ENUM_NS(EventType)
|
2019-09-18 23:58:25 +03:00
|
|
|
|
|
|
|
enum EventState
|
|
|
|
{
|
|
|
|
//! The plaintext message was received by the server.
|
|
|
|
Received,
|
|
|
|
//! At least one of the participants has read the message.
|
|
|
|
Read,
|
|
|
|
//! The client sent the message. Not yet received.
|
|
|
|
Sent,
|
|
|
|
//! When the message is loaded from cache or backfill.
|
|
|
|
Empty,
|
|
|
|
//! When the message failed to send
|
|
|
|
Failed,
|
|
|
|
};
|
|
|
|
Q_ENUM_NS(EventState)
|
2019-09-03 00:28:05 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:44:25 +03:00
|
|
|
class StateKeeper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StateKeeper(std::function<void()> &&fn)
|
|
|
|
: fn_(std::move(fn))
|
|
|
|
{}
|
|
|
|
|
|
|
|
~StateKeeper() { fn_(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void()> fn_;
|
|
|
|
};
|
|
|
|
|
2019-09-08 17:50:32 +03:00
|
|
|
struct DecryptionResult
|
|
|
|
{
|
|
|
|
//! The decrypted content as a normal plaintext event.
|
|
|
|
mtx::events::collections::TimelineEvents event;
|
|
|
|
//! Whether or not the decryption was successful.
|
|
|
|
bool isDecrypted = false;
|
|
|
|
};
|
|
|
|
|
2019-10-03 19:07:01 +03:00
|
|
|
class TimelineViewManager;
|
|
|
|
|
2019-08-31 00:20:53 +03:00
|
|
|
class TimelineModel : public QAbstractListModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2019-09-18 21:34:30 +03:00
|
|
|
Q_PROPERTY(
|
|
|
|
int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
|
2019-08-31 00:20:53 +03:00
|
|
|
|
|
|
|
public:
|
2019-10-03 19:07:01 +03:00
|
|
|
explicit TimelineModel(TimelineViewManager *manager, QString room_id, QObject *parent = 0);
|
2019-08-31 00:20:53 +03:00
|
|
|
|
|
|
|
enum Roles
|
|
|
|
{
|
2019-09-01 15:17:33 +03:00
|
|
|
Section,
|
2019-08-31 00:20:53 +03:00
|
|
|
Type,
|
|
|
|
Body,
|
|
|
|
FormattedBody,
|
|
|
|
UserId,
|
|
|
|
UserName,
|
|
|
|
Timestamp,
|
2019-09-08 13:44:46 +03:00
|
|
|
Url,
|
2019-10-09 01:36:03 +03:00
|
|
|
ThumbnailUrl,
|
2019-09-29 11:45:35 +03:00
|
|
|
Filename,
|
2019-10-04 02:10:46 +03:00
|
|
|
Filesize,
|
2019-09-29 11:45:35 +03:00
|
|
|
MimeType,
|
2019-09-08 13:44:46 +03:00
|
|
|
Height,
|
|
|
|
Width,
|
|
|
|
ProportionalHeight,
|
2019-09-08 16:26:46 +03:00
|
|
|
Id,
|
2019-09-18 23:58:25 +03:00
|
|
|
State,
|
2019-09-20 00:02:56 +03:00
|
|
|
IsEncrypted,
|
2019-11-05 19:16:04 +03:00
|
|
|
ReplyTo,
|
2019-08-31 00:20:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
QHash<int, QByteArray> roleNames() const override;
|
2019-09-03 00:28:05 +03:00
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
2019-08-31 00:20:53 +03:00
|
|
|
|
|
|
|
Q_INVOKABLE QColor userColor(QString id, QColor background);
|
2019-09-01 23:34:36 +03:00
|
|
|
Q_INVOKABLE QString displayName(QString id) const;
|
2019-09-07 23:22:07 +03:00
|
|
|
Q_INVOKABLE QString avatarUrl(QString id) const;
|
2019-09-01 23:58:26 +03:00
|
|
|
Q_INVOKABLE QString formatDateSeparator(QDate date) const;
|
2019-10-03 19:07:01 +03:00
|
|
|
|
2019-09-07 03:01:44 +03:00
|
|
|
Q_INVOKABLE QString escapeEmoji(QString str) const;
|
2019-09-08 16:26:46 +03:00
|
|
|
Q_INVOKABLE void viewRawMessage(QString id) const;
|
2019-11-03 04:09:36 +03:00
|
|
|
Q_INVOKABLE void openUserProfile(QString userid) const;
|
2019-09-11 01:54:40 +03:00
|
|
|
Q_INVOKABLE void replyAction(QString id);
|
2019-09-19 00:37:30 +03:00
|
|
|
Q_INVOKABLE void readReceiptsAction(QString id) const;
|
2019-09-29 13:29:17 +03:00
|
|
|
Q_INVOKABLE void redactEvent(QString id);
|
2019-09-18 21:34:30 +03:00
|
|
|
Q_INVOKABLE int idToIndex(QString id) const;
|
|
|
|
Q_INVOKABLE QString indexToId(int index) const;
|
2019-12-03 04:26:41 +03:00
|
|
|
Q_INVOKABLE void cacheMedia(QString eventId);
|
|
|
|
Q_INVOKABLE void saveMedia(QString eventId) const;
|
2019-09-01 00:44:17 +03:00
|
|
|
|
2019-08-31 23:43:31 +03:00
|
|
|
void addEvents(const mtx::responses::Timeline &events);
|
2019-09-11 01:00:04 +03:00
|
|
|
template<class T>
|
|
|
|
void sendMessage(const T &msg);
|
2019-08-31 23:43:31 +03:00
|
|
|
|
2019-09-01 00:44:17 +03:00
|
|
|
public slots:
|
|
|
|
void fetchHistory();
|
2019-10-03 23:39:56 +03:00
|
|
|
void setCurrentIndex(int index);
|
2019-09-18 21:34:30 +03:00
|
|
|
int currentIndex() const { return idToIndex(currentId); }
|
2019-09-19 00:37:30 +03:00
|
|
|
void markEventsAsRead(const std::vector<QString> &event_ids);
|
2019-09-01 00:44:17 +03:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
// Add old events at the top of the timeline.
|
|
|
|
void addBackwardsEvents(const mtx::responses::Messages &msgs);
|
2019-11-10 02:30:02 +03:00
|
|
|
void processOnePendingMessage();
|
|
|
|
void addPendingMessage(mtx::events::collections::TimelineEvents event);
|
2019-09-01 00:44:17 +03:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void oldMessagesRetrieved(const mtx::responses::Messages &res);
|
2019-09-18 23:58:25 +03:00
|
|
|
void messageFailed(QString txn_id);
|
|
|
|
void messageSent(QString txn_id, QString event_id);
|
2019-09-18 21:34:30 +03:00
|
|
|
void currentIndexChanged(int index);
|
2019-09-29 13:29:17 +03:00
|
|
|
void redactionFailed(QString id);
|
|
|
|
void eventRedacted(QString id);
|
2019-11-10 02:30:02 +03:00
|
|
|
void nextPendingMessage();
|
|
|
|
void newMessageToSend(mtx::events::collections::TimelineEvents event);
|
2019-12-03 04:26:41 +03:00
|
|
|
void mediaCached(QString mxcUrl, QString cacheUrl);
|
2019-09-01 00:44:17 +03:00
|
|
|
|
2019-08-31 00:20:53 +03:00
|
|
|
private:
|
2019-09-08 17:50:32 +03:00
|
|
|
DecryptionResult decryptEvent(
|
|
|
|
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e) const;
|
2019-09-09 22:42:33 +03:00
|
|
|
std::vector<QString> internalAddEvents(
|
|
|
|
const std::vector<mtx::events::collections::TimelineEvents> &timeline);
|
2019-09-19 23:44:25 +03:00
|
|
|
void sendEncryptedMessage(const std::string &txn_id, nlohmann::json content);
|
|
|
|
void handleClaimedKeys(std::shared_ptr<StateKeeper> keeper,
|
|
|
|
const std::map<std::string, std::string> &room_key,
|
|
|
|
const std::map<std::string, DevicePublicKeys> &pks,
|
|
|
|
const std::string &user_id,
|
|
|
|
const mtx::responses::ClaimKeys &res,
|
|
|
|
mtx::http::RequestErr err);
|
2019-10-31 16:09:51 +03:00
|
|
|
void updateLastMessage();
|
|
|
|
void readEvent(const std::string &id);
|
2019-09-08 17:50:32 +03:00
|
|
|
|
2019-08-31 23:43:31 +03:00
|
|
|
QHash<QString, mtx::events::collections::TimelineEvents> events;
|
2019-11-10 02:30:02 +03:00
|
|
|
QSet<QString> failed, read;
|
|
|
|
QList<QString> pending;
|
2019-08-31 00:20:53 +03:00
|
|
|
std::vector<QString> eventOrder;
|
|
|
|
|
2019-09-01 00:44:17 +03:00
|
|
|
QString room_id_;
|
|
|
|
QString prev_batch_token_;
|
|
|
|
|
2019-09-03 09:23:07 +03:00
|
|
|
bool isInitialSync = true;
|
2019-09-01 00:44:17 +03:00
|
|
|
bool paginationInProgress = false;
|
2019-11-10 02:30:02 +03:00
|
|
|
bool isProcessingPending = false;
|
2019-09-01 00:44:17 +03:00
|
|
|
|
2019-08-31 23:43:31 +03:00
|
|
|
QHash<QString, QColor> userColors;
|
2019-09-18 21:34:30 +03:00
|
|
|
QString currentId;
|
2019-10-03 19:07:01 +03:00
|
|
|
|
|
|
|
TimelineViewManager *manager_;
|
2019-11-10 02:30:02 +03:00
|
|
|
|
|
|
|
friend struct SendMessageVisitor;
|
2019-08-31 00:20:53 +03:00
|
|
|
};
|
2019-09-11 01:00:04 +03:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
TimelineModel::sendMessage(const T &msg)
|
|
|
|
{
|
2019-09-18 23:58:25 +03:00
|
|
|
auto txn_id = http::client()->generate_txn_id();
|
|
|
|
mtx::events::RoomEvent<T> msgCopy = {};
|
|
|
|
msgCopy.content = msg;
|
|
|
|
msgCopy.type = mtx::events::EventType::RoomMessage;
|
|
|
|
msgCopy.event_id = txn_id;
|
|
|
|
msgCopy.sender = http::client()->user_id().to_string();
|
|
|
|
msgCopy.origin_server_ts = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
2019-11-10 02:30:02 +03:00
|
|
|
emit newMessageToSend(msgCopy);
|
2019-09-11 01:00:04 +03:00
|
|
|
}
|