Debian Codegraph @

mtxclient/include/mtx/events/power_levels.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief The state event governing the different permissions in a room.
5
6 #if __has_include(<nlohmann/json_fwd.hpp>)
7 #include <nlohmann/json_fwd.hpp>
8 #else
9 #include <nlohmann/json.hpp>
10 #endif
11
12 #include <string>
13 #include <string_view>
14
15 namespace mtx {
16 namespace events {
17 namespace state {
18 //! The type representing a power level.
19 using power_level_t = int64_t;
20
21 //! The default level required for events
22 inline constexpr power_level_t EventsDefault = 0;
23 //! The default level for users.
24 inline constexpr power_level_t UsersDefault = 0;
25 //! The default level required for state events.
26 inline constexpr power_level_t StatesDefault = 50;
27
28 //! The power level usually associated with normal users.
29 inline constexpr power_level_t User = 0;
30 //! The power level usually associated with moderators.
31 inline constexpr power_level_t Moderator = 50;
32 //! The power level usually associated with admins.
33 inline constexpr power_level_t Admin = 100;
34
35 //! different predefined keys for notification levels
36 namespace notification_keys {
37 //! The level required to trigger an @room notification. Defaults to 50 if unspecified.
38 inline constexpr std::string_view room = "room";
39 }
40
41 /// @brief Content for the `m.room.power_levels` state event.
42 ///
43 /// This event specifies the minimum level a user must have in
44 /// order to perform a certain action. It also specifies the
45 /// levels of each user in the room.
46 struct PowerLevels
47 {
48 //! Returns the power_level for a given event type.
49 [[nodiscard]] inline power_level_t event_level(const std::string &event_type) const
50 {
51 if (events.find(event_type) == events.end())
52 return events_default;
53
54 return events.at(event_type);
55 }
56
57 //! Returns the power_level for a given event type.
58 [[nodiscard]] inline power_level_t state_level(const std::string &event_type) const
59 {
60 if (events.find(event_type) == events.end())
61 return state_default;
62
63 return events.at(event_type);
64 }
65
66 //! Returns the power_level for a given user id.
67 [[nodiscard]] inline power_level_t user_level(const std::string &user_id) const
68 {
69 if (users.find(user_id) == users.end())
70 return users_default;
71
72 return users.at(user_id);
73 }
74
75 [[nodiscard]] inline power_level_t notification_level(std::string_view notification_key) const
76 {
77 if (auto it = notifications.find(notification_key); it != notifications.end())
78 return it->second;
79 else if (notification_key == notification_keys::room)
80 return 50;
81 else // spec doesn't actually specify that?
82 return 50;
83 }
84
85 //! The level required to ban a user. Defaults to **50** if unspecified.
86 power_level_t ban = Moderator;
87 //! The level required to invite a user.
88 //! Defaults to **50** if unspecified.
89 power_level_t invite = Moderator;
90 //! The level required to kick a user.
91 //! Defaults to **50** if unspecified.
92 power_level_t kick = Moderator;
93 //! The level required to redact an event.
94 //! Defaults to **50** if unspecified.
95 power_level_t redact = Moderator;
96 //! The default level required to send message events.
97 //! Defaults to **0** if unspecified.
98 power_level_t events_default = User;
99 //! The default power level for every user in the room,
100 //! unless their user_id is mentioned in the users key.
101 //! Defaults to **0** if unspecified.
102 power_level_t users_default = User;
103 //! The default level required to send state events.
104 power_level_t state_default = Moderator;
105 //! The level required to send specific event types.
106 //! This is a mapping from event type to power level required.
107 std::map<std::string, power_level_t, std::less<>> events;
108 //! The power levels for specific users.
109 //! This is a mapping from user_id to power level for that user.
110 std::map<std::string, power_level_t, std::less<>> users;
111 //! The power level requirements for specific notification types. This is a mapping from key
112 //! to power level for that notifications key.
113 std::map<std::string, power_level_t, std::less<>> notifications;
114
115 friend void from_json(const nlohmann::json &obj, PowerLevels &power_levels);
116 friend void to_json(nlohmann::json &obj, const PowerLevels &power_levels);
117 };
118
119 } // namespace state
120 } // namespace events
121 } // namespace mtx