2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-02-10 18:52:42 +03:00
|
|
|
#include "RoomSettings.h"
|
|
|
|
|
2021-02-11 21:09:11 +03:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QStandardPaths>
|
2022-01-13 01:26:50 +03:00
|
|
|
#include <mtx/events/event_type.hpp>
|
2021-02-10 18:52:42 +03:00
|
|
|
#include <mtx/responses/common.hpp>
|
|
|
|
#include <mtx/responses/media.hpp>
|
2022-01-13 01:26:50 +03:00
|
|
|
#include <mtxclient/http/client.hpp>
|
2021-02-10 18:52:42 +03:00
|
|
|
|
|
|
|
#include "Cache.h"
|
2022-01-11 09:38:27 +03:00
|
|
|
#include "Cache_p.h"
|
2021-02-12 10:18:12 +03:00
|
|
|
#include "Config.h"
|
2021-02-13 21:29:42 +03:00
|
|
|
#include "Logging.h"
|
2021-02-11 17:24:09 +03:00
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
2021-02-10 18:52:42 +03:00
|
|
|
|
|
|
|
RoomSettings::RoomSettings(QString roomid, QObject *parent)
|
2021-02-14 08:56:10 +03:00
|
|
|
: QObject(parent)
|
|
|
|
, roomid_{std::move(roomid)}
|
2021-02-10 18:52:42 +03:00
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
connect(this, &RoomSettings::accessJoinRulesChanged, &RoomSettings::allowedRoomsChanged);
|
2021-09-18 01:22:33 +03:00
|
|
|
retrieveRoomInfo();
|
|
|
|
|
|
|
|
// get room setting notifications
|
|
|
|
http::client()->get_pushrules(
|
|
|
|
"global",
|
|
|
|
"override",
|
|
|
|
roomid_.toStdString(),
|
2022-10-04 00:57:30 +03:00
|
|
|
[this](const mtx::pushrules::PushRule &rule, mtx::http::RequestErr err) {
|
2021-09-18 01:22:33 +03:00
|
|
|
if (err) {
|
|
|
|
if (err->status_code == 404)
|
|
|
|
http::client()->get_pushrules(
|
|
|
|
"global",
|
|
|
|
"room",
|
|
|
|
roomid_.toStdString(),
|
2022-10-04 00:57:30 +03:00
|
|
|
[this](const mtx::pushrules::PushRule &rule, mtx::http::RequestErr err) {
|
2021-09-18 01:22:33 +03:00
|
|
|
if (err) {
|
|
|
|
notifications_ = 2; // all messages
|
|
|
|
emit notificationsChanged();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule.enabled) {
|
|
|
|
notifications_ = 1; // mentions only
|
|
|
|
emit notificationsChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule.enabled) {
|
|
|
|
notifications_ = 0; // muted
|
|
|
|
emit notificationsChanged();
|
|
|
|
} else {
|
|
|
|
notifications_ = 2; // all messages
|
|
|
|
emit notificationsChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// access rules
|
2022-09-19 22:39:37 +03:00
|
|
|
this->accessRules_ = cache::client()
|
|
|
|
->getStateEvent<mtx::events::state::JoinRules>(roomid_.toStdString())
|
|
|
|
.value_or(mtx::events::StateEvent<mtx::events::state::JoinRules>{})
|
|
|
|
.content;
|
|
|
|
using mtx::events::state::AccessState;
|
|
|
|
guestRules_ = info_.guest_access ? AccessState::CanJoin : AccessState::Forbidden;
|
2021-09-18 01:22:33 +03:00
|
|
|
emit accessJoinRulesChanged();
|
2022-09-19 22:39:37 +03:00
|
|
|
|
|
|
|
this->allowedRoomsModel = new RoomSettingsAllowedRoomsModel(this);
|
2021-02-10 18:52:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
RoomSettings::roomName() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return utils::replaceEmoji(QString::fromStdString(info_.name).toHtmlEscaped());
|
2021-02-13 16:38:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
RoomSettings::roomTopic() const
|
|
|
|
{
|
2021-12-29 08:01:38 +03:00
|
|
|
return utils::replaceEmoji(
|
|
|
|
utils::linkifyMessage(QString::fromStdString(info_.topic)
|
|
|
|
.toHtmlEscaped()
|
|
|
|
.replace(QLatin1String("\n"), QLatin1String("<br>"))));
|
2021-02-10 18:52:42 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 07:45:31 +03:00
|
|
|
QString
|
|
|
|
RoomSettings::plainRoomName() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(info_.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
RoomSettings::plainRoomTopic() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(info_.topic);
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:53:33 +03:00
|
|
|
QString
|
|
|
|
RoomSettings::roomId() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return roomid_;
|
2021-02-11 18:53:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
RoomSettings::roomVersion() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString::fromStdString(info_.version);
|
2021-02-11 18:53:33 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:09:11 +03:00
|
|
|
bool
|
|
|
|
RoomSettings::isLoading() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return isLoading_;
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
RoomSettings::roomAvatarUrl()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return QString::fromStdString(info_.avatar_url);
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 18:53:33 +03:00
|
|
|
int
|
|
|
|
RoomSettings::memberCount() const
|
|
|
|
{
|
2022-10-26 02:10:35 +03:00
|
|
|
return static_cast<int>(info_.member_count);
|
2021-02-11 18:53:33 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:52:42 +03:00
|
|
|
void
|
|
|
|
RoomSettings::retrieveRoomInfo()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
usesEncryption_ = cache::isRoomEncrypted(roomid_.toStdString());
|
|
|
|
info_ = cache::singleRoomInfo(roomid_.toStdString());
|
|
|
|
} catch (const lmdb::error &) {
|
|
|
|
nhlog::db()->warn("failed to retrieve room info from cache: {}", roomid_.toStdString());
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RoomSettings::notifications()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return notifications_;
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-19 22:39:37 +03:00
|
|
|
bool
|
|
|
|
RoomSettings::privateAccess() const
|
|
|
|
{
|
|
|
|
return accessRules_.join_rule != mtx::events::state::JoinRule::Public;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RoomSettings::guestAccess() const
|
2021-02-11 17:24:09 +03:00
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
return guestRules_ == mtx::events::state::AccessState::CanJoin;
|
|
|
|
}
|
|
|
|
bool
|
|
|
|
RoomSettings::knockingEnabled() const
|
|
|
|
{
|
|
|
|
return accessRules_.join_rule == mtx::events::state::JoinRule::Knock ||
|
|
|
|
accessRules_.join_rule == mtx::events::state::JoinRule::KnockRestricted;
|
|
|
|
}
|
|
|
|
bool
|
|
|
|
RoomSettings::restrictedEnabled() const
|
|
|
|
{
|
|
|
|
return accessRules_.join_rule == mtx::events::state::JoinRule::Restricted ||
|
|
|
|
accessRules_.join_rule == mtx::events::state::JoinRule::KnockRestricted;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList
|
|
|
|
RoomSettings::allowedRooms() const
|
|
|
|
{
|
|
|
|
QStringList rooms;
|
2022-10-26 02:10:35 +03:00
|
|
|
assert(accessRules_.allow.size() < std::numeric_limits<int>::max());
|
|
|
|
rooms.reserve(static_cast<int>(accessRules_.allow.size()));
|
2022-09-19 22:39:37 +03:00
|
|
|
for (const auto &e : accessRules_.allow) {
|
|
|
|
if (e.type == mtx::events::state::JoinAllowanceType::RoomMembership)
|
|
|
|
rooms.push_back(QString::fromStdString(e.room_id));
|
|
|
|
}
|
|
|
|
return rooms;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
RoomSettings::setAllowedRooms(QStringList rooms)
|
|
|
|
{
|
|
|
|
accessRules_.allow.clear();
|
|
|
|
for (const auto &e : rooms) {
|
|
|
|
accessRules_.allow.push_back(
|
|
|
|
{mtx::events::state::JoinAllowanceType::RoomMembership, e.toStdString()});
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettings::enableEncryption()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (usesEncryption_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto room_id = roomid_.toStdString();
|
|
|
|
http::client()->enable_encryption(
|
|
|
|
room_id, [room_id, this](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
int status_code = static_cast<int>(err->status_code);
|
|
|
|
nhlog::net()->warn("failed to enable encryption in room ({}): {} {}",
|
|
|
|
room_id,
|
|
|
|
err->matrix_error.error,
|
|
|
|
status_code);
|
|
|
|
emit displayError(tr("Failed to enable encryption: %1")
|
|
|
|
.arg(QString::fromStdString(err->matrix_error.error)));
|
|
|
|
usesEncryption_ = false;
|
|
|
|
emit encryptionChanged();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->info("enabled encryption on room ({})", room_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
usesEncryption_ = true;
|
|
|
|
emit encryptionChanged();
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RoomSettings::canChangeJoinRules() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
return cache::hasEnoughPowerLevel(
|
|
|
|
{EventType::RoomJoinRules}, roomid_.toStdString(), utils::localUser().toStdString());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("lmdb error: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:09:11 +03:00
|
|
|
bool
|
2022-03-30 07:45:31 +03:00
|
|
|
RoomSettings::canChangeName() const
|
2021-02-11 21:09:11 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
2022-03-30 07:45:31 +03:00
|
|
|
return cache::hasEnoughPowerLevel(
|
|
|
|
{EventType::RoomName}, roomid_.toStdString(), utils::localUser().toStdString());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("lmdb error: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RoomSettings::canChangeTopic() const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return cache::hasEnoughPowerLevel(
|
|
|
|
{EventType::RoomTopic}, roomid_.toStdString(), utils::localUser().toStdString());
|
2021-09-18 01:22:33 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("lmdb error: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RoomSettings::canChangeAvatar() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
try {
|
|
|
|
return cache::hasEnoughPowerLevel(
|
|
|
|
{EventType::RoomAvatar}, roomid_.toStdString(), utils::localUser().toStdString());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("lmdb error: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:24:09 +03:00
|
|
|
bool
|
|
|
|
RoomSettings::isEncryptionEnabled() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return usesEncryption_;
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:31:25 +03:00
|
|
|
bool
|
|
|
|
RoomSettings::supportsKnocking() const
|
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
const static std::set<std::string_view> unsupported{
|
|
|
|
"",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
};
|
|
|
|
return !unsupported.count(info_.version);
|
2021-08-18 00:31:25 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
RoomSettings::supportsRestricted() const
|
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
const static std::set<std::string_view> unsupported{
|
|
|
|
"",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
"7",
|
|
|
|
};
|
|
|
|
return !unsupported.count(info_.version);
|
2021-08-18 00:31:25 +03:00
|
|
|
}
|
2022-06-18 01:17:24 +03:00
|
|
|
bool
|
|
|
|
RoomSettings::supportsKnockRestricted() const
|
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
const static std::set<std::string_view> unsupported{
|
|
|
|
"",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
"7",
|
|
|
|
"8",
|
|
|
|
"9",
|
|
|
|
};
|
|
|
|
return !unsupported.count(info_.version);
|
2022-06-18 01:17:24 +03:00
|
|
|
}
|
2021-08-18 00:31:25 +03:00
|
|
|
|
2021-02-11 17:24:09 +03:00
|
|
|
void
|
|
|
|
RoomSettings::changeNotifications(int currentIndex)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
notifications_ = currentIndex;
|
|
|
|
|
|
|
|
std::string room_id = roomid_.toStdString();
|
|
|
|
if (notifications_ == 0) {
|
|
|
|
// mute room
|
|
|
|
// delete old rule first, then add new rule
|
|
|
|
mtx::pushrules::PushRule rule;
|
|
|
|
rule.actions = {mtx::pushrules::actions::dont_notify{}};
|
|
|
|
mtx::pushrules::PushCondition condition;
|
|
|
|
condition.kind = "event_match";
|
|
|
|
condition.key = "room_id";
|
|
|
|
condition.pattern = room_id;
|
|
|
|
rule.conditions = {condition};
|
|
|
|
|
|
|
|
http::client()->put_pushrules(
|
|
|
|
"global", "override", room_id, rule, [room_id](mtx::http::RequestErr &err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->error("failed to set pushrule for room {}: {} {}",
|
|
|
|
room_id,
|
|
|
|
static_cast<int>(err->status_code),
|
|
|
|
err->matrix_error.error);
|
|
|
|
http::client()->delete_pushrules(
|
|
|
|
"global", "room", room_id, [room_id](mtx::http::RequestErr &) {});
|
|
|
|
});
|
|
|
|
} else if (notifications_ == 1) {
|
|
|
|
// mentions only
|
|
|
|
// delete old rule first, then add new rule
|
|
|
|
mtx::pushrules::PushRule rule;
|
|
|
|
rule.actions = {mtx::pushrules::actions::dont_notify{}};
|
|
|
|
http::client()->put_pushrules(
|
|
|
|
"global", "room", room_id, rule, [room_id](mtx::http::RequestErr &err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->error("failed to set pushrule for room {}: {} {}",
|
|
|
|
room_id,
|
|
|
|
static_cast<int>(err->status_code),
|
|
|
|
err->matrix_error.error);
|
|
|
|
http::client()->delete_pushrules(
|
|
|
|
"global", "override", room_id, [room_id](mtx::http::RequestErr &) {});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// all messages
|
|
|
|
http::client()->delete_pushrules(
|
|
|
|
"global", "override", room_id, [room_id](mtx::http::RequestErr &) {
|
|
|
|
http::client()->delete_pushrules(
|
|
|
|
"global", "room", room_id, [room_id](mtx::http::RequestErr &) {});
|
|
|
|
});
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-09-19 22:39:37 +03:00
|
|
|
RoomSettings::changeAccessRules(bool private_,
|
|
|
|
bool guestsAllowed,
|
|
|
|
bool knockingAllowed,
|
|
|
|
bool restrictedAllowed)
|
2021-02-11 17:24:09 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace mtx::events::state;
|
|
|
|
|
2022-09-19 22:39:37 +03:00
|
|
|
auto guest_access = [guestsAllowed]() -> state::GuestAccess {
|
2021-09-18 01:22:33 +03:00
|
|
|
state::GuestAccess event;
|
|
|
|
|
2022-09-19 22:39:37 +03:00
|
|
|
if (guestsAllowed)
|
2021-09-18 01:22:33 +03:00
|
|
|
event.guest_access = state::AccessState::CanJoin;
|
|
|
|
else
|
|
|
|
event.guest_access = state::AccessState::Forbidden;
|
|
|
|
|
|
|
|
return event;
|
2022-09-19 22:39:37 +03:00
|
|
|
}();
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-09-19 22:39:37 +03:00
|
|
|
auto join_rule = [this, private_, knockingAllowed, restrictedAllowed]() -> state::JoinRules {
|
|
|
|
state::JoinRules event = this->accessRules_;
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-09-19 22:39:37 +03:00
|
|
|
if (!private_) {
|
2021-09-18 01:22:33 +03:00
|
|
|
event.join_rule = state::JoinRule::Public;
|
2022-09-19 22:39:37 +03:00
|
|
|
} else if (knockingAllowed && restrictedAllowed && supportsKnockRestricted()) {
|
|
|
|
event.join_rule = state::JoinRule::KnockRestricted;
|
|
|
|
} else if (knockingAllowed && supportsKnocking()) {
|
2021-09-18 01:22:33 +03:00
|
|
|
event.join_rule = state::JoinRule::Knock;
|
2022-09-19 22:39:37 +03:00
|
|
|
} else if (restrictedAllowed && supportsRestricted()) {
|
2021-09-18 01:22:33 +03:00
|
|
|
event.join_rule = state::JoinRule::Restricted;
|
2022-09-19 22:39:37 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
event.join_rule = state::JoinRule::Invite;
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return event;
|
2022-09-19 22:39:37 +03:00
|
|
|
}();
|
2021-02-11 17:24:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
updateAccessRules(roomid_.toStdString(), join_rule, guest_access);
|
2021-02-11 17:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 07:45:31 +03:00
|
|
|
void
|
2022-10-04 00:57:30 +03:00
|
|
|
RoomSettings::changeName(const QString &name)
|
2022-03-30 07:45:31 +03:00
|
|
|
{
|
|
|
|
// Check if the values are changed from the originals.
|
|
|
|
auto newName = name.trimmed().toStdString();
|
|
|
|
|
|
|
|
if (newName == info_.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
|
|
|
auto proxy = std::make_shared<ThreadProxy>();
|
2022-10-04 00:57:30 +03:00
|
|
|
connect(proxy.get(), &ThreadProxy::nameEventSent, this, [this](const QString &newRoomName) {
|
2022-03-30 07:45:31 +03:00
|
|
|
this->info_.name = newRoomName.toStdString();
|
|
|
|
emit roomNameChanged();
|
|
|
|
});
|
|
|
|
connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError);
|
|
|
|
|
|
|
|
state::Name body;
|
|
|
|
body.name = newName;
|
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
roomid_.toStdString(),
|
|
|
|
body,
|
|
|
|
[proxy, newName](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
emit proxy->error(QString::fromStdString(err->matrix_error.error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit proxy->nameEventSent(QString::fromStdString(newName));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-10-04 00:57:30 +03:00
|
|
|
RoomSettings::changeTopic(const QString &topic)
|
2022-03-30 07:45:31 +03:00
|
|
|
{
|
|
|
|
// Check if the values are changed from the originals.
|
|
|
|
auto newTopic = topic.trimmed().toStdString();
|
|
|
|
|
|
|
|
if (newTopic == info_.topic) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
|
|
|
auto proxy = std::make_shared<ThreadProxy>();
|
2022-10-04 00:57:30 +03:00
|
|
|
connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this](const QString &newRoomTopic) {
|
2022-03-30 07:45:31 +03:00
|
|
|
this->info_.topic = newRoomTopic.toStdString();
|
|
|
|
emit roomTopicChanged();
|
|
|
|
});
|
|
|
|
connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError);
|
|
|
|
|
|
|
|
state::Topic body;
|
|
|
|
body.topic = newTopic;
|
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
roomid_.toStdString(),
|
|
|
|
body,
|
|
|
|
[proxy, newTopic](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
emit proxy->error(QString::fromStdString(err->matrix_error.error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit proxy->topicEventSent(QString::fromStdString(newTopic));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-11 17:24:09 +03:00
|
|
|
void
|
|
|
|
RoomSettings::updateAccessRules(const std::string &room_id,
|
|
|
|
const mtx::events::state::JoinRules &join_rule,
|
|
|
|
const mtx::events::state::GuestAccess &guest_access)
|
|
|
|
{
|
2022-09-19 22:39:37 +03:00
|
|
|
isLoading_ = true;
|
|
|
|
allowedRoomsModified_ = false;
|
2021-09-18 01:22:33 +03:00
|
|
|
emit loadingChanged();
|
2022-09-19 22:39:37 +03:00
|
|
|
emit allowedRoomsModifiedChanged();
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
room_id,
|
|
|
|
join_rule,
|
2022-09-19 22:39:37 +03:00
|
|
|
[this, room_id, guest_access, join_rule](const mtx::responses::EventId &,
|
|
|
|
mtx::http::RequestErr err) {
|
2021-09-18 01:22:33 +03:00
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to send m.room.join_rule: {} {}",
|
|
|
|
static_cast<int>(err->status_code),
|
|
|
|
err->matrix_error.error);
|
|
|
|
emit displayError(QString::fromStdString(err->matrix_error.error));
|
|
|
|
isLoading_ = false;
|
|
|
|
emit loadingChanged();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
room_id,
|
|
|
|
guest_access,
|
2022-09-19 22:39:37 +03:00
|
|
|
[this, join_rule](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
2021-09-18 01:22:33 +03:00
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to send m.room.guest_access: {} {}",
|
|
|
|
static_cast<int>(err->status_code),
|
|
|
|
err->matrix_error.error);
|
|
|
|
emit displayError(QString::fromStdString(err->matrix_error.error));
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
isLoading_ = false;
|
|
|
|
emit loadingChanged();
|
2022-09-19 22:39:37 +03:00
|
|
|
|
|
|
|
this->accessRules_ = join_rule;
|
|
|
|
emit accessJoinRulesChanged();
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
|
|
|
});
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettings::stopLoading()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
isLoading_ = false;
|
|
|
|
emit loadingChanged();
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettings::avatarChanged()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
retrieveRoomInfo();
|
|
|
|
emit avatarUrlChanged();
|
2021-02-11 21:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettings::updateAvatar()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const QString picturesFolder =
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
|
|
|
const QString fileName = QFileDialog::getOpenFileName(
|
|
|
|
nullptr, tr("Select an avatar"), picturesFolder, tr("All Files (*)"));
|
|
|
|
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType mime = db.mimeTypeForFile(fileName, QMimeDatabase::MatchContent);
|
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
const auto format = mime.name().split(QStringLiteral("/"))[0];
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
QFile file{fileName, this};
|
2021-12-29 06:28:08 +03:00
|
|
|
if (format != QLatin1String("image")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
emit displayError(tr("The selected file is not an image"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
emit displayError(tr("Error while reading file: %1").arg(file.errorString()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLoading_ = true;
|
|
|
|
emit loadingChanged();
|
|
|
|
|
|
|
|
// Events emitted from the http callbacks (different threads) will
|
|
|
|
// be queued back into the UI thread through this proxy object.
|
|
|
|
auto proxy = std::make_shared<ThreadProxy>();
|
|
|
|
connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError);
|
|
|
|
connect(proxy.get(), &ThreadProxy::stopLoading, this, &RoomSettings::stopLoading);
|
|
|
|
|
|
|
|
const auto bin = file.peek(file.size());
|
|
|
|
const auto payload = std::string(bin.data(), bin.size());
|
|
|
|
const auto dimensions = QImageReader(&file).size();
|
|
|
|
|
|
|
|
// First we need to create a new mxc URI
|
|
|
|
// (i.e upload media to the Matrix content repository) for the new avatar.
|
|
|
|
http::client()->upload(
|
|
|
|
payload,
|
|
|
|
mime.name().toStdString(),
|
|
|
|
QFileInfo(fileName).fileName().toStdString(),
|
|
|
|
[proxy = std::move(proxy),
|
|
|
|
dimensions,
|
|
|
|
payload,
|
|
|
|
mimetype = mime.name().toStdString(),
|
|
|
|
size = payload.size(),
|
|
|
|
room_id = roomid_.toStdString(),
|
|
|
|
content = std::move(bin)](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
emit proxy->stopLoading();
|
|
|
|
emit proxy->error(tr("Failed to upload image: %s")
|
|
|
|
.arg(QString::fromStdString(err->matrix_error.error)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
|
|
|
state::Avatar avatar_event;
|
|
|
|
avatar_event.image_info.w = dimensions.width();
|
|
|
|
avatar_event.image_info.h = dimensions.height();
|
|
|
|
avatar_event.image_info.mimetype = mimetype;
|
|
|
|
avatar_event.image_info.size = size;
|
|
|
|
avatar_event.url = res.content_uri;
|
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
room_id,
|
|
|
|
avatar_event,
|
|
|
|
[content = std::move(content),
|
|
|
|
proxy = std::move(proxy)](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
emit proxy->error(tr("Failed to upload image: %s")
|
2021-02-11 21:09:11 +03:00
|
|
|
.arg(QString::fromStdString(err->matrix_error.error)));
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-02-11 17:24:09 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
emit proxy->stopLoading();
|
|
|
|
});
|
|
|
|
});
|
2021-02-22 23:35:11 +03:00
|
|
|
}
|
2022-09-19 22:39:37 +03:00
|
|
|
|
|
|
|
RoomSettingsAllowedRoomsModel::RoomSettingsAllowedRoomsModel(RoomSettings *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, settings(parent)
|
|
|
|
{
|
|
|
|
this->allowedRoomIds = settings->allowedRooms();
|
|
|
|
|
|
|
|
auto prIds = cache::client()->getParentRoomIds(settings->roomId().toStdString());
|
|
|
|
for (const auto &prId : prIds) {
|
|
|
|
this->parentSpaces.insert(QString::fromStdString(prId));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->listedRoomIds = QStringList(parentSpaces.begin(), parentSpaces.end());
|
|
|
|
|
2022-09-19 22:57:25 +03:00
|
|
|
for (const auto &e : qAsConst(this->allowedRoomIds)) {
|
2022-09-19 22:39:37 +03:00
|
|
|
if (!this->parentSpaces.count(e))
|
|
|
|
this->listedRoomIds.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
RoomSettingsAllowedRoomsModel::roleNames() const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
{Roles::Name, "name"},
|
|
|
|
{Roles::IsAllowed, "allowed"},
|
|
|
|
{Roles::IsSpaceParent, "isParent"},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RoomSettingsAllowedRoomsModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return listedRoomIds.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
RoomSettingsAllowedRoomsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() > listedRoomIds.size())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (role == Roles::IsAllowed) {
|
|
|
|
return allowedRoomIds.contains(listedRoomIds.at(index.row()));
|
|
|
|
} else if (role == Roles::IsSpaceParent) {
|
|
|
|
return parentSpaces.find(listedRoomIds.at(index.row())) != parentSpaces.cend();
|
|
|
|
} else if (role == Roles::Name) {
|
|
|
|
auto id = listedRoomIds.at(index.row());
|
|
|
|
auto info = cache::client()->getRoomInfo({
|
|
|
|
id.toStdString(),
|
|
|
|
});
|
|
|
|
if (!info.empty())
|
|
|
|
return QString::fromStdString(info[id].name);
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RoomSettingsAllowedRoomsModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() > listedRoomIds.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (role != Roles::IsAllowed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (value.toBool()) {
|
|
|
|
if (!allowedRoomIds.contains(listedRoomIds.at(index.row())))
|
|
|
|
allowedRoomIds.push_back(listedRoomIds.at(index.row()));
|
|
|
|
} else {
|
|
|
|
allowedRoomIds.removeAll(listedRoomIds.at(index.row()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettingsAllowedRoomsModel::addRoom(QString room)
|
|
|
|
{
|
|
|
|
if (listedRoomIds.contains(room) || !room.startsWith('!'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), listedRoomIds.size(), listedRoomIds.size());
|
|
|
|
listedRoomIds.push_back(room);
|
|
|
|
allowedRoomIds.push_back(room);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomSettings::applyAllowedFromModel()
|
|
|
|
{
|
|
|
|
this->setAllowedRooms(this->allowedRoomsModel->allowedRoomIds);
|
|
|
|
this->allowedRoomsModified_ = true;
|
|
|
|
emit allowedRoomsModifiedChanged();
|
|
|
|
}
|