1 | // SPDX-FileCopyrightText: 2019 Black Hat <bhat@encom.eu.org> |
2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
3 | |
4 | #include "roompowerlevelsevent.h" |
5 | |
6 | using namespace Quotient; |
7 | |
8 | // The default values used below are defined in |
9 | // https://spec.matrix.org/v1.3/client-server-api/#mroompower_levels |
10 | PowerLevelsEventContent::PowerLevelsEventContent(const QJsonObject& json) : |
11 | invite(json["invite" _ls].toInt(defaultValue: 50)), |
12 | kick(json["kick" _ls].toInt(defaultValue: 50)), |
13 | ban(json["ban" _ls].toInt(defaultValue: 50)), |
14 | redact(json["redact" _ls].toInt(defaultValue: 50)), |
15 | events(fromJson<QHash<QString, int>>(json: json["events" _ls])), |
16 | eventsDefault(json["events_default" _ls].toInt(defaultValue: 0)), |
17 | stateDefault(json["state_default" _ls].toInt(defaultValue: 0)), |
18 | users(fromJson<QHash<QString, int>>(json: json["users" _ls])), |
19 | usersDefault(json["users_default" _ls].toInt(defaultValue: 0)), |
20 | notifications(Notifications{.room: json["notifications" _ls].toObject()["room" _ls].toInt(defaultValue: 50)}) |
21 | {} |
22 | |
23 | QJsonObject PowerLevelsEventContent::toJson() const |
24 | { |
25 | QJsonObject o; |
26 | o.insert(QStringLiteral("invite" ), value: invite); |
27 | o.insert(QStringLiteral("kick" ), value: kick); |
28 | o.insert(QStringLiteral("ban" ), value: ban); |
29 | o.insert(QStringLiteral("redact" ), value: redact); |
30 | o.insert(QStringLiteral("events" ), value: Quotient::toJson(pod: events)); |
31 | o.insert(QStringLiteral("events_default" ), value: eventsDefault); |
32 | o.insert(QStringLiteral("state_default" ), value: stateDefault); |
33 | o.insert(QStringLiteral("users" ), value: Quotient::toJson(pod: users)); |
34 | o.insert(QStringLiteral("users_default" ), value: usersDefault); |
35 | o.insert(QStringLiteral("notifications" ), |
36 | value: QJsonObject { { "room"_ls , notifications.room } }); |
37 | return o; |
38 | } |
39 | |
40 | int RoomPowerLevelsEvent::powerLevelForEvent(const QString& eventTypeId) const |
41 | { |
42 | return events().value(key: eventTypeId, defaultValue: eventsDefault()); |
43 | } |
44 | |
45 | int RoomPowerLevelsEvent::powerLevelForState(const QString& eventTypeId) const |
46 | { |
47 | return events().value(key: eventTypeId, defaultValue: stateDefault()); |
48 | } |
49 | |
50 | int RoomPowerLevelsEvent::powerLevelForUser(const QString& userId) const |
51 | { |
52 | return users().value(key: userId, defaultValue: usersDefault()); |
53 | } |
54 | |