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 <string>
13 #include <variant>
14 #include <vector>
15 
16 namespace mtx {
17 //! Namespace for the pushrules specific endpoints.
18 namespace pushrules {
19 //! A condition to match pushrules on.
20 struct PushCondition
21 {
22     //! Required. The kind of condition to apply. See conditions for more information on the
23     //! allowed kinds and how they work.
24     std::string kind;
25     //! Required for event_match conditions. The dot- separated field of the event to match.
26     //!
27     //! Required for sender_notification_permission conditions. The field in the power level
28     //! event the user needs a minimum power level for. Fields must be specified under the
29     //! notifications property in the power level event's content.
30     std::string key;
31     //! Required for event_match conditions. The glob- style pattern to match against. Patterns
32     //! with no special glob characters should be treated as having asterisks prepended and
33     //! appended when testing the condition.
34     std::string pattern;
35     //! Required for room_member_count conditions. A decimal integer optionally prefixed by one
36     //! of, ==, <, >, >= or <=. A prefix of < matches rooms where the member count is strictly
37     //! less than the given number and so forth. If no prefix is present, this parameter
38     //! defaults to ==.
39     std::string is;
40 };
41 
42 void
43 to_json(nlohmann::json &obj, const PushCondition &condition);
44 
45 void
46 from_json(const nlohmann::json &obj, PushCondition &condition);
47 
48 //! Namespace for the different push actions.
49 namespace actions {
50 //! Notify the user.
51 struct notify
52 {};
53 //! Don't notify the user.
54 struct dont_notify
55 {};
56 /// @brief This enables notifications for matching events but activates homeserver specific
57 /// behaviour to intelligently coalesce multiple events into a single notification.
58 ///
59 /// Not all homeservers may support this. Those that do not support it should treat it as the notify
60 /// action.
61 struct coalesce
62 {};
63 //! Play a sound.
64 struct set_tweak_sound
65 {
66     //! The sound to play.
67     std::string value = "default";
68 };
69 //! Highlight the message.
70 struct set_tweak_highlight
71 {
72     bool value = true;
73 };
74 
75 //! A collection for the different actions.
76 using Action = std::variant<actions::notify,
77                             actions::dont_notify,
78                             actions::coalesce,
79                             actions::set_tweak_sound,
80                             actions::set_tweak_highlight>;
81 
82 void
83 to_json(nlohmann::json &obj, const Action &action);
84 
85 void
86 from_json(const nlohmann::json &obj, Action &action);
87 
88 //! A list of actions.
89 struct Actions
90 {
91     std::vector<Action> actions;
92 };
93 void
94 to_json(nlohmann::json &obj, const Actions &action);
95 
96 void
97 from_json(const nlohmann::json &obj, Actions &action);
98 }
99 
100 //! A pushrule defining the notification behaviour for a message.
101 struct PushRule
102 {
103     //! Required. Whether this is a default rule, or has been set explicitly.
104     bool default_ = false;
105     //! Required. Whether the push rule is enabled or not.
106     bool enabled = true;
107     //! Required. The actions to perform when this rule is matched.
108     std::vector<actions::Action> actions;
109     //! Required. The ID of this rule.
110     std::string rule_id;
111     //! The glob-style pattern to match against. Only applicable to content rules.
112     std::string pattern;
113     //! The conditions that must hold true for an event in order for a rule to be applied to an
114     //! event. A rule with no conditions always matches. Only applicable to underride and
115     //! override rules.
116     std::vector<PushCondition> conditions;
117 };
118 
119 void
120 to_json(nlohmann::json &obj, const PushRule &condition);
121 
122 void
123 from_json(const nlohmann::json &obj, PushRule &condition);
124 
125 //! All the pushrules to evaluate for events.
126 struct Ruleset
127 {
128     //! see https://matrix.org/docs/spec/client_server/latest#push-rules
129     //
130     // A push rule is a single rule that states under what conditions an event should be passed
131     // onto a push gateway and how the notification should be presented. There are different
132     // "kinds" of push rules and each rule has an associated priority. Every push rule MUST have
133     // a kind and rule_id. The rule_id is a unique string within the kind of rule and its'
134     // scope: rule_ids do not need to be unique between rules of the same kind on different
135     // devices. Rules may have extra keys depending on the value of kind. The different kinds of
136     // rule in descending order of priority are:
137     std::vector<PushRule> override_;
138     std::vector<PushRule> content;
139     std::vector<PushRule> room;
140     std::vector<PushRule> sender;
141     std::vector<PushRule> underride;
142 };
143 
144 void
145 to_json(nlohmann::json &obj, const Ruleset &condition);
146 
147 void
148 from_json(const nlohmann::json &obj, Ruleset &condition);
149 
150 //! The global ruleset applied to all events.
151 struct GlobalRuleset
152 {
153     //! The actual ruleset.
154     Ruleset global;
155 };
156 
157 void
158 to_json(nlohmann::json &obj, const GlobalRuleset &set);
159 
160 void
161 from_json(const nlohmann::json &obj, GlobalRuleset &set);
162 
163 //! The response for queries, if a specific ruleset is enabled.
164 struct Enabled
165 {
166     bool enabled = true;
167 };
168 
169 void
170 to_json(nlohmann::json &obj, const Enabled &enabled);
171 
172 void
173 from_json(const nlohmann::json &obj, Enabled &enabled);
174 }
175 }
176