matrixion/src/timeline/TimelineModel.cpp

2823 lines
102 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2019-08-31 00:20:53 +03:00
#include "TimelineModel.h"
2019-09-09 22:42:33 +03:00
#include <algorithm>
2020-01-25 03:06:01 +03:00
#include <thread>
#include <type_traits>
2021-04-29 22:46:49 +03:00
#include <QClipboard>
2021-01-24 03:29:30 +03:00
#include <QDesktopServices>
2019-12-03 04:26:41 +03:00
#include <QFileDialog>
2021-04-29 22:46:49 +03:00
#include <QGuiApplication>
2019-12-03 04:26:41 +03:00
#include <QMimeDatabase>
#include <QRegularExpression>
2019-12-03 04:26:41 +03:00
#include <QStandardPaths>
2021-11-29 03:59:57 +03:00
#include <QVariant>
#include <utility>
2022-06-16 02:19:26 +03:00
#include "Cache.h"
2021-04-29 22:46:49 +03:00
#include "Cache_p.h"
2019-09-11 01:54:40 +03:00
#include "ChatPage.h"
#include "Config.h"
2019-12-27 23:49:55 +03:00
#include "EventAccessors.h"
2019-08-31 23:43:31 +03:00
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"
2019-10-03 19:07:01 +03:00
#include "TimelineViewManager.h"
2019-08-31 00:20:53 +03:00
#include "Utils.h"
#include "encryption/Olm.h"
2019-08-31 00:20:53 +03:00
Q_DECLARE_METATYPE(QModelIndex)
2020-04-14 21:15:11 +03:00
namespace std {
2021-12-29 00:30:12 +03:00
inline uint // clazy:exclude=qhash-namespace
2020-04-14 21:15:11 +03:00
qHash(const std::string &key, uint seed = 0)
{
2021-09-18 01:22:33 +03:00
return qHash(QByteArray::fromRawData(key.data(), (int)key.length()), seed);
2020-04-14 21:15:11 +03:00
}
}
2019-08-31 23:43:31 +03:00
namespace {
2019-12-27 23:49:55 +03:00
struct RoomEventType
{
2021-09-18 01:22:33 +03:00
template<class T>
qml_mtx_events::EventType operator()(const mtx::events::Event<T> &e)
{
return qml_mtx_events::toRoomEventType(e.type);
2021-09-18 01:22:33 +03:00
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Audio> &)
{
return qml_mtx_events::EventType::AudioMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Emote> &)
{
return qml_mtx_events::EventType::EmoteMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::File> &)
{
return qml_mtx_events::EventType::FileMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Image> &)
{
return qml_mtx_events::EventType::ImageMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Notice> &)
{
return qml_mtx_events::EventType::NoticeMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Text> &)
{
return qml_mtx_events::EventType::TextMessage;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Video> &)
{
return qml_mtx_events::EventType::VideoMessage;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationRequest> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationRequest;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationStart> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationStart;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationMac> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationMac;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationAccept> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationAccept;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationReady> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationReady;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationCancel> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationCancel;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationKey> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationKey;
}
qml_mtx_events::EventType
operator()(const mtx::events::Event<mtx::events::msg::KeyVerificationDone> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::KeyVerificationDone;
}
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::msg::Redacted> &)
{
return qml_mtx_events::EventType::Redacted;
}
2022-06-27 19:09:31 +03:00
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallInvite> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::CallInvite;
}
2022-06-27 19:09:31 +03:00
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallAnswer> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::CallAnswer;
}
2022-06-27 19:09:31 +03:00
qml_mtx_events::EventType operator()(const mtx::events::Event<mtx::events::voip::CallHangUp> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::CallHangUp;
}
qml_mtx_events::EventType
2022-06-27 19:09:31 +03:00
operator()(const mtx::events::Event<mtx::events::voip::CallCandidates> &)
2021-09-18 01:22:33 +03:00
{
return qml_mtx_events::EventType::CallCandidates;
}
// ::EventType::Type operator()(const Event<mtx::events::msg::Location> &e) { return
// ::EventType::LocationMessage; }
2019-12-27 23:49:55 +03:00
};
}
2019-09-09 22:42:33 +03:00
qml_mtx_events::EventType
qml_mtx_events::toRoomEventType(mtx::events::EventType e)
{
using mtx::events::EventType;
switch (e) {
case EventType::RoomKeyRequest:
return qml_mtx_events::EventType::KeyRequest;
case EventType::Reaction:
return qml_mtx_events::EventType::Reaction;
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::RoomCreate;
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::RoomGuestAccess;
case EventType::RoomHistoryVisibility:
return qml_mtx_events::EventType::RoomHistoryVisibility;
case EventType::RoomJoinRules:
return qml_mtx_events::EventType::RoomJoinRules;
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;
2022-07-09 16:36:15 +03:00
case EventType::PolicyRuleUser:
return qml_mtx_events::EventType::PolicyRuleUser;
case EventType::PolicyRuleRoom:
return qml_mtx_events::EventType::PolicyRuleRoom;
case EventType::PolicyRuleServer:
return qml_mtx_events::EventType::PolicyRuleServer;
case EventType::SpaceParent:
return qml_mtx_events::EventType::SpaceParent;
case EventType::SpaceChild:
return qml_mtx_events::EventType::SpaceChild;
case EventType::ImagePackInRoom:
return qml_mtx_events::ImagePackInRoom;
case EventType::ImagePackInAccountData:
return qml_mtx_events::ImagePackInAccountData;
case EventType::ImagePackRooms:
return qml_mtx_events::ImagePackRooms;
case EventType::Unsupported:
return qml_mtx_events::EventType::Unsupported;
default:
return qml_mtx_events::EventType::UnknownMessage;
}
}
qml_mtx_events::EventType
2019-12-27 23:49:55 +03:00
toRoomEventType(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(RoomEventType{}, event);
2019-08-31 23:43:31 +03:00
}
2020-01-23 22:59:17 +03:00
QString
toRoomEventTypeString(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit([](const auto &e) { return QString::fromStdString(to_string(e.type)); },
event);
2020-01-23 22:59:17 +03:00
}
mtx::events::EventType
qml_mtx_events::fromRoomEventType(qml_mtx_events::EventType t)
{
2021-09-18 01:22:33 +03:00
switch (t) {
// Unsupported event
case qml_mtx_events::Unsupported:
return mtx::events::EventType::Unsupported;
/// m.room_key_request
case qml_mtx_events::KeyRequest:
return mtx::events::EventType::RoomKeyRequest;
/// m.reaction:
case qml_mtx_events::Reaction:
return mtx::events::EventType::Reaction;
/// m.room.aliases
case qml_mtx_events::Aliases:
return mtx::events::EventType::RoomAliases;
/// m.room.avatar
case qml_mtx_events::Avatar:
return mtx::events::EventType::RoomAvatar;
/// m.call.invite
case qml_mtx_events::CallInvite:
return mtx::events::EventType::CallInvite;
/// m.call.answer
case qml_mtx_events::CallAnswer:
return mtx::events::EventType::CallAnswer;
/// m.call.hangup
case qml_mtx_events::CallHangUp:
return mtx::events::EventType::CallHangUp;
/// m.call.candidates
case qml_mtx_events::CallCandidates:
return mtx::events::EventType::CallCandidates;
/// m.room.canonical_alias
case qml_mtx_events::CanonicalAlias:
return mtx::events::EventType::RoomCanonicalAlias;
/// m.room.create
case qml_mtx_events::RoomCreate:
return mtx::events::EventType::RoomCreate;
/// m.room.encrypted.
case qml_mtx_events::Encrypted:
return mtx::events::EventType::RoomEncrypted;
/// m.room.encryption.
case qml_mtx_events::Encryption:
return mtx::events::EventType::RoomEncryption;
/// m.room.guest_access
case qml_mtx_events::RoomGuestAccess:
return mtx::events::EventType::RoomGuestAccess;
/// m.room.history_visibility
case qml_mtx_events::RoomHistoryVisibility:
return mtx::events::EventType::RoomHistoryVisibility;
/// m.room.join_rules
case qml_mtx_events::RoomJoinRules:
return mtx::events::EventType::RoomJoinRules;
/// m.room.member
case qml_mtx_events::Member:
return mtx::events::EventType::RoomMember;
/// m.room.name
case qml_mtx_events::Name:
return mtx::events::EventType::RoomName;
/// m.room.power_levels
case qml_mtx_events::PowerLevels:
return mtx::events::EventType::RoomPowerLevels;
/// m.room.tombstone
case qml_mtx_events::Tombstone:
return mtx::events::EventType::RoomTombstone;
/// m.room.topic
case qml_mtx_events::Topic:
return mtx::events::EventType::RoomTopic;
/// m.room.redaction
case qml_mtx_events::Redaction:
return mtx::events::EventType::RoomRedaction;
/// m.room.pinned_events
case qml_mtx_events::PinnedEvents:
return mtx::events::EventType::RoomPinnedEvents;
2022-02-04 20:47:17 +03:00
/// m.widget
case qml_mtx_events::Widget:
return mtx::events::EventType::Widget;
2021-09-18 01:22:33 +03:00
// m.sticker
case qml_mtx_events::Sticker:
return mtx::events::EventType::Sticker;
// m.tag
case qml_mtx_events::Tag:
return mtx::events::EventType::Tag;
2022-07-09 16:36:15 +03:00
case qml_mtx_events::PolicyRuleUser:
return mtx::events::EventType::PolicyRuleUser;
case qml_mtx_events::PolicyRuleRoom:
return mtx::events::EventType::PolicyRuleRoom;
case qml_mtx_events::PolicyRuleServer:
return mtx::events::EventType::PolicyRuleServer;
2021-12-11 08:10:41 +03:00
// m.space.parent
case qml_mtx_events::SpaceParent:
return mtx::events::EventType::SpaceParent;
// m.space.child
case qml_mtx_events::SpaceChild:
return mtx::events::EventType::SpaceChild;
2021-09-18 01:22:33 +03:00
/// m.room.message
case qml_mtx_events::AudioMessage:
case qml_mtx_events::EmoteMessage:
case qml_mtx_events::FileMessage:
case qml_mtx_events::ImageMessage:
case qml_mtx_events::LocationMessage:
case qml_mtx_events::NoticeMessage:
case qml_mtx_events::TextMessage:
case qml_mtx_events::VideoMessage:
case qml_mtx_events::Redacted:
case qml_mtx_events::UnknownMessage:
case qml_mtx_events::KeyVerificationRequest:
case qml_mtx_events::KeyVerificationStart:
case qml_mtx_events::KeyVerificationMac:
case qml_mtx_events::KeyVerificationAccept:
case qml_mtx_events::KeyVerificationCancel:
case qml_mtx_events::KeyVerificationKey:
case qml_mtx_events::KeyVerificationDone:
case qml_mtx_events::KeyVerificationReady:
return mtx::events::EventType::RoomMessage;
//! m.image_pack, currently im.ponies.room_emotes
case qml_mtx_events::ImagePackInRoom:
return mtx::events::EventType::ImagePackInRoom;
//! m.image_pack, currently im.ponies.user_emotes
case qml_mtx_events::ImagePackInAccountData:
return mtx::events::EventType::ImagePackInAccountData;
//! m.image_pack.rooms, currently im.ponies.emote_rooms
case qml_mtx_events::ImagePackRooms:
return mtx::events::EventType::ImagePackRooms;
default:
return mtx::events::EventType::Unsupported;
};
}
2021-12-28 20:44:26 +03:00
TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObject *parent)
: QAbstractListModel(parent)
2021-12-28 20:44:26 +03:00
, room_id_(std::move(room_id))
, events(room_id_.toStdString(), this)
2019-10-03 19:07:01 +03:00
, manager_(manager)
2021-12-28 20:44:26 +03:00
, permissions_{room_id_}
{
2021-09-18 01:22:33 +03:00
this->isEncrypted_ = cache::isRoomEncrypted(room_id_.toStdString());
auto roomInfo = cache::singleRoomInfo(room_id_.toStdString());
this->isSpace_ = roomInfo.is_space;
this->notification_count = roomInfo.notification_count;
this->highlight_count = roomInfo.highlight_count;
lastMessage_.timestamp = roomInfo.approximate_last_modification_ts;
2021-09-18 01:22:33 +03:00
connect(
this,
&TimelineModel::redactionFailed,
this,
[](const QString &msg) { emit ChatPage::instance()->showNotification(msg); },
Qt::QueuedConnection);
connect(this, &TimelineModel::dataAtIdChanged, this, [this](const QString &id) {
2021-11-08 21:32:14 +03:00
relatedEventCacheBuster++;
auto idx = idToIndex(id);
if (idx != -1) {
auto pos = index(idx);
nhlog::ui()->debug("data changed at {}", id.toStdString());
emit dataChanged(pos, pos);
} else {
nhlog::ui()->debug("id not found {}", id.toStdString());
}
});
2021-09-18 01:22:33 +03:00
connect(this,
&TimelineModel::newMessageToSend,
this,
&TimelineModel::addPendingMessage,
Qt::QueuedConnection);
connect(this, &TimelineModel::addPendingMessageToStore, &events, &EventStore::addPending);
connect(&events, &EventStore::dataChanged, this, [this](int from, int to) {
relatedEventCacheBuster++;
nhlog::ui()->debug(
"data changed {} to {}", events.size() - to - 1, events.size() - from - 1);
emit dataChanged(index(events.size() - to - 1, 0), index(events.size() - from - 1, 0));
});
connect(&events, &EventStore::pinsChanged, this, &TimelineModel::pinnedMessagesChanged);
2021-09-18 01:22:33 +03:00
connect(&events, &EventStore::beginInsertRows, this, [this](int from, int to) {
int first = events.size() - to;
int last = events.size() - from;
if (from >= events.size()) {
int batch_size = to - from;
first += batch_size;
last += batch_size;
} else {
first -= 1;
last -= 1;
}
nhlog::ui()->debug("begin insert from {} to {}", first, last);
beginInsertRows(QModelIndex(), first, last);
});
connect(&events, &EventStore::endInsertRows, this, [this]() { endInsertRows(); });
connect(&events, &EventStore::beginResetModel, this, [this]() { beginResetModel(); });
connect(&events, &EventStore::endResetModel, this, [this]() { endResetModel(); });
connect(&events, &EventStore::newEncryptedImage, this, &TimelineModel::newEncryptedImage);
connect(&events, &EventStore::fetchedMore, this, [this]() {
setPaginationInProgress(false);
updateLastMessage();
});
2021-09-18 01:22:33 +03:00
connect(&events,
&EventStore::startDMVerification,
this,
[this](const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &msg) {
2021-09-18 01:22:33 +03:00
ChatPage::instance()->receivedRoomDeviceVerificationRequest(msg, this);
});
connect(&events, &EventStore::updateFlowEventId, this, [this](std::string event_id) {
this->updateFlowEventId(std::move(event_id));
2021-09-18 01:22:33 +03:00
});
// When a message is sent, check if the current edit/reply relates to that message,
// and update the event_id so that it points to the sent message and not the pending one.
connect(
&events,
&EventStore::messageSent,
this,
[this](const std::string &txn_id, const std::string &event_id) {
if (edit_.toStdString() == txn_id) {
edit_ = QString::fromStdString(event_id);
emit editChanged(edit_);
}
if (reply_.toStdString() == txn_id) {
reply_ = QString::fromStdString(event_id);
emit replyChanged(reply_);
}
},
Qt::QueuedConnection);
2021-09-18 01:22:33 +03:00
connect(
manager_, &TimelineViewManager::initialSyncChanged, &events, &EventStore::enableKeyRequests);
connect(this, &TimelineModel::encryptionChanged, this, &TimelineModel::trustlevelChanged);
connect(this, &TimelineModel::roomMemberCountChanged, this, &TimelineModel::trustlevelChanged);
connect(
cache::client(), &Cache::verificationStatusChanged, this, &TimelineModel::trustlevelChanged);
showEventTimer.callOnTimeout(this, &TimelineModel::scrollTimerEvent);
2022-02-05 10:40:56 +03:00
connect(this, &TimelineModel::newState, this, [this](mtx::responses::StateEvents events_) {
cache::client()->updateState(room_id_.toStdString(), events_);
this->syncState({std::move(events_.events)});
});
}
2019-08-31 00:20:53 +03:00
QHash<int, QByteArray>
TimelineModel::roleNames() const
{
static QHash<int, QByteArray> roles{
2021-09-18 01:22:33 +03:00
{Type, "type"},
{TypeString, "typeString"},
{IsOnlyEmoji, "isOnlyEmoji"},
{Body, "body"},
{FormattedBody, "formattedBody"},
{PreviousMessageUserId, "previousMessageUserId"},
{IsSender, "isSender"},
{UserId, "userId"},
{UserName, "userName"},
{PreviousMessageDay, "previousMessageDay"},
{PreviousMessageIsStateEvent, "previousMessageIsStateEvent"},
2021-09-18 01:22:33 +03:00
{Day, "day"},
{Timestamp, "timestamp"},
{Url, "url"},
{ThumbnailUrl, "thumbnailUrl"},
2022-03-21 02:48:27 +03:00
{Duration, "duration"},
2021-09-18 01:22:33 +03:00
{Blurhash, "blurhash"},
{Filename, "filename"},
{Filesize, "filesize"},
{MimeType, "mimetype"},
{OriginalHeight, "originalHeight"},
{OriginalWidth, "originalWidth"},
{ProportionalHeight, "proportionalHeight"},
{EventId, "eventId"},
{State, "status"},
{IsEdited, "isEdited"},
{IsEditable, "isEditable"},
{IsEncrypted, "isEncrypted"},
{IsStateEvent, "isStateEvent"},
2021-09-18 01:22:33 +03:00
{Trustlevel, "trustlevel"},
{EncryptionError, "encryptionError"},
{ReplyTo, "replyTo"},
{Reactions, "reactions"},
{RoomId, "roomId"},
{RoomName, "roomName"},
{RoomTopic, "roomTopic"},
{CallType, "callType"},
{Dump, "dump"},
{RelatedEventCacheBuster, "relatedEventCacheBuster"},
};
return roles;
2019-08-31 00:20:53 +03:00
}
int
TimelineModel::rowCount(const QModelIndex &parent) const
{
2021-09-18 01:22:33 +03:00
Q_UNUSED(parent);
return this->events.size();
2019-08-31 00:20:53 +03:00
}
QVariantMap
TimelineModel::getDump(const QString &eventId, const QString &relatedTo) const
{
2021-09-18 01:22:33 +03:00
if (auto event = events.get(eventId.toStdString(), relatedTo.toStdString()))
return data(*event, Dump).toMap();
return {};
}
2019-08-31 00:20:53 +03:00
QVariant
2020-07-10 00:15:22 +03:00
TimelineModel::data(const mtx::events::collections::TimelineEvents &event, int role) const
2019-08-31 00:20:53 +03:00
{
2021-09-18 01:22:33 +03:00
using namespace mtx::accessors;
namespace acc = mtx::accessors;
switch (role) {
case IsSender:
return {acc::sender(event) == http::client()->user_id().to_string()};
2021-09-18 01:22:33 +03:00
case UserId:
return QVariant(QString::fromStdString(acc::sender(event)));
case UserName:
return QVariant(displayName(QString::fromStdString(acc::sender(event))));
case Day: {
QDateTime prevDate = origin_server_ts(event);
prevDate.setTime(QTime());
return {prevDate.toMSecsSinceEpoch()};
2021-09-18 01:22:33 +03:00
}
case Timestamp:
return QVariant(origin_server_ts(event));
case Type:
return {toRoomEventType(event)};
2021-09-18 01:22:33 +03:00
case TypeString:
return QVariant(toRoomEventTypeString(event));
case IsOnlyEmoji: {
QString qBody = QString::fromStdString(body(event));
QVector<uint> utf32_string = qBody.toUcs4();
int emojiCount = 0;
for (auto &code : utf32_string) {
if (utils::codepointIsEmoji(code)) {
emojiCount++;
} else {
return {0};
2021-09-18 01:22:33 +03:00
}
}
return {emojiCount};
2021-09-18 01:22:33 +03:00
}
case Body:
return QVariant(utils::replaceEmoji(QString::fromStdString(body(event)).toHtmlEscaped()));
case FormattedBody: {
const static QRegularExpression replyFallback(
QStringLiteral("<mx-reply>.*</mx-reply>"),
QRegularExpression::DotMatchesEverythingOption);
2021-09-18 01:22:33 +03:00
auto ascent = QFontMetrics(UserSettings::instance()->font()).ascent();
bool isReply = utils::isReply(event);
auto formattedBody_ = QString::fromStdString(formatted_body(event));
if (formattedBody_.isEmpty()) {
auto body_ = QString::fromStdString(body(event));
if (isReply) {
while (body_.startsWith(QLatin1String("> ")))
2021-09-18 01:22:33 +03:00
body_ = body_.right(body_.size() - body_.indexOf('\n') - 1);
if (body_.startsWith('\n'))
body_ = body_.right(body_.size() - 1);
}
formattedBody_ = body_.toHtmlEscaped().replace('\n', QLatin1String("<br>"));
2021-09-18 01:22:33 +03:00
} else {
if (isReply)
formattedBody_ = formattedBody_.remove(replyFallback);
}
// TODO(Nico): Don't parse html with a regex
const static QRegularExpression matchIsImg(QStringLiteral("<img [^>]+>"));
auto itIsImg = matchIsImg.globalMatch(formattedBody_);
while (itIsImg.hasNext()) {
// The current <img> tag.
const QString curImg = itIsImg.next().captured(0);
// The replacement for the current <img>.
auto imgReplacement = curImg;
// Construct image parameters later used by MxcImageProvider.
QString imgParams;
if (curImg.contains(QLatin1String("height"))) {
const static QRegularExpression matchImgHeight(
QStringLiteral("height=([\"\']?)(\\d+)([\"\']?)"));
// Make emoticons twice as high as the font.
if (curImg.contains(QLatin1String("data-mx-emoticon"))) {
imgReplacement =
imgReplacement.replace(matchImgHeight, "height=\\1%1\\3").arg(ascent * 2);
}
const auto height = matchImgHeight.match(imgReplacement).captured(2).toInt();
imgParams = QStringLiteral("?scale&height=%1").arg(height);
}
// Replace src in current <img>.
const static QRegularExpression matchImgUri(QStringLiteral("src=\"mxc://([^\"]*)\""));
imgReplacement.replace(matchImgUri,
QStringLiteral(R"(src="image://mxcImage/\1%1")").arg(imgParams));
// Same regex but for single quotes around the src
const static QRegularExpression matchImgUri2(QStringLiteral("src=\'mxc://([^\']*)\'"));
imgReplacement.replace(matchImgUri2,
QStringLiteral("src=\'image://mxcImage/\\1%1\'").arg(imgParams));
// Replace <img> in formattedBody_ with our new <img>.
formattedBody_.replace(curImg, imgReplacement);
}
2021-09-18 01:22:33 +03:00
return QVariant(
utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_))));
}
case Url:
return QVariant(QString::fromStdString(url(event)));
case ThumbnailUrl:
return QVariant(QString::fromStdString(thumbnail_url(event)));
2022-03-21 02:48:27 +03:00
case Duration:
return QVariant(static_cast<qulonglong>(duration(event)));
2021-09-18 01:22:33 +03:00
case Blurhash:
return QVariant(QString::fromStdString(blurhash(event)));
case Filename:
return QVariant(QString::fromStdString(filename(event)));
case Filesize:
return QVariant(utils::humanReadableFileSize(filesize(event)));
case MimeType:
return QVariant(QString::fromStdString(mimetype(event)));
case OriginalHeight:
return QVariant(qulonglong{media_height(event)});
case OriginalWidth:
return QVariant(qulonglong{media_width(event)});
case ProportionalHeight: {
auto w = media_width(event);
if (w == 0)
w = 1;
double prop = media_height(event) / (double)w;
return {prop > 0 ? prop : 1.};
2021-09-18 01:22:33 +03:00
}
case EventId: {
if (auto replaces = relations(event).replaces())
return QVariant(QString::fromStdString(replaces.value()));
else
return QVariant(QString::fromStdString(event_id(event)));
}
case State: {
auto id = QString::fromStdString(event_id(event));
auto containsOthers = [](const auto &vec) {
for (const auto &e : vec)
if (e.second != http::client()->user_id().to_string())
return true;
return false;
};
2021-09-18 01:22:33 +03:00
// only show read receipts for messages not from us
if (acc::sender(event) != http::client()->user_id().to_string())
return qml_mtx_events::Empty;
2021-12-28 22:09:08 +03:00
else if (!id.isEmpty() && id[0] == 'm')
2021-09-18 01:22:33 +03:00
return qml_mtx_events::Sent;
else if (read.contains(id) || containsOthers(cache::readReceipts(id, room_id_)))
return qml_mtx_events::Read;
else
return qml_mtx_events::Received;
}
case IsEdited:
return {relations(event).replaces().has_value()};
2021-09-18 01:22:33 +03:00
case IsEditable:
return {!is_state_event(event) &&
mtx::accessors::sender(event) == http::client()->user_id().to_string()};
2021-09-18 01:22:33 +03:00
case IsEncrypted: {
auto encrypted_event = events.get(event_id(event), "", false);
2021-09-18 01:22:33 +03:00
return encrypted_event &&
std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
*encrypted_event);
}
case IsStateEvent: {
return is_state_event(event);
}
2021-09-18 01:22:33 +03:00
case Trustlevel: {
auto encrypted_event = events.get(event_id(event), "", false);
2021-09-18 01:22:33 +03:00
if (encrypted_event) {
if (auto encrypted =
std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
&*encrypted_event)) {
return olm::calculate_trust(
encrypted->sender,
MegolmSessionIndex(room_id_.toStdString(), encrypted->content));
}
}
return crypto::Trust::Unverified;
}
case EncryptionError:
return events.decryptionError(event_id(event));
case ReplyTo:
return QVariant(QString::fromStdString(relations(event).reply_to().value_or("")));
case Reactions: {
auto id = relations(event).replaces().value_or(event_id(event));
return QVariant::fromValue(events.reactions(id));
}
case RoomId:
return QVariant(room_id_);
case RoomName:
return QVariant(
utils::replaceEmoji(QString::fromStdString(room_name(event)).toHtmlEscaped()));
case RoomTopic:
return QVariant(utils::replaceEmoji(
utils::linkifyMessage(QString::fromStdString(room_topic(event))
.toHtmlEscaped()
.replace(QLatin1String("\n"), QLatin1String("<br>")))));
2021-09-18 01:22:33 +03:00
case CallType:
return QVariant(QString::fromStdString(call_type(event)));
case Dump: {
QVariantMap m;
auto names = roleNames();
m.insert(names[Type], data(event, static_cast<int>(Type)));
m.insert(names[TypeString], data(event, static_cast<int>(TypeString)));
m.insert(names[IsOnlyEmoji], data(event, static_cast<int>(IsOnlyEmoji)));
m.insert(names[Body], data(event, static_cast<int>(Body)));
m.insert(names[FormattedBody], data(event, static_cast<int>(FormattedBody)));
m.insert(names[IsSender], data(event, static_cast<int>(IsSender)));
m.insert(names[UserId], data(event, static_cast<int>(UserId)));
m.insert(names[UserName], data(event, static_cast<int>(UserName)));
m.insert(names[Day], data(event, static_cast<int>(Day)));
m.insert(names[Timestamp], data(event, static_cast<int>(Timestamp)));
m.insert(names[Url], data(event, static_cast<int>(Url)));
m.insert(names[ThumbnailUrl], data(event, static_cast<int>(ThumbnailUrl)));
2022-03-21 02:48:27 +03:00
m.insert(names[Duration], data(event, static_cast<int>(Duration)));
2021-09-18 01:22:33 +03:00
m.insert(names[Blurhash], data(event, static_cast<int>(Blurhash)));
m.insert(names[Filename], data(event, static_cast<int>(Filename)));
m.insert(names[Filesize], data(event, static_cast<int>(Filesize)));
m.insert(names[MimeType], data(event, static_cast<int>(MimeType)));
m.insert(names[OriginalHeight], data(event, static_cast<int>(OriginalHeight)));
m.insert(names[OriginalWidth], data(event, static_cast<int>(OriginalWidth)));
m.insert(names[ProportionalHeight], data(event, static_cast<int>(ProportionalHeight)));
m.insert(names[EventId], data(event, static_cast<int>(EventId)));
m.insert(names[State], data(event, static_cast<int>(State)));
m.insert(names[IsEdited], data(event, static_cast<int>(IsEdited)));
m.insert(names[IsEditable], data(event, static_cast<int>(IsEditable)));
m.insert(names[IsEncrypted], data(event, static_cast<int>(IsEncrypted)));
m.insert(names[IsStateEvent], data(event, static_cast<int>(IsStateEvent)));
2021-09-18 01:22:33 +03:00
m.insert(names[ReplyTo], data(event, static_cast<int>(ReplyTo)));
m.insert(names[RoomName], data(event, static_cast<int>(RoomName)));
m.insert(names[RoomTopic], data(event, static_cast<int>(RoomTopic)));
m.insert(names[CallType], data(event, static_cast<int>(CallType)));
m.insert(names[EncryptionError], data(event, static_cast<int>(EncryptionError)));
return QVariant(m);
}
case RelatedEventCacheBuster:
return relatedEventCacheBuster;
default:
return {};
2021-09-18 01:22:33 +03:00
}
2019-08-31 00:20:53 +03:00
}
2020-01-11 20:53:32 +03:00
QVariant
TimelineModel::data(const QModelIndex &index, int role) const
{
2021-09-18 01:22:33 +03:00
using namespace mtx::accessors;
namespace acc = mtx::accessors;
if (index.row() < 0 && index.row() >= rowCount())
return {};
2020-01-11 20:53:32 +03:00
2021-09-18 01:22:33 +03:00
// HACK(Nico): fetchMore likes to break with dynamically sized delegates and reuseItems
if (index.row() + 1 == rowCount() && !m_paginationInProgress)
const_cast<TimelineModel *>(this)->fetchMore(index);
2021-09-18 01:22:33 +03:00
auto event = events.get(rowCount() - index.row() - 1);
2020-01-11 20:53:32 +03:00
2021-09-18 01:22:33 +03:00
if (!event)
return "";
2020-01-11 20:53:32 +03:00
if (role == PreviousMessageDay || role == PreviousMessageUserId ||
role == PreviousMessageIsStateEvent) {
2021-09-18 01:22:33 +03:00
int prevIdx = rowCount() - index.row() - 2;
if (prevIdx < 0)
return {};
2021-09-18 01:22:33 +03:00
auto tempEv = events.get(prevIdx);
if (!tempEv)
return {};
2021-09-18 01:22:33 +03:00
if (role == PreviousMessageUserId)
return data(*tempEv, UserId);
else if (role == PreviousMessageDay)
2021-09-18 01:22:33 +03:00
return data(*tempEv, Day);
else
return data(*tempEv, IsStateEvent);
2021-09-18 01:22:33 +03:00
}
return data(*event, role);
2020-01-11 20:53:32 +03:00
}
QVariant
TimelineModel::dataById(const QString &id, int role, const QString &relatedTo)
{
2021-09-18 01:22:33 +03:00
if (auto event = events.get(id.toStdString(), relatedTo.toStdString()))
return data(*event, role);
return {};
}
bool
TimelineModel::canFetchMore(const QModelIndex &) const
{
2021-09-18 01:22:33 +03:00
if (!events.size())
return true;
if (auto first = events.get(0);
first &&
!std::holds_alternative<mtx::events::StateEvent<mtx::events::state::Create>>(*first))
return true;
else
2021-09-18 01:22:33 +03:00
return false;
}
void
TimelineModel::setPaginationInProgress(const bool paginationInProgress)
{
2021-09-18 01:22:33 +03:00
if (m_paginationInProgress == paginationInProgress) {
return;
}
2021-09-18 01:22:33 +03:00
m_paginationInProgress = paginationInProgress;
emit paginationInProgressChanged(m_paginationInProgress);
}
void
TimelineModel::fetchMore(const QModelIndex &)
{
2021-09-18 01:22:33 +03:00
if (m_paginationInProgress) {
nhlog::ui()->warn("Already loading older messages");
return;
}
2021-09-18 01:22:33 +03:00
setPaginationInProgress(true);
2020-07-13 01:08:58 +03:00
2021-09-18 01:22:33 +03:00
events.fetchMore();
}
2021-05-21 22:19:03 +03:00
void
TimelineModel::sync(const mtx::responses::JoinedRoom &room)
{
2021-09-18 01:22:33 +03:00
this->syncState(room.state);
this->addEvents(room.timeline);
2021-05-21 22:19:03 +03:00
2021-09-18 01:22:33 +03:00
if (room.unread_notifications.highlight_count != highlight_count ||
room.unread_notifications.notification_count != notification_count) {
notification_count = room.unread_notifications.notification_count;
highlight_count = room.unread_notifications.highlight_count;
emit notificationsChanged();
}
2021-05-21 22:19:03 +03:00
}
void
TimelineModel::syncState(const mtx::responses::State &s)
{
2021-09-18 01:22:33 +03:00
using namespace mtx::events;
for (const auto &e : s.events) {
if (std::holds_alternative<StateEvent<state::Avatar>>(e))
emit roomAvatarUrlChanged();
else if (std::holds_alternative<StateEvent<state::Name>>(e))
emit roomNameChanged();
else if (std::holds_alternative<StateEvent<state::Topic>>(e))
emit roomTopicChanged();
2021-12-11 08:10:41 +03:00
else if (std::holds_alternative<StateEvent<state::PinnedEvents>>(e))
emit pinnedMessagesChanged();
2022-02-04 20:47:17 +03:00
else if (std::holds_alternative<StateEvent<state::Widget>>(e))
emit widgetLinksChanged();
2021-12-11 08:10:41 +03:00
else if (std::holds_alternative<StateEvent<state::PowerLevels>>(e)) {
2021-09-18 01:22:33 +03:00
permissions_.invalidate();
emit permissionsChanged();
} else if (std::holds_alternative<StateEvent<state::Member>>(e)) {
emit roomAvatarUrlChanged();
emit roomNameChanged();
emit roomMemberCountChanged();
if (roomMemberCount() <= 2) {
emit isDirectChanged();
emit directChatOtherUserIdChanged();
}
} else if (std::holds_alternative<StateEvent<state::Encryption>>(e)) {
this->isEncrypted_ = cache::isRoomEncrypted(room_id_.toStdString());
emit encryptionChanged();
}
}
}
2019-08-31 23:43:31 +03:00
void
2019-09-03 03:14:49 +03:00
TimelineModel::addEvents(const mtx::responses::Timeline &timeline)
2019-08-31 23:43:31 +03:00
{
2021-09-18 01:22:33 +03:00
if (timeline.events.empty())
return;
2019-10-03 19:07:01 +03:00
2021-09-18 01:22:33 +03:00
events.handleSync(timeline);
using namespace mtx::events;
for (auto e : timeline.events) {
if (auto encryptedEvent = std::get_if<EncryptedEvent<msg::Encrypted>>(&e)) {
MegolmSessionIndex index(room_id_.toStdString(), encryptedEvent->content);
auto result = olm::decryptEvent(index, *encryptedEvent);
if (result.event)
e = result.event.value();
}
2022-06-27 19:09:31 +03:00
if (std::holds_alternative<RoomEvent<voip::CallCandidates>>(e) ||
std::holds_alternative<RoomEvent<voip::CallInvite>>(e) ||
std::holds_alternative<RoomEvent<voip::CallAnswer>>(e) ||
std::holds_alternative<RoomEvent<voip::CallHangUp>>(e))
2021-09-18 01:22:33 +03:00
std::visit(
[this](auto &event) {
event.room_id = room_id_.toStdString();
if constexpr (std::is_same_v<std::decay_t<decltype(event)>,
2022-06-27 19:09:31 +03:00
RoomEvent<voip::CallAnswer>> ||
2021-09-18 01:22:33 +03:00
std::is_same_v<std::decay_t<decltype(event)>,
2022-06-27 19:09:31 +03:00
RoomEvent<voip::CallHangUp>>)
2021-09-18 01:22:33 +03:00
emit newCallEvent(event);
else {
if (event.sender != http::client()->user_id().to_string())
emit newCallEvent(event);
}
},
e);
else if (std::holds_alternative<StateEvent<state::Avatar>>(e))
emit roomAvatarUrlChanged();
else if (std::holds_alternative<StateEvent<state::Name>>(e))
emit roomNameChanged();
else if (std::holds_alternative<StateEvent<state::Topic>>(e))
emit roomTopicChanged();
2021-12-11 08:10:41 +03:00
else if (std::holds_alternative<StateEvent<state::PinnedEvents>>(e))
emit pinnedMessagesChanged();
2022-02-04 20:47:17 +03:00
else if (std::holds_alternative<StateEvent<state::Widget>>(e))
emit widgetLinksChanged();
2021-09-18 01:22:33 +03:00
else if (std::holds_alternative<StateEvent<state::PowerLevels>>(e)) {
permissions_.invalidate();
emit permissionsChanged();
} else if (std::holds_alternative<StateEvent<state::Member>>(e)) {
emit roomAvatarUrlChanged();
emit roomNameChanged();
emit roomMemberCountChanged();
} else if (std::holds_alternative<StateEvent<state::Encryption>>(e)) {
this->isEncrypted_ = cache::isRoomEncrypted(room_id_.toStdString());
emit encryptionChanged();
}
}
updateLastMessage();
}
2019-10-03 19:07:01 +03:00
2019-11-09 05:30:17 +03:00
template<typename T>
auto
isMessage(const mtx::events::RoomEvent<T> &e)
-> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool>
{
2021-09-18 01:22:33 +03:00
return true;
2019-11-09 05:30:17 +03:00
}
template<typename T>
auto
isMessage(const mtx::events::Event<T> &)
{
2021-09-18 01:22:33 +03:00
return false;
2019-11-09 05:30:17 +03:00
}
template<typename T>
auto
isMessage(const mtx::events::EncryptedEvent<T> &)
{
2021-09-18 01:22:33 +03:00
return true;
}
2020-07-11 02:19:48 +03:00
auto
2022-06-27 19:09:31 +03:00
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &)
2020-07-11 02:19:48 +03:00
{
2021-09-18 01:22:33 +03:00
return true;
2020-07-11 02:19:48 +03:00
}
auto
2022-06-27 19:09:31 +03:00
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &)
2020-07-11 02:19:48 +03:00
{
2021-09-18 01:22:33 +03:00
return true;
2020-07-11 02:19:48 +03:00
}
auto
2022-06-27 19:09:31 +03:00
isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &)
2020-07-11 02:19:48 +03:00
{
2021-09-18 01:22:33 +03:00
return true;
2020-07-11 02:19:48 +03:00
}
// Workaround. We also want to see a room at the top, if we just joined it
auto
isYourJoin(const mtx::events::StateEvent<mtx::events::state::Member> &e)
{
2021-09-18 01:22:33 +03:00
return e.content.membership == mtx::events::state::Membership::Join &&
e.state_key == http::client()->user_id().to_string();
}
template<typename T>
auto
isYourJoin(const mtx::events::Event<T> &)
{
2021-09-18 01:22:33 +03:00
return false;
}
DescInfo
TimelineModel::lastMessage() const
{
if (lastMessage_.event_id.isEmpty())
QTimer::singleShot(0, this, &TimelineModel::updateLastMessage);
return lastMessage_;
}
void
TimelineModel::updateLastMessage()
{
// only try to generate a preview for the last 1000 messages
auto end = std::max(events.size() - 1001, 0);
for (auto it = events.size() - 1; it >= end; --it) {
2021-09-18 01:22:33 +03:00
auto event = events.get(it, decryptDescription);
if (!event)
continue;
if (std::visit([](const auto &e) -> bool { return isYourJoin(e); }, *event)) {
auto time = mtx::accessors::origin_server_ts(*event);
uint64_t ts = time.toMSecsSinceEpoch();
auto description =
DescInfo{QString::fromStdString(mtx::accessors::event_id(*event)),
QString::fromStdString(http::client()->user_id().to_string()),
tr("You joined this room."),
utils::descriptiveTime(time),
ts,
time};
if (description != lastMessage_) {
2022-06-26 02:48:16 +03:00
if (lastMessage_.timestamp == 0) {
cache::client()->updateLastMessageTimestamp(room_id_.toStdString(),
description.timestamp);
}
2021-09-18 01:22:33 +03:00
lastMessage_ = description;
emit lastMessageChanged();
}
return;
}
if (!std::visit([](const auto &e) -> bool { return isMessage(e); }, *event))
continue;
auto description = utils::getMessageDescription(
*event,
QString::fromStdString(http::client()->user_id().to_string()),
cache::displayName(room_id_, QString::fromStdString(mtx::accessors::sender(*event))));
if (description != lastMessage_) {
2022-06-26 02:48:16 +03:00
if (lastMessage_.timestamp == 0) {
cache::client()->updateLastMessageTimestamp(room_id_.toStdString(),
description.timestamp);
}
2021-09-18 01:22:33 +03:00
lastMessage_ = description;
emit lastMessageChanged();
2019-11-09 05:30:17 +03:00
}
2021-09-18 01:22:33 +03:00
return;
}
2019-09-09 22:42:33 +03:00
}
2019-10-03 23:39:56 +03:00
void
TimelineModel::setCurrentIndex(int index)
{
2021-09-18 01:22:33 +03:00
auto oldIndex = idToIndex(currentId);
currentId = indexToId(index);
if (index != oldIndex)
emit currentIndexChanged(index);
2022-05-07 19:53:16 +03:00
if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->isActive() ||
MainWindow::instance()->windowForRoom(roomId()) != QGuiApplication::focusWindow())
2021-09-18 01:22:33 +03:00
return;
2021-12-28 22:09:08 +03:00
if (!currentId.startsWith('m')) {
2021-09-18 01:22:33 +03:00
auto oldReadIndex =
cache::getEventIndex(roomId().toStdString(), currentReadId.toStdString());
auto nextEventIndexAndId =
cache::lastInvisibleEventAfter(roomId().toStdString(), currentId.toStdString());
2021-09-18 01:22:33 +03:00
if (nextEventIndexAndId && (!oldReadIndex || *oldReadIndex < nextEventIndexAndId->first)) {
readEvent(nextEventIndexAndId->second);
currentReadId = QString::fromStdString(nextEventIndexAndId->second);
2019-10-03 23:39:56 +03:00
}
2021-09-18 01:22:33 +03:00
}
2019-10-03 23:39:56 +03:00
}
void
TimelineModel::readEvent(const std::string &id)
{
2022-02-20 15:00:00 +03:00
http::client()->read_event(
room_id_.toStdString(),
id,
[this](mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn(
"failed to read_event ({}, {})", room_id_.toStdString(), currentId.toStdString());
}
},
!UserSettings::instance()->readReceipts());
}
QString
TimelineModel::displayName(const QString &id) const
{
2021-09-18 01:22:33 +03:00
return cache::displayName(room_id_, id).toHtmlEscaped();
}
2019-09-07 23:22:07 +03:00
QString
TimelineModel::avatarUrl(const QString &id) const
2019-09-07 23:22:07 +03:00
{
2021-09-18 01:22:33 +03:00
return cache::avatarUrl(room_id_, id);
2019-09-07 23:22:07 +03:00
}
QString
TimelineModel::formatDateSeparator(QDate date) const
{
2021-09-18 01:22:33 +03:00
auto now = QDateTime::currentDateTime();
2021-09-18 01:22:33 +03:00
QString fmt = QLocale::system().dateFormat(QLocale::LongFormat);
2021-09-18 01:22:33 +03:00
if (now.date().year() == date.year()) {
QRegularExpression rx(QStringLiteral("[^a-zA-Z]*y+[^a-zA-Z]*"));
2021-09-18 01:22:33 +03:00
fmt = fmt.remove(rx);
}
2021-09-18 01:22:33 +03:00
return date.toString(fmt);
}
2019-09-07 03:01:44 +03:00
2019-09-08 16:26:46 +03:00
void
TimelineModel::viewRawMessage(const QString &id)
2019-09-08 16:26:46 +03:00
{
2021-09-18 01:22:33 +03:00
auto e = events.get(id.toStdString(), "", false);
if (!e)
return;
std::string ev = mtx::accessors::serialize_event(*e).dump(4);
emit showRawMessageDialog(QString::fromStdString(ev));
2019-09-08 16:26:46 +03:00
}
2019-09-08 17:50:32 +03:00
2021-04-11 17:31:49 +03:00
void
2021-12-27 22:49:27 +03:00
TimelineModel::forwardMessage(const QString &eventId, QString roomId)
2021-04-11 17:31:49 +03:00
{
2021-09-18 01:22:33 +03:00
auto e = events.get(eventId.toStdString(), "");
if (!e)
return;
2021-04-11 17:31:49 +03:00
emit forwardToRoom(e, std::move(roomId));
2021-04-11 17:31:49 +03:00
}
void
TimelineModel::viewDecryptedRawMessage(const QString &id)
{
2021-09-18 01:22:33 +03:00
auto e = events.get(id.toStdString(), "");
if (!e)
return;
2021-09-18 01:22:33 +03:00
std::string ev = mtx::accessors::serialize_event(*e).dump(4);
emit showRawMessageDialog(QString::fromStdString(ev));
}
void
TimelineModel::openUserProfile(QString userid)
{
UserProfile *userProfile = new UserProfile(room_id_, std::move(userid), manager_, this);
2021-09-18 01:22:33 +03:00
connect(this, &TimelineModel::roomAvatarUrlChanged, userProfile, &UserProfile::updateAvatarUrl);
emit manager_->openProfile(userProfile);
}
2019-09-11 01:54:40 +03:00
void
TimelineModel::replyAction(const QString &id)
2019-09-11 01:54:40 +03:00
{
2021-09-18 01:22:33 +03:00
setReply(id);
2020-04-13 17:22:30 +03:00
}
2021-12-11 08:10:41 +03:00
void
TimelineModel::unpin(const QString &id)
2021-12-11 08:10:41 +03:00
{
auto pinned =
cache::client()->getStateEvent<mtx::events::state::PinnedEvents>(room_id_.toStdString());
mtx::events::state::PinnedEvents content{};
if (pinned)
content = pinned->content;
auto idStr = id.toStdString();
for (auto it = content.pinned.begin(); it != content.pinned.end(); ++it) {
if (*it == idStr) {
content.pinned.erase(it);
break;
}
}
http::client()->send_state_event(
room_id_.toStdString(),
content,
[idStr](const mtx::responses::EventId &, mtx::http::RequestErr err) {
if (err)
nhlog::net()->error("Failed to unpin {}: {}", idStr, *err);
else
nhlog::net()->debug("Unpinned {}", idStr);
});
}
void
TimelineModel::pin(const QString &id)
2021-12-11 08:10:41 +03:00
{
auto pinned =
cache::client()->getStateEvent<mtx::events::state::PinnedEvents>(room_id_.toStdString());
mtx::events::state::PinnedEvents content{};
if (pinned)
content = pinned->content;
auto idStr = id.toStdString();
content.pinned.push_back(idStr);
http::client()->send_state_event(
room_id_.toStdString(),
content,
[idStr](const mtx::responses::EventId &, mtx::http::RequestErr err) {
if (err)
nhlog::net()->error("Failed to pin {}: {}", idStr, *err);
else
nhlog::net()->debug("Pinned {}", idStr);
});
}
2021-02-01 00:41:43 +03:00
void
TimelineModel::editAction(QString id)
{
2021-09-18 01:22:33 +03:00
setEdit(id);
2021-02-01 00:41:43 +03:00
}
2020-04-13 17:22:30 +03:00
RelatedInfo
TimelineModel::relatedInfo(const QString &id)
2020-04-13 17:22:30 +03:00
{
2021-09-18 01:22:33 +03:00
auto event = events.get(id.toStdString(), "");
if (!event)
return {};
2020-04-13 17:22:30 +03:00
2021-09-18 01:22:33 +03:00
return utils::stripReplyFallbacks(*event, id.toStdString(), room_id_);
}
void
TimelineModel::showReadReceipts(QString id)
{
2021-09-18 01:22:33 +03:00
emit openReadReceiptsDialog(new ReadReceiptsProxy{id, roomId(), this});
2019-09-11 01:54:40 +03:00
}
2019-09-18 21:34:30 +03:00
void
TimelineModel::redactAllFromUser(const QString &userid, const QString &reason)
{
auto user = userid.toStdString();
std::vector<QString> toRedact;
for (auto it = events.size() - 1; it >= 0; --it) {
auto event = events.get(it, false);
if (event && mtx::accessors::sender(*event) == user &&
!std::holds_alternative<mtx::events::RoomEvent<mtx::events::msg::Redacted>>(*event)) {
toRedact.push_back(QString::fromStdString(mtx::accessors::event_id(*event)));
}
}
for (const auto &e : toRedact) {
redactEvent(e, reason);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
2019-09-29 13:29:17 +03:00
void
TimelineModel::redactEvent(const QString &id, const QString &reason)
2019-09-29 13:29:17 +03:00
{
2021-11-08 21:32:14 +03:00
if (!id.isEmpty()) {
auto edits = events.edits(id.toStdString());
2021-09-18 01:22:33 +03:00
http::client()->redact_event(
room_id_.toStdString(),
id.toStdString(),
[this, id](const mtx::responses::EventId &, mtx::http::RequestErr err) {
if (err) {
emit redactionFailed(tr("Message redaction failed: %1")
.arg(QString::fromStdString(err->matrix_error.error)));
return;
}
2021-11-08 21:32:14 +03:00
emit dataAtIdChanged(id);
},
reason.toStdString());
2021-11-08 21:32:14 +03:00
// redact all edits to prevent leaks
for (const auto &e : edits) {
const auto &id_ = mtx::accessors::event_id(e);
2021-11-08 21:32:14 +03:00
http::client()->redact_event(
room_id_.toStdString(),
id_,
[this, id, id_](const mtx::responses::EventId &, mtx::http::RequestErr err) {
if (err) {
emit redactionFailed(tr("Message redaction failed: %1")
.arg(QString::fromStdString(err->matrix_error.error)));
return;
}
emit dataAtIdChanged(id);
},
reason.toStdString());
2021-11-08 21:32:14 +03:00
}
}
2019-09-29 13:29:17 +03:00
}
2019-09-18 21:34:30 +03:00
int
TimelineModel::idToIndex(const QString &id) const
2019-09-18 21:34:30 +03:00
{
2021-09-18 01:22:33 +03:00
if (id.isEmpty())
return -1;
2020-07-10 00:15:22 +03:00
2021-09-18 01:22:33 +03:00
auto idx = events.idToIndex(id.toStdString());
if (idx)
return events.size() - *idx - 1;
else
return -1;
2019-09-18 21:34:30 +03:00
}
QString
TimelineModel::indexToId(int index) const
{
2021-09-18 01:22:33 +03:00
auto id = events.indexToId(events.size() - index - 1);
return id ? QString::fromStdString(*id) : QLatin1String("");
2019-09-18 21:34:30 +03:00
}
2019-09-19 22:47:16 +03:00
// Note: this will only be called for our messages
void
TimelineModel::markEventsAsRead(const std::vector<QString> &event_ids)
{
2021-09-18 01:22:33 +03:00
for (const auto &id : event_ids) {
read.insert(id);
int idx = idToIndex(id);
if (idx < 0) {
return;
}
2021-09-18 01:22:33 +03:00
emit dataChanged(index(idx, 0), index(idx, 0));
}
}
2019-09-19 23:44:25 +03:00
2020-08-09 06:05:15 +03:00
template<typename T>
2019-09-19 23:44:25 +03:00
void
TimelineModel::sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::EventType eventType)
2019-09-19 23:44:25 +03:00
{
2021-09-18 01:22:33 +03:00
const auto room_id = room_id_.toStdString();
2021-09-18 01:22:33 +03:00
using namespace mtx::events;
using namespace mtx::identifiers;
2020-07-11 02:19:48 +03:00
nlohmann::json doc = {{"type", mtx::events::to_string(eventType)},
{"content", nlohmann::json(msg.content)},
{"room_id", room_id}};
2020-07-11 02:19:48 +03:00
2021-09-18 01:22:33 +03:00
try {
mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> event;
event.content = olm::encrypt_group_message(room_id, http::client()->device_id(), doc);
event.event_id = msg.event_id;
event.room_id = room_id;
event.sender = http::client()->user_id().to_string();
event.type = mtx::events::EventType::RoomEncrypted;
event.origin_server_ts = QDateTime::currentMSecsSinceEpoch();
2020-05-10 05:39:25 +03:00
2021-09-18 01:22:33 +03:00
emit this->addPendingMessageToStore(event);
2020-08-09 06:05:15 +03:00
2021-09-18 01:22:33 +03:00
// TODO: Let the user know about the errors.
} catch (const lmdb::error &e) {
nhlog::db()->critical("failed to open outbound megolm session ({}): {}", room_id, e.what());
emit ChatPage::instance()->showNotification(
tr("Failed to encrypt event, sending aborted!"));
} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical(
"failed to open outbound megolm session ({}): {}", room_id, e.what());
emit ChatPage::instance()->showNotification(
tr("Failed to encrypt event, sending aborted!"));
}
}
2021-09-18 01:22:33 +03:00
struct SendMessageVisitor
{
explicit SendMessageVisitor(TimelineModel *model)
: model_(model)
2022-05-10 04:53:35 +03:00
{}
2021-09-18 01:22:33 +03:00
template<typename T, mtx::events::EventType Event>
void sendRoomEvent(mtx::events::RoomEvent<T> msg)
{
if (cache::isRoomEncrypted(model_->room_id_.toStdString())) {
auto encInfo = mtx::accessors::file(msg);
if (encInfo)
emit model_->newEncryptedImage(encInfo.value());
encInfo = mtx::accessors::thumbnail_file(msg);
if (encInfo)
emit model_->newEncryptedImage(encInfo.value());
2021-09-18 01:22:33 +03:00
model_->sendEncryptedMessage(msg, Event);
} else {
msg.type = Event;
emit model_->addPendingMessageToStore(msg);
}
}
// Do-nothing operator for all unhandled events
template<typename T>
void operator()(const mtx::events::Event<T> &)
2022-05-10 04:53:35 +03:00
{}
2021-09-18 01:22:33 +03:00
// Operator for m.room.message events that contain a msgtype in their content
template<typename T,
std::enable_if_t<std::is_same<decltype(T::msgtype), std::string>::value, int> = 0>
void operator()(mtx::events::RoomEvent<T> msg)
{
sendRoomEvent<T, mtx::events::EventType::RoomMessage>(msg);
}
// Special operator for reactions, which are a type of m.room.message, but need to be
// handled distinctly for their differences from normal room messages. Specifically,
// reactions need to have the relation outside of ciphertext, or synapse / the homeserver
// cannot handle it correctly. See the MSC for more details:
// https://github.com/matrix-org/matrix-doc/blob/matthew/msc1849/proposals/1849-aggregations.md#end-to-end-encryption
void operator()(mtx::events::RoomEvent<mtx::events::msg::Reaction> msg)
{
msg.type = mtx::events::EventType::Reaction;
emit model_->addPendingMessageToStore(msg);
}
2022-06-27 19:09:31 +03:00
void operator()(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &event)
2021-09-18 01:22:33 +03:00
{
2022-06-27 19:09:31 +03:00
sendRoomEvent<mtx::events::voip::CallInvite, mtx::events::EventType::CallInvite>(event);
2021-09-18 01:22:33 +03:00
}
2022-06-27 19:09:31 +03:00
void operator()(const mtx::events::RoomEvent<mtx::events::voip::CallCandidates> &event)
2021-09-18 01:22:33 +03:00
{
2022-06-27 19:09:31 +03:00
sendRoomEvent<mtx::events::voip::CallCandidates, mtx::events::EventType::CallCandidates>(
2021-09-18 01:22:33 +03:00
event);
}
2022-06-27 19:09:31 +03:00
void operator()(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &event)
2021-09-18 01:22:33 +03:00
{
2022-06-27 19:09:31 +03:00
sendRoomEvent<mtx::events::voip::CallAnswer, mtx::events::EventType::CallAnswer>(event);
2021-09-18 01:22:33 +03:00
}
2022-06-27 19:09:31 +03:00
void operator()(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &event)
2021-09-18 01:22:33 +03:00
{
2022-06-27 19:09:31 +03:00
sendRoomEvent<mtx::events::voip::CallHangUp, mtx::events::EventType::CallHangUp>(event);
2021-09-18 01:22:33 +03:00
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationRequest,
mtx::events::EventType::RoomMessage>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationReady> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationReady,
mtx::events::EventType::KeyVerificationReady>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationStart> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationStart,
mtx::events::EventType::KeyVerificationStart>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationAccept> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationAccept,
mtx::events::EventType::KeyVerificationAccept>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationMac> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationMac,
mtx::events::EventType::KeyVerificationMac>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationKey> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationKey,
mtx::events::EventType::KeyVerificationKey>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationDone> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationDone,
mtx::events::EventType::KeyVerificationDone>(msg);
}
void operator()(const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationCancel> &msg)
{
sendRoomEvent<mtx::events::msg::KeyVerificationCancel,
mtx::events::EventType::KeyVerificationCancel>(msg);
}
void operator()(mtx::events::Sticker msg)
{
msg.type = mtx::events::EventType::Sticker;
if (cache::isRoomEncrypted(model_->room_id_.toStdString())) {
model_->sendEncryptedMessage(msg, mtx::events::EventType::Sticker);
} else
emit model_->addPendingMessageToStore(msg);
}
2020-05-10 05:39:25 +03:00
2021-09-18 01:22:33 +03:00
TimelineModel *model_;
};
void
TimelineModel::addPendingMessage(mtx::events::collections::TimelineEvents event)
{
2021-09-18 01:22:33 +03:00
std::visit(
[](auto &msg) {
// gets overwritten for reactions and stickers in SendMessageVisitor
msg.type = mtx::events::EventType::RoomMessage;
msg.event_id = "m" + http::client()->generate_txn_id();
msg.sender = http::client()->user_id().to_string();
msg.origin_server_ts = QDateTime::currentMSecsSinceEpoch();
},
event);
2020-07-18 18:43:49 +03:00
2021-09-18 01:22:33 +03:00
std::visit(SendMessageVisitor{this}, event);
}
2019-12-03 04:26:41 +03:00
2021-01-24 03:29:30 +03:00
void
TimelineModel::openMedia(const QString &eventId)
2021-01-24 03:29:30 +03:00
{
2021-12-27 22:49:27 +03:00
cacheMedia(eventId, [](const QString &filename) {
QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
});
2021-01-24 03:29:30 +03:00
}
bool
TimelineModel::saveMedia(const QString &eventId) const
2019-12-03 04:26:41 +03:00
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *event = events.get(eventId.toStdString(), "");
if (!event)
return false;
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
QString mxcUrl = QString::fromStdString(mtx::accessors::url(*event));
QString originalFilename = QString::fromStdString(mtx::accessors::filename(*event));
QString mimeType = QString::fromStdString(mtx::accessors::mimetype(*event));
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
auto encryptionInfo = mtx::accessors::file(*event);
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
qml_mtx_events::EventType eventType = toRoomEventType(*event);
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
QString dialogTitle;
if (eventType == qml_mtx_events::EventType::ImageMessage) {
dialogTitle = tr("Save image");
} else if (eventType == qml_mtx_events::EventType::VideoMessage) {
dialogTitle = tr("Save video");
} else if (eventType == qml_mtx_events::EventType::AudioMessage) {
dialogTitle = tr("Save audio");
} else {
dialogTitle = tr("Save file");
}
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
const QString filterString = QMimeDatabase().mimeTypeForName(mimeType).filterString();
const QString downloadsFolder =
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
const QString openLocation = downloadsFolder + "/" + originalFilename;
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
const QString filename =
2022-01-12 21:09:46 +03:00
QFileDialog::getSaveFileName(nullptr, dialogTitle, openLocation, filterString);
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
if (filename.isEmpty())
return false;
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
const auto url = mxcUrl.toStdString();
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
http::client()->download(url,
[filename, url, encryptionInfo](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve image {}: {} {}",
url,
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
2021-09-18 01:22:33 +03:00
try {
auto temp = data;
if (encryptionInfo)
temp = mtx::crypto::to_string(
mtx::crypto::decrypt_file(temp, encryptionInfo.value()));
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return;
file.write(QByteArray(temp.data(), (int)temp.size()));
file.close();
return;
} catch (const std::exception &e) {
nhlog::ui()->warn("Error while saving file to: {}", e.what());
}
});
return true;
2019-12-03 04:26:41 +03:00
}
void
2021-12-27 22:49:27 +03:00
TimelineModel::cacheMedia(const QString &eventId,
const std::function<void(const QString)> &callback)
2019-12-03 04:26:41 +03:00
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *event = events.get(eventId.toStdString(), "");
if (!event)
return;
2019-12-03 04:26:41 +03:00
2021-12-29 00:30:12 +03:00
QString mxcUrl = QString::fromStdString(mtx::accessors::url(*event));
QString mimeType = QString::fromStdString(mtx::accessors::mimetype(*event));
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
auto encryptionInfo = mtx::accessors::file(*event);
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
// If the message is a link to a non mxcUrl, don't download it
if (!mxcUrl.startsWith(QLatin1String("mxc://"))) {
2021-09-18 01:22:33 +03:00
emit mediaCached(mxcUrl, mxcUrl);
return;
}
QString suffix = QMimeDatabase().mimeTypeForName(mimeType).preferredSuffix();
const auto url = mxcUrl.toStdString();
const auto name = QString(mxcUrl).remove(QStringLiteral("mxc://"));
2021-12-29 00:30:12 +03:00
QFileInfo filename(
QStringLiteral("%1/media_cache/%2.%3")
2021-12-29 00:30:12 +03:00
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), name, suffix));
2021-09-18 01:22:33 +03:00
if (QDir::cleanPath(name) != name) {
nhlog::net()->warn("mxcUrl '{}' is not safe, not downloading file", url);
return;
}
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
QDir().mkpath(filename.path());
2019-12-03 04:26:41 +03:00
2021-09-18 01:22:33 +03:00
if (filename.isReadable()) {
2021-03-15 06:33:06 +03:00
#if defined(Q_OS_WIN)
2021-09-18 01:22:33 +03:00
emit mediaCached(mxcUrl, filename.filePath());
2021-03-15 06:33:06 +03:00
#else
2021-09-18 01:22:33 +03:00
emit mediaCached(mxcUrl, "file://" + filename.filePath());
2021-03-15 06:33:06 +03:00
#endif
2021-09-18 01:22:33 +03:00
if (callback) {
callback(filename.filePath());
2019-12-03 04:26:41 +03:00
}
2021-09-18 01:22:33 +03:00
return;
}
http::client()->download(
url,
[this, callback, mxcUrl, filename, url, encryptionInfo](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve image {}: {} {}",
url,
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
try {
auto temp = data;
if (encryptionInfo)
temp =
mtx::crypto::to_string(mtx::crypto::decrypt_file(temp, encryptionInfo.value()));
QFile file(filename.filePath());
if (!file.open(QIODevice::WriteOnly))
return;
file.write(QByteArray(temp.data(), (int)temp.size()));
file.close();
if (callback) {
callback(filename.filePath());
}
} catch (const std::exception &e) {
nhlog::ui()->warn("Error while saving file to: {}", e.what());
}
2019-12-03 04:26:41 +03:00
2021-03-15 06:33:06 +03:00
#if defined(Q_OS_WIN)
2021-09-18 01:22:33 +03:00
emit mediaCached(mxcUrl, filename.filePath());
2021-03-15 06:33:06 +03:00
#else
2021-09-18 01:22:33 +03:00
emit mediaCached(mxcUrl, "file://" + filename.filePath());
2021-03-15 06:33:06 +03:00
#endif
2021-09-18 01:22:33 +03:00
});
2019-12-03 04:26:41 +03:00
}
2020-01-17 03:25:14 +03:00
2021-01-24 03:29:30 +03:00
void
TimelineModel::cacheMedia(const QString &eventId)
2021-01-24 03:29:30 +03:00
{
cacheMedia(eventId, nullptr);
2021-01-24 03:29:30 +03:00
}
2021-04-29 20:09:16 +03:00
void
TimelineModel::showEvent(QString eventId)
{
2021-09-18 01:22:33 +03:00
using namespace std::chrono_literals;
// Direct to eventId
if (eventId[0] == '$') {
int idx = idToIndex(eventId);
if (idx == -1) {
nhlog::ui()->warn("Scrolling to event id {}, failed - no known index",
eventId.toStdString());
return;
2021-04-29 20:09:16 +03:00
}
eventIdToShow = eventId;
emit scrollTargetChanged();
showEventTimer.start(50ms);
return;
2021-09-18 01:22:33 +03:00
}
// to message index
eventId = indexToId(eventId.toInt());
eventIdToShow = eventId;
emit scrollTargetChanged();
showEventTimer.start(50ms);
return;
2021-04-29 20:09:16 +03:00
}
void
TimelineModel::eventShown()
{
2021-09-18 01:22:33 +03:00
eventIdToShow.clear();
emit scrollTargetChanged();
2021-04-29 20:09:16 +03:00
}
QString
TimelineModel::scrollTarget() const
{
2021-09-18 01:22:33 +03:00
return eventIdToShow;
2021-04-29 20:09:16 +03:00
}
void
TimelineModel::scrollTimerEvent()
{
2021-09-18 01:22:33 +03:00
if (eventIdToShow.isEmpty() || showEventTimerCounter > 3) {
showEventTimer.stop();
showEventTimerCounter = 0;
} else {
emit scrollToIndex(idToIndex(eventIdToShow));
showEventTimerCounter++;
}
2021-04-29 20:09:16 +03:00
}
void
TimelineModel::requestKeyForEvent(const QString &id)
{
2021-09-18 01:22:33 +03:00
auto encrypted_event = events.get(id.toStdString(), "", false);
if (encrypted_event) {
if (auto ev = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
encrypted_event))
events.requestSession(*ev, true);
}
}
QString
TimelineModel::getBareRoomLink(const QString &roomId)
2021-04-29 22:46:49 +03:00
{
2022-06-16 02:49:51 +03:00
auto alias =
cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(roomId.toStdString());
2021-09-18 01:22:33 +03:00
QString room;
if (alias) {
2022-06-16 02:49:51 +03:00
room = QString::fromStdString(alias->content.alias);
if (room.isEmpty() && !alias->content.alt_aliases.empty()) {
room = QString::fromStdString(alias->content.alt_aliases.front());
2021-04-29 22:46:49 +03:00
}
2021-09-18 01:22:33 +03:00
}
2021-04-29 22:46:49 +03:00
2021-09-18 01:22:33 +03:00
if (room.isEmpty())
room = roomId;
return QStringLiteral("https://matrix.to/#/%1").arg(QString(QUrl::toPercentEncoding(room)));
}
QString
TimelineModel::getRoomVias(const QString &roomId)
{
QStringList vias;
2021-04-29 22:46:49 +03:00
vias.push_back(QStringLiteral("via=%1").arg(QString(
2021-09-18 01:22:33 +03:00
QUrl::toPercentEncoding(QString::fromStdString(http::client()->user_id().hostname())))));
auto members = cache::getMembers(roomId.toStdString(), 0, 100);
2021-09-18 01:22:33 +03:00
for (const auto &m : members) {
if (vias.size() >= 4)
break;
2021-04-29 22:46:49 +03:00
2021-09-18 01:22:33 +03:00
auto user_id = mtx::identifiers::parse<mtx::identifiers::User>(m.user_id.toStdString());
QString server = QStringLiteral("via=%1").arg(
2021-09-18 01:22:33 +03:00
QString(QUrl::toPercentEncoding(QString::fromStdString(user_id.hostname()))));
2021-04-29 22:46:49 +03:00
2021-09-18 01:22:33 +03:00
if (!vias.contains(server))
vias.push_back(server);
}
2021-04-29 22:46:49 +03:00
return vias.join("&");
}
2021-04-29 22:46:49 +03:00
void
TimelineModel::copyLinkToEvent(const QString &eventId) const
{
auto link = QStringLiteral("%1/%2?%3")
.arg(getBareRoomLink(room_id_),
QString(QUrl::toPercentEncoding(eventId)),
getRoomVias(room_id_));
2021-09-18 01:22:33 +03:00
QGuiApplication::clipboard()->setText(link);
2021-04-29 22:46:49 +03:00
}
2020-01-17 03:25:14 +03:00
QString
TimelineModel::formatTypingUsers(const std::vector<QString> &users, const QColor &bg)
2020-01-17 03:25:14 +03:00
{
2021-09-18 01:22:33 +03:00
QString temp =
tr("%1 and %2 are typing.",
"Multiple users are typing. First argument is a comma separated list of potentially "
"multiple users. Second argument is the last user of that list. (If only one user is "
"typing, %1 is empty. You should still use it in your string though to silence Qt "
"warnings.)",
(int)users.size());
2020-01-17 03:25:14 +03:00
2021-09-18 01:22:33 +03:00
if (users.empty()) {
return {};
2021-09-18 01:22:33 +03:00
}
2020-01-17 03:25:14 +03:00
2021-09-18 01:22:33 +03:00
QStringList uidWithoutLast;
2020-01-17 03:25:14 +03:00
2021-09-18 01:22:33 +03:00
auto formatUser = [this, bg](const QString &user_id) -> QString {
auto uncoloredUsername = utils::replaceEmoji(displayName(user_id));
QString prefix =
QStringLiteral("<font color=\"%1\">").arg(manager_->userColor(user_id, bg).name());
2021-09-18 01:22:33 +03:00
// color only parts that don't have a font already specified
QString coloredUsername;
int index = 0;
do {
auto startIndex = uncoloredUsername.indexOf(QLatin1String("<font"), index);
2021-09-18 01:22:33 +03:00
if (startIndex - index != 0)
coloredUsername +=
2021-12-28 22:09:08 +03:00
prefix + uncoloredUsername.mid(index, startIndex > 0 ? startIndex - index : -1) +
QStringLiteral("</font>");
auto endIndex = uncoloredUsername.indexOf(QLatin1String("</font>"), startIndex);
2021-09-18 01:22:33 +03:00
if (endIndex > 0)
endIndex += sizeof("</font>") - 1;
2021-09-18 01:22:33 +03:00
if (endIndex - startIndex != 0)
2021-12-28 22:09:08 +03:00
coloredUsername +=
QStringView(uncoloredUsername).mid(startIndex, endIndex - startIndex);
2021-09-18 01:22:33 +03:00
index = endIndex;
} while (index > 0 && index < uncoloredUsername.size());
2021-09-18 01:22:33 +03:00
return coloredUsername;
};
2020-01-17 03:25:14 +03:00
uidWithoutLast.reserve(static_cast<int>(users.size()));
2021-09-18 01:22:33 +03:00
for (size_t i = 0; i + 1 < users.size(); i++) {
uidWithoutLast.append(formatUser(users[i]));
}
2020-01-17 03:25:14 +03:00
return temp.arg(uidWithoutLast.join(QStringLiteral(", ")), formatUser(users.back()));
2020-01-17 03:25:14 +03:00
}
2020-01-23 22:34:04 +03:00
QString
TimelineModel::formatJoinRuleEvent(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::JoinRules>>(e);
if (!event)
return {};
2021-09-18 01:22:33 +03:00
QString user = QString::fromStdString(event->sender);
QString name = utils::replaceEmoji(displayName(user));
switch (event->content.join_rule) {
case mtx::events::state::JoinRule::Public:
return tr("%1 opened the room to the public.").arg(name);
case mtx::events::state::JoinRule::Invite:
return tr("%1 made this room require and invitation to join.").arg(name);
case mtx::events::state::JoinRule::Knock:
return tr("%1 allowed to join this room by knocking.").arg(name);
case mtx::events::state::JoinRule::Restricted: {
QStringList rooms;
for (const auto &r : event->content.allow) {
if (r.type == mtx::events::state::JoinAllowanceType::RoomMembership)
rooms.push_back(QString::fromStdString(r.room_id));
}
return tr("%1 allowed members of the following rooms to automatically join this "
"room: %2")
.arg(name, rooms.join(QStringLiteral(", ")));
2021-09-18 01:22:33 +03:00
}
default:
// Currently, knock and private are reserved keywords and not implemented in Matrix.
return {};
2021-09-18 01:22:33 +03:00
}
}
QString
TimelineModel::formatGuestAccessEvent(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::GuestAccess>>(e);
if (!event)
return {};
2021-09-18 01:22:33 +03:00
QString user = QString::fromStdString(event->sender);
QString name = utils::replaceEmoji(displayName(user));
2021-09-18 01:22:33 +03:00
switch (event->content.guest_access) {
case mtx::events::state::AccessState::CanJoin:
return tr("%1 made the room open to guests.").arg(name);
case mtx::events::state::AccessState::Forbidden:
return tr("%1 has closed the room to guest access.").arg(name);
default:
return {};
2021-09-18 01:22:33 +03:00
}
}
QString
TimelineModel::formatHistoryVisibilityEvent(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::HistoryVisibility>>(e);
2021-09-18 01:22:33 +03:00
if (!event)
return {};
2021-09-18 01:22:33 +03:00
QString user = QString::fromStdString(event->sender);
QString name = utils::replaceEmoji(displayName(user));
switch (event->content.history_visibility) {
case mtx::events::state::Visibility::WorldReadable:
return tr("%1 made the room history world readable. Events may be now read by "
"non-joined people.")
.arg(name);
case mtx::events::state::Visibility::Shared:
return tr("%1 set the room history visible to members from this point on.").arg(name);
case mtx::events::state::Visibility::Invited:
return tr("%1 set the room history visible to members since they were invited.").arg(name);
case mtx::events::state::Visibility::Joined:
return tr("%1 set the room history visible to members since they joined the room.")
.arg(name);
default:
return {};
2021-09-18 01:22:33 +03:00
}
}
QString
TimelineModel::formatPowerLevelEvent(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::PowerLevels>>(e);
if (!event)
return QString();
mtx::events::StateEvent<mtx::events::state::PowerLevels> *prevEvent = nullptr;
if (!event->unsigned_data.replaces_state.empty()) {
auto tempPrevEvent = events.get(event->unsigned_data.replaces_state, event->event_id);
if (tempPrevEvent) {
prevEvent =
std::get_if<mtx::events::StateEvent<mtx::events::state::PowerLevels>>(tempPrevEvent);
}
}
QString user = QString::fromStdString(event->sender);
QString sender_name = utils::replaceEmoji(displayName(user));
// Get the rooms levels for redactions and powerlevel changes to determine "Administrator" and
// "Moderator" powerlevels.
auto administrator_power_level = event->content.state_level("m.room.power_levels");
auto moderator_power_level = event->content.redact;
auto default_powerlevel = event->content.users_default;
if (!prevEvent)
return tr("%1 has changed the room's permissions.").arg(sender_name);
auto calc_affected = [&event,
&prevEvent](int64_t newPowerlevelSetting) -> std::pair<QStringList, int> {
QStringList affected{};
auto numberOfAffected = 0;
// We do only compare to people with explicit PL. Usually others are not going to be
// affected either way and this is cheaper to iterate over.
for (auto const &[mxid, currentPowerlevel] : event->content.users) {
if (currentPowerlevel == newPowerlevelSetting &&
prevEvent->content.user_level(mxid) < newPowerlevelSetting) {
numberOfAffected++;
if (numberOfAffected <= 2) {
affected.push_back(QString::fromStdString(mxid));
}
}
}
return {affected, numberOfAffected};
};
QStringList resultingMessage{};
// These affect only a few people. Therefor we can print who is affected.
if (event->content.kick != prevEvent->content.kick) {
auto default_message = tr("%1 has changed the room's kick powerlevel from %2 to %3.")
.arg(sender_name)
.arg(prevEvent->content.kick)
.arg(event->content.kick);
// We only calculate affected users if we change to a level above the default users PL
// to not accidentally have a DoS vector
if (event->content.kick > default_powerlevel) {
auto [affected, number_of_affected] = calc_affected(event->content.kick);
if (number_of_affected != 0) {
auto true_affected_rest = number_of_affected - affected.size();
if (number_of_affected > 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%n member(s) can now kick room members.", nullptr, true_affected_rest));
} else if (number_of_affected == 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%1 can now kick room members.")
.arg(utils::replaceEmoji(displayName(affected.at(0)))));
}
} else {
resultingMessage.append(default_message);
}
} else {
resultingMessage.append(default_message);
}
}
if (event->content.redact != prevEvent->content.redact) {
auto default_message = tr("%1 has changed the room's redact powerlevel from %2 to %3.")
.arg(sender_name)
.arg(prevEvent->content.redact)
.arg(event->content.redact);
// We only calculate affected users if we change to a level above the default users PL
// to not accidentally have a DoS vector
if (event->content.redact > default_powerlevel) {
auto [affected, number_of_affected] = calc_affected(event->content.redact);
if (number_of_affected != 0) {
auto true_affected_rest = number_of_affected - affected.size();
if (number_of_affected > 1) {
2022-04-10 23:48:16 +03:00
resultingMessage.append(default_message + QStringLiteral(" ") +
tr("%n member(s) can now redact room messages.",
nullptr,
true_affected_rest));
} else if (number_of_affected == 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%1 can now redact room messages.")
.arg(utils::replaceEmoji(displayName(affected.at(0)))));
}
} else {
resultingMessage.append(default_message);
}
} else {
resultingMessage.append(default_message);
}
}
if (event->content.ban != prevEvent->content.ban) {
auto default_message = tr("%1 has changed the room's ban powerlevel from %2 to %3.")
.arg(sender_name)
.arg(prevEvent->content.ban)
.arg(event->content.ban);
// We only calculate affected users if we change to a level above the default users PL
// to not accidentally have a DoS vector
if (event->content.ban > default_powerlevel) {
auto [affected, number_of_affected] = calc_affected(event->content.ban);
if (number_of_affected != 0) {
auto true_affected_rest = number_of_affected - affected.size();
if (number_of_affected > 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%n member(s) can now ban room members.", nullptr, true_affected_rest));
} else if (number_of_affected == 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%1 can now ban room members.")
.arg(utils::replaceEmoji(displayName(affected.at(0)))));
}
} else {
resultingMessage.append(default_message);
}
} else {
resultingMessage.append(default_message);
}
}
if (event->content.state_default != prevEvent->content.state_default) {
auto default_message =
tr("%1 has changed the room's state_default powerlevel from %2 to %3.")
.arg(sender_name)
.arg(prevEvent->content.state_default)
.arg(event->content.state_default);
// We only calculate affected users if we change to a level above the default users PL
// to not accidentally have a DoS vector
if (event->content.state_default > default_powerlevel) {
auto [affected, number_of_affected] = calc_affected(event->content.kick);
if (number_of_affected != 0) {
auto true_affected_rest = number_of_affected - affected.size();
if (number_of_affected > 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%n member(s) can now send state events.", nullptr, true_affected_rest));
} else if (number_of_affected == 1) {
resultingMessage.append(
default_message + QStringLiteral(" ") +
tr("%1 can now send state events.")
.arg(utils::replaceEmoji(displayName(affected.at(0)))));
}
} else {
resultingMessage.append(default_message);
}
} else {
resultingMessage.append(default_message);
}
}
// These affect potentially the whole room. We there for do not calculate who gets affected
// by this to prevent huge lists of people.
if (event->content.invite != prevEvent->content.invite) {
resultingMessage.append(tr("%1 has changed the room's invite powerlevel from %2 to %3.")
.arg(sender_name,
QString::number(prevEvent->content.invite),
QString::number(event->content.invite)));
}
if (event->content.events_default != prevEvent->content.events_default) {
if ((event->content.events_default > default_powerlevel) &&
prevEvent->content.events_default <= default_powerlevel) {
resultingMessage.append(
tr("%1 has changed the room's events_default powerlevel from %2 to %3. New "
"users can now not send any events.")
.arg(sender_name,
QString::number(prevEvent->content.events_default),
QString::number(event->content.events_default)));
} else if ((event->content.events_default < prevEvent->content.events_default) &&
(event->content.events_default < default_powerlevel) &&
(prevEvent->content.events_default > default_powerlevel)) {
resultingMessage.append(
tr("%1 has changed the room's events_default powerlevel from %2 to %3. New "
"users can now send events that are not otherwise restricted.")
.arg(sender_name,
QString::number(prevEvent->content.events_default),
QString::number(event->content.events_default)));
} else {
resultingMessage.append(
tr("%1 has changed the room's events_default powerlevel from %2 to %3.")
.arg(sender_name,
QString::number(prevEvent->content.events_default),
QString::number(event->content.events_default)));
}
}
// Compare if a Powerlevel of a user changed
for (auto const &[mxid, powerlevel] : event->content.users) {
auto nameOfChangedUser = utils::replaceEmoji(displayName(QString::fromStdString(mxid)));
if (prevEvent->content.user_level(mxid) != powerlevel) {
if (powerlevel >= administrator_power_level) {
resultingMessage.append(tr("%1 has made %2 an administrator of this room.")
.arg(sender_name, nameOfChangedUser));
} else if (powerlevel >= moderator_power_level &&
powerlevel > prevEvent->content.user_level(mxid)) {
resultingMessage.append(tr("%1 has made %2 a moderator of this room.")
.arg(sender_name, nameOfChangedUser));
} else if (powerlevel >= moderator_power_level &&
powerlevel < prevEvent->content.user_level(mxid)) {
resultingMessage.append(tr("%1 has downgraded %2 to moderator of this room.")
.arg(sender_name, nameOfChangedUser));
} else {
resultingMessage.append(tr("%1 has changed the powerlevel of %2 from %3 to %4.")
.arg(sender_name,
nameOfChangedUser,
QString::number(prevEvent->content.user_level(mxid)),
QString::number(powerlevel)));
}
}
}
// Handle added/removed/changed event type
for (auto const &[event_type, powerlevel] : event->content.events) {
auto prev_not_present =
prevEvent->content.events.find(event_type) == prevEvent->content.events.end();
if (prev_not_present || prevEvent->content.events.at(event_type) != powerlevel) {
if (powerlevel >= administrator_power_level) {
resultingMessage.append(tr("%1 allowed only administrators to send \"%2\".")
.arg(sender_name, QString::fromStdString(event_type)));
} else if (powerlevel >= moderator_power_level) {
resultingMessage.append(tr("%1 allowed only moderators to send \"%2\".")
.arg(sender_name, QString::fromStdString(event_type)));
} else if (powerlevel == default_powerlevel) {
resultingMessage.append(tr("%1 allowed everyone to send \"%2\".")
.arg(sender_name, QString::fromStdString(event_type)));
} else if (prev_not_present) {
resultingMessage.append(
tr("%1 has changed the powerlevel of event type \"%2\" from the default to %3.")
.arg(sender_name,
QString::fromStdString(event_type),
QString::number(powerlevel)));
} else {
resultingMessage.append(
tr("%1 has changed the powerlevel of event type \"%2\" from %3 to %4.")
.arg(sender_name,
QString::fromStdString(event_type),
QString::number(prevEvent->content.events.at(event_type)),
QString::number(powerlevel)));
}
}
}
if (!resultingMessage.isEmpty()) {
return resultingMessage.join("<br/>");
} else {
return tr("%1 has changed the room's permissions.").arg(sender_name);
}
}
QString
TimelineModel::formatImagePackEvent(const QString &id)
{
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
auto event = std::get_if<mtx::events::StateEvent<mtx::events::msc2545::ImagePack>>(e);
if (!event)
return {};
mtx::events::StateEvent<mtx::events::msc2545::ImagePack> *prevEvent = nullptr;
if (!event->unsigned_data.replaces_state.empty()) {
auto tempPrevEvent = events.get(event->unsigned_data.replaces_state, event->event_id);
if (tempPrevEvent) {
prevEvent =
std::get_if<mtx::events::StateEvent<mtx::events::msc2545::ImagePack>>(tempPrevEvent);
}
}
const auto &newImages = event->content.images;
const auto oldImages = prevEvent ? prevEvent->content.images : decltype(newImages){};
auto ascent = QFontMetrics(UserSettings::instance()->font()).ascent();
auto calcChange = [ascent](const std::map<std::string, mtx::events::msc2545::PackImage> &newI,
const std::map<std::string, mtx::events::msc2545::PackImage> &oldI) {
QStringList added;
for (const auto &[shortcode, img] : newI) {
if (!oldI.count(shortcode))
added.push_back(QStringLiteral("<img data-mx-emoticon height=%1 src=\"%2\"> (~%3)")
.arg(ascent)
.arg(QString::fromStdString(img.url)
.replace("mxc://", "image://mxcImage/")
.toHtmlEscaped(),
QString::fromStdString(shortcode)));
}
return added;
};
auto added = calcChange(newImages, oldImages);
auto removed = calcChange(oldImages, newImages);
auto sender = utils::replaceEmoji(displayName(QString::fromStdString(event->sender)));
const auto packId = [&event]() -> QString {
if (!event->content.pack->display_name.empty()) {
return event->content.pack->display_name.c_str();
} else if (!event->state_key.empty()) {
return event->state_key.c_str();
}
return tr("(empty)");
}();
QString msg;
if (!removed.isEmpty()) {
msg = tr("%1 removed the following images from the pack %2:<br>%3")
.arg(sender, packId, removed.join(", "));
}
if (!added.isEmpty()) {
if (!msg.isEmpty())
msg += "<br>";
msg += tr("%1 added the following images to the pack %2:<br>%3")
.arg(sender, packId, added.join(", "));
}
if (msg.isEmpty())
return tr("%1 changed the sticker and emotes in this room.").arg(sender);
else
return msg;
}
2022-07-09 16:36:15 +03:00
QString
TimelineModel::formatPolicyRule(const QString &id)
{
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
auto qsHtml = [](const std::string &s) { return QString::fromStdString(s).toHtmlEscaped(); };
constexpr std::string_view unstable_ban = "org.matrix.mjolnir.ban";
if (auto userRule =
std::get_if<mtx::events::StateEvent<mtx::events::state::policy_rule::UserRule>>(e)) {
auto sender = utils::replaceEmoji(displayName(QString::fromStdString(userRule->sender)));
if (userRule->content.entity.empty() ||
(userRule->content.recommendation !=
mtx::events::state::policy_rule::recommendation::ban &&
userRule->content.recommendation != unstable_ban)) {
return tr("%1 disabled the rule to ban users matching %2.")
.arg(sender, qsHtml(userRule->content.entity));
} else {
return tr("%1 added a rule to ban users matching %2 for '%3'.")
.arg(sender, qsHtml(userRule->content.entity), qsHtml(userRule->content.reason));
}
} else if (auto roomRule =
std::get_if<mtx::events::StateEvent<mtx::events::state::policy_rule::RoomRule>>(
e)) {
auto sender = utils::replaceEmoji(displayName(QString::fromStdString(roomRule->sender)));
if (roomRule->content.entity.empty() ||
(roomRule->content.recommendation !=
mtx::events::state::policy_rule::recommendation::ban &&
roomRule->content.recommendation != unstable_ban)) {
return tr("%1 disabled the rule to ban rooms matching %2.")
.arg(sender, qsHtml(roomRule->content.entity));
} else {
return tr("%1 added a rule to ban rooms matching %2 for '%3'.")
.arg(sender, qsHtml(roomRule->content.entity), qsHtml(roomRule->content.reason));
}
} else if (auto serverRule =
std::get_if<mtx::events::StateEvent<mtx::events::state::policy_rule::ServerRule>>(
e)) {
auto sender = utils::replaceEmoji(displayName(QString::fromStdString(serverRule->sender)));
if (serverRule->content.entity.empty() ||
(serverRule->content.recommendation !=
mtx::events::state::policy_rule::recommendation::ban &&
serverRule->content.recommendation != unstable_ban)) {
return tr("%1 disabled the rule to ban servers matching %2.")
.arg(sender, qsHtml(serverRule->content.entity));
} else {
return tr("%1 added a rule to ban servers matching %2 for '%3'.")
.arg(sender, qsHtml(serverRule->content.entity), qsHtml(serverRule->content.reason));
}
}
return {};
}
2021-11-29 03:59:57 +03:00
QVariantMap
TimelineModel::formatRedactedEvent(const QString &id)
2021-11-29 03:59:57 +03:00
{
QVariantMap pair{{"first", ""}, {"second", ""}};
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return pair;
auto event = std::get_if<mtx::events::RoomEvent<mtx::events::msg::Redacted>>(e);
if (!event)
return pair;
QString dateTime = QDateTime::fromMSecsSinceEpoch(event->origin_server_ts).toString();
QString reason = QLatin1String("");
2021-11-29 03:59:57 +03:00
auto because = event->unsigned_data.redacted_because;
// User info about who actually sent the redacted event.
QString redactedUser;
QString redactedName;
2021-11-29 03:59:57 +03:00
if (because.has_value()) {
redactedUser = QString::fromStdString(because->sender).toHtmlEscaped();
redactedName = utils::replaceEmoji(displayName(redactedUser));
reason = QString::fromStdString(because->content.reason).toHtmlEscaped();
2021-11-29 03:59:57 +03:00
}
if (reason.isEmpty()) {
pair[QStringLiteral("first")] = tr("Removed by %1").arg(redactedName);
pair[QStringLiteral("second")] =
2021-11-29 03:59:57 +03:00
tr("%1 (%2) removed this message at %3").arg(redactedName, redactedUser, dateTime);
} else {
pair[QStringLiteral("first")] = tr("Removed by %1 because: %2").arg(redactedName, reason);
pair[QStringLiteral("second")] = tr("%1 (%2) removed this message at %3\nReason: %4")
.arg(redactedName, redactedUser, dateTime, reason);
2021-11-29 03:59:57 +03:00
}
return pair;
}
void
TimelineModel::acceptKnock(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return;
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::Member>>(e);
if (!event)
return;
2021-09-18 01:22:33 +03:00
if (!permissions_.canInvite())
return;
2021-09-18 01:22:33 +03:00
if (cache::isRoomMember(event->state_key, room_id_.toStdString()))
return;
2021-09-18 01:22:33 +03:00
using namespace mtx::events::state;
if (event->content.membership != Membership::Knock)
return;
2022-05-07 19:53:16 +03:00
ChatPage::instance()->inviteUser(
room_id_, QString::fromStdString(event->state_key), QLatin1String(""));
}
bool
TimelineModel::showAcceptKnockButton(const QString &id)
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return false;
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::Member>>(e);
if (!event)
return false;
2021-09-18 01:22:33 +03:00
if (!permissions_.canInvite())
return false;
2021-09-18 01:22:33 +03:00
if (cache::isRoomMember(event->state_key, room_id_.toStdString()))
return false;
2021-09-18 01:22:33 +03:00
using namespace mtx::events::state;
return event->content.membership == Membership::Knock;
}
2020-01-23 22:34:04 +03:00
QString
TimelineModel::formatMemberEvent(const QString &id)
2020-01-23 22:34:04 +03:00
{
2021-09-18 01:22:33 +03:00
mtx::events::collections::TimelineEvents *e = events.get(id.toStdString(), "");
if (!e)
return {};
2021-09-18 01:22:33 +03:00
auto event = std::get_if<mtx::events::StateEvent<mtx::events::state::Member>>(e);
if (!event)
return {};
2021-09-18 01:22:33 +03:00
mtx::events::StateEvent<mtx::events::state::Member> *prevEvent = nullptr;
if (!event->unsigned_data.replaces_state.empty()) {
auto tempPrevEvent = events.get(event->unsigned_data.replaces_state, event->event_id);
if (tempPrevEvent) {
prevEvent =
std::get_if<mtx::events::StateEvent<mtx::events::state::Member>>(tempPrevEvent);
}
}
QString user = QString::fromStdString(event->state_key);
QString name = utils::replaceEmoji(displayName(user));
QString rendered;
QString sender = QString::fromStdString(event->sender);
QString senderName = utils::replaceEmoji(displayName(sender));
2021-09-18 01:22:33 +03:00
// see table https://matrix.org/docs/spec/client_server/latest#m-room-member
using namespace mtx::events::state;
switch (event->content.membership) {
case Membership::Invite:
rendered = tr("%1 invited %2.").arg(senderName, name);
2021-09-18 01:22:33 +03:00
break;
case Membership::Join:
if (prevEvent && prevEvent->content.membership == Membership::Join) {
QString oldName = utils::replaceEmoji(
QString::fromStdString(prevEvent->content.display_name).toHtmlEscaped());
bool displayNameChanged =
prevEvent->content.display_name != event->content.display_name;
bool avatarChanged = prevEvent->content.avatar_url != event->content.avatar_url;
if (displayNameChanged && avatarChanged)
rendered = tr("%1 has changed their avatar and changed their "
"display name to %2.")
.arg(oldName, name);
else if (displayNameChanged)
rendered = tr("%1 has changed their display name to %2.").arg(oldName, name);
else if (avatarChanged)
rendered = tr("%1 changed their avatar.").arg(name);
else
rendered = tr("%1 changed some profile info.").arg(name);
// the case of nothing changed but join follows join shouldn't happen, so
// just show it as join
} else {
if (event->content.join_authorised_via_users_server.empty())
rendered = tr("%1 joined.").arg(name);
else
rendered =
tr("%1 joined via authorisation from %2's server.")
2021-12-29 00:30:12 +03:00
.arg(name,
QString::fromStdString(event->content.join_authorised_via_users_server));
2021-09-18 01:22:33 +03:00
}
break;
case Membership::Leave:
2022-03-28 22:27:42 +03:00
if (!prevEvent || prevEvent->content.membership == Membership::Join) {
2021-09-18 01:22:33 +03:00
if (event->state_key == event->sender)
rendered = tr("%1 left the room.").arg(name);
else
rendered = tr("%2 kicked %1.").arg(name, senderName);
2022-03-28 22:27:42 +03:00
} else if (prevEvent->content.membership == Membership::Invite) {
if (event->state_key == event->sender)
rendered = tr("%1 rejected their invite.").arg(name);
else
rendered = tr("%2 revoked the invite to %1.").arg(name, senderName);
2021-09-18 01:22:33 +03:00
} else if (prevEvent->content.membership == Membership::Ban) {
rendered = tr("%2 unbanned %1.").arg(name, senderName);
2021-09-18 01:22:33 +03:00
} else if (prevEvent->content.membership == Membership::Knock) {
if (event->state_key == event->sender)
rendered = tr("%1 redacted their knock.").arg(name);
else
rendered = tr("%2 rejected the knock from %1.").arg(name, senderName);
2021-09-18 01:22:33 +03:00
} else
return tr("%1 left after having already left!",
"This is a leave event after the user already left and shouldn't "
"happen apart from state resets")
.arg(name);
break;
2020-01-23 22:34:04 +03:00
2021-09-18 01:22:33 +03:00
case Membership::Ban:
rendered = tr("%1 banned %2").arg(senderName, name);
2021-09-18 01:22:33 +03:00
break;
case Membership::Knock:
rendered = tr("%1 knocked.").arg(name);
break;
}
2021-09-18 01:22:33 +03:00
if (event->content.reason != "") {
rendered += " " + tr("Reason: %1").arg(QString::fromStdString(event->content.reason));
}
2021-09-18 01:22:33 +03:00
return rendered;
2020-01-23 22:34:04 +03:00
}
2021-02-01 00:41:43 +03:00
void
TimelineModel::setEdit(const QString &newEdit)
2021-02-01 00:41:43 +03:00
{
2021-09-18 01:22:33 +03:00
if (newEdit.isEmpty()) {
resetEdit();
return;
}
if (edit_.isEmpty()) {
this->textBeforeEdit = input()->text();
this->replyBeforeEdit = reply_;
nhlog::ui()->debug("Stored: {}", textBeforeEdit.toStdString());
}
if (edit_ != newEdit) {
auto ev = events.get(newEdit.toStdString(), "");
if (ev && mtx::accessors::sender(*ev) == http::client()->user_id().to_string()) {
auto e = *ev;
setReply(QString::fromStdString(mtx::accessors::relations(e).reply_to().value_or("")));
auto msgType = mtx::accessors::msg_type(e);
if (msgType == mtx::events::MessageType::Text ||
msgType == mtx::events::MessageType::Notice ||
msgType == mtx::events::MessageType::Emote) {
auto relInfo = relatedInfo(newEdit);
auto editText = relInfo.quoted_body;
if (!relInfo.quoted_formatted_body.isEmpty()) {
auto matches =
conf::strings::matrixToLink.globalMatch(relInfo.quoted_formatted_body);
std::map<QString, QString> reverseNameMapping;
while (matches.hasNext()) {
auto m = matches.next();
reverseNameMapping[m.captured(2)] = m.captured(1);
}
for (const auto &[user, link] : reverseNameMapping) {
// TODO(Nico): html unescape the user name
editText.replace(user, QStringLiteral("[%1](%2)").arg(user, link));
}
}
2021-02-25 01:51:05 +03:00
2021-09-18 01:22:33 +03:00
if (msgType == mtx::events::MessageType::Emote)
input()->setText("/me " + editText);
else
input()->setText(editText);
} else {
input()->setText(QLatin1String(""));
2021-09-18 01:22:33 +03:00
}
2021-02-25 01:51:05 +03:00
2021-09-18 01:22:33 +03:00
edit_ = newEdit;
} else {
resetReply();
input()->setText(QLatin1String(""));
edit_ = QLatin1String("");
2021-02-01 00:41:43 +03:00
}
2021-09-18 01:22:33 +03:00
emit editChanged(edit_);
}
2021-02-01 00:41:43 +03:00
}
void
TimelineModel::resetEdit()
{
2021-09-18 01:22:33 +03:00
if (!edit_.isEmpty()) {
edit_ = QLatin1String("");
2021-09-18 01:22:33 +03:00
emit editChanged(edit_);
nhlog::ui()->debug("Restoring: {}", textBeforeEdit.toStdString());
input()->setText(textBeforeEdit);
textBeforeEdit.clear();
if (replyBeforeEdit.isEmpty())
resetReply();
else
setReply(replyBeforeEdit);
replyBeforeEdit.clear();
}
}
2022-02-05 10:40:56 +03:00
void
TimelineModel::resetState()
{
http::client()->get_state(
room_id_.toStdString(),
[this](const mtx::responses::StateEvents &events_, mtx::http::RequestErr e) {
if (e) {
nhlog::net()->error("Failed to retrieve current room state: {}", *e);
2022-02-05 10:40:56 +03:00
return;
}
emit newState(events_);
});
}
QString
TimelineModel::roomName() const
{
2021-09-18 01:22:33 +03:00
auto info = cache::getRoomInfo({room_id_.toStdString()});
2021-09-18 01:22:33 +03:00
if (!info.count(room_id_))
return {};
2021-09-18 01:22:33 +03:00
else
return utils::replaceEmoji(QString::fromStdString(info[room_id_].name).toHtmlEscaped());
}
2021-05-22 11:16:42 +03:00
QString
TimelineModel::plainRoomName() const
{
2021-09-18 01:22:33 +03:00
auto info = cache::getRoomInfo({room_id_.toStdString()});
2021-05-22 11:16:42 +03:00
2021-09-18 01:22:33 +03:00
if (!info.count(room_id_))
return {};
2021-09-18 01:22:33 +03:00
else
return QString::fromStdString(info[room_id_].name);
2021-05-22 11:16:42 +03:00
}
QString
TimelineModel::roomAvatarUrl() const
{
2021-09-18 01:22:33 +03:00
auto info = cache::getRoomInfo({room_id_.toStdString()});
2021-09-18 01:22:33 +03:00
if (!info.count(room_id_))
return {};
2021-09-18 01:22:33 +03:00
else
return QString::fromStdString(info[room_id_].avatar_url);
}
QString
TimelineModel::roomTopic() const
{
2021-09-18 01:22:33 +03:00
auto info = cache::getRoomInfo({room_id_.toStdString()});
2021-09-18 01:22:33 +03:00
if (!info.count(room_id_))
return {};
2021-09-18 01:22:33 +03:00
else
return utils::replaceEmoji(
utils::linkifyMessage(QString::fromStdString(info[room_id_].topic).toHtmlEscaped()));
}
2021-06-18 17:22:06 +03:00
2021-12-11 08:10:41 +03:00
QStringList
TimelineModel::pinnedMessages() const
{
auto pinned =
cache::client()->getStateEvent<mtx::events::state::PinnedEvents>(room_id_.toStdString());
if (!pinned || pinned->content.pinned.empty())
return {};
QStringList list;
list.reserve(pinned->content.pinned.size());
for (const auto &p : pinned->content.pinned)
list.push_back(QString::fromStdString(p));
return list;
}
2022-02-04 20:47:17 +03:00
QStringList
TimelineModel::widgetLinks() const
{
auto evs =
cache::client()->getStateEventsWithType<mtx::events::state::Widget>(room_id_.toStdString());
auto evs2 = cache::client()->getStateEventsWithType<mtx::events::state::Widget>(
room_id_.toStdString(), mtx::events::EventType::Widget);
evs.insert(
evs.end(), std::make_move_iterator(evs2.begin()), std::make_move_iterator(evs2.end()));
if (evs.empty())
return {};
QStringList list;
2022-02-05 10:40:56 +03:00
auto user = utils::localUser();
auto av = QUrl::toPercentEncoding(
QString::fromStdString(http::client()->mxc_to_download_url(avatarUrl(user).toStdString())));
2022-02-04 20:47:17 +03:00
auto disp = QUrl::toPercentEncoding(displayName(user));
auto theme = UserSettings::instance()->theme();
if (theme == QStringLiteral("system"))
theme.clear();
user = QUrl::toPercentEncoding(user);
list.reserve(evs.size());
for (const auto &p : evs) {
auto url = QString::fromStdString(p.content.url);
for (const auto &[k, v] : p.content.data)
url.replace("$" + QString::fromStdString(k),
QUrl::toPercentEncoding(QString::fromStdString(v)));
url.replace("$matrix_user_id", user);
url.replace("$matrix_room_id", QUrl::toPercentEncoding(room_id_));
url.replace("$matrix_display_name", disp);
url.replace("$matrix_avatar_url", av);
url.replace("$matrix_widget_id",
QUrl::toPercentEncoding(QString::fromStdString(p.content.id)));
// url.replace("$matrix_client_theme", theme);
url.replace("$org.matrix.msc2873.client_theme", theme);
url.replace("$org.matrix.msc2873.client_id", "im.nheko");
// compat with some widgets, i.e. FOSDEM
url.replace("$theme", theme);
2022-02-05 11:27:16 +03:00
url = QUrl::toPercentEncoding(url, "/:@?#&=%");
2022-02-04 20:47:17 +03:00
list.push_back(
QLatin1String("<a href='%1'>%2</a>")
.arg(url,
QString::fromStdString(p.content.name.empty() ? p.state_key : p.content.name)
.toHtmlEscaped()));
}
return list;
}
crypto::Trust
TimelineModel::trustlevel() const
{
2021-09-18 01:22:33 +03:00
if (!isEncrypted_)
return crypto::Trust::Unverified;
2021-09-18 01:22:33 +03:00
return cache::client()->roomVerificationStatus(room_id_.toStdString());
}
2021-06-18 17:22:06 +03:00
int
TimelineModel::roomMemberCount() const
{
2021-09-18 01:22:33 +03:00
return (int)cache::client()->memberCount(room_id_.toStdString());
2021-06-18 17:22:06 +03:00
}
2021-08-14 03:05:26 +03:00
QString
2021-09-05 03:53:33 +03:00
TimelineModel::directChatOtherUserId() const
2021-08-14 03:05:26 +03:00
{
2021-09-18 01:22:33 +03:00
if (roomMemberCount() < 3) {
QString id;
for (const auto &member : cache::getMembers(room_id_.toStdString()))
2021-09-18 01:22:33 +03:00
if (member.user_id != UserSettings::instance()->userId())
id = member.user_id;
return id;
} else
return {};
2021-08-14 03:05:26 +03:00
}