matrixion/src/timeline/Permissions.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "Permissions.h"
#include "Cache_p.h"
#include "MatrixClient.h"
#include "TimelineModel.h"
Permissions::Permissions(QString roomId, QObject *parent)
: QObject(parent)
2021-12-28 20:44:26 +03:00
, roomId_(std::move(roomId))
{
2021-09-18 01:22:33 +03:00
invalidate();
}
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;
}
bool
Permissions::canInvite()
{
2021-09-18 01:22:33 +03:00
return pl.user_level(http::client()->user_id().to_string()) >= pl.invite;
}
bool
Permissions::canBan()
{
2021-09-18 01:22:33 +03:00
return pl.user_level(http::client()->user_id().to_string()) >= pl.ban;
}
bool
Permissions::canKick()
{
2021-09-18 01:22:33 +03:00
return pl.user_level(http::client()->user_id().to_string()) >= pl.kick;
}
bool
Permissions::canRedact()
{
2021-09-18 01:22:33 +03:00
return pl.user_level(http::client()->user_id().to_string()) >= pl.redact;
}
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))));
}
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))));
}
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);
}