Debian Codegraph @

mtxclient/lib/structs/pushrules.cpp

raw symbols SCIP
1 #include "mtx/pushrules.hpp"
2
3 #include <charconv>
4
5 #include <nlohmann/json.hpp>
6 #include <re2/re2.h>
7
8 #include "mtx/events/collections.hpp"
9 #include "mtx/log.hpp"
10
11 using non_compound_json_value = std::variant<std::string, std::int64_t, bool, std::nullptr_t>;
12 using push_json_value = std::
13 variant<std::string, std::int64_t, bool, std::nullptr_t, std::vector<non_compound_json_value>>;
14
15 namespace {
16 struct RelatedEvents
17 {
18 std::vector<std::unordered_map<std::string, push_json_value>>
19 fallbacks; //!< fallback related events
20 std::vector<std::unordered_map<std::string, push_json_value>> events; //!< related events
21 };
22 }
23
24 static std::optional<non_compound_json_value>
25 to_non_compound_json_value(const nlohmann::json &val)
26 {
27 switch (val.type()) {
28 case nlohmann::json::value_t::string: {
29 return val.get<std::string>();
30 }
31 case nlohmann::json::value_t::null: {
32 return nullptr;
33 }
34 case nlohmann::json::value_t::boolean: {
35 return val.get<bool>();
36 }
37 case nlohmann::json::value_t::number_integer:
38 case nlohmann::json::value_t::number_unsigned: {
39 return val.get<std::int64_t>();
40 }
41 default:
42 return std::nullopt;
43 }
44 }
45
46 namespace mtx {
47 namespace pushrules {
48
49 void
50 to_json(nlohmann::json &obj, const PushCondition &condition)
51 {
52 obj["kind"] = condition.kind;
53 if (!condition.key.empty())
54 obj["key"] = condition.key;
55 if (!condition.pattern.empty())
56 obj["pattern"] = condition.pattern;
57 if (!condition.is.empty())
58 obj["is"] = condition.is;
59 if (condition.rel_type != mtx::common::RelationType::Unsupported)
60 obj["rel_type"] = condition.rel_type;
61
62 if (condition.value) {
63 std::visit([&obj](const auto &e) { obj["value"] = e; }, *condition.value);
64 }
65 }
66
67 void
68 from_json(const nlohmann::json &obj, PushCondition &condition)
69 {
70 condition.kind = obj["kind"].get<std::string>();
71 condition.key = obj.value("key", "");
72 condition.pattern = obj.value("pattern", "");
73 condition.is = obj.value("is", "");
74 condition.rel_type = obj.value("rel_type", mtx::common::RelationType::Unsupported);
75 condition.include_fallback = obj.value("include_fallback", false);
76
77 if (obj.contains("value"))
78 if (auto val = to_non_compound_json_value(obj.at("value")))
79 condition.value = *val;
80 }
81
82 namespace actions {
83 void
84 to_json(nlohmann::json &obj, const Action &action)
85 {
86 if (std::holds_alternative<notify>(action))
87 obj = "notify";
88 else if (std::holds_alternative<dont_notify>(action))
89 obj = "dont_notify";
90 else if (auto n = std::get_if<set_tweak_sound>(&action)) {
91 obj["set_tweak"] = "sound";
92 obj["value"] = n->value;
93 } else if (auto h = std::get_if<set_tweak_highlight>(&action)) {
94 obj["set_tweak"] = "highlight";
95 if (h->value == false)
96 obj["value"] = false;
97 }
98 }
99
100 void
101 from_json(const nlohmann::json &obj, Action &action)
102 {
103 if (obj.is_string()) {
104 if (obj == "notify")
105 action = notify{};
106 else if (obj == "dont_notify")
107 action = dont_notify{};
108 } else if (obj.contains("set_tweak")) {
109 if (obj["set_tweak"] == "sound")
110 action = set_tweak_sound{obj.value("value", "default")};
111 else if (obj["set_tweak"] == "highlight")
112 action = set_tweak_highlight{obj.value("value", true)};
113 }
114 }
115
116 void
117 to_json(nlohmann::json &obj, const Actions &action)
118 {
119 obj["actions"] = action.actions;
120 }
121
122 void
123 from_json(const nlohmann::json &obj, Actions &action)
124 {
125 action.actions = obj["actions"].get<std::vector<Action>>();
126 }
127 }
128
129 void
130 to_json(nlohmann::json &obj, const PushRule &rule)
131 {
132 if (rule.default_)
133 obj["default"] = rule.default_;
134
135 if (!rule.enabled)
136 obj["enabled"] = rule.enabled;
137
138 for (const auto &action : rule.actions)
139 obj["actions"].push_back(action);
140
141 if (!rule.rule_id.empty())
142 obj["rule_id"] = rule.rule_id;
143
144 if (!rule.pattern.empty())
145 obj["pattern"] = rule.pattern;
146
147 for (const auto &condition : rule.conditions)
148 obj["conditions"].push_back(condition);
149 }
150
151 void
152 from_json(const nlohmann::json &obj, PushRule &rule)
153 {
154 rule.rule_id = obj.value("rule_id", "");
155 rule.default_ = obj.value("default", false);
156 rule.enabled = obj.value("enabled", true);
157
158 if (obj.contains("actions"))
159 for (const auto &action : obj["actions"])
160 rule.actions.push_back(action.get<actions::Action>());
161
162 rule.pattern = obj.value("pattern", "");
163
164 if (obj.contains("conditions"))
165 for (const auto &condition : obj["conditions"])
166 rule.conditions.push_back(condition.get<PushCondition>());
167 }
168
169 void
170 to_json(nlohmann::json &obj, const Ruleset &set)
171 {
172 obj["override"] = set.override_;
173 obj["content"] = set.content;
174 obj["room"] = set.room;
175 obj["sender"] = set.sender;
176 obj["underride"] = set.underride;
177 }
178
179 void
180 from_json(const nlohmann::json &obj, Ruleset &set)
181 {
182 if (obj.contains("override"))
183 for (const auto &e : obj["override"])
184 set.override_.push_back(e.get<PushRule>());
185 if (obj.contains("content"))
186 for (const auto &e : obj["content"])
187 set.content.push_back(e.get<PushRule>());
188 if (obj.contains("room"))
189 for (const auto &e : obj["room"])
190 set.room.push_back(e.get<PushRule>());
191 if (obj.contains("sender"))
192 for (const auto &e : obj["sender"])
193 set.sender.push_back(e.get<PushRule>());
194 if (obj.contains("underride"))
195 for (const auto &e : obj["underride"])
196 set.underride.push_back(e.get<PushRule>());
197 }
198 void
199 to_json(nlohmann::json &obj, const GlobalRuleset &set)
200 {
201 obj["global"] = set.global;
202 }
203
204 void
205 from_json(const nlohmann::json &obj, GlobalRuleset &set)
206 {
207 set.global = obj["global"].get<Ruleset>();
208 }
209
210 void
211 to_json(nlohmann::json &obj, const Enabled &enabled)
212 {
213 obj["enabled"] = enabled.enabled;
214 }
215
216 void
217 from_json(const nlohmann::json &obj, Enabled &enabled)
218 {
219 enabled.enabled = obj.value("enabled", true);
220 }
221
222 struct PushRuleEvaluator::OptimizedRules
223 {
224 //! The individual rule to apply
225 struct OptimizedRule
226 {
227 //! a pattern condition to match
228 struct PatternCondition
229 {
230 std::unique_ptr<re2::RE2> pattern; //!< the pattern
231 std::string field; //!< the field to match with pattern
232
233 [[nodiscard]] bool matches(
234 const std::unordered_map<std::string, push_json_value> &ev) const
235 {
236 if (auto it = ev.find(field); it != ev.end()) {
237 if (pattern && std::holds_alternative<std::string>(it->second)) {
238 if (field == "content.body") {
239 if (!re2::RE2::PartialMatch(std::get<std::string>(it->second),
240 *pattern))
241 return false;
242 } else {
243 if (!re2::RE2::FullMatch(std::get<std::string>(it->second), *pattern))
244 return false;
245 }
246 }
247 } else {
248 return false;
249 }
250
251 // We have some internal rules, which just match on a field being present.
252 return true;
253 }
254 };
255 // TODO(Nico): Sort by field for faster matching?
256 std::vector<PatternCondition> patterns; //!< conditions that match on a field
257
258 //! a event_property_is condition to match
259 struct IsCondition
260 {
261 non_compound_json_value value; //!< the pattern
262 std::string field; //!< the field to match with pattern
263
264 [[nodiscard]] bool matches(
265 const std::unordered_map<std::string, push_json_value> &ev) const
266 {
267 if (auto it = ev.find(field); it != ev.end()) {
268 return std::visit(
269 [it](const auto &e) { return it->second == push_json_value(e); }, value);
270 }
271
272 return false;
273 }
274 };
275 std::vector<IsCondition> is; //!< conditions that match on a field being an exact value
276 //!
277 //! a event_property_contains condition to match
278 struct ContainsCondition
279 {
280 non_compound_json_value value; //!< the pattern
281 std::string field; //!< the field to match with pattern
282
283 [[nodiscard]] bool matches(
284 const std::unordered_map<std::string, push_json_value> &ev) const
285 {
286 if (auto it = ev.find(field); it != ev.end()) {
287 if (auto arr = std::get_if<std::vector<non_compound_json_value>>(&it->second)) {
288 return std::find(arr->begin(), arr->end(), value) != arr->end();
289 }
290 }
291
292 return false;
293 }
294 };
295 std::vector<ContainsCondition> contains; //!< conditions that match on arrays
296
297 bool no_mentions_field = false; //!< rule only matches if no content.m\\.mentions is present
298
299 //! a pattern condition to match on a related event
300 struct RelatedEventCondition
301 {
302 PatternCondition ev_match;
303 mtx::common::RelationType rel_type = mtx::common::RelationType::Unsupported;
304 bool include_fallbacks = false;
305 };
306 std::vector<RelatedEventCondition>
307 related_event_patterns; //!< conditions that match on fields of the related event.
308
309 //! a member count condition
310 struct MemberCountCondition
311 {
312 //! the count to compare against
313 std::size_t count = 0;
314 //! the comparison operation
315 enum Comp
316 {
317 Eq, //< ==
318 Lt, //< <
319 Le, //< <=
320 Ge, //< >=
321 Gt, //< >
322 };
323
324 Comp op = Comp::Eq;
325 };
326 std::vector<MemberCountCondition> membercounts; //< conditions that match on member count
327
328 std::vector<std::string> notification_levels;
329
330 //! evaluate contains_display_name condition
331 bool check_displayname = false;
332
333 std::vector<actions::Action> actions; //< the actions to apply on match
334
335 [[nodiscard]] bool matches(
336 const std::unordered_map<std::string, push_json_value> &ev,
337 const PushRuleEvaluator::RoomContext &ctx,
338 const std::map<mtx::common::RelationType, RelatedEvents> &relatedEventsFlat) const
339 {
340 if (no_mentions_field && ev.contains("content.m\\.mentions")) {
341 return false;
342 }
343
344 for (const auto &cond : membercounts) {
345 if (![&cond, &ctx] {
346 switch (cond.op) {
347 case MemberCountCondition::Eq:
348 return ctx.member_count == cond.count;
349 case MemberCountCondition::Le:
350 return ctx.member_count <= cond.count;
351 case MemberCountCondition::Ge:
352 return ctx.member_count >= cond.count;
353 case MemberCountCondition::Lt:
354 return ctx.member_count < cond.count;
355 case MemberCountCondition::Gt:
356 return ctx.member_count > cond.count;
357 default:
358 return false;
359 }
360 }())
361 return false;
362 }
363
364 if (!notification_levels.empty()) {
365 auto sender_ = ev.find("sender");
366 if (sender_ == ev.end())
367 return false;
368
369 auto sender_level =
370 ctx.power_levels.user_level(std::get<std::string>(sender_->second));
371
372 for (const auto &n : notification_levels) {
373 if (sender_level < ctx.power_levels.notification_level(n))
374 return false;
375 }
376 }
377
378 for (const auto &cond : patterns) {
379 if (!cond.matches(ev))
380 return false;
381 }
382 for (const auto &cond : is) {
383 if (!cond.matches(ev))
384 return false;
385 }
386 for (const auto &cond : contains) {
387 if (!cond.matches(ev))
388 return false;
389 }
390
391 for (const auto &cond : related_event_patterns) {
392 bool matched = false;
393 for (const auto &[rel_type, rel_ev] : relatedEventsFlat) {
394 if (cond.rel_type == rel_type) {
395 for (const auto &e : rel_ev.events) {
396 if (cond.ev_match.field.empty() || !cond.ev_match.pattern ||
397 cond.ev_match.matches(e)) {
398 matched = true;
399 break;
400 }
401 }
402 if (cond.include_fallbacks) {
403 for (const auto &e : rel_ev.fallbacks) {
404 if (cond.ev_match.field.empty() || !cond.ev_match.pattern ||
405 cond.ev_match.matches(e)) {
406 matched = true;
407 break;
408 }
409 }
410 }
411 }
412 }
413 if (!matched)
414 return false;
415 }
416
417 if (check_displayname) {
418 if (ctx.user_display_name.empty())
419 return false;
420
421 if (auto it = ev.find("content.body");
422 it != ev.end() && std::holds_alternative<std::string>(it->second)) {
423 re2::RE2::Options opts;
424 opts.set_case_sensitive(false);
425
426 if (!re2::RE2::PartialMatch(
427 std::get<std::string>(it->second),
428 re2::RE2("(\\W|^)" + re2::RE2::QuoteMeta(ctx.user_display_name) +
429 "(\\W|$)",
430 opts)))
431 return false;
432 } else {
433 return false;
434 }
435 }
436
437 return true;
438 }
439 };
440
441 std::vector<OptimizedRule> override_;
442 std::unordered_map<std::string, OptimizedRule> room;
443 std::unordered_map<std::string, OptimizedRule> sender;
444 std::vector<OptimizedRule> content;
445 std::vector<OptimizedRule> underride;
446 };
447
448 static std::unique_ptr<re2::RE2>
449 construct_re_from_pattern(std::string pat, const std::string &field)
450 {
451 pat = re2::RE2::QuoteMeta(pat);
452
453 // Quote also espaces the globs, so we need to match them including the backslash
454 static re2::RE2 matchGlobStar("\\*");
455 re2::RE2::GlobalReplace(&pat, matchGlobStar, ".*");
456
457 static re2::RE2 matchGlobQuest("\\?");
458 re2::RE2::GlobalReplace(&pat, matchGlobQuest, ".");
459
460 re2::RE2::Options opts;
461 opts.set_case_sensitive(false);
462
463 if (field == "content.body")
464 return std::make_unique<re2::RE2>("(\\W|^)" + pat + "(\\W|$)", opts);
465 else
466 return std::make_unique<re2::RE2>(pat, opts);
467 }
468
469 PushRuleEvaluator::~PushRuleEvaluator() = default;
470 PushRuleEvaluator::PushRuleEvaluator(const Ruleset &rules_)
471 : rules(std::make_unique<OptimizedRules>())
472 {
473 // In theory we should check the server version, but we can't access that here and this should
474 // work on all servers for now.
475 bool server_supports_mentions = false;
476 for (const auto &r : rules_.override_) {
477 if (r.rule_id == ".m.rule.is_user_mention") {
478 server_supports_mentions = true;
479 break;
480 }
481 }
482
483 auto add_conditions_to_rule =
484 [server_supports_mentions](OptimizedRules::OptimizedRule &rule,
485 const std::vector<PushCondition> &conditions,
486 std::string_view rule_id) {
487 // These rules should only be enabled, if there is no m.mentions property, but the spec
488 // doesn't actually add such a condition, so let's do it ourselves...
489 if (server_supports_mentions &&
490 (rule_id == ".m.rule.contains_display_name" || rule_id == ".m.rule.roomnotif" ||
491 rule_id == ".m.rule.contains_user_name")) {
492 rule.no_mentions_field = true;
493 }
494
495 for (const auto &cond : conditions) {
496 if (cond.kind == "event_match") {
497 OptimizedRules::OptimizedRule::PatternCondition c;
498 c.field = cond.key;
499 c.pattern = construct_re_from_pattern(cond.pattern, cond.key);
500 if (c.pattern)
501 rule.patterns.push_back(std::move(c));
502 } else if (cond.kind == "event_property_is" && cond.value) {
503 OptimizedRules::OptimizedRule::IsCondition c;
504 c.field = cond.key;
505 c.value = cond.value.value();
506 rule.is.push_back(std::move(c));
507 } else if (cond.kind == "event_property_contains" && cond.value) {
508 OptimizedRules::OptimizedRule::ContainsCondition c;
509 c.field = cond.key;
510 c.value = cond.value.value();
511 rule.contains.push_back(std::move(c));
512 } else if (cond.kind == "im.nheko.msc3664.related_event_match") {
513 OptimizedRules::OptimizedRule::RelatedEventCondition c;
514
515 if (cond.rel_type != mtx::common::RelationType::Unsupported) {
516 c.rel_type = cond.rel_type;
517 c.include_fallbacks = cond.include_fallback;
518
519 if (!cond.key.empty() && !cond.pattern.empty()) {
520 c.ev_match.field = cond.key;
521 c.ev_match.pattern = construct_re_from_pattern(cond.pattern, cond.key);
522 }
523 rule.related_event_patterns.push_back(std::move(c));
524 } else {
525 mtx::utils::log::log()->info(
526 "Skipping rel_event_match rule with unknown rel_type.");
527 return false;
528 }
529 } else if (cond.kind == "contains_display_name") {
530 rule.check_displayname = true;
531 } else if (cond.kind == "room_member_count") {
532 OptimizedRules::OptimizedRule::MemberCountCondition c;
533 std::string_view is = cond.is;
534 if (is.starts_with("==")) {
535 c.op = c.Comp::Eq;
536 is = is.substr(2);
537 } else if (is.starts_with(">=")) {
538 c.op = c.Comp::Ge;
539 is = is.substr(2);
540 } else if (is.starts_with("<=")) {
541 c.op = c.Comp::Le;
542 is = is.substr(2);
543 } else if (is.starts_with('<')) {
544 c.op = c.Comp::Lt;
545 is = is.substr(1);
546 } else if (is.starts_with('>')) {
547 c.op = c.Comp::Gt;
548 is = is.substr(1);
549 }
550
551 std::from_chars(is.data(), is.data() + is.size(), c.count);
552 rule.membercounts.push_back(c);
553 } else if (cond.kind == "sender_notification_permission") {
554 rule.notification_levels.push_back(cond.key);
555 } else {
556 mtx::utils::log::log()->info("Skipping rule with unknown condition type: {}",
557 cond.kind);
558 return false;
559 }
560 }
561
562 return true;
563 };
564
565 for (const auto &rule_ : rules_.override_) {
566 if (!rule_.enabled)
567 continue;
568
569 OptimizedRules::OptimizedRule rule;
570 rule.actions = rule_.actions;
571
572 if (!add_conditions_to_rule(rule, rule_.conditions, rule_.rule_id))
573 continue;
574
575 rules->override_.push_back(std::move(rule));
576 }
577
578 for (const auto &rule_ : rules_.underride) {
579 if (!rule_.enabled)
580 continue;
581
582 OptimizedRules::OptimizedRule rule;
583 rule.actions = rule_.actions;
584
585 if (!add_conditions_to_rule(rule, rule_.conditions, rule_.rule_id))
586 continue;
587
588 rules->underride.push_back(std::move(rule));
589 }
590
591 for (const auto &rule_ : rules_.room) {
592 if (!rule_.enabled)
593 continue;
594
595 if (!rule_.rule_id.starts_with("!"))
596 continue;
597
598 OptimizedRules::OptimizedRule rule;
599 rule.actions = rule_.actions;
600 rules->room[rule_.rule_id] = std::move(rule);
601 }
602
603 for (const auto &rule_ : rules_.sender) {
604 if (!rule_.enabled)
605 continue;
606
607 if (!rule_.rule_id.starts_with("@"))
608 continue;
609
610 OptimizedRules::OptimizedRule rule;
611 rule.actions = rule_.actions;
612 rules->sender[rule_.rule_id] = std::move(rule);
613 }
614
615 for (const auto &rule_ : rules_.content) {
616 if (!rule_.enabled)
617 continue;
618
619 // Work around construct sending invalid content rules.
620 // Also this seems like just a sane thing to do, an empty pattern will always match and that
621 // is usually not what you want.
622 if (rule_.pattern.empty())
623 continue;
624
625 OptimizedRules::OptimizedRule rule;
626 rule.actions = rule_.actions;
627
628 std::vector<PushCondition> conditions{
629 PushCondition{.kind = "event_match", .key = "content.body", .pattern = rule_.pattern},
630 };
631
632 if (!add_conditions_to_rule(rule, conditions, rule_.rule_id))
633 continue;
634
635 rules->content.push_back(std::move(rule));
636 }
637 }
638
639 static void
640 flatten_impl(const nlohmann::json &value,
641 std::unordered_map<std::string, push_json_value> &result,
642 const std::string &current_path,
643 int current_depth)
644 {
645 if (current_depth > 100)
646 return;
647
648 auto escape_key = [](std::string input) {
649 for (size_t i = 0; i < input.size(); i++) {
650 if (input[i] == '.') {
651 input.insert(i, 1, '\\');
652 i++;
653 }
654 }
655 return input;
656 };
657
658 switch (value.type()) {
659 case nlohmann::json::value_t::object: {
660 // Workaround to be able to check, if the m.mentions object is present
661 if (current_path == "content.m\\.mentions") {
662 result[current_path] = nullptr;
663 }
664
665 // iterate object and use keys as reference string
666 std::string prefix;
667 if (!current_path.empty())
668 prefix = current_path + ".";
669 for (const auto &element : value.items()) {
670 flatten_impl(
671 element.value(), result, prefix + escape_key(element.key()), current_depth + 1);
672 }
673 break;
674 }
675
676 case nlohmann::json::value_t::array: {
677 std::vector<non_compound_json_value> arr;
678 for (const auto &val : value) {
679 switch (val.type()) {
680 case nlohmann::json::value_t::string: {
681 arr.emplace_back(val.get<std::string>());
682 break;
683 }
684 case nlohmann::json::value_t::null: {
685 arr.emplace_back(nullptr);
686 break;
687 }
688 case nlohmann::json::value_t::boolean: {
689 arr.emplace_back(val.get<bool>());
690 break;
691 }
692 case nlohmann::json::value_t::number_integer:
693 case nlohmann::json::value_t::number_unsigned: {
694 arr.emplace_back(val.get<std::int64_t>());
695 break;
696 }
697 default:
698 break;
699 }
700 }
701
702 result[current_path] = arr;
703 break;
704 }
705
706 case nlohmann::json::value_t::string: {
707 result[current_path] = value.get<std::string>();
708 break;
709 }
710 case nlohmann::json::value_t::null: {
711 result[current_path] = nullptr;
712 break;
713 }
714 case nlohmann::json::value_t::boolean: {
715 result[current_path] = value.get<bool>();
716 break;
717 }
718 case nlohmann::json::value_t::number_integer:
719 case nlohmann::json::value_t::number_unsigned: {
720 result[current_path] = value.get<std::int64_t>();
721 break;
722 }
723
724 case nlohmann::json::value_t::number_float: // matrix events can't have floats
725 case nlohmann::json::value_t::binary:
726 case nlohmann::json::value_t::discarded:
727 default:
728 break;
729 }
730 }
731
732 static std::unordered_map<std::string, push_json_value>
733 flatten_event(const nlohmann::json &j)
734 {
735 std::unordered_map<std::string, push_json_value> flat;
736 flatten_impl(j, flat, "", 0);
737 return flat;
738 }
739
740 std::vector<actions::Action>
741 PushRuleEvaluator::evaluate(
742 const mtx::events::collections::TimelineEvents &event,
743 const RoomContext &ctx,
744 const std::vector<std::pair<mtx::common::Relation, mtx::events::collections::TimelineEvents>>
745 &relatedEvents) const
746 {
747 auto event_json = nlohmann::json(event);
748 auto flat_event = flatten_event(event_json);
749
750 std::map<mtx::common::RelationType, RelatedEvents> relatedEventsFlat;
751 for (const auto &[rel, ev] : relatedEvents) {
752 if (rel.rel_type != mtx::common::RelationType::Unsupported) {
753 if (rel.is_fallback)
754 relatedEventsFlat[rel.rel_type].fallbacks.push_back(
755 flatten_event(nlohmann::json(ev)));
756 else
757 relatedEventsFlat[rel.rel_type].events.push_back(flatten_event(nlohmann::json(ev)));
758 }
759 }
760
761 for (const auto &rule : rules->override_) {
762 if (rule.matches(flat_event, ctx, relatedEventsFlat))
763 return rule.actions;
764 }
765
766 for (const auto &rule : rules->content) {
767 if (rule.matches(flat_event, ctx, relatedEventsFlat))
768 return rule.actions;
769 }
770
771 // room rule always matches if present
772 if (auto room_rule = rules->room.find(event_json.value("room_id", ""));
773 room_rule != rules->room.end()) {
774 return room_rule->second.actions;
775 }
776
777 // sender rule always matches if present
778 if (auto sender_rule = rules->sender.find(event_json.value("sender", ""));
779 sender_rule != rules->sender.end()) {
780 return sender_rule->second.actions;
781 }
782
783 for (const auto &rule : rules->underride) {
784 if (rule.matches(flat_event, ctx, relatedEventsFlat))
785 return rule.actions;
786 }
787 return {};
788 }
789
790 }
791 }