Debian Codegraph @

mtxclient/include/mtx/pushrules.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Pushrules and notification settings.
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 <compare>
13 #include <string>
14 #include <variant>
15 #include <vector>
16
17 #include "mtx/events/common.hpp"
18 #include "mtx/events/power_levels.hpp"
19
20 namespace mtx {
21 namespace events {
22 namespace collections {
23 struct TimelineEvents;
24 }
25 }
26
27 //! Namespace for the pushrules specific endpoints.
28 namespace pushrules {
29 //! A condition to match pushrules on.
30 struct PushCondition
31 {
32 //! Required. The kind of condition to apply. See conditions for more information on the
33 //! allowed kinds and how they work.
34 std::string kind;
35 //! Required for event_match conditions. The dot- separated field of the event to match.
36 //!
37 //! Required for sender_notification_permission conditions. The field in the power level
38 //! event the user needs a minimum power level for. Fields must be specified under the
39 //! notifications property in the power level event's content.
40 std::string key;
41 //! Required for event_match conditions. The glob- style pattern to match against. Patterns
42 //! with no special glob characters should be treated as having asterisks prepended and
43 //! appended when testing the condition.
44 std::string pattern;
45 //! Required for event_property_is and event_property_contains conditions. A non compound json
46 //! value.
47 std::optional<std::variant<std::string, std::int64_t, bool, std::nullptr_t>> value;
48 //! Required for room_member_count conditions. A decimal integer optionally prefixed by one
49 //! of, ==, <, >, >= or <=. A prefix of < matches rooms where the member count is strictly
50 //! less than the given number and so forth. If no prefix is present, this parameter
51 //! defaults to ==.
52 std::string is;
53
54 //! The relation type to match on. Only valid for `im.nheko.msc3664.related_event_match`
55 //! conditions.
56 mtx::common::RelationType rel_type = mtx::common::RelationType::Unsupported;
57 //! Wether to match fallback relations or not.
58 bool include_fallback = false;
59
60 friend void to_json(nlohmann::json &obj, const PushCondition &condition);
61 friend void from_json(const nlohmann::json &obj, PushCondition &condition);
62 };
63 //! Namespace for the different push actions.
64 namespace actions {
65 //! Notify the user.
66 struct notify
67 {
68 bool operator==(const notify &) const noexcept = default;
69 };
70 //! Don't notify the user.
71 struct dont_notify
72 {
73 bool operator==(const dont_notify &) const noexcept = default;
74 };
75 /// @brief This enables notifications for matching events but activates homeserver specific
76 /// behaviour to intelligently coalesce multiple events into a single notification.
77 ///
78 /// Not all homeservers may support this. Those that do not support it should treat it as the notify
79 /// action.
80 struct coalesce
81 {
82 bool operator==(const coalesce &) const noexcept = default;
83 };
84 //! Play a sound.
85 struct set_tweak_sound
86 {
87 //! The sound to play.
88 std::string value = "default";
89
90 bool operator==(const set_tweak_sound &) const noexcept = default;
91 };
92 //! Highlight the message.
93 struct set_tweak_highlight
94 {
95 bool value = true;
96
97 bool operator==(const set_tweak_highlight &) const noexcept = default;
98 };
99
100 //! A collection for the different actions.
101 using Action = std::variant<actions::notify,
102 actions::dont_notify,
103 actions::coalesce,
104 actions::set_tweak_sound,
105 actions::set_tweak_highlight>;
106
107 void
108 to_json(nlohmann::json &obj, const Action &action);
109
110 void
111 from_json(const nlohmann::json &obj, Action &action);
112
113 //! A list of actions.
114 struct Actions
115 {
116 std::vector<Action> actions;
117
118 friend void to_json(nlohmann::json &obj, const Actions &action);
119 friend void from_json(const nlohmann::json &obj, Actions &action);
120
121 bool operator==(const Actions &) const noexcept = default;
122 };
123 }
124
125 //! A pushrule defining the notification behaviour for a message.
126 struct PushRule
127 {
128 //! Required. Whether this is a default rule, or has been set explicitly.
129 bool default_ = false;
130 //! Required. Whether the push rule is enabled or not.
131 bool enabled = true;
132 //! Required. The actions to perform when this rule is matched.
133 std::vector<actions::Action> actions;
134 //! Required. The ID of this rule.
135 std::string rule_id;
136 //! The glob-style pattern to match against. Only applicable to content rules.
137 std::string pattern;
138 //! The conditions that must hold true for an event in order for a rule to be applied to an
139 //! event. A rule with no conditions always matches. Only applicable to underride and
140 //! override rules.
141 std::vector<PushCondition> conditions;
142
143 friend void to_json(nlohmann::json &obj, const PushRule &condition);
144 friend void from_json(const nlohmann::json &obj, PushRule &condition);
145 };
146
147 //! All the pushrules to evaluate for events.
148 struct Ruleset
149 {
150 //! see https://matrix.org/docs/spec/client_server/latest#push-rules
151 //
152 // A push rule is a single rule that states under what conditions an event should be passed
153 // onto a push gateway and how the notification should be presented. There are different
154 // "kinds" of push rules and each rule has an associated priority. Every push rule MUST have
155 // a kind and rule_id. The rule_id is a unique string within the kind of rule and its'
156 // scope: rule_ids do not need to be unique between rules of the same kind on different
157 // devices. Rules may have extra keys depending on the value of kind. The different kinds of
158 // rule in descending order of priority are:
159 std::vector<PushRule> override_;
160 std::vector<PushRule> content;
161 std::vector<PushRule> room;
162 std::vector<PushRule> sender;
163 std::vector<PushRule> underride;
164
165 friend void to_json(nlohmann::json &obj, const Ruleset &condition);
166 friend void from_json(const nlohmann::json &obj, Ruleset &condition);
167 };
168
169 //! The global ruleset applied to all events.
170 struct GlobalRuleset
171 {
172 //! The actual ruleset.
173 Ruleset global;
174
175 friend void to_json(nlohmann::json &obj, const GlobalRuleset &set);
176 friend void from_json(const nlohmann::json &obj, GlobalRuleset &set);
177 };
178
179 //! The response for queries, if a specific ruleset is enabled.
180 struct Enabled
181 {
182 bool enabled = true;
183
184 friend void to_json(nlohmann::json &obj, const Enabled &enabled);
185 friend void from_json(const nlohmann::json &obj, Enabled &enabled);
186 };
187
188 //! An optimized structure to calculate notifications for events.
189 ///
190 /// You will want to cache this for as long as possible (until the pushrules change), since
191 /// constructing this is somewhat expensive.
192 class PushRuleEvaluator
193 {
194 public:
195 //! Construct a new push evaluator. Pass the current set of pushrules to evaluate.
196 PushRuleEvaluator(const Ruleset &rules);
197 ~PushRuleEvaluator();
198
199 //! Additional room information needed to evaluate push rules.
200 struct RoomContext
201 {
202 //! The displayname of the user in the room.
203 std::string user_display_name;
204 //! the membercount of the room
205 std::size_t member_count = 0;
206 //! The powerlevels event in this room
207 mtx::events::state::PowerLevels power_levels;
208 };
209
210 //! Evaluate the pushrules for @event .
211 ///
212 /// You need to have the room_id set for the event.
213 /// `relatedEvents` is a mapping of rel_type to event. Pass all the events that are related to
214 /// by this event here.
215 /// \returns the actions to apply.
216 [[nodiscard]] std::vector<actions::Action> evaluate(
217 const mtx::events::collections::TimelineEvents &event,
218 const RoomContext &ctx,
219 const std::vector<std::pair<mtx::common::Relation, mtx::events::collections::TimelineEvents>>
220 &relatedEvents) const;
221
222 private:
223 struct OptimizedRules;
224 std::unique_ptr<OptimizedRules> rules;
225 };
226 }
227 }