2019-08-31 00:20:53 +03:00
|
|
|
#include "TimelineModel.h"
|
|
|
|
|
2019-09-09 22:42:33 +03:00
|
|
|
#include <algorithm>
|
2019-09-03 00:28:05 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2019-09-01 23:58:26 +03:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
2019-09-11 01:54:40 +03:00
|
|
|
#include "ChatPage.h"
|
2019-08-31 23:43:31 +03:00
|
|
|
#include "Logging.h"
|
2019-09-19 00:37:30 +03:00
|
|
|
#include "MainWindow.h"
|
2019-09-08 17:50:32 +03:00
|
|
|
#include "Olm.h"
|
2019-08-31 00:20:53 +03:00
|
|
|
#include "Utils.h"
|
2019-09-08 16:26:46 +03:00
|
|
|
#include "dialogs/RawMessage.h"
|
2019-08-31 00:20:53 +03:00
|
|
|
|
2019-08-31 23:43:31 +03:00
|
|
|
namespace {
|
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
eventId(const T &event)
|
|
|
|
{
|
|
|
|
return QString::fromStdString(event.event_id);
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
roomId(const T &event)
|
|
|
|
{
|
|
|
|
return QString::fromStdString(event.room_id);
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
senderId(const T &event)
|
|
|
|
{
|
|
|
|
return QString::fromStdString(event.sender);
|
|
|
|
}
|
2019-09-01 15:17:33 +03:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
QDateTime
|
|
|
|
eventTimestamp(const T &event)
|
|
|
|
{
|
|
|
|
return QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
|
|
|
|
}
|
2019-09-03 00:28:05 +03:00
|
|
|
|
2019-09-11 01:54:40 +03:00
|
|
|
template<class T>
|
|
|
|
std::string
|
|
|
|
eventMsgType(const mtx::events::Event<T> &)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventMsgType(const mtx::events::RoomEvent<T> &e) -> decltype(e.content.msgtype)
|
|
|
|
{
|
|
|
|
return e.content.msgtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
eventBody(const mtx::events::Event<T> &)
|
|
|
|
{
|
|
|
|
return QString("");
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventBody(const mtx::events::RoomEvent<T> &e)
|
|
|
|
-> std::enable_if_t<std::is_same<decltype(e.content.body), std::string>::value, QString>
|
|
|
|
{
|
|
|
|
return QString::fromStdString(e.content.body);
|
|
|
|
}
|
|
|
|
|
2019-09-03 00:28:05 +03:00
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
eventFormattedBody(const mtx::events::Event<T> &)
|
|
|
|
{
|
|
|
|
return QString("");
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventFormattedBody(const mtx::events::RoomEvent<T> &e)
|
|
|
|
-> std::enable_if_t<std::is_same<decltype(e.content.formatted_body), std::string>::value, QString>
|
|
|
|
{
|
|
|
|
auto temp = e.content.formatted_body;
|
|
|
|
if (!temp.empty()) {
|
|
|
|
auto pos = temp.find("<mx-reply>");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
temp.erase(pos, std::string("<mx-reply>").size());
|
|
|
|
pos = temp.find("</mx-reply>");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
temp.erase(pos, std::string("</mx-reply>").size());
|
|
|
|
return QString::fromStdString(temp);
|
|
|
|
} else
|
|
|
|
return QString::fromStdString(e.content.body);
|
|
|
|
}
|
|
|
|
|
2019-09-08 13:44:46 +03:00
|
|
|
template<class T>
|
|
|
|
QString
|
|
|
|
eventUrl(const T &)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventUrl(const mtx::events::RoomEvent<T> &e)
|
|
|
|
-> std::enable_if_t<std::is_same<decltype(e.content.url), std::string>::value, QString>
|
|
|
|
{
|
|
|
|
return QString::fromStdString(e.content.url);
|
|
|
|
}
|
|
|
|
|
2019-09-03 00:28:05 +03:00
|
|
|
template<class T>
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<T> &e)
|
|
|
|
{
|
|
|
|
using mtx::events::EventType;
|
|
|
|
switch (e.type) {
|
|
|
|
case EventType::RoomKeyRequest:
|
|
|
|
return qml_mtx_events::EventType::KeyRequest;
|
|
|
|
case EventType::RoomAliases:
|
|
|
|
return qml_mtx_events::EventType::Aliases;
|
|
|
|
case EventType::RoomAvatar:
|
|
|
|
return qml_mtx_events::EventType::Avatar;
|
|
|
|
case EventType::RoomCanonicalAlias:
|
|
|
|
return qml_mtx_events::EventType::CanonicalAlias;
|
|
|
|
case EventType::RoomCreate:
|
|
|
|
return qml_mtx_events::EventType::Create;
|
|
|
|
case EventType::RoomEncrypted:
|
|
|
|
return qml_mtx_events::EventType::Encrypted;
|
|
|
|
case EventType::RoomEncryption:
|
|
|
|
return qml_mtx_events::EventType::Encryption;
|
|
|
|
case EventType::RoomGuestAccess:
|
|
|
|
return qml_mtx_events::EventType::GuestAccess;
|
|
|
|
case EventType::RoomHistoryVisibility:
|
|
|
|
return qml_mtx_events::EventType::HistoryVisibility;
|
|
|
|
case EventType::RoomJoinRules:
|
|
|
|
return qml_mtx_events::EventType::JoinRules;
|
|
|
|
case EventType::RoomMember:
|
|
|
|
return qml_mtx_events::EventType::Member;
|
|
|
|
case EventType::RoomMessage:
|
|
|
|
return qml_mtx_events::EventType::UnknownMessage;
|
|
|
|
case EventType::RoomName:
|
|
|
|
return qml_mtx_events::EventType::Name;
|
|
|
|
case EventType::RoomPowerLevels:
|
|
|
|
return qml_mtx_events::EventType::PowerLevels;
|
|
|
|
case EventType::RoomTopic:
|
|
|
|
return qml_mtx_events::EventType::Topic;
|
|
|
|
case EventType::RoomTombstone:
|
|
|
|
return qml_mtx_events::EventType::Tombstone;
|
|
|
|
case EventType::RoomRedaction:
|
|
|
|
return qml_mtx_events::EventType::Redaction;
|
|
|
|
case EventType::RoomPinnedEvents:
|
|
|
|
return qml_mtx_events::EventType::PinnedEvents;
|
|
|
|
case EventType::Sticker:
|
|
|
|
return qml_mtx_events::EventType::Sticker;
|
|
|
|
case EventType::Tag:
|
|
|
|
return qml_mtx_events::EventType::Tag;
|
|
|
|
case EventType::Unsupported:
|
|
|
|
default:
|
|
|
|
return qml_mtx_events::EventType::Unsupported;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Audio> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::AudioMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Emote> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::EmoteMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::File> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::FileMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Image> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::ImageMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Notice> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::NoticeMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Text> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::TextMessage;
|
|
|
|
}
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Video> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::VideoMessage;
|
|
|
|
}
|
2019-09-09 22:42:33 +03:00
|
|
|
|
|
|
|
qml_mtx_events::EventType
|
|
|
|
toRoomEventType(const mtx::events::Event<mtx::events::msg::Redacted> &)
|
|
|
|
{
|
|
|
|
return qml_mtx_events::EventType::Redacted;
|
|
|
|
}
|
2019-09-03 00:28:05 +03:00
|
|
|
// ::EventType::Type toRoomEventType(const Event<mtx::events::msg::Location> &e) { return
|
|
|
|
// ::EventType::LocationMessage; }
|
2019-09-08 13:44:46 +03:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
uint64_t
|
|
|
|
eventHeight(const mtx::events::Event<T> &)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventHeight(const mtx::events::RoomEvent<T> &e) -> decltype(e.content.info.h)
|
|
|
|
{
|
|
|
|
return e.content.info.h;
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
uint64_t
|
|
|
|
eventWidth(const mtx::events::Event<T> &)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
eventWidth(const mtx::events::RoomEvent<T> &e) -> decltype(e.content.info.w)
|
|
|
|
{
|
|
|
|
return e.content.info.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
double
|
|
|
|
eventPropHeight(const mtx::events::RoomEvent<T> &e)
|
|
|
|
{
|
|
|
|
auto w = eventWidth(e);
|
|
|
|
if (w == 0)
|
|
|
|
w = 1;
|
|
|
|
return eventHeight(e) / (double)w;
|
|
|
|
}
|
2019-08-31 23:43:31 +03:00
|
|
|
}
|
|
|
|
|
2019-09-01 00:44:17 +03:00
|
|
|
TimelineModel::TimelineModel(QString room_id, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, room_id_(room_id)
|
|
|
|
{
|
|
|
|
connect(
|
|
|
|
this, &TimelineModel::oldMessagesRetrieved, this, &TimelineModel::addBackwardsEvents);
|
2019-09-18 23:58:25 +03:00
|
|
|
connect(this, &TimelineModel::messageFailed, this, [this](QString txn_id) {
|
|
|
|
pending.remove(txn_id);
|
|
|
|
failed.insert(txn_id);
|
|
|
|
int idx = idToIndex(txn_id);
|
|
|
|
if (idx < 0) {
|
|
|
|
nhlog::ui()->warn("Failed index out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit dataChanged(index(idx, 0), index(idx, 0));
|
|
|
|
});
|
|
|
|
connect(this, &TimelineModel::messageSent, this, [this](QString txn_id, QString event_id) {
|
|
|
|
int idx = idToIndex(txn_id);
|
|
|
|
if (idx < 0) {
|
|
|
|
nhlog::ui()->warn("Sent index out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
eventOrder[idx] = event_id;
|
|
|
|
auto ev = events.value(txn_id);
|
|
|
|
ev = boost::apply_visitor(
|
|
|
|
[event_id](const auto &e) -> mtx::events::collections::TimelineEvents {
|
|
|
|
auto eventCopy = e;
|
|
|
|
eventCopy.event_id = event_id.toStdString();
|
|
|
|
return eventCopy;
|
|
|
|
},
|
|
|
|
ev);
|
|
|
|
events.remove(txn_id);
|
|
|
|
events.insert(event_id, ev);
|
2019-09-19 22:47:16 +03:00
|
|
|
|
|
|
|
// ask to be notified for read receipts
|
|
|
|
cache::client()->addPendingReceipt(room_id_, event_id);
|
|
|
|
|
2019-09-18 23:58:25 +03:00
|
|
|
emit dataChanged(index(idx, 0), index(idx, 0));
|
|
|
|
});
|
2019-09-01 00:44:17 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:20:53 +03:00
|
|
|
QHash<int, QByteArray>
|
|
|
|
TimelineModel::roleNames() const
|
|
|
|
{
|
|
|
|
return {
|
2019-09-01 15:17:33 +03:00
|
|
|
{Section, "section"},
|
2019-08-31 00:20:53 +03:00
|
|
|
{Type, "type"},
|
|
|
|
{Body, "body"},
|
|
|
|
{FormattedBody, "formattedBody"},
|
|
|
|
{UserId, "userId"},
|
|
|
|
{UserName, "userName"},
|
|
|
|
{Timestamp, "timestamp"},
|
2019-09-08 13:44:46 +03:00
|
|
|
{Url, "url"},
|
|
|
|
{Height, "height"},
|
|
|
|
{Width, "width"},
|
|
|
|
{ProportionalHeight, "proportionalHeight"},
|
2019-09-08 16:26:46 +03:00
|
|
|
{Id, "id"},
|
2019-09-18 23:58:25 +03:00
|
|
|
{State, "state"},
|
2019-08-31 00:20:53 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
int
|
|
|
|
TimelineModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return (int)this->eventOrder.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
TimelineModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() < 0 && index.row() >= (int)eventOrder.size())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
QString id = eventOrder[index.row()];
|
|
|
|
|
2019-09-08 17:50:32 +03:00
|
|
|
mtx::events::collections::TimelineEvents event = events.value(id);
|
|
|
|
|
|
|
|
if (auto e = boost::get<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(&event)) {
|
|
|
|
event = decryptEvent(*e).event;
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:20:53 +03:00
|
|
|
switch (role) {
|
2019-09-01 15:17:33 +03:00
|
|
|
case Section: {
|
|
|
|
QDateTime date = boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QDateTime { return eventTimestamp(e); }, event);
|
2019-09-01 15:17:33 +03:00
|
|
|
date.setTime(QTime());
|
|
|
|
|
2019-09-08 17:50:32 +03:00
|
|
|
QString userId =
|
|
|
|
boost::apply_visitor([](const auto &e) -> QString { return senderId(e); }, event);
|
2019-09-01 15:17:33 +03:00
|
|
|
|
|
|
|
for (int r = index.row() - 1; r > 0; r--) {
|
|
|
|
QDateTime prevDate = boost::apply_visitor(
|
|
|
|
[](const auto &e) -> QDateTime { return eventTimestamp(e); },
|
|
|
|
events.value(eventOrder[r]));
|
|
|
|
prevDate.setTime(QTime());
|
|
|
|
if (prevDate != date)
|
|
|
|
return QString("%2 %1").arg(date.toMSecsSinceEpoch()).arg(userId);
|
|
|
|
|
|
|
|
QString prevUserId =
|
|
|
|
boost::apply_visitor([](const auto &e) -> QString { return senderId(e); },
|
|
|
|
events.value(eventOrder[r]));
|
|
|
|
if (userId != prevUserId)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString("%1").arg(userId);
|
|
|
|
}
|
2019-08-31 00:20:53 +03:00
|
|
|
case UserId:
|
2019-08-31 23:43:31 +03:00
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QString { return senderId(e); }, event));
|
2019-09-01 15:17:33 +03:00
|
|
|
case UserName:
|
2019-09-01 23:34:36 +03:00
|
|
|
return QVariant(displayName(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QString { return senderId(e); }, event)));
|
2019-09-01 15:17:33 +03:00
|
|
|
|
|
|
|
case Timestamp:
|
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QDateTime { return eventTimestamp(e); }, event));
|
2019-09-03 00:28:05 +03:00
|
|
|
case Type:
|
|
|
|
return QVariant(boost::apply_visitor(
|
|
|
|
[](const auto &e) -> qml_mtx_events::EventType { return toRoomEventType(e); },
|
2019-09-08 17:50:32 +03:00
|
|
|
event));
|
2019-09-11 01:54:40 +03:00
|
|
|
case Body:
|
|
|
|
return QVariant(utils::replaceEmoji(boost::apply_visitor(
|
|
|
|
[](const auto &e) -> QString { return eventBody(e); }, event)));
|
2019-09-03 00:28:05 +03:00
|
|
|
case FormattedBody:
|
|
|
|
return QVariant(utils::replaceEmoji(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QString { return eventFormattedBody(e); }, event)));
|
2019-09-08 13:44:46 +03:00
|
|
|
case Url:
|
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> QString { return eventUrl(e); }, event));
|
2019-09-08 13:44:46 +03:00
|
|
|
case Height:
|
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> qulonglong { return eventHeight(e); }, event));
|
2019-09-08 13:44:46 +03:00
|
|
|
case Width:
|
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> qulonglong { return eventWidth(e); }, event));
|
2019-09-08 13:44:46 +03:00
|
|
|
case ProportionalHeight:
|
|
|
|
return QVariant(boost::apply_visitor(
|
2019-09-08 17:50:32 +03:00
|
|
|
[](const auto &e) -> double { return eventPropHeight(e); }, event));
|
2019-09-08 16:26:46 +03:00
|
|
|
case Id:
|
|
|
|
return id;
|
2019-09-18 23:58:25 +03:00
|
|
|
case State:
|
2019-09-19 22:47:16 +03:00
|
|
|
// only show read receipts for messages not from us
|
|
|
|
if (boost::apply_visitor([](const auto &e) -> QString { return senderId(e); },
|
|
|
|
event)
|
|
|
|
.toStdString() != http::client()->user_id().to_string())
|
|
|
|
return qml_mtx_events::Empty;
|
|
|
|
else if (failed.contains(id))
|
2019-09-18 23:58:25 +03:00
|
|
|
return qml_mtx_events::Failed;
|
|
|
|
else if (pending.contains(id))
|
|
|
|
return qml_mtx_events::Sent;
|
2019-09-19 22:47:16 +03:00
|
|
|
else if (read.contains(id) ||
|
|
|
|
cache::client()->readReceipts(id, room_id_).size() > 1)
|
2019-09-19 00:37:30 +03:00
|
|
|
return qml_mtx_events::Read;
|
2019-09-18 23:58:25 +03:00
|
|
|
else
|
|
|
|
return qml_mtx_events::Received;
|
2019-08-31 00:20:53 +03:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-09-01 00:44:17 +03:00
|
|
|
if (isInitialSync) {
|
2019-09-03 03:14:49 +03:00
|
|
|
prev_batch_token_ = QString::fromStdString(timeline.prev_batch);
|
2019-09-01 00:44:17 +03:00
|
|
|
isInitialSync = false;
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:37:54 +03:00
|
|
|
if (timeline.events.empty())
|
|
|
|
return;
|
|
|
|
|
2019-09-09 22:42:33 +03:00
|
|
|
std::vector<QString> ids = internalAddEvents(timeline.events);
|
|
|
|
|
|
|
|
if (ids.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(),
|
|
|
|
static_cast<int>(this->eventOrder.size()),
|
|
|
|
static_cast<int>(this->eventOrder.size() + ids.size() - 1));
|
|
|
|
this->eventOrder.insert(this->eventOrder.end(), ids.begin(), ids.end());
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString>
|
|
|
|
TimelineModel::internalAddEvents(
|
|
|
|
const std::vector<mtx::events::collections::TimelineEvents> &timeline)
|
|
|
|
{
|
2019-08-31 23:43:31 +03:00
|
|
|
std::vector<QString> ids;
|
2019-09-09 22:42:33 +03:00
|
|
|
for (const auto &e : timeline) {
|
2019-08-31 23:43:31 +03:00
|
|
|
QString id =
|
|
|
|
boost::apply_visitor([](const auto &e) -> QString { return eventId(e); }, e);
|
|
|
|
|
2019-09-18 23:58:25 +03:00
|
|
|
if (this->events.contains(id)) {
|
|
|
|
this->events.insert(id, e);
|
|
|
|
int idx = idToIndex(id);
|
|
|
|
emit dataChanged(index(idx, 0), index(idx, 0));
|
2019-09-09 21:33:22 +03:00
|
|
|
continue;
|
2019-09-18 23:58:25 +03:00
|
|
|
}
|
2019-09-09 21:33:22 +03:00
|
|
|
|
2019-09-09 22:42:33 +03:00
|
|
|
if (auto redaction =
|
|
|
|
boost::get<mtx::events::RedactionEvent<mtx::events::msg::Redaction>>(&e)) {
|
|
|
|
QString redacts = QString::fromStdString(redaction->redacts);
|
|
|
|
auto redacted = std::find(eventOrder.begin(), eventOrder.end(), redacts);
|
|
|
|
|
|
|
|
if (redacted != eventOrder.end()) {
|
|
|
|
auto redactedEvent = boost::apply_visitor(
|
|
|
|
[](const auto &ev)
|
|
|
|
-> mtx::events::RoomEvent<mtx::events::msg::Redacted> {
|
|
|
|
mtx::events::RoomEvent<mtx::events::msg::Redacted>
|
|
|
|
replacement = {};
|
|
|
|
replacement.event_id = ev.event_id;
|
|
|
|
replacement.room_id = ev.room_id;
|
|
|
|
replacement.sender = ev.sender;
|
|
|
|
replacement.origin_server_ts = ev.origin_server_ts;
|
|
|
|
replacement.type = ev.type;
|
|
|
|
return replacement;
|
|
|
|
},
|
|
|
|
e);
|
|
|
|
events.insert(redacts, redactedEvent);
|
|
|
|
|
|
|
|
int row = (int)std::distance(eventOrder.begin(), redacted);
|
|
|
|
emit dataChanged(index(row, 0), index(row, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue; // don't insert redaction into timeline
|
|
|
|
}
|
|
|
|
|
2019-08-31 23:43:31 +03:00
|
|
|
this->events.insert(id, e);
|
|
|
|
ids.push_back(id);
|
|
|
|
}
|
2019-09-09 22:42:33 +03:00
|
|
|
return ids;
|
2019-08-31 23:43:31 +03:00
|
|
|
}
|
|
|
|
|
2019-09-01 00:44:17 +03:00
|
|
|
void
|
|
|
|
TimelineModel::fetchHistory()
|
|
|
|
{
|
|
|
|
if (paginationInProgress) {
|
|
|
|
nhlog::ui()->warn("Already loading older messages");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationInProgress = true;
|
|
|
|
mtx::http::MessagesOpts opts;
|
|
|
|
opts.room_id = room_id_.toStdString();
|
|
|
|
opts.from = prev_batch_token_.toStdString();
|
|
|
|
|
|
|
|
nhlog::ui()->info("Paginationg room {}", opts.room_id);
|
|
|
|
|
|
|
|
http::client()->messages(
|
|
|
|
opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->error("failed to call /messages ({}): {} - {}",
|
|
|
|
opts.room_id,
|
|
|
|
mtx::errors::to_string(err->matrix_error.errcode),
|
|
|
|
err->matrix_error.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit oldMessagesRetrieved(std::move(res));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::addBackwardsEvents(const mtx::responses::Messages &msgs)
|
|
|
|
{
|
2019-09-09 22:42:33 +03:00
|
|
|
std::vector<QString> ids = internalAddEvents(msgs.chunk);
|
2019-09-01 00:44:17 +03:00
|
|
|
|
2019-09-09 21:33:22 +03:00
|
|
|
if (!ids.empty()) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, static_cast<int>(ids.size() - 1));
|
|
|
|
this->eventOrder.insert(this->eventOrder.begin(), ids.rbegin(), ids.rend());
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2019-09-01 00:44:17 +03:00
|
|
|
|
|
|
|
prev_batch_token_ = QString::fromStdString(msgs.end);
|
|
|
|
|
|
|
|
paginationInProgress = false;
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:20:53 +03:00
|
|
|
QColor
|
|
|
|
TimelineModel::userColor(QString id, QColor background)
|
|
|
|
{
|
2019-08-31 23:43:31 +03:00
|
|
|
if (!userColors.contains(id))
|
2019-08-31 00:20:53 +03:00
|
|
|
userColors.insert(
|
2019-08-31 23:43:31 +03:00
|
|
|
id, QColor(utils::generateContrastingHexColor(id, background.name())));
|
|
|
|
return userColors.value(id);
|
2019-08-31 00:20:53 +03:00
|
|
|
}
|
2019-09-01 23:34:36 +03:00
|
|
|
|
|
|
|
QString
|
|
|
|
TimelineModel::displayName(QString id) const
|
|
|
|
{
|
|
|
|
return Cache::displayName(room_id_, id);
|
|
|
|
}
|
2019-09-01 23:58:26 +03:00
|
|
|
|
2019-09-07 23:22:07 +03:00
|
|
|
QString
|
|
|
|
TimelineModel::avatarUrl(QString id) const
|
|
|
|
{
|
|
|
|
return Cache::avatarUrl(room_id_, id);
|
|
|
|
}
|
|
|
|
|
2019-09-01 23:58:26 +03:00
|
|
|
QString
|
|
|
|
TimelineModel::formatDateSeparator(QDate date) const
|
|
|
|
{
|
|
|
|
auto now = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
QString fmt = QLocale::system().dateFormat(QLocale::LongFormat);
|
|
|
|
|
|
|
|
if (now.date().year() == date.year()) {
|
|
|
|
QRegularExpression rx("[^a-zA-Z]*y+[^a-zA-Z]*");
|
|
|
|
fmt = fmt.remove(rx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.toString(fmt);
|
|
|
|
}
|
2019-09-07 03:01:44 +03:00
|
|
|
|
|
|
|
QString
|
|
|
|
TimelineModel::escapeEmoji(QString str) const
|
|
|
|
{
|
|
|
|
return utils::replaceEmoji(str);
|
|
|
|
}
|
2019-09-08 16:26:46 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::viewRawMessage(QString id) const
|
|
|
|
{
|
|
|
|
std::string ev = utils::serialize_event(events.value(id)).dump(4);
|
|
|
|
auto dialog = new dialogs::RawMessage(QString::fromStdString(ev));
|
|
|
|
Q_UNUSED(dialog);
|
|
|
|
}
|
2019-09-08 17:50:32 +03:00
|
|
|
|
|
|
|
DecryptionResult
|
|
|
|
TimelineModel::decryptEvent(const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e) const
|
|
|
|
{
|
|
|
|
MegolmSessionIndex index;
|
|
|
|
index.room_id = room_id_.toStdString();
|
|
|
|
index.session_id = e.content.session_id;
|
|
|
|
index.sender_key = e.content.sender_key;
|
|
|
|
|
|
|
|
mtx::events::RoomEvent<mtx::events::msg::Notice> dummy;
|
|
|
|
dummy.origin_server_ts = e.origin_server_ts;
|
|
|
|
dummy.event_id = e.event_id;
|
|
|
|
dummy.sender = e.sender;
|
|
|
|
dummy.content.body =
|
|
|
|
tr("-- Encrypted Event (No keys found for decryption) --",
|
|
|
|
"Placeholder, when the message was not decrypted yet or can't be decrypted")
|
|
|
|
.toStdString();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!cache::client()->inboundMegolmSessionExists(index)) {
|
|
|
|
nhlog::crypto()->info("Could not find inbound megolm session ({}, {}, {})",
|
|
|
|
index.room_id,
|
|
|
|
index.session_id,
|
|
|
|
e.sender);
|
|
|
|
// TODO: request megolm session_id & session_key from the sender.
|
|
|
|
return {dummy, false};
|
|
|
|
}
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical("failed to check megolm session's existence: {}", e.what());
|
|
|
|
dummy.content.body = tr("-- Decryption Error (failed to communicate with DB) --",
|
|
|
|
"Placeholder, when the message can't be decrypted, because "
|
|
|
|
"the DB access failed when trying to lookup the session.")
|
|
|
|
.toStdString();
|
|
|
|
return {dummy, false};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string msg_str;
|
|
|
|
try {
|
|
|
|
auto session = cache::client()->getInboundMegolmSession(index);
|
|
|
|
auto res = olm::client()->decrypt_group_message(session, e.content.ciphertext);
|
|
|
|
msg_str = std::string((char *)res.data.data(), res.data.size());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical("failed to retrieve megolm session with index ({}, {}, {})",
|
|
|
|
index.room_id,
|
|
|
|
index.session_id,
|
|
|
|
index.sender_key,
|
|
|
|
e.what());
|
|
|
|
dummy.content.body =
|
|
|
|
tr("-- Decryption Error (failed to retrieve megolm keys from db) --",
|
|
|
|
"Placeholder, when the message can't be decrypted, because the DB access "
|
|
|
|
"failed.")
|
|
|
|
.toStdString();
|
|
|
|
return {dummy, false};
|
|
|
|
} catch (const mtx::crypto::olm_exception &e) {
|
|
|
|
nhlog::crypto()->critical("failed to decrypt message with index ({}, {}, {}): {}",
|
|
|
|
index.room_id,
|
|
|
|
index.session_id,
|
|
|
|
index.sender_key,
|
|
|
|
e.what());
|
|
|
|
dummy.content.body =
|
|
|
|
tr("-- Decryption Error (%1) --",
|
|
|
|
"Placeholder, when the message can't be decrypted. In this case, the Olm "
|
|
|
|
"decrytion returned an error, which is passed ad %1")
|
|
|
|
.arg(e.what())
|
|
|
|
.toStdString();
|
|
|
|
return {dummy, false};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add missing fields for the event.
|
|
|
|
json body = json::parse(msg_str);
|
|
|
|
body["event_id"] = e.event_id;
|
|
|
|
body["sender"] = e.sender;
|
|
|
|
body["origin_server_ts"] = e.origin_server_ts;
|
|
|
|
body["unsigned"] = e.unsigned_data;
|
|
|
|
|
|
|
|
json event_array = json::array();
|
|
|
|
event_array.push_back(body);
|
|
|
|
|
2019-09-08 18:28:40 +03:00
|
|
|
std::vector<mtx::events::collections::TimelineEvents> temp_events;
|
|
|
|
mtx::responses::utils::parse_timeline_events(event_array, temp_events);
|
2019-09-08 17:50:32 +03:00
|
|
|
|
2019-09-08 18:28:40 +03:00
|
|
|
if (temp_events.size() == 1)
|
|
|
|
return {temp_events.at(0), true};
|
2019-09-08 17:50:32 +03:00
|
|
|
|
|
|
|
dummy.content.body =
|
|
|
|
tr("-- Encrypted Event (Unknown event type) --",
|
|
|
|
"Placeholder, when the message was decrypted, but we couldn't parse it, because "
|
|
|
|
"Nheko/mtxclient don't support that event type yet")
|
|
|
|
.toStdString();
|
|
|
|
return {dummy, false};
|
|
|
|
}
|
2019-09-11 01:54:40 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::replyAction(QString id)
|
|
|
|
{
|
|
|
|
auto event = events.value(id);
|
|
|
|
RelatedInfo related = boost::apply_visitor(
|
|
|
|
[](const auto &ev) -> RelatedInfo {
|
|
|
|
RelatedInfo related_ = {};
|
|
|
|
related_.quoted_user = QString::fromStdString(ev.sender);
|
|
|
|
related_.related_event = ev.event_id;
|
|
|
|
return related_;
|
|
|
|
},
|
|
|
|
event);
|
|
|
|
related.type = mtx::events::getMessageType(boost::apply_visitor(
|
|
|
|
[](const auto &e) -> std::string { return eventMsgType(e); }, event));
|
|
|
|
related.quoted_body =
|
|
|
|
boost::apply_visitor([](const auto &e) -> QString { return eventBody(e); }, event);
|
|
|
|
|
|
|
|
if (related.quoted_body.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2019-09-19 00:37:30 +03:00
|
|
|
ChatPage::instance()->messageReply(related);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::readReceiptsAction(QString id) const
|
|
|
|
{
|
|
|
|
MainWindow::instance()->openReadReceiptsDialog(id);
|
2019-09-11 01:54:40 +03:00
|
|
|
}
|
2019-09-18 21:34:30 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
TimelineModel::idToIndex(QString id) const
|
|
|
|
{
|
|
|
|
if (id.isEmpty())
|
|
|
|
return -1;
|
|
|
|
for (int i = 0; i < (int)eventOrder.size(); i++)
|
|
|
|
if (id == eventOrder[i])
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
TimelineModel::indexToId(int index) const
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= (int)eventOrder.size())
|
|
|
|
return "";
|
|
|
|
return eventOrder[index];
|
|
|
|
}
|
2019-09-19 00:37:30 +03:00
|
|
|
|
2019-09-19 22:47:16 +03:00
|
|
|
// Note: this will only be called for our messages
|
2019-09-19 00:37:30 +03:00
|
|
|
void
|
|
|
|
TimelineModel::markEventsAsRead(const std::vector<QString> &event_ids)
|
|
|
|
{
|
|
|
|
for (const auto &id : event_ids) {
|
|
|
|
read.insert(id);
|
|
|
|
int idx = idToIndex(id);
|
|
|
|
if (idx < 0) {
|
|
|
|
nhlog::ui()->warn("Read index out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit dataChanged(index(idx, 0), index(idx, 0));
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 23:44:25 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::sendEncryptedMessage(const std::string &txn_id, nlohmann::json content)
|
|
|
|
{
|
|
|
|
const auto room_id = room_id_.toStdString();
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::identifiers;
|
|
|
|
|
|
|
|
json doc{{"type", "m.room.message"}, {"content", content}, {"room_id", room_id}};
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Check if we have already an outbound megolm session then we can use.
|
|
|
|
if (cache::client()->outboundMegolmSessionExists(room_id)) {
|
|
|
|
auto data = olm::encrypt_group_message(
|
|
|
|
room_id, http::client()->device_id(), doc.dump());
|
|
|
|
|
|
|
|
http::client()->send_room_message<msg::Encrypted, EventType::RoomEncrypted>(
|
|
|
|
room_id,
|
|
|
|
txn_id,
|
|
|
|
data,
|
|
|
|
[this, txn_id](const mtx::responses::EventId &res,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
const int status_code =
|
|
|
|
static_cast<int>(err->status_code);
|
|
|
|
nhlog::net()->warn("[{}] failed to send message: {} {}",
|
|
|
|
txn_id,
|
|
|
|
err->matrix_error.error,
|
|
|
|
status_code);
|
|
|
|
emit messageFailed(QString::fromStdString(txn_id));
|
|
|
|
}
|
|
|
|
emit messageSent(
|
|
|
|
QString::fromStdString(txn_id),
|
|
|
|
QString::fromStdString(res.event_id.to_string()));
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::ui()->debug("creating new outbound megolm session");
|
|
|
|
|
|
|
|
// Create a new outbound megolm session.
|
|
|
|
auto outbound_session = olm::client()->init_outbound_group_session();
|
|
|
|
const auto session_id = mtx::crypto::session_id(outbound_session.get());
|
|
|
|
const auto session_key = mtx::crypto::session_key(outbound_session.get());
|
|
|
|
|
|
|
|
// TODO: needs to be moved in the lib.
|
|
|
|
auto megolm_payload = json{{"algorithm", "m.megolm.v1.aes-sha2"},
|
|
|
|
{"room_id", room_id},
|
|
|
|
{"session_id", session_id},
|
|
|
|
{"session_key", session_key}};
|
|
|
|
|
|
|
|
// Saving the new megolm session.
|
|
|
|
// TODO: Maybe it's too early to save.
|
|
|
|
OutboundGroupSessionData session_data;
|
|
|
|
session_data.session_id = session_id;
|
|
|
|
session_data.session_key = session_key;
|
|
|
|
session_data.message_index = 0; // TODO Update me
|
|
|
|
cache::client()->saveOutboundMegolmSession(
|
|
|
|
room_id, session_data, std::move(outbound_session));
|
|
|
|
|
|
|
|
const auto members = cache::client()->roomMembers(room_id);
|
|
|
|
nhlog::ui()->info("retrieved {} members for {}", members.size(), room_id);
|
|
|
|
|
|
|
|
auto keeper =
|
|
|
|
std::make_shared<StateKeeper>([megolm_payload, room_id, doc, txn_id, this]() {
|
|
|
|
try {
|
|
|
|
auto data = olm::encrypt_group_message(
|
|
|
|
room_id, http::client()->device_id(), doc.dump());
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_room_message<msg::Encrypted, EventType::RoomEncrypted>(
|
|
|
|
room_id,
|
|
|
|
txn_id,
|
|
|
|
data,
|
|
|
|
[this, txn_id](const mtx::responses::EventId &res,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
const int status_code =
|
|
|
|
static_cast<int>(err->status_code);
|
|
|
|
nhlog::net()->warn(
|
|
|
|
"[{}] failed to send message: {} {}",
|
|
|
|
txn_id,
|
|
|
|
err->matrix_error.error,
|
|
|
|
status_code);
|
|
|
|
emit messageFailed(
|
|
|
|
QString::fromStdString(txn_id));
|
|
|
|
}
|
|
|
|
emit messageSent(
|
|
|
|
QString::fromStdString(txn_id),
|
|
|
|
QString::fromStdString(res.event_id.to_string()));
|
|
|
|
});
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical(
|
|
|
|
"failed to save megolm outbound session: {}", e.what());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mtx::requests::QueryKeys req;
|
|
|
|
for (const auto &member : members)
|
|
|
|
req.device_keys[member] = {};
|
|
|
|
|
|
|
|
http::client()->query_keys(
|
|
|
|
req,
|
|
|
|
[keeper = std::move(keeper), megolm_payload, this](
|
|
|
|
const mtx::responses::QueryKeys &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to query device keys: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
// TODO: Mark the event as failed. Communicate with the UI.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &user : res.device_keys) {
|
|
|
|
// Mapping from a device_id with valid identity keys to the
|
|
|
|
// generated room_key event used for sharing the megolm session.
|
|
|
|
std::map<std::string, std::string> room_key_msgs;
|
|
|
|
std::map<std::string, DevicePublicKeys> deviceKeys;
|
|
|
|
|
|
|
|
room_key_msgs.clear();
|
|
|
|
deviceKeys.clear();
|
|
|
|
|
|
|
|
for (const auto &dev : user.second) {
|
|
|
|
const auto user_id = ::UserId(dev.second.user_id);
|
|
|
|
const auto device_id = DeviceId(dev.second.device_id);
|
|
|
|
|
|
|
|
const auto device_keys = dev.second.keys;
|
|
|
|
const auto curveKey = "curve25519:" + device_id.get();
|
|
|
|
const auto edKey = "ed25519:" + device_id.get();
|
|
|
|
|
|
|
|
if ((device_keys.find(curveKey) == device_keys.end()) ||
|
|
|
|
(device_keys.find(edKey) == device_keys.end())) {
|
|
|
|
nhlog::net()->debug(
|
|
|
|
"ignoring malformed keys for device {}",
|
|
|
|
device_id.get());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DevicePublicKeys pks;
|
|
|
|
pks.ed25519 = device_keys.at(edKey);
|
|
|
|
pks.curve25519 = device_keys.at(curveKey);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!mtx::crypto::verify_identity_signature(
|
|
|
|
json(dev.second), device_id, user_id)) {
|
|
|
|
nhlog::crypto()->warn(
|
|
|
|
"failed to verify identity keys: {}",
|
|
|
|
json(dev.second).dump(2));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
nhlog::crypto()->warn(
|
|
|
|
"failed to parse device key json: {}",
|
|
|
|
e.what());
|
|
|
|
continue;
|
|
|
|
} catch (const mtx::crypto::olm_exception &e) {
|
|
|
|
nhlog::crypto()->warn(
|
|
|
|
"failed to verify device key json: {}",
|
|
|
|
e.what());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto room_key = olm::client()
|
|
|
|
->create_room_key_event(
|
|
|
|
user_id, pks.ed25519, megolm_payload)
|
|
|
|
.dump();
|
|
|
|
|
|
|
|
room_key_msgs.emplace(device_id, room_key);
|
|
|
|
deviceKeys.emplace(device_id, pks);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> valid_devices;
|
|
|
|
valid_devices.reserve(room_key_msgs.size());
|
|
|
|
for (auto const &d : room_key_msgs) {
|
|
|
|
valid_devices.push_back(d.first);
|
|
|
|
|
|
|
|
nhlog::net()->info("{}", d.first);
|
|
|
|
nhlog::net()->info(" curve25519 {}",
|
|
|
|
deviceKeys.at(d.first).curve25519);
|
|
|
|
nhlog::net()->info(" ed25519 {}",
|
|
|
|
deviceKeys.at(d.first).ed25519);
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->info(
|
|
|
|
"sending claim request for user {} with {} devices",
|
|
|
|
user.first,
|
|
|
|
valid_devices.size());
|
|
|
|
|
|
|
|
http::client()->claim_keys(
|
|
|
|
user.first,
|
|
|
|
valid_devices,
|
|
|
|
std::bind(&TimelineModel::handleClaimedKeys,
|
|
|
|
this,
|
|
|
|
keeper,
|
|
|
|
room_key_msgs,
|
|
|
|
deviceKeys,
|
|
|
|
user.first,
|
|
|
|
std::placeholders::_1,
|
|
|
|
std::placeholders::_2));
|
|
|
|
|
|
|
|
// TODO: Wait before sending the next batch of requests.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
} catch (const mtx::crypto::olm_exception &e) {
|
|
|
|
nhlog::crypto()->critical(
|
|
|
|
"failed to open outbound megolm session ({}): {}", room_id, e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineModel::handleClaimedKeys(std::shared_ptr<StateKeeper> keeper,
|
|
|
|
const std::map<std::string, std::string> &room_keys,
|
|
|
|
const std::map<std::string, DevicePublicKeys> &pks,
|
|
|
|
const std::string &user_id,
|
|
|
|
const mtx::responses::ClaimKeys &res,
|
|
|
|
mtx::http::RequestErr err)
|
|
|
|
{
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("claim keys error: {} {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
err->parse_error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->debug("claimed keys for {}", user_id);
|
|
|
|
|
|
|
|
if (res.one_time_keys.size() == 0) {
|
|
|
|
nhlog::net()->debug("no one-time keys found for user_id: {}", user_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.one_time_keys.find(user_id) == res.one_time_keys.end()) {
|
|
|
|
nhlog::net()->debug("no one-time keys found for user_id: {}", user_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto retrieved_devices = res.one_time_keys.at(user_id);
|
|
|
|
|
|
|
|
// Payload with all the to_device message to be sent.
|
|
|
|
json body;
|
|
|
|
body["messages"][user_id] = json::object();
|
|
|
|
|
|
|
|
for (const auto &rd : retrieved_devices) {
|
|
|
|
const auto device_id = rd.first;
|
|
|
|
nhlog::net()->debug("{} : \n {}", device_id, rd.second.dump(2));
|
|
|
|
|
|
|
|
// TODO: Verify signatures
|
|
|
|
auto otk = rd.second.begin()->at("key");
|
|
|
|
|
|
|
|
if (pks.find(device_id) == pks.end()) {
|
|
|
|
nhlog::net()->critical("couldn't find public key for device: {}",
|
|
|
|
device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto id_key = pks.at(device_id).curve25519;
|
|
|
|
auto s = olm::client()->create_outbound_session(id_key, otk);
|
|
|
|
|
|
|
|
if (room_keys.find(device_id) == room_keys.end()) {
|
|
|
|
nhlog::net()->critical("couldn't find m.room_key for device: {}",
|
|
|
|
device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto device_msg = olm::client()->create_olm_encrypted_content(
|
|
|
|
s.get(), room_keys.at(device_id), pks.at(device_id).curve25519);
|
|
|
|
|
|
|
|
try {
|
|
|
|
cache::client()->saveOlmSession(id_key, std::move(s));
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->critical("failed to save outbound olm session: {}", e.what());
|
|
|
|
} catch (const mtx::crypto::olm_exception &e) {
|
|
|
|
nhlog::crypto()->critical("failed to pickle outbound olm session: {}",
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
body["messages"][user_id][device_id] = device_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->info("send_to_device: {}", user_id);
|
|
|
|
|
|
|
|
http::client()->send_to_device(
|
|
|
|
"m.room.encrypted", body, [keeper](mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to send "
|
|
|
|
"send_to_device "
|
|
|
|
"message: {}",
|
|
|
|
err->matrix_error.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)keeper;
|
|
|
|
});
|
|
|
|
}
|