summary refs log tree commit diff
path: root/src/Utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Utils.cpp')
-rw-r--r--src/Utils.cpp63
1 files changed, 63 insertions, 0 deletions
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<mtx::events::account_data::Direct>( + [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<RoomMember> members) +{ + http::client()->get_account_data<mtx::events::account_data::Direct>( + [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); + }); + }); +}