From 5ef3250994d21954ae3736f42611268bbe03ad0e Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 20 Nov 2021 22:48:04 +0100 Subject: Add a filter for direct chats fixes #317 --- src/ChatPage.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/ChatPage.cpp') diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 77a8edcf..4b37864b 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -176,7 +176,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) this, &ChatPage::initializeViews, view_manager_, - [this](const mtx::responses::Rooms &rooms) { view_manager_->sync(rooms); }, + [this](const mtx::responses::Sync &sync) { view_manager_->sync(sync); }, Qt::QueuedConnection); connect(this, &ChatPage::initializeEmptyViews, @@ -184,12 +184,12 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) &TimelineViewManager::initializeRoomlist); connect( this, &ChatPage::chatFocusChanged, view_manager_, &TimelineViewManager::chatFocusChanged); - connect(this, &ChatPage::syncUI, this, [this](const mtx::responses::Rooms &rooms) { - view_manager_->sync(rooms); + connect(this, &ChatPage::syncUI, this, [this](const mtx::responses::Sync &sync) { + view_manager_->sync(sync); static unsigned int prevNotificationCount = 0; unsigned int notificationCount = 0; - for (const auto &room : rooms.join) { + for (const auto &room : sync.rooms.join) { notificationCount += room.second.unread_notifications.notification_count; } @@ -583,7 +583,7 @@ ChatPage::startInitialSync() olm::handle_to_device_messages(res.to_device.events); - emit initializeViews(std::move(res.rooms)); + emit initializeViews(std::move(res)); emit initializeMentions(cache::getTimelineMentions()); cache::calculateRoomReadStatus(); @@ -622,7 +622,7 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string auto updates = cache::getRoomInfo(cache::client()->roomsWithStateUpdates(res)); - emit syncUI(res.rooms); + emit syncUI(std::move(res)); // if we process a lot of syncs (1 every 200ms), this means we clean the // db every 100s -- cgit 1.5.1 From 4dc5b647c6faea3d73fc7f6a4b61b392087015dc Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 21 Nov 2021 05:04:48 +0100 Subject: Pretty error printing --- CMakeLists.txt | 4 +- io.github.NhekoReborn.Nheko.yaml | 6 +- src/ChatPage.cpp | 8 +-- src/MatrixClient.h | 116 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 13 deletions(-) (limited to 'src/ChatPage.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d84df5f..2352b799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -413,7 +413,7 @@ if(USE_BUNDLED_MTXCLIENT) FetchContent_Declare( MatrixClient GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git - GIT_TAG v0.6.0 + GIT_TAG ffc1d3e13a507fa501966b2d7e9d4eda881f6bf4 ) set(BUILD_LIB_EXAMPLES OFF CACHE INTERNAL "") set(BUILD_LIB_TESTS OFF CACHE INTERNAL "") @@ -710,7 +710,7 @@ if(USE_BUNDLED_COEURL) FetchContent_Declare( coeurl GIT_REPOSITORY https://nheko.im/Nheko-Reborn/coeurl.git - GIT_TAG v0.1.0 + GIT_TAG abafd60d7e9f5cce76c9abad3b2b3dc1382e5349 ) FetchContent_MakeAvailable(coeurl) target_link_libraries(nheko PUBLIC coeurl::coeurl) diff --git a/io.github.NhekoReborn.Nheko.yaml b/io.github.NhekoReborn.Nheko.yaml index 70c3af83..22e52ae8 100644 --- a/io.github.NhekoReborn.Nheko.yaml +++ b/io.github.NhekoReborn.Nheko.yaml @@ -175,8 +175,7 @@ modules: - -Ddefault_library=static name: coeurl sources: - - commit: a08f619adaa1ccd34eb6315d6578eddae0d1cc9b - tag: v0.1.0 + - commit: abafd60d7e9f5cce76c9abad3b2b3dc1382e5349 type: git url: https://nheko.im/nheko-reborn/coeurl.git - config-opts: @@ -187,8 +186,7 @@ modules: buildsystem: cmake-ninja name: mtxclient sources: - - commit: eecc4e93f2137c658014f17cefd62ad569063769 - tag: v0.6.0 + - commit: ffc1d3e13a507fa501966b2d7e9d4eda881f6bf4 type: git url: https://github.com/Nheko-Reborn/mtxclient.git - config-opts: diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 4b37864b..bd85dc75 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -662,8 +662,6 @@ ChatPage::trySync() if (err) { const auto error = QString::fromStdString(err->matrix_error.error); const auto msg = tr("Please try to login again: %1").arg(error); - const auto err_code = mtx::errors::to_string(err->matrix_error.errcode); - const int status_code = static_cast(err->status_code); if ((http::is_logged_in() && (err->matrix_error.errcode == mtx::errors::ErrorCode::M_UNKNOWN_TOKEN || @@ -673,11 +671,7 @@ ChatPage::trySync() return; } - nhlog::net()->error("sync error: {} {} {} {}", - err->parse_error, - status_code, - err->error_code, - err_code); + nhlog::net()->error("sync error: {}", *err); emit tryDelayedSyncCb(); return; } diff --git a/src/MatrixClient.h b/src/MatrixClient.h index 605ba5e0..7d86537b 100644 --- a/src/MatrixClient.h +++ b/src/MatrixClient.h @@ -6,6 +6,10 @@ #include +#include + +#include "Logging.h" + namespace http { mtx::http::Client * client(); @@ -17,3 +21,115 @@ is_logged_in(); void init(); } + +template<> +struct fmt::formatter +{ + // Presentation format: 'f' - fixed, 'e' - exponential. + bool print_network_error = false; + bool print_http_error = false; + bool print_parser_error = false; + bool print_matrix_error = false; + + // Parses format specifications of the form ['f' | 'e']. + constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) + { + // [ctx.begin(), ctx.end()) is a character range that contains a part of + // the format string starting from the format specifications to be parsed, + // e.g. in + // + // fmt::format("{:f} - point of interest", point{1, 2}); + // + // the range will contain "f} - point of interest". The formatter should + // parse specifiers until '}' or the end of the range. In this example + // the formatter should parse the 'f' specifier and return an iterator + // pointing to '}'. + + // Parse the presentation format and store it in the formatter: + auto it = ctx.begin(), end = ctx.end(); + + while (it != end && *it != '}') { + auto tmp = *it++; + + switch (tmp) { + case 'n': + print_matrix_error = true; + break; + case 'h': + print_matrix_error = true; + break; + case 'p': + print_matrix_error = true; + break; + case 'm': + print_matrix_error = true; + break; + default: + throw format_error("invalid format specifier for mtx error"); + } + } + + // Check if reached the end of the range: + if (it != end && *it != '}') + throw format_error("invalid format"); + + // Return an iterator past the end of the parsed range: + return it; + } + + // Formats the point p using the parsed format specification (presentation) + // stored in this formatter. + template + auto format(const mtx::http::ClientError &e, FormatContext &ctx) -> decltype(ctx.out()) + { + // ctx.out() is an output iterator to write to. + bool prepend_comma = false; + format_to(ctx.out(), "("); + if (print_network_error || e.error_code) { + format_to(ctx.out(), "connection: {}", e.error_code_string()); + prepend_comma = true; + } + + if (print_http_error || + (e.status_code != 0 && (e.status_code < 200 || e.status_code >= 300))) { + if (prepend_comma) + format_to(ctx.out(), ", "); + format_to(ctx.out(), "http: {}", e.status_code); + prepend_comma = true; + } + + if (print_parser_error || !e.parse_error.empty()) { + if (prepend_comma) + format_to(ctx.out(), ", "); + format_to(ctx.out(), "parser: {}", e.parse_error); + prepend_comma = true; + } + + if (print_parser_error || + (e.matrix_error.errcode != mtx::errors::ErrorCode::M_UNRECOGNIZED && + !e.matrix_error.error.empty())) { + if (prepend_comma) + format_to(ctx.out(), ", "); + format_to(ctx.out(), + "matrix: {}:'{}'", + to_string(e.matrix_error.errcode), + e.matrix_error.error); + } + + return format_to(ctx.out(), ")"); + } +}; + +template<> +struct fmt::formatter> : formatter +{ + // parse is inherited from formatter. + template + auto format(std::optional c, FormatContext &ctx) + { + if (!c) + return format_to(ctx.out(), "(no error)"); + else + return formatter::format(*c, ctx); + } +}; -- cgit 1.5.1 From 3d92e8ae606024f5054795281ccd488abee1795a Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 21 Nov 2021 05:23:38 +0100 Subject: Mark rooms as direct chats Either by accepting an invite or manually using /converttodm and revert with /converttoroom. --- src/ChatPage.cpp | 4 +-- src/Utils.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++ src/Utils.h | 7 +++++ src/timeline/InputBar.cpp | 5 ++++ src/timeline/RoomlistModel.cpp | 2 ++ 5 files changed, 79 insertions(+), 2 deletions(-) (limited to 'src/ChatPage.cpp') diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index bd85dc75..c1c7eb7d 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -660,8 +660,8 @@ ChatPage::trySync() http::client()->sync( opts, [this, since = opts.since](const mtx::responses::Sync &res, mtx::http::RequestErr err) { if (err) { - const auto error = QString::fromStdString(err->matrix_error.error); - const auto msg = tr("Please try to login again: %1").arg(error); + const auto error = QString::fromStdString(err->matrix_error.error); + const auto msg = tr("Please try to login again: %1").arg(error); if ((http::is_logged_in() && (err->matrix_error.errcode == mtx::errors::ErrorCode::M_UNKNOWN_TOKEN || diff --git a/src/Utils.cpp b/src/Utils.cpp index 59b02298..dda6f685 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -27,6 +27,7 @@ #include "Cache.h" #include "Config.h" #include "EventAccessors.h" +#include "Logging.h" #include "MatrixClient.h" #include "UserSettingsPage.h" @@ -813,3 +814,65 @@ utils::isReply(const mtx::events::collections::TimelineEvents &e) { return mtx::accessors::relations(e).reply_to().has_value(); } + +void +utils::removeDirectFromRoom(QString roomid) +{ + http::client()->get_account_data( + [roomid](mtx::events::account_data::Direct ev, mtx::http::RequestErr e) { + if (e && e->status_code == 404) + ev = {}; + else if (e) { + nhlog::net()->error("Failed to retrieve m.direct: {}", *e); + return; + } + + auto r = roomid.toStdString(); + + for (auto it = ev.user_to_rooms.begin(); it != ev.user_to_rooms.end();) { + for (auto rit = it->second.begin(); rit != it->second.end();) { + if (r == *rit) + rit = it->second.erase(rit); + else + ++rit; + } + + if (it->second.empty()) + it = ev.user_to_rooms.erase(it); + else + ++it; + } + + http::client()->put_account_data(ev, [r](mtx::http::RequestErr e) { + if (e) + nhlog::net()->error("Failed to update m.direct: {}", *e); + }); + }); +} +void +utils::markRoomAsDirect(QString roomid, std::vector members) +{ + http::client()->get_account_data( + [roomid, members](mtx::events::account_data::Direct ev, mtx::http::RequestErr e) { + if (e && e->status_code == 404) + ev = {}; + else if (e) { + nhlog::net()->error("Failed to retrieve m.direct: {}", *e); + return; + } + + auto local = utils::localUser(); + auto r = roomid.toStdString(); + + for (const auto &m : members) { + if (m.user_id != local) { + ev.user_to_rooms[m.user_id.toStdString()].push_back(r); + } + } + + http::client()->put_account_data(ev, [r](mtx::http::RequestErr e) { + if (e) + nhlog::net()->error("Failed to update m.direct: {}", *e); + }); + }); +} diff --git a/src/Utils.h b/src/Utils.h index da82ec7c..701ec8fc 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -304,4 +305,10 @@ readImage(const QByteArray &data); bool isReply(const mtx::events::collections::TimelineEvents &e); + +void +removeDirectFromRoom(QString roomid); + +void +markRoomAsDirect(QString roomid, std::vector members); } diff --git a/src/timeline/InputBar.cpp b/src/timeline/InputBar.cpp index 44df3411..bd4f59d8 100644 --- a/src/timeline/InputBar.cpp +++ b/src/timeline/InputBar.cpp @@ -645,6 +645,11 @@ InputBar::command(QString command, QString args) return; } nhlog::net()->error("Could not resolve goto: {}", args.toStdString()); + } else if (command == "converttodm") { + utils::markRoomAsDirect(this->room->roomId(), + cache::getMembers(this->room->roomId().toStdString(), 0, -1)); + } else if (command == "converttoroom") { + utils::removeDirectFromRoom(this->room->roomId()); } } diff --git a/src/timeline/RoomlistModel.cpp b/src/timeline/RoomlistModel.cpp index 53fd9498..7d727659 100644 --- a/src/timeline/RoomlistModel.cpp +++ b/src/timeline/RoomlistModel.cpp @@ -627,6 +627,8 @@ RoomlistModel::acceptInvite(QString roomid) if (invites.contains(roomid)) { // Don't remove invite yet, so that we can switch to it ChatPage::instance()->joinRoom(roomid); + utils::markRoomAsDirect(roomid, + cache::client()->getMembersFromInvite(roomid.toStdString(), 0, -1)); } } void -- cgit 1.5.1