move detection code to nheko namespace and fix a few other bugs

This commit is contained in:
targetakhil 2021-04-17 22:58:04 +05:30
parent eb13f7c169
commit 06e12a0a16
6 changed files with 80 additions and 114 deletions

View File

@ -11,32 +11,8 @@
#include <type_traits>
namespace {
struct nonesuch
{
~nonesuch() = delete;
nonesuch(nonesuch const &) = delete;
void operator=(nonesuch const &) = delete;
};
namespace detail {
template<class Default, class AlwaysVoid, template<class...> class Op, class... Args>
struct detector
{
using value_t = std::false_type;
using type = Default;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
{
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
template<template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
using is_detected = typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t;
struct IsStateEvent
{

View File

@ -11,6 +11,32 @@
#include <mtx/events/collections.hpp>
namespace nheko {
struct nonesuch
{
~nonesuch() = delete;
nonesuch(nonesuch const &) = delete;
void operator=(nonesuch const &) = delete;
};
namespace detail {
template<class Default, class AlwaysVoid, template<class...> class Op, class... Args>
struct detector
{
using value_t = std::false_type;
using type = Default;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
{
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
}
namespace mtx::accessors {
std::string
event_id(const mtx::events::collections::TimelineEvents &event);

View File

@ -829,7 +829,7 @@ TimelineModel::forwardMessage(QString eventId, QString roomId)
if (!e)
return;
emit forwardToRoom(e, roomId, cache::isRoomEncrypted(room_id_.toStdString()));
emit forwardToRoom(e, roomId);
}
void

View File

@ -323,9 +323,7 @@ signals:
void roomNameChanged();
void roomTopicChanged();
void roomAvatarUrlChanged();
void forwardToRoom(mtx::events::collections::TimelineEvents *e,
QString roomId,
bool sentFromEncrypted);
void forwardToRoom(mtx::events::collections::TimelineEvents *e, QString roomId);
private:
template<typename T>

View File

@ -621,68 +621,63 @@ TimelineViewManager::focusTimeline()
void
TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEvents *e,
QString roomId,
bool sentFromEncrypted)
QString roomId)
{
auto elem = *e;
auto room = models.find(roomId);
auto messageType = mtx::accessors::msg_type(elem);
auto content = mtx::accessors::url(elem);
if (sentFromEncrypted) {
std::optional<mtx::crypto::EncryptedFile> encryptionInfo =
mtx::accessors::file(elem);
auto elem = *e;
auto room = models.find(roomId);
auto content = mtx::accessors::url(elem);
std::optional<mtx::crypto::EncryptedFile> encryptionInfo = mtx::accessors::file(elem);
if (encryptionInfo) {
http::client()->download(
content,
[this, roomId, e, encryptionInfo](const std::string &res,
const std::string &content_type,
const std::string &originalFilename,
mtx::http::RequestErr err) {
const std::string &content_type,
const std::string &originalFilename,
mtx::http::RequestErr err) {
if (err) {
return;
}
assert(encryptionInfo);
auto data = mtx::crypto::to_string(
mtx::crypto::decrypt_file(res, encryptionInfo.value()));
auto data = mtx::crypto::to_string(
mtx::crypto::decrypt_file(res, encryptionInfo.value()));
http::client()->upload(
data,
content_type,
originalFilename,
[this, roomId, e](const mtx::responses::ContentURI &res,
mtx::http::RequestErr err) mutable {
if (err) {
nhlog::net()->warn("failed to upload media: {} {} ({})",
err->matrix_error.error,
to_string(err->matrix_error.errcode),
static_cast<int>(err->status_code));
return;
}
http::client()->upload(
data,
content_type,
originalFilename,
[this, roomId, e](const mtx::responses::ContentURI &res,
mtx::http::RequestErr err) mutable {
if (err) {
nhlog::net()->warn("failed to upload media: {} {} ({})",
err->matrix_error.error,
to_string(err->matrix_error.errcode),
static_cast<int>(err->status_code));
return;
}
std::visit(
[this, roomId, e, url = res.content_uri](auto ev) {
if constexpr (mtx::events::message_content_to_type<
decltype(ev.content)> ==
mtx::events::EventType::RoomMessage) {
if constexpr (messageWithFileAndUrl(ev)) {
ev.content.relations.relations
.clear();
ev.content.file.reset();
ev.content.url = url;
}
std::visit(
[this, roomId, e, url = res.content_uri](auto ev) {
if constexpr (mtx::events::message_content_to_type<
decltype(ev.content)> ==
mtx::events::EventType::RoomMessage) {
if constexpr (messageWithFileAndUrl(ev)) {
ev.content.relations.relations.clear();
ev.content.file.reset();
ev.content.url = url;
auto room = models.find(roomId);
room.value()->sendMessageEvent(
ev.content,
mtx::events::EventType::RoomMessage);
}
},
*e);
});
auto room = models.find(roomId);
room.value()->sendMessageEvent(
ev.content,
mtx::events::EventType::RoomMessage);
}
}
},
*e);
});
return;
return;
});
return;

View File

@ -32,33 +32,6 @@ class UserSettings;
class ChatPage;
class DeviceVerificationFlow;
struct nonesuch
{
~nonesuch() = delete;
nonesuch(nonesuch const &) = delete;
void operator=(nonesuch const &) = delete;
};
namespace detail {
template<class Default, class AlwaysVoid, template<class...> class Op, class... Args>
struct detector
{
using value_t = std::false_type;
using type = Default;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
{
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
template<template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
class TimelineViewManager : public QObject
{
Q_OBJECT
@ -175,15 +148,17 @@ public slots:
void backToRooms() { emit showRoomList(); }
QObject *completerFor(QString completerName, QString roomId = "");
void forwardMessageToRoom(mtx::events::collections::TimelineEvents *e,
QString roomId,
bool sentFromEncrypted);
void forwardMessageToRoom(mtx::events::collections::TimelineEvents *e, QString roomId);
private slots:
void openImageOverlayInternal(QString eventId, QImage img);
private:
template<class Content>
template<template<class...> class Op, class... Args>
using is_detected =
typename nheko::detail::detector<nheko::nonesuch, void, Op, Args...>::value_t;
template<class Content>
using f_t = decltype(Content::file);
template<class Content>
@ -192,11 +167,7 @@ private:
template<typename T>
static constexpr bool messageWithFileAndUrl(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<f_t, T>::value && is_detected<u_t, T>::value) {
return true;
}
return false;
return is_detected<f_t, T>::value && is_detected<u_t, T>::value;
}
private: