matrixion/src/EventAccessors.cpp

500 lines
13 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-12-27 23:49:55 +03:00
#include "EventAccessors.h"
2020-10-27 19:45:28 +03:00
#include <nlohmann/json.hpp>
2020-07-24 20:30:12 +03:00
#include <algorithm>
#include <cctype>
2019-12-27 23:49:55 +03:00
#include <type_traits>
namespace {
template<template<class...> class Op, class... Args>
using is_detected = typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t;
2019-12-27 23:49:55 +03:00
2021-02-01 00:41:43 +03:00
struct IsStateEvent
{
2021-09-18 01:22:33 +03:00
template<class T>
bool operator()(const mtx::events::StateEvent<T> &)
{
return true;
}
template<class T>
bool operator()(const mtx::events::Event<T> &)
{
return false;
}
2021-02-01 00:41:43 +03:00
};
2019-12-27 23:49:55 +03:00
struct EventMsgType
{
2021-09-18 01:22:33 +03:00
template<class E>
using msgtype_t = decltype(E::msgtype);
template<class T>
mtx::events::MessageType operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<msgtype_t, T>::value) {
if constexpr (std::is_same_v<std::optional<std::string>,
std::remove_cv_t<decltype(e.content.msgtype)>>)
return mtx::events::getMessageType(e.content.msgtype.value());
else if constexpr (std::is_same_v<std::string,
std::remove_cv_t<decltype(e.content.msgtype)>>)
return mtx::events::getMessageType(e.content.msgtype);
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return mtx::events::MessageType::Unknown;
}
2019-12-27 23:49:55 +03:00
};
struct EventRoomName
{
2021-09-18 01:22:33 +03:00
template<class T>
std::string operator()(const T &e)
{
if constexpr (std::is_same_v<mtx::events::StateEvent<mtx::events::state::Name>, T>)
return e.content.name;
return "";
}
2019-12-27 23:49:55 +03:00
};
struct EventRoomTopic
{
2021-09-18 01:22:33 +03:00
template<class T>
std::string operator()(const T &e)
{
if constexpr (std::is_same_v<mtx::events::StateEvent<mtx::events::state::Topic>, T>)
return e.content.topic;
return "";
}
2019-12-27 23:49:55 +03:00
};
2020-07-24 20:30:12 +03:00
struct CallType
{
2021-09-18 01:22:33 +03:00
template<class T>
std::string operator()(const T &e)
{
if constexpr (std::is_same_v<mtx::events::RoomEvent<mtx::events::msg::CallInvite>, T>) {
const char video[] = "m=video";
const std::string &sdp = e.content.sdp;
return std::search(sdp.cbegin(),
sdp.cend(),
std::cbegin(video),
std::cend(video) - 1,
[](unsigned char c1, unsigned char c2) {
return std::tolower(c1) == std::tolower(c2);
}) != sdp.cend()
? "video"
: "voice";
2020-07-24 20:30:12 +03:00
}
return "";
2021-09-18 01:22:33 +03:00
}
2020-07-24 20:30:12 +03:00
};
2019-12-27 23:49:55 +03:00
struct EventBody
{
2021-09-18 01:22:33 +03:00
template<class C>
using body_t = decltype(C::body);
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<body_t, T>::value) {
if constexpr (std::is_same_v<std::optional<std::string>,
std::remove_cv_t<decltype(e.content.body)>>)
return e.content.body ? e.content.body.value() : "";
else if constexpr (std::is_same_v<std::string,
std::remove_cv_t<decltype(e.content.body)>>)
return e.content.body;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return "";
}
2019-12-27 23:49:55 +03:00
};
struct EventFormattedBody
{
2021-09-18 01:22:33 +03:00
template<class C>
using formatted_body_t = decltype(C::formatted_body);
template<class T>
std::string operator()(const mtx::events::RoomEvent<T> &e)
{
if constexpr (is_detected<formatted_body_t, T>::value) {
if (e.content.format == "org.matrix.custom.html")
return e.content.formatted_body;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return "";
}
2019-12-27 23:49:55 +03:00
};
struct EventFile
{
2021-09-18 01:22:33 +03:00
template<class Content>
using file_t = decltype(Content::file);
template<class T>
std::optional<mtx::crypto::EncryptedFile> operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<file_t, T>::value)
return e.content.file;
return std::nullopt;
}
2019-12-27 23:49:55 +03:00
};
struct EventUrl
{
2021-09-18 01:22:33 +03:00
template<class Content>
using url_t = decltype(Content::url);
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<url_t, T>::value) {
if (auto file = EventFile{}(e))
return file->url;
return e.content.url;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return "";
}
2019-12-27 23:49:55 +03:00
};
struct EventThumbnailUrl
{
2021-09-18 01:22:33 +03:00
template<class Content>
using thumbnail_url_t = decltype(Content::info.thumbnail_url);
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<thumbnail_url_t, T>::value) {
return e.content.info.thumbnail_url;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return "";
}
2019-12-27 23:49:55 +03:00
};
2022-03-21 02:48:27 +03:00
struct EventDuration
{
template<class Content>
using thumbnail_url_t = decltype(Content::info.duration);
template<class T>
uint64_t operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<thumbnail_url_t, T>::value) {
return e.content.info.duration;
}
return 0;
}
};
struct EventBlurhash
{
2021-09-18 01:22:33 +03:00
template<class Content>
using blurhash_t = decltype(Content::info.blurhash);
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<blurhash_t, T>::value) {
return e.content.info.blurhash;
}
2021-09-18 01:22:33 +03:00
return "";
}
};
2019-12-27 23:49:55 +03:00
struct EventFilename
{
2021-09-18 01:22:33 +03:00
template<class T>
std::string operator()(const mtx::events::Event<T> &)
{
return "";
}
std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Audio> &e)
{
// body may be the original filename
return e.content.body;
}
std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Video> &e)
{
// body may be the original filename
return e.content.body;
}
std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Image> &e)
{
// body may be the original filename
return e.content.body;
}
std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::File> &e)
{
// body may be the original filename
if (!e.content.filename.empty())
return e.content.filename;
return e.content.body;
}
2019-12-27 23:49:55 +03:00
};
struct EventMimeType
{
2021-09-18 01:22:33 +03:00
template<class Content>
using mimetype_t = decltype(Content::info.mimetype);
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<mimetype_t, T>::value) {
return e.content.info.mimetype;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return "";
}
2019-12-27 23:49:55 +03:00
};
struct EventFilesize
{
2021-09-18 01:22:33 +03:00
template<class Content>
using filesize_t = decltype(Content::info.size);
template<class T>
int64_t operator()(const mtx::events::RoomEvent<T> &e)
{
if constexpr (is_detected<filesize_t, T>::value) {
return e.content.info.size;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return 0;
}
2019-12-27 23:49:55 +03:00
};
2021-01-27 00:36:35 +03:00
struct EventRelations
2019-12-27 23:49:55 +03:00
{
inline const static mtx::common::Relations empty;
2021-09-18 01:22:33 +03:00
template<class Content>
using related_ev_id_t = decltype(Content::relations);
template<class T>
const mtx::common::Relations &operator()(const mtx::events::Event<T> &e)
2021-09-18 01:22:33 +03:00
{
if constexpr (is_detected<related_ev_id_t, T>::value) {
return e.content.relations;
2020-07-20 01:42:48 +03:00
}
return empty;
2021-09-18 01:22:33 +03:00
}
2020-07-20 01:42:48 +03:00
};
2021-01-27 18:14:03 +03:00
struct SetEventRelations
{
2021-09-18 01:22:33 +03:00
mtx::common::Relations new_relations;
template<class Content>
using related_ev_id_t = decltype(Content::relations);
template<class T>
void operator()(mtx::events::Event<T> &e)
{
if constexpr (is_detected<related_ev_id_t, T>::value) {
e.content.relations = std::move(new_relations);
2021-01-27 18:14:03 +03:00
}
2021-09-18 01:22:33 +03:00
}
2021-01-27 18:14:03 +03:00
};
struct EventTransactionId
{
2021-09-18 01:22:33 +03:00
template<class T>
std::string operator()(const mtx::events::RoomEvent<T> &e)
{
return e.unsigned_data.transaction_id;
}
template<class T>
std::string operator()(const mtx::events::Event<T> &e)
{
return e.unsigned_data.transaction_id;
}
};
2019-12-27 23:49:55 +03:00
struct EventMediaHeight
{
2021-09-18 01:22:33 +03:00
template<class Content>
using h_t = decltype(Content::info.h);
template<class T>
uint64_t operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<h_t, T>::value) {
return e.content.info.h;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return -1;
}
2019-12-27 23:49:55 +03:00
};
struct EventMediaWidth
{
2021-09-18 01:22:33 +03:00
template<class Content>
using w_t = decltype(Content::info.w);
template<class T>
uint64_t operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<w_t, T>::value) {
return e.content.info.w;
2019-12-27 23:49:55 +03:00
}
2021-09-18 01:22:33 +03:00
return -1;
}
2019-12-27 23:49:55 +03:00
};
template<class T>
double
eventPropHeight(const mtx::events::RoomEvent<T> &e)
{
2021-09-18 01:22:33 +03:00
auto w = eventWidth(e);
if (w == 0)
w = 1;
2019-12-27 23:49:55 +03:00
2021-09-18 01:22:33 +03:00
double prop = eventHeight(e) / (double)w;
2019-12-27 23:49:55 +03:00
2021-09-18 01:22:33 +03:00
return prop > 0 ? prop : 1.;
2019-12-27 23:49:55 +03:00
}
}
const std::string &
2019-12-27 23:49:55 +03:00
mtx::accessors::event_id(const mtx::events::collections::TimelineEvents &event)
{
return std::visit([](const auto &e) -> const std::string & { return e.event_id; }, event);
2019-12-27 23:49:55 +03:00
}
const std::string &
2019-12-27 23:49:55 +03:00
mtx::accessors::room_id(const mtx::events::collections::TimelineEvents &event)
{
return std::visit([](const auto &e) -> const std::string & { return e.room_id; }, event);
2019-12-27 23:49:55 +03:00
}
const std::string &
2019-12-27 23:49:55 +03:00
mtx::accessors::sender(const mtx::events::collections::TimelineEvents &event)
{
return std::visit([](const auto &e) -> const std::string & { return e.sender; }, event);
2019-12-27 23:49:55 +03:00
}
QDateTime
mtx::accessors::origin_server_ts(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return QDateTime::fromMSecsSinceEpoch(
std::visit([](const auto &e) { return e.origin_server_ts; }, event));
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::filename(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventFilename{}, event);
2019-12-27 23:49:55 +03:00
}
mtx::events::MessageType
mtx::accessors::msg_type(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventMsgType{}, event);
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::room_name(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventRoomName{}, event);
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::room_topic(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventRoomTopic{}, event);
2019-12-27 23:49:55 +03:00
}
2020-07-24 20:30:12 +03:00
std::string
mtx::accessors::call_type(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(CallType{}, event);
2020-07-24 20:30:12 +03:00
}
2019-12-27 23:49:55 +03:00
std::string
mtx::accessors::body(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventBody{}, event);
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::formatted_body(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventFormattedBody{}, event);
2019-12-27 23:49:55 +03:00
}
QString
mtx::accessors::formattedBodyWithFallback(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
auto formatted = formatted_body(event);
if (!formatted.empty())
return QString::fromStdString(formatted);
else
return QString::fromStdString(body(event))
.toHtmlEscaped()
.replace(QLatin1String("\n"), QLatin1String("<br>"));
2019-12-27 23:49:55 +03:00
}
std::optional<mtx::crypto::EncryptedFile>
mtx::accessors::file(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventFile{}, event);
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::url(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventUrl{}, event);
2019-12-27 23:49:55 +03:00
}
std::string
mtx::accessors::thumbnail_url(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventThumbnailUrl{}, event);
2019-12-27 23:49:55 +03:00
}
2022-03-21 02:48:27 +03:00
uint64_t
mtx::accessors::duration(const mtx::events::collections::TimelineEvents &event)
{
return std::visit(EventDuration{}, event);
}
2019-12-27 23:49:55 +03:00
std::string
mtx::accessors::blurhash(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventBlurhash{}, event);
}
std::string
2019-12-27 23:49:55 +03:00
mtx::accessors::mimetype(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventMimeType{}, event);
2019-12-27 23:49:55 +03:00
}
const mtx::common::Relations &
2021-01-27 00:36:35 +03:00
mtx::accessors::relations(const mtx::events::collections::TimelineEvents &event)
2020-07-20 01:42:48 +03:00
{
2021-09-18 01:22:33 +03:00
return std::visit(EventRelations{}, event);
2020-07-20 01:42:48 +03:00
}
2019-12-27 23:49:55 +03:00
2021-01-27 18:14:03 +03:00
void
mtx::accessors::set_relations(mtx::events::collections::TimelineEvents &event,
mtx::common::Relations relations)
{
2021-09-18 01:22:33 +03:00
std::visit(SetEventRelations{std::move(relations)}, event);
2021-01-27 18:14:03 +03:00
}
std::string
mtx::accessors::transaction_id(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventTransactionId{}, event);
}
2019-12-27 23:49:55 +03:00
int64_t
mtx::accessors::filesize(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventFilesize{}, event);
2019-12-27 23:49:55 +03:00
}
uint64_t
mtx::accessors::media_height(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventMediaHeight{}, event);
2019-12-27 23:49:55 +03:00
}
uint64_t
mtx::accessors::media_width(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(EventMediaWidth{}, event);
2019-12-27 23:49:55 +03:00
}
2020-05-30 17:37:51 +03:00
nlohmann::json
mtx::accessors::serialize_event(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit([](const auto &e) { return nlohmann::json(e); }, event);
2020-05-30 17:37:51 +03:00
}
2021-02-01 00:41:43 +03:00
bool
mtx::accessors::is_state_event(const mtx::events::collections::TimelineEvents &event)
{
2021-09-18 01:22:33 +03:00
return std::visit(IsStateEvent{}, event);
2021-02-01 00:41:43 +03:00
}