2021-05-02 19:01:18 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-05-02 19:01:18 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "Permissions.h"
|
|
|
|
|
|
|
|
#include "Cache_p.h"
|
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "TimelineModel.h"
|
|
|
|
|
2021-06-12 20:17:31 +03:00
|
|
|
Permissions::Permissions(QString roomId, QObject *parent)
|
2021-05-02 19:01:18 +03:00
|
|
|
: QObject(parent)
|
2021-12-28 20:44:26 +03:00
|
|
|
, roomId_(std::move(roomId))
|
2021-05-02 19:01:18 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
invalidate();
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Permissions::invalidate()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
pl = cache::client()
|
|
|
|
->getStateEvent<mtx::events::state::PowerLevels>(roomId_.toStdString())
|
|
|
|
.value_or(mtx::events::StateEvent<mtx::events::state::PowerLevels>{})
|
|
|
|
.content;
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Permissions::canInvite()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >= pl.invite;
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Permissions::canBan()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >= pl.ban;
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Permissions::canKick()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >= pl.kick;
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Permissions::canRedact()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >= pl.redact;
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
Permissions::canChange(int eventType)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >=
|
|
|
|
pl.state_level(to_string(
|
|
|
|
qml_mtx_events::fromRoomEventType(static_cast<qml_mtx_events::EventType>(eventType))));
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
|
|
|
bool
|
|
|
|
Permissions::canSend(int eventType)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >=
|
|
|
|
pl.event_level(to_string(
|
|
|
|
qml_mtx_events::fromRoomEventType(static_cast<qml_mtx_events::EventType>(eventType))));
|
2021-05-02 19:01:18 +03:00
|
|
|
}
|
2021-09-02 04:15:07 +03:00
|
|
|
|
2022-05-07 03:30:16 +03:00
|
|
|
int
|
|
|
|
Permissions::defaultLevel()
|
|
|
|
{
|
2022-05-07 12:14:28 +03:00
|
|
|
return pl.users_default;
|
2022-05-07 03:30:16 +03:00
|
|
|
}
|
|
|
|
int
|
|
|
|
Permissions::redactLevel()
|
|
|
|
{
|
2022-05-07 12:14:28 +03:00
|
|
|
return pl.redact;
|
2022-05-07 03:30:16 +03:00
|
|
|
}
|
|
|
|
int
|
|
|
|
Permissions::changeLevel(int eventType)
|
|
|
|
{
|
2022-05-07 12:14:28 +03:00
|
|
|
return pl.state_level(to_string(
|
|
|
|
qml_mtx_events::fromRoomEventType(static_cast<qml_mtx_events::EventType>(eventType))));
|
2022-05-07 03:30:16 +03:00
|
|
|
}
|
|
|
|
int
|
|
|
|
Permissions::sendLevel(int eventType)
|
|
|
|
{
|
2022-05-07 12:14:28 +03:00
|
|
|
return pl.event_level(to_string(
|
|
|
|
qml_mtx_events::fromRoomEventType(static_cast<qml_mtx_events::EventType>(eventType))));
|
2022-05-07 03:30:16 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 04:15:07 +03:00
|
|
|
bool
|
|
|
|
Permissions::canPingRoom()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return pl.user_level(http::client()->user_id().to_string()) >=
|
|
|
|
pl.notification_level(mtx::events::state::notification_keys::room);
|
2021-09-02 04:15:07 +03:00
|
|
|
}
|