Mark rooms as direct chats

Either by accepting an invite or manually using /converttodm and revert
with /converttoroom.
This commit is contained in:
Nicolas Werner 2021-11-21 05:23:38 +01:00
parent 4dc5b647c6
commit 3d92e8ae60
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
5 changed files with 79 additions and 2 deletions

View file

@ -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 ||

View file

@ -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);
});
});
}

View file

@ -6,6 +6,7 @@
#include <variant>
#include <CacheStructs.h>
#include <QCoreApplication>
#include <QDateTime>
#include <QPixmap>
@ -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<RoomMember> members);
}

View file

@ -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());
}
}

View file

@ -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