From 3d92e8ae606024f5054795281ccd488abee1795a Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 21 Nov 2021 05:23:38 +0100 Subject: [PATCH] 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(-) 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