Debian Codegraph @

mtxclient/tests/pushrules.cpp

raw symbols SCIP
1 #include <gtest/gtest.h>
2
3 #include <iostream>
4
5 #include "mtx/identifiers.hpp"
6 #include "mtx/requests.hpp"
7 #include "mtx/responses/create_room.hpp"
8 #include <mtx/pushrules.hpp>
9 #include <nlohmann/json.hpp>
10
11 #include "test_helpers.hpp"
12
13 using json = nlohmann::json;
14
15 namespace ns = mtx::pushrules;
16
17 using namespace mtx::http;
18
19 TEST(Pushrules, GlobalRuleset)
20 {
21 json data = R"({
22 "global": {
23 "content": [
24 {
25 "actions": [
26 "notify",
27 {
28 "set_tweak": "sound",
29 "value": "default"
30 },
31 {
32 "set_tweak": "highlight"
33 }
34 ],
35 "default": true,
36 "pattern": "alice",
37 "rule_id": ".m.rule.contains_user_name"
38 }
39 ],
40 "override": [
41 {
42 "actions": [
43 "dont_notify"
44 ],
45 "default": true,
46 "enabled": false,
47 "rule_id": ".m.rule.master"
48 },
49 {
50 "actions": [
51 "dont_notify"
52 ],
53 "conditions": [
54 {
55 "key": "content.msgtype",
56 "kind": "event_match",
57 "pattern": "m.notice"
58 }
59 ],
60 "default": true,
61 "rule_id": ".m.rule.suppress_notices"
62 },
63 {
64 "actions": [
65 "notify",
66 {
67 "set_tweak": "highlight"
68 },
69 {
70 "set_tweak": "sound",
71 "value": "default"
72 }
73 ],
74 "conditions": [
75 {
76 "key": "content.m\\.mentions.user_ids",
77 "kind": "event_property_contains"
78 }
79 ],
80 "default": true,
81 "rule_id": ".m.rule.is_user_mention"
82 }
83 ],
84 "room": [],
85 "sender": [],
86 "underride": [
87 {
88 "actions": [
89 "notify",
90 {
91 "set_tweak": "sound",
92 "value": "ring"
93 },
94 {
95 "set_tweak": "highlight",
96 "value": false
97 }
98 ],
99 "conditions": [
100 {
101 "key": "type",
102 "kind": "event_match",
103 "pattern": "m.call.invite"
104 }
105 ],
106 "default": true,
107 "rule_id": ".m.rule.call"
108 },
109 {
110 "actions": [
111 "notify",
112 {
113 "set_tweak": "sound",
114 "value": "default"
115 },
116 {
117 "set_tweak": "highlight"
118 }
119 ],
120 "conditions": [
121 {
122 "kind": "contains_display_name"
123 }
124 ],
125 "default": true,
126 "rule_id": ".m.rule.contains_display_name"
127 },
128 {
129 "actions": [
130 "notify",
131 {
132 "set_tweak": "sound",
133 "value": "default"
134 },
135 {
136 "set_tweak": "highlight",
137 "value": false
138 }
139 ],
140 "conditions": [
141 {
142 "kind": "room_member_count",
143 "is": "2"
144 },
145 {
146 "kind": "event_match",
147 "key": "type",
148 "pattern": "m.room.message"
149 }
150 ],
151 "default": true,
152 "rule_id": ".m.rule.room_one_to_one"
153 },
154 {
155 "actions": [
156 "notify",
157 {
158 "set_tweak": "sound",
159 "value": "default"
160 },
161 {
162 "set_tweak": "highlight",
163 "value": false
164 }
165 ],
166 "conditions": [
167 {
168 "key": "type",
169 "kind": "event_match",
170 "pattern": "m.room.member"
171 },
172 {
173 "key": "content.membership",
174 "kind": "event_match",
175 "pattern": "invite"
176 },
177 {
178 "key": "state_key",
179 "kind": "event_match",
180 "pattern": "@alice:example.com"
181 }
182 ],
183 "default": true,
184 "rule_id": ".m.rule.invite_for_me"
185 },
186 {
187 "actions": [
188 "notify",
189 {
190 "set_tweak": "highlight",
191 "value": false
192 }
193 ],
194 "conditions": [
195 {
196 "key": "type",
197 "kind": "event_match",
198 "pattern": "m.room.member"
199 }
200 ],
201 "default": true,
202 "rule_id": ".m.rule.member_event"
203 },
204 {
205 "actions": [
206 "notify",
207 {
208 "set_tweak": "highlight",
209 "value": false
210 }
211 ],
212 "conditions": [
213 {
214 "key": "type",
215 "kind": "event_match",
216 "pattern": "m.room.message"
217 }
218 ],
219 "default": true,
220 "rule_id": ".m.rule.message"
221 }
222 ]
223 }
224 })"_json;
225
226 ns::GlobalRuleset rules = data.get<ns::GlobalRuleset>();
227 EXPECT_EQ(rules.global.content.at(0).actions.size(), 3);
228 EXPECT_TRUE(
229 std::holds_alternative<ns::actions::notify>(rules.global.content.at(0).actions.at(0)));
230 EXPECT_EQ(
231 std::get<ns::actions::set_tweak_sound>(rules.global.content.at(0).actions.at(1)).value,
232 "default");
233 EXPECT_TRUE(std::holds_alternative<ns::actions::set_tweak_highlight>(
234 rules.global.content.at(0).actions.at(2)));
235
236 EXPECT_EQ(rules.global.override_.size(), 3);
237 EXPECT_EQ(rules.global.room.size(), 0);
238 EXPECT_EQ(rules.global.sender.size(), 0);
239 EXPECT_EQ(rules.global.underride.size(), 6);
240 EXPECT_EQ(rules.global.underride[0].conditions.at(0).key, "type");
241 EXPECT_EQ(rules.global.content[0].rule_id, ".m.rule.contains_user_name");
242 EXPECT_EQ(data, json(rules));
243 }
244
245 TEST(Pushrules, GetGlobalRuleset)
246 {
247 std::shared_ptr<Client> client = make_test_client();
248
249 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
250 check_error(err);
251 validate_login("@alice:" + server_name(), res);
252
253 client->get_pushrules(
254 [](const mtx::pushrules::GlobalRuleset &, RequestErr err) { EXPECT_TRUE(!err); });
255 });
256 client->close();
257 }
258
259 TEST(Pushrules, GetRuleset)
260 {
261 std::shared_ptr<Client> client = make_test_client();
262
263 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
264 check_error(err);
265 validate_login("@alice:" + server_name(), res);
266
267 client->get_pushrules("global",
268 "content",
269 ".m.rule.contains_user_name",
270 [](const mtx::pushrules::PushRule &rule, RequestErr err) {
271 EXPECT_TRUE(!err);
272 EXPECT_EQ(rule.pattern, "alice");
273 });
274 });
275 client->close();
276 }
277
278 TEST(Pushrules, PutAndDeleteRuleset)
279 {
280 std::shared_ptr<Client> client = make_test_client();
281
282 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
283 check_error(err);
284 validate_login("@alice:" + server_name(), res);
285
286 mtx::pushrules::PushRule rule;
287 rule.pattern = "cake";
288 rule.actions = {mtx::pushrules::actions::notify{},
289 mtx::pushrules::actions::set_tweak_sound{"cakealarm.wav"}};
290 client->put_pushrules(
291 "global", "content", "SSByZWFsbHkgbGlrZSBjYWtl", rule, [client](RequestErr err) {
292 EXPECT_TRUE(!err);
293
294 client->delete_pushrules("global",
295 "content",
296 "SSByZWFsbHkgbGlrZSBjYWtl",
297 [](RequestErr err) { EXPECT_TRUE(!err); });
298 });
299 });
300 client->close();
301 }
302
303 TEST(Pushrules, RulesetEnabled)
304 {
305 std::shared_ptr<Client> client = make_test_client();
306
307 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
308 check_error(err);
309 validate_login("@alice:" + server_name(), res);
310
311 client->get_pushrules_enabled(
312 "global",
313 "content",
314 ".m.rule.contains_user_name",
315 [client](const mtx::pushrules::Enabled &enabled, RequestErr err) {
316 EXPECT_TRUE(!err);
317
318 EXPECT_TRUE(enabled.enabled);
319 client->put_pushrules_enabled(
320 "global", "content", ".m.rule.contains_user_name", false, [client](RequestErr err) {
321 EXPECT_TRUE(!err);
322 client->get_pushrules_enabled(
323 "global",
324 "content",
325 ".m.rule.contains_user_name",
326 [client](const mtx::pushrules::Enabled &enabled, RequestErr err) {
327 EXPECT_TRUE(!err);
328
329 EXPECT_FALSE(enabled.enabled);
330 client->put_pushrules_enabled("global",
331 "content",
332 ".m.rule.contains_"
333 "user_name",
334 true,
335 [](RequestErr err) { EXPECT_TRUE(!err); });
336 });
337 });
338 });
339 });
340 client->close();
341 }
342
343 TEST(Pushrules, Actions)
344 {
345 std::shared_ptr<Client> client = make_test_client();
346
347 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
348 check_error(err);
349 validate_login("@alice:" + server_name(), res);
350
351 mtx::pushrules::actions::Actions actions = {
352 {mtx::pushrules::actions::notify{},
353 mtx::pushrules::actions::set_tweak_sound{"cakealarm.wav"},
354 mtx::pushrules::actions::set_tweak_highlight{}}};
355 client->put_pushrules_actions(
356 "global", "content", ".m.rule.contains_user_name", actions, [client](RequestErr err) {
357 EXPECT_TRUE(!err);
358
359 client->get_pushrules_actions("global",
360 "content",
361 ".m.rule.contains_user_name",
362 [client](auto actions_, RequestErr err) {
363 EXPECT_TRUE(!err);
364 EXPECT_EQ(actions_.actions.size(), 3);
365 });
366 });
367 });
368 client->close();
369 }
370
371 TEST(Pushrules, RoomRuleMute)
372 {
373 std::shared_ptr<Client> client = make_test_client();
374
375 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
376 check_error(err);
377 validate_login("@alice:" + server_name(), res);
378
379 mtx::requests::CreateRoom req;
380 req.name = "Name";
381 req.topic = "Topic";
382
383 client->create_room(req, [client](const mtx::responses::CreateRoom &res, RequestErr err) {
384 check_error(err);
385 ASSERT_TRUE(res.room_id.localpart().size() > 10);
386 EXPECT_EQ(res.room_id.hostname(), server_name());
387
388 mtx::pushrules::PushRule rule;
389 rule.actions = {mtx::pushrules::actions::dont_notify{}};
390 mtx::pushrules::PushCondition condition;
391 condition.kind = "event_match";
392 condition.key = "room_id";
393 condition.pattern = res.room_id.to_string();
394 rule.conditions = {condition};
395
396 client->put_pushrules(
397 "global", "override", res.room_id.to_string(), rule, [](mtx::http::RequestErr &err) {
398 check_error(err);
399 EXPECT_TRUE(!err);
400 });
401 });
402 });
403 client->close();
404 }
405
406 TEST(Pushrules, RoomRuleMentions)
407 {
408 std::shared_ptr<Client> client = make_test_client();
409
410 client->login("alice", "secret", [client](const mtx::responses::Login &res, RequestErr err) {
411 check_error(err);
412 validate_login("@alice:" + server_name(), res);
413
414 mtx::requests::CreateRoom req;
415 req.name = "Name";
416 req.topic = "Topic";
417
418 client->create_room(req, [client](const mtx::responses::CreateRoom &res, RequestErr err) {
419 check_error(err);
420 ASSERT_TRUE(res.room_id.localpart().size() > 10);
421 EXPECT_EQ(res.room_id.hostname(), server_name());
422
423 mtx::pushrules::PushRule rule;
424 rule.actions = {mtx::pushrules::actions::dont_notify{}};
425
426 client->put_pushrules(
427 "global", "room", res.room_id.to_string(), rule, [](mtx::http::RequestErr &err) {
428 check_error(err);
429 EXPECT_TRUE(!err);
430 });
431 });
432 });
433 client->close();
434 }
435
436 TEST(Pushrules, ExplicitMentions)
437 {
438 nlohmann::json raw_rule = R"(
439 {
440 "global": {
441 "underride": [
442 {
443 "conditions": [
444 {
445 "kind": "event_match",
446 "key": "type",
447 "pattern": "m.call.invite"
448 }
449 ],
450 "actions": [
451 "notify",
452 {
453 "set_tweak": "sound",
454 "value": "ring"
455 },
456 {
457 "set_tweak": "highlight",
458 "value": false
459 }
460 ],
461 "rule_id": ".m.rule.call",
462 "default": true,
463 "enabled": true
464 },
465 {
466 "conditions": [
467 {
468 "kind": "event_match",
469 "key": "type",
470 "pattern": "m.room.message"
471 },
472 {
473 "kind": "room_member_count",
474 "is": "2"
475 }
476 ],
477 "actions": [
478 "notify",
479 {
480 "set_tweak": "sound",
481 "value": "default"
482 },
483 {
484 "set_tweak": "highlight",
485 "value": false
486 }
487 ],
488 "rule_id": ".m.rule.room_one_to_one",
489 "default": true,
490 "enabled": true
491 },
492 {
493 "conditions": [
494 {
495 "kind": "event_match",
496 "key": "type",
497 "pattern": "m.room.encrypted"
498 },
499 {
500 "kind": "room_member_count",
501 "is": "2"
502 }
503 ],
504 "actions": [
505 "notify",
506 {
507 "set_tweak": "sound",
508 "value": "default"
509 },
510 {
511 "set_tweak": "highlight",
512 "value": false
513 }
514 ],
515 "rule_id": ".m.rule.encrypted_room_one_to_one",
516 "default": true,
517 "enabled": true
518 },
519 {
520 "conditions": [
521 {
522 "kind": "event_match",
523 "key": "type",
524 "pattern": "m.room.message"
525 }
526 ],
527 "actions": [
528 "notify",
529 {
530 "set_tweak": "sound",
531 "value": "default"
532 },
533 {
534 "set_tweak": "highlight",
535 "value": false
536 }
537 ],
538 "rule_id": ".m.rule.message",
539 "default": true,
540 "enabled": true
541 },
542 {
543 "conditions": [
544 {
545 "kind": "event_match",
546 "key": "type",
547 "pattern": "m.room.encrypted"
548 }
549 ],
550 "actions": [
551 "notify",
552 {
553 "set_tweak": "sound",
554 "value": "default"
555 },
556 {
557 "set_tweak": "highlight",
558 "value": false
559 }
560 ],
561 "rule_id": ".m.rule.encrypted",
562 "default": true,
563 "enabled": true
564 },
565 {
566 "conditions": [
567 {
568 "kind": "event_match",
569 "key": "type",
570 "pattern": "im.vector.modular.widgets"
571 },
572 {
573 "kind": "event_match",
574 "key": "content.type",
575 "pattern": "jitsi"
576 },
577 {
578 "kind": "event_match",
579 "key": "state_key",
580 "pattern": "*"
581 }
582 ],
583 "actions": [
584 "notify",
585 {
586 "set_tweak": "highlight",
587 "value": false
588 }
589 ],
590 "rule_id": ".im.vector.jitsi",
591 "default": true,
592 "enabled": true
593 }
594 ],
595 "sender": [],
596 "room": [],
597 "content": [
598 {
599 "actions": [
600 "notify",
601 {
602 "set_tweak": "highlight"
603 },
604 {
605 "set_tweak": "sound",
606 "value": "default"
607 }
608 ],
609 "rule_id": ".m.rule.contains_user_name",
610 "default": true,
611 "pattern": "deepbluev7",
612 "enabled": true
613 }
614 ],
615 "override": [
616 {
617 "conditions": [],
618 "actions": [],
619 "rule_id": ".m.rule.master",
620 "default": true,
621 "enabled": false
622 },
623 {
624 "conditions": [
625 {
626 "kind": "event_match",
627 "key": "content.msgtype",
628 "pattern": "m.notice"
629 }
630 ],
631 "actions": [],
632 "rule_id": ".m.rule.suppress_notices",
633 "default": true,
634 "enabled": true
635 },
636 {
637 "conditions": [
638 {
639 "kind": "event_match",
640 "key": "type",
641 "pattern": "m.room.member"
642 },
643 {
644 "kind": "event_match",
645 "key": "content.membership",
646 "pattern": "invite"
647 },
648 {
649 "kind": "event_match",
650 "key": "state_key",
651 "pattern": "@deepbluev7:neko.dev"
652 }
653 ],
654 "actions": [
655 "notify",
656 {
657 "set_tweak": "highlight",
658 "value": false
659 },
660 {
661 "set_tweak": "sound",
662 "value": "default"
663 }
664 ],
665 "rule_id": ".m.rule.invite_for_me",
666 "default": true,
667 "enabled": true
668 },
669 {
670 "conditions": [
671 {
672 "kind": "event_match",
673 "key": "type",
674 "pattern": "m.room.member"
675 }
676 ],
677 "actions": [],
678 "rule_id": ".m.rule.member_event",
679 "default": true,
680 "enabled": true
681 },
682 {
683 "conditions": [
684 {
685 "kind": "im.nheko.msc3664.related_event_match",
686 "key": "sender",
687 "rel_type": "m.in_reply_to",
688 "pattern": "@deepbluev7:neko.dev"
689 }
690 ],
691 "actions": [
692 "notify",
693 {
694 "set_tweak": "highlight"
695 },
696 {
697 "set_tweak": "sound",
698 "value": "default"
699 }
700 ],
701 "rule_id": ".im.nheko.msc3664.reply",
702 "default": true,
703 "enabled": true
704 },
705 {
706 "conditions": [
707 {
708 "kind": "event_property_contains",
709 "key": "content.m\\.mentions.user_ids",
710 "value": "@deepbluev7:neko.dev"
711 }
712 ],
713 "actions": [
714 "notify",
715 {
716 "set_tweak": "highlight"
717 },
718 {
719 "set_tweak": "sound",
720 "value": "default"
721 }
722 ],
723 "rule_id": ".m.rule.is_user_mention",
724 "default": true,
725 "enabled": true
726 },
727 {
728 "conditions": [
729 {
730 "kind": "contains_display_name"
731 }
732 ],
733 "actions": [
734 "notify",
735 {
736 "set_tweak": "sound",
737 "value": "default"
738 },
739 {
740 "set_tweak": "highlight"
741 }
742 ],
743 "rule_id": ".m.rule.contains_display_name",
744 "default": true,
745 "enabled": true
746 },
747 {
748 "conditions": [
749 {
750 "kind": "event_property_is",
751 "key": "content.m\\.mentions.room",
752 "value": true
753 },
754 {
755 "kind": "sender_notification_permission",
756 "key": "room"
757 }
758 ],
759 "actions": [
760 "notify",
761 {
762 "set_tweak": "highlight"
763 }
764 ],
765 "rule_id": ".m.rule.is_room_mention",
766 "default": true,
767 "enabled": true
768 },
769 {
770 "conditions": [
771 {
772 "kind": "sender_notification_permission",
773 "key": "room"
774 },
775 {
776 "kind": "event_match",
777 "key": "content.body",
778 "pattern": "@room"
779 }
780 ],
781 "actions": [
782 "notify",
783 {
784 "set_tweak": "highlight"
785 }
786 ],
787 "rule_id": ".m.rule.roomnotif",
788 "default": true,
789 "enabled": true
790 },
791 {
792 "conditions": [
793 {
794 "kind": "event_match",
795 "key": "type",
796 "pattern": "m.room.tombstone"
797 },
798 {
799 "kind": "event_match",
800 "key": "state_key",
801 "pattern": ""
802 }
803 ],
804 "actions": [
805 "notify",
806 {
807 "set_tweak": "highlight"
808 }
809 ],
810 "rule_id": ".m.rule.tombstone",
811 "default": true,
812 "enabled": true
813 },
814 {
815 "conditions": [
816 {
817 "kind": "event_match",
818 "key": "type",
819 "pattern": "m.reaction"
820 }
821 ],
822 "actions": [],
823 "rule_id": ".m.rule.reaction",
824 "default": true,
825 "enabled": true
826 },
827 {
828 "conditions": [
829 {
830 "kind": "event_match",
831 "key": "type",
832 "pattern": "m.room.server_acl"
833 },
834 {
835 "kind": "event_match",
836 "key": "state_key",
837 "pattern": ""
838 }
839 ],
840 "actions": [],
841 "rule_id": ".m.rule.room.server_acl",
842 "default": true,
843 "enabled": true
844 },
845 {
846 "conditions": [
847 {
848 "kind": "event_property_is",
849 "key": "content.m\\.relates_to.rel_type",
850 "value": "m.replace"
851 }
852 ],
853 "actions": [],
854 "rule_id": ".m.rule.suppress_edits",
855 "default": true,
856 "enabled": true
857 }
858 ]
859 }
860 })"_json;
861
862 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
863
864 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
865 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
866 ctx.user_display_name = "Nico";
867
868 auto simple_explicit = R"({
869 "content": {
870 "body": "Nico",
871 "format": "org.matrix.custom.html",
872 "formatted_body": "<a href=\"https://matrix.to/#/%40deepbluev7%3Aneko.dev\">Nico</a>",
873 "m.mentions": {
874 "user_ids": [
875 "@deepbluev7:neko.dev"
876 ]
877 },
878 "msgtype": "m.text"
879 },
880 "event_id": "$eRdwtw6KD2SRUJL9RyPUP9kkVcK90NqK3S2DbJXltL0",
881 "origin_server_ts": 1709919179046,
882 "sender": "@test:neko.dev",
883 "type": "m.room.message",
884 "unsigned": {
885 "age": 61
886 }
887 })"_json.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
888
889 auto simple_explicit_actions = evaluator.evaluate({simple_explicit}, ctx, {});
890
891 std::vector<mtx::pushrules::actions::Action> highlight_actions = {
892 mtx::pushrules::actions::notify{},
893 mtx::pushrules::actions::set_tweak_highlight{},
894 mtx::pushrules::actions::set_tweak_sound{},
895 };
896 std::vector<mtx::pushrules::actions::Action> highlight_actions2 = {
897 mtx::pushrules::actions::notify{},
898 mtx::pushrules::actions::set_tweak_sound{},
899 mtx::pushrules::actions::set_tweak_highlight{},
900 };
901 std::vector<mtx::pushrules::actions::Action> no_highlight_actions = {
902 mtx::pushrules::actions::notify{},
903 mtx::pushrules::actions::set_tweak_sound{},
904 mtx::pushrules::actions::set_tweak_highlight{false},
905 };
906
907 EXPECT_EQ(simple_explicit_actions, highlight_actions);
908
909 auto simple_explicit_empty = R"({
910 "content": {
911 "body": "Nico",
912 "format": "org.matrix.custom.html",
913 "formatted_body": "<a href=\"https://matrix.to/#/%40deepbluev7%3Aneko.dev\">Nico</a>",
914 "m.mentions": {
915 "user_ids": [
916 ]
917 },
918 "msgtype": "m.text"
919 },
920 "event_id": "$eRdwtw6KD2SRUJL9RyPUP9kkVcK90NqK3S2DbJXltL0",
921 "origin_server_ts": 1709919179046,
922 "sender": "@test:neko.dev",
923 "type": "m.room.message",
924 "unsigned": {
925 "age": 61
926 }
927 })"_json.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
928
929 auto simple_explicit_empty_actions = evaluator.evaluate({simple_explicit_empty}, ctx, {});
930 EXPECT_EQ(simple_explicit_empty_actions, no_highlight_actions);
931
932 auto simple = R"({
933 "content": {
934 "body": "Nico",
935 "format": "org.matrix.custom.html",
936 "formatted_body": "<a href=\"https://matrix.to/#/%40deepbluev7%3Aneko.dev\">Nico</a>",
937 "msgtype": "m.text"
938 },
939 "event_id": "$eRdwtw6KD2SRUJL9RyPUP9kkVcK90NqK3S2DbJXltL0",
940 "origin_server_ts": 1709919179046,
941 "sender": "@test:neko.dev",
942 "type": "m.room.message",
943 "unsigned": {
944 "age": 61
945 }
946 })"_json.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
947
948 auto simple_actions = evaluator.evaluate({simple}, ctx, {});
949 EXPECT_EQ(simple_actions, highlight_actions2);
950 }
951 TEST(Pushrules, EventMatches)
952 {
953 mtx::pushrules::PushRule event_match_rule;
954 event_match_rule.actions = {
955 mtx::pushrules::actions::notify{},
956 mtx::pushrules::actions::set_tweak_highlight{},
957 };
958 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
959 .kind = "event_match",
960 .key = "content.body",
961 .pattern = "honk",
962 .value = std::nullopt,
963 .is = "",
964 });
965
966 mtx::events::RoomEvent<mtx::events::msg::Text> textEv{};
967 textEv.content.body = "abc def ghi honk jkl";
968 textEv.room_id = "!abc:def.ghi";
969 textEv.event_id = "$abc1234567890:def.ghi";
970 textEv.sender = "@me:def.ghi";
971
972 auto testEval = [actions = event_match_rule.actions,
973 &textEv](const mtx::pushrules::PushRuleEvaluator &evaluator) {
974 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
975 EXPECT_EQ(evaluator.evaluate({textEv}, ctx, {}), actions);
976
977 auto textEvEnd = textEv;
978 textEvEnd.content.body = "abc honk";
979 EXPECT_EQ(evaluator.evaluate({textEvEnd}, ctx, {}), actions);
980 auto textEvStart = textEv;
981 textEvStart.content.body = "honk abc";
982 EXPECT_EQ(evaluator.evaluate({textEvStart}, ctx, {}), actions);
983 auto textEvNL = textEv;
984 textEvNL.content.body = "abc\nhonk\nabc";
985 EXPECT_EQ(evaluator.evaluate({textEvNL}, ctx, {}), actions);
986 auto textEvFull = textEv;
987 textEvFull.content.body = "honk";
988 EXPECT_EQ(evaluator.evaluate({textEvFull}, ctx, {}), actions);
989 auto textEvCase = textEv;
990 textEvCase.content.body = "HoNk";
991 EXPECT_EQ(evaluator.evaluate({textEvCase}, ctx, {}), actions);
992 auto textEvNo = textEv;
993 textEvNo.content.body = "HoN";
994 EXPECT_TRUE(evaluator.evaluate({textEvNo}, ctx, {}).empty());
995 auto textEvNo2 = textEv;
996 textEvNo2.content.body = "honkb";
997 EXPECT_TRUE(evaluator.evaluate({textEvNo2}, ctx, {}).empty());
998 auto textEvWordBoundaries = textEv;
999 textEvWordBoundaries.content.body = "@honk:";
1000 EXPECT_EQ(evaluator.evaluate({textEvWordBoundaries}, ctx, {}), actions);
1001
1002 // It is what the spec says ¯\_(ツ)_/¯
1003 auto textEvWordBoundaries2 = textEv;
1004 textEvWordBoundaries2.content.body = "ähonkü";
1005 EXPECT_EQ(evaluator.evaluate({textEvWordBoundaries2}, ctx, {}), actions);
1006 };
1007
1008 mtx::pushrules::Ruleset override_ruleset;
1009 override_ruleset.override_.push_back(event_match_rule);
1010 mtx::pushrules::PushRuleEvaluator over_evaluator{override_ruleset};
1011 testEval(over_evaluator);
1012
1013 mtx::pushrules::Ruleset underride_ruleset;
1014 underride_ruleset.underride.push_back(event_match_rule);
1015 mtx::pushrules::PushRuleEvaluator under_evaluator{underride_ruleset};
1016 testEval(under_evaluator);
1017
1018 mtx::pushrules::Ruleset room_ruleset;
1019 auto room_rule = event_match_rule;
1020 room_rule.rule_id = "!abc:def.ghi";
1021 room_ruleset.room.push_back(room_rule);
1022 mtx::pushrules::PushRuleEvaluator room_evaluator{room_ruleset};
1023 EXPECT_EQ(room_evaluator.evaluate({textEv}, {}, {}), room_rule.actions);
1024
1025 mtx::pushrules::Ruleset sender_ruleset;
1026 auto sender_rule = event_match_rule;
1027 sender_rule.rule_id = "@me:def.ghi";
1028 sender_ruleset.sender.push_back(sender_rule);
1029 mtx::pushrules::PushRuleEvaluator sender_evaluator{sender_ruleset};
1030 EXPECT_EQ(sender_evaluator.evaluate({textEv}, {}, {}), sender_rule.actions);
1031
1032 mtx::pushrules::Ruleset content_ruleset;
1033 mtx::pushrules::PushRule content_match_rule;
1034 content_match_rule.actions = event_match_rule.actions;
1035 content_match_rule.pattern = "honk";
1036 content_ruleset.content.push_back(content_match_rule);
1037 mtx::pushrules::PushRuleEvaluator content_evaluator{content_ruleset};
1038 testEval(content_evaluator);
1039 }
1040
1041 TEST(Pushrules, DisplaynameMatches)
1042 {
1043 mtx::pushrules::PushRule event_match_rule;
1044 event_match_rule.actions = {
1045 mtx::pushrules::actions::notify{},
1046 mtx::pushrules::actions::set_tweak_highlight{},
1047 };
1048 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
1049 .kind = "contains_display_name",
1050 .key = "",
1051 .pattern = "",
1052 .value = std::nullopt,
1053 .is = "",
1054 });
1055
1056 mtx::events::RoomEvent<mtx::events::msg::Text> textEv{};
1057 textEv.content.body = "abc def ghi honk jkl";
1058 textEv.room_id = "!abc:def.ghi";
1059 textEv.event_id = "$abc1234567890:def.ghi";
1060 textEv.sender = "@me:def.ghi";
1061
1062 auto testEval = [actions = event_match_rule.actions,
1063 &textEv](const mtx::pushrules::PushRuleEvaluator &evaluator) {
1064 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1065 ctx.user_display_name = "honk";
1066
1067 EXPECT_EQ(evaluator.evaluate({textEv}, ctx, {}), actions);
1068
1069 auto textEvEnd = textEv;
1070 textEvEnd.content.body = "abc honk";
1071 EXPECT_EQ(evaluator.evaluate({textEvEnd}, ctx, {}), actions);
1072 auto textEvStart = textEv;
1073 textEvStart.content.body = "honk abc";
1074 EXPECT_EQ(evaluator.evaluate({textEvStart}, ctx, {}), actions);
1075 auto textEvNL = textEv;
1076 textEvNL.content.body = "abc\nhonk\nabc";
1077 EXPECT_EQ(evaluator.evaluate({textEvNL}, ctx, {}), actions);
1078 auto textEvFull = textEv;
1079 textEvFull.content.body = "honk";
1080 EXPECT_EQ(evaluator.evaluate({textEvFull}, ctx, {}), actions);
1081 auto textEvCase = textEv;
1082 textEvCase.content.body = "HoNk";
1083 EXPECT_EQ(evaluator.evaluate({textEvCase}, ctx, {}), actions);
1084 auto textEvNo = textEv;
1085 textEvNo.content.body = "HoN";
1086 EXPECT_TRUE(evaluator.evaluate({textEvNo}, ctx, {}).empty());
1087 auto textEvNo2 = textEv;
1088 textEvNo2.content.body = "honkb";
1089 EXPECT_TRUE(evaluator.evaluate({textEvNo2}, ctx, {}).empty());
1090 auto textEvWordBoundaries = textEv;
1091 textEvWordBoundaries.content.body = "@honk:";
1092 EXPECT_EQ(evaluator.evaluate({textEvWordBoundaries}, ctx, {}), actions);
1093
1094 // It is what the spec says ¯\_(ツ)_/¯
1095 auto textEvWordBoundaries2 = textEv;
1096 textEvWordBoundaries2.content.body = "ähonkü";
1097 EXPECT_EQ(evaluator.evaluate({textEvWordBoundaries2}, ctx, {}), actions);
1098
1099 // assert empty displayname does not match
1100 mtx::pushrules::PushRuleEvaluator::RoomContext ctxEmpty{};
1101 EXPECT_TRUE(evaluator.evaluate({textEvWordBoundaries}, ctxEmpty, {}).empty());
1102 };
1103
1104 mtx::pushrules::Ruleset override_ruleset;
1105 override_ruleset.override_.push_back(event_match_rule);
1106 mtx::pushrules::PushRuleEvaluator over_evaluator{override_ruleset};
1107 testEval(over_evaluator);
1108
1109 mtx::pushrules::Ruleset underride_ruleset;
1110 underride_ruleset.underride.push_back(event_match_rule);
1111 mtx::pushrules::PushRuleEvaluator under_evaluator{underride_ruleset};
1112 testEval(under_evaluator);
1113 }
1114
1115 TEST(Pushrules, PowerLevelMatches)
1116 {
1117 mtx::pushrules::PushRule event_match_rule;
1118 event_match_rule.actions = {
1119 mtx::pushrules::actions::notify{},
1120 mtx::pushrules::actions::set_tweak_highlight{},
1121 };
1122 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
1123 .kind = "sender_notification_permission",
1124 .key = "room",
1125 .pattern = "",
1126 .value = std::nullopt,
1127 .is = "",
1128 });
1129
1130 auto testEval =
1131 [actions = event_match_rule.actions](const mtx::pushrules::PushRuleEvaluator &evaluator) {
1132 mtx::events::RoomEvent<mtx::events::msg::Text> textEv{};
1133 textEv.content.body = "abc def ghi honk @room jkl";
1134 textEv.room_id = "!abc:def.ghi";
1135 textEv.event_id = "$abc1234567890:def.ghi";
1136 textEv.sender = "@me:def.ghi";
1137
1138 mtx::events::state::PowerLevels pls;
1139 pls.notifications["room"] = 1;
1140 pls.users["@me:def.ghi"] = 1;
1141 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{
1142 .user_display_name = "me",
1143 .member_count = 100,
1144 .power_levels = pls,
1145 };
1146
1147 EXPECT_EQ(evaluator.evaluate({textEv}, ctx, {}), actions);
1148
1149 ctx.power_levels.users["@me:def.ghi"] = 0;
1150 EXPECT_TRUE(evaluator.evaluate({textEv}, ctx, {}).empty());
1151 };
1152
1153 mtx::pushrules::Ruleset override_ruleset;
1154 override_ruleset.override_.push_back(event_match_rule);
1155 mtx::pushrules::PushRuleEvaluator over_evaluator{override_ruleset};
1156 testEval(over_evaluator);
1157
1158 mtx::pushrules::Ruleset underride_ruleset;
1159 underride_ruleset.underride.push_back(event_match_rule);
1160 mtx::pushrules::PushRuleEvaluator under_evaluator{underride_ruleset};
1161 testEval(under_evaluator);
1162 }
1163
1164 TEST(Pushrules, MemberCountMatches)
1165 {
1166 auto testEval = [](const std::string &is, bool lt, bool eq, bool gt) {
1167 mtx::events::RoomEvent<mtx::events::msg::Text> textEv{};
1168 textEv.content.body = "abc def ghi honk @room jkl";
1169 textEv.room_id = "!abc:def.ghi";
1170 textEv.event_id = "$abc1234567890:def.ghi";
1171 textEv.sender = "@me:def.ghi";
1172
1173 mtx::pushrules::PushRule event_match_rule;
1174 event_match_rule.actions = {
1175 mtx::pushrules::actions::notify{},
1176 mtx::pushrules::actions::set_tweak_highlight{},
1177 };
1178 event_match_rule.conditions = {
1179 mtx::pushrules::PushCondition{
1180 .kind = "room_member_count",
1181 .key = "",
1182 .pattern = "",
1183 .value = std::nullopt,
1184 .is = is,
1185 },
1186 };
1187 mtx::pushrules::Ruleset ruleset;
1188 ruleset.override_.push_back(event_match_rule);
1189 mtx::pushrules::PushRuleEvaluator evaluator{ruleset};
1190
1191 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1192
1193 ctx.member_count = 99;
1194 EXPECT_EQ(!evaluator.evaluate({textEv}, ctx, {}).empty(), lt);
1195 ctx.member_count = 100;
1196 EXPECT_EQ(!evaluator.evaluate({textEv}, ctx, {}).empty(), eq);
1197 ctx.member_count = 101;
1198 EXPECT_EQ(!evaluator.evaluate({textEv}, ctx, {}).empty(), gt);
1199 };
1200
1201 testEval("100", false, true, false);
1202 testEval("==100", false, true, false);
1203 testEval(">=100", false, true, true);
1204 testEval("<=100", true, true, false);
1205 testEval(">100", false, false, true);
1206 testEval("<100", true, false, false);
1207 }
1208
1209 TEST(Pushrules, EventPropertyIsMatches)
1210 {
1211 mtx::pushrules::PushRule event_match_rule;
1212 event_match_rule.actions = {
1213 mtx::pushrules::actions::notify{},
1214 mtx::pushrules::actions::set_tweak_highlight{},
1215 };
1216 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
1217 .kind = "event_property_is",
1218 .key = "content.body",
1219 .pattern = "",
1220 .value = "honk",
1221 .is = "",
1222 });
1223
1224 mtx::events::RoomEvent<mtx::events::msg::Text> textEv{};
1225 textEv.content.body = "honk";
1226 textEv.room_id = "!abc:def.ghi";
1227 textEv.event_id = "$abc1234567890:def.ghi";
1228 textEv.sender = "@me:def.ghi";
1229
1230 auto testEval = [actions = event_match_rule.actions,
1231 &textEv](const mtx::pushrules::PushRuleEvaluator &evaluator) {
1232 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1233 EXPECT_EQ(evaluator.evaluate({textEv}, ctx, {}), actions);
1234
1235 auto textEvNo = textEv;
1236 textEvNo.content.body = "hon";
1237 EXPECT_TRUE(evaluator.evaluate({textEvNo}, ctx, {}).empty());
1238 auto textEvNo2 = textEv;
1239 textEvNo2.content.body = "honkb";
1240 EXPECT_TRUE(evaluator.evaluate({textEvNo2}, ctx, {}).empty());
1241 auto textEvWordBoundaries = textEv;
1242 textEvWordBoundaries.content.body = "@honk:";
1243 EXPECT_TRUE(evaluator.evaluate({textEvWordBoundaries}, ctx, {}).empty());
1244 };
1245
1246 mtx::pushrules::Ruleset override_ruleset;
1247 override_ruleset.override_.push_back(event_match_rule);
1248 mtx::pushrules::PushRuleEvaluator over_evaluator{override_ruleset};
1249 testEval(over_evaluator);
1250
1251 mtx::pushrules::Ruleset underride_ruleset;
1252 underride_ruleset.underride.push_back(event_match_rule);
1253 mtx::pushrules::PushRuleEvaluator under_evaluator{underride_ruleset};
1254 testEval(under_evaluator);
1255
1256 mtx::pushrules::Ruleset room_ruleset;
1257 auto room_rule = event_match_rule;
1258 room_rule.rule_id = "!abc:def.ghi";
1259 room_ruleset.room.push_back(room_rule);
1260 mtx::pushrules::PushRuleEvaluator room_evaluator{room_ruleset};
1261 EXPECT_EQ(room_evaluator.evaluate({textEv}, {}, {}), room_rule.actions);
1262
1263 mtx::pushrules::Ruleset sender_ruleset;
1264 auto sender_rule = event_match_rule;
1265 sender_rule.rule_id = "@me:def.ghi";
1266 sender_ruleset.sender.push_back(sender_rule);
1267 mtx::pushrules::PushRuleEvaluator sender_evaluator{sender_ruleset};
1268 EXPECT_EQ(sender_evaluator.evaluate({textEv}, {}, {}), sender_rule.actions);
1269
1270 mtx::pushrules::PushRule event_match_5_rule;
1271 event_match_rule.actions = {
1272 mtx::pushrules::actions::notify{},
1273 mtx::pushrules::actions::set_tweak_highlight{},
1274 };
1275 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
1276 .kind = "event_property_is",
1277 .key = "content.info.size",
1278 .pattern = "",
1279 .value = 5,
1280 .is = "",
1281 });
1282 mtx::pushrules::Ruleset match5_ruleset;
1283 match5_ruleset.override_.push_back(event_match_rule);
1284 mtx::pushrules::PushRuleEvaluator over_evaluator5{match5_ruleset};
1285 mtx::events::RoomEvent<mtx::events::msg::Image> imageEv{};
1286 imageEv.content.info.size = 5;
1287 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1288 EXPECT_EQ(over_evaluator5.evaluate({imageEv}, ctx, {}), event_match_5_rule.actions);
1289 imageEv.content.info.size = 6;
1290 EXPECT_TRUE(over_evaluator5.evaluate({imageEv}, ctx, {}).empty());
1291 }
1292
1293 TEST(Pushrules, EventPropertyContainsMatches)
1294 {
1295 mtx::pushrules::PushRule event_match_rule;
1296 event_match_rule.actions = {
1297 mtx::pushrules::actions::notify{},
1298 mtx::pushrules::actions::set_tweak_highlight{},
1299 };
1300 event_match_rule.conditions.push_back(mtx::pushrules::PushCondition{
1301 .kind = "event_property_contains",
1302 .key = "content.via",
1303 .pattern = "",
1304 .value = "honk",
1305 .is = "",
1306 });
1307
1308 mtx::events::StateEvent<mtx::events::state::space::Child> childEv{};
1309 childEv.content.via = {"honk"};
1310 childEv.room_id = "!abc:def.ghi";
1311 childEv.event_id = "$abc1234567890:def.ghi";
1312 childEv.sender = "@me:def.ghi";
1313
1314 mtx::pushrules::Ruleset override_ruleset;
1315 override_ruleset.override_.push_back(event_match_rule);
1316 mtx::pushrules::PushRuleEvaluator evaluator{override_ruleset};
1317 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1318
1319 EXPECT_EQ(evaluator.evaluate({childEv}, ctx, {}), event_match_rule.actions);
1320
1321 childEv.content.via = {"abc", "honk", ""};
1322 EXPECT_EQ(evaluator.evaluate({childEv}, ctx, {}), event_match_rule.actions);
1323
1324 childEv.content.via = {};
1325 EXPECT_TRUE(evaluator.evaluate({childEv}, ctx, {}).empty());
1326
1327 childEv.content.via = {"not honk"};
1328 EXPECT_TRUE(evaluator.evaluate({childEv}, ctx, {}).empty());
1329 }
1330
1331 TEST(Pushrules, ContentOverRoomRulesMatches)
1332 {
1333 json raw_rule = R"(
1334 {
1335 "global": {
1336 "content": [
1337 {
1338 "actions": [
1339 "notify",
1340 {
1341 "set_tweak": "sound",
1342 "value": "default"
1343 },
1344 {
1345 "set_tweak": "highlight"
1346 }
1347 ],
1348 "default": true,
1349 "enabled": true,
1350 "pattern": "lordmzte",
1351 "rule_id": ".m.rules.contains_user_name"
1352 }
1353 ],
1354 "override": [
1355 {
1356 "actions": [
1357 "dont_notify"
1358 ],
1359 "conditions": [],
1360 "default": true,
1361 "enabled": false,
1362 "rule_id": ".m.rule.master"
1363 },
1364 {
1365 "actions": [
1366 "dont_notify"
1367 ],
1368 "conditions": [
1369 {
1370 "key": "content.msgtype",
1371 "kind": "event_match",
1372 "pattern": "m.notice"
1373 }
1374 ],
1375 "default": true,
1376 "enabled": true,
1377 "rule_id": ".m.rule.suppress_notices"
1378 },
1379 {
1380 "actions": [
1381 "notify",
1382 {
1383 "set_tweak": "sound",
1384 "value": "default"
1385 },
1386 {
1387 "set_tweak": "highlight",
1388 "value": false
1389 }
1390 ],
1391 "conditions": [
1392 {
1393 "key": "type",
1394 "kind": "event_match",
1395 "pattern": "m.room.member"
1396 },
1397 {
1398 "key": "content.membership",
1399 "kind": "event_match",
1400 "pattern": "invite"
1401 },
1402 {
1403 "key": "state_key",
1404 "kind": "event_match",
1405 "pattern": "@lordmzte:mzte.de"
1406 }
1407 ],
1408 "default": true,
1409 "enabled": true,
1410 "rule_id": ".m.rule.invite_for_me"
1411 },
1412 {
1413 "actions": [
1414 "dont_notify"
1415 ],
1416 "conditions": [
1417 {
1418 "key": "type",
1419 "kind": "event_match",
1420 "pattern": "m.room.member"
1421 }
1422 ],
1423 "default": true,
1424 "enabled": true,
1425 "rule_id": ".m.rule.member_event"
1426 },
1427 {
1428 "actions": [
1429 "notify",
1430 {
1431 "set_tweak": "sound",
1432 "value": "default"
1433 },
1434 {
1435 "set_tweak": "highlight"
1436 }
1437 ],
1438 "conditions": [
1439 {
1440 "kind": "contains_display_name"
1441 }
1442 ],
1443 "default": true,
1444 "enabled": true,
1445 "rule_id": ".m.rule.contains_display_name"
1446 },
1447 {
1448 "actions": [
1449 "notify",
1450 {
1451 "set_tweak": "highlight"
1452 }
1453 ],
1454 "conditions": [
1455 {
1456 "key": "type",
1457 "kind": "event_match",
1458 "pattern": "m.room.tombstone"
1459 },
1460 {
1461 "key": "state_key",
1462 "kind": "event_match",
1463 "pattern": ""
1464 }
1465 ],
1466 "default": true,
1467 "enabled": false,
1468 "rule_id": ".m.rule.tombstone"
1469 },
1470 {
1471 "actions": [
1472 "notify",
1473 {
1474 "set_tweak": "highlight"
1475 }
1476 ],
1477 "conditions": [
1478 {
1479 "key": "content.body",
1480 "kind": "event_match",
1481 "pattern": "@room"
1482 },
1483 {
1484 "key": "room",
1485 "kind": "sender_notification_permission"
1486 }
1487 ],
1488 "default": true,
1489 "enabled": true,
1490 "rule_id": ".m.rule.roomnotif"
1491 },
1492 {
1493 "actions": [
1494 "dont_notify"
1495 ],
1496 "conditions": [
1497 {
1498 "key": "type",
1499 "kind": "event_match",
1500 "pattern": "m.reaction"
1501 }
1502 ],
1503 "default": true,
1504 "enabled": true,
1505 "rule_id": ".m.rule.reaction"
1506 }
1507 ],
1508 "room": [
1509 {
1510 "actions": [
1511 "dont_notify"
1512 ],
1513 "default": false,
1514 "enabled": true,
1515 "rule_id": "!UbCmIlGTHNIgIRZcpt:nheko.im"
1516 },
1517 {
1518 "actions": [
1519 "dont_notify"
1520 ],
1521 "default": false,
1522 "enabled": true,
1523 "rule_id": "!gXyPibMbVgafeoHKIc:matrix.org"
1524 },
1525 {
1526 "actions": [
1527 "dont_notify"
1528 ],
1529 "default": false,
1530 "enabled": true,
1531 "rule_id": "!tSZeOIVRJwMOVgkcCT:libera.chat"
1532 }
1533 ],
1534 "sender": [],
1535 "underride": [
1536 {
1537 "actions": [
1538 "notify",
1539 {
1540 "set_tweak": "sound",
1541 "value": "ring"
1542 },
1543 {
1544 "set_tweak": "highlight",
1545 "value": false
1546 }
1547 ],
1548 "conditions": [
1549 {
1550 "key": "type",
1551 "kind": "event_match",
1552 "pattern": "m.call.invite"
1553 }
1554 ],
1555 "default": true,
1556 "enabled": true,
1557 "rule_id": ".m.rules.call"
1558 },
1559 {
1560 "actions": [
1561 "notify",
1562 {
1563 "set_tweak": "sound",
1564 "value": "default"
1565 },
1566 {
1567 "set_tweak": "highlight",
1568 "value": false
1569 }
1570 ],
1571 "conditions": [
1572 {
1573 "is": "2",
1574 "kind": "room_member_count"
1575 },
1576 {
1577 "key": "type",
1578 "kind": "event_match",
1579 "pattern": "m.room.encrypted"
1580 }
1581 ],
1582 "default": true,
1583 "enabled": true,
1584 "rule_id": ".m.rules.encrypted_room_one_to_one"
1585 },
1586 {
1587 "actions": [
1588 "notify",
1589 {
1590 "set_tweak": "sound",
1591 "value": "default"
1592 },
1593 {
1594 "set_tweak": "highlight",
1595 "value": false
1596 }
1597 ],
1598 "conditions": [
1599 {
1600 "is": "2",
1601 "kind": "room_member_count"
1602 },
1603 {
1604 "key": "type",
1605 "kind": "event_match",
1606 "pattern": "m.room.message"
1607 }
1608 ],
1609 "default": true,
1610 "enabled": true,
1611 "rule_id": ".m.rules.room_one_to_one"
1612 },
1613 {
1614 "actions": [
1615 "notify",
1616 {
1617 "set_tweak": "highlight",
1618 "value": false
1619 }
1620 ],
1621 "conditions": [
1622 {
1623 "key": "type",
1624 "kind": "event_match",
1625 "pattern": "m.room.message"
1626 }
1627 ],
1628 "default": true,
1629 "enabled": true,
1630 "rule_id": ".m.rules.message"
1631 },
1632 {
1633 "actions": [
1634 "notify",
1635 {
1636 "set_tweak": "highlight",
1637 "value": false
1638 }
1639 ],
1640 "conditions": [
1641 {
1642 "key": "type",
1643 "kind": "event_match",
1644 "pattern": "m.room.encrypted"
1645 }
1646 ],
1647 "default": true,
1648 "enabled": true,
1649 "rule_id": ".m.rules.encrypted"
1650 }
1651 ]
1652 }
1653 })"_json;
1654
1655 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
1656
1657 json raw_event = R"(
1658 {
1659 "content": {
1660 "body": "> <@lordmzte:mzte.de> btw, im still not getting notification on replies, only explicit mentions.\n\nprobably because of your username :p",
1661 "format": "org.matrix.custom.html",
1662 "formatted_body": "<mx-reply><blockquote><a href=\"https://matrix.to/#/!UbCmIlGTHNIgIRZcpt:nheko.im/$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU\">In reply to</a> <a href=\"https://matrix.to/#/@lordmzte:mzte.de\">@lordmzte:mzte.de</a><br/>btw, im still not getting notification on replies, only explicit mentions.</blockquote></mx-reply>probably because of your username :p",
1663 "im.nheko.relations.v1.relations": [
1664 {
1665 "event_id": "$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU",
1666 "rel_type": "im.nheko.relations.v1.in_reply_to"
1667 }
1668 ],
1669 "m.relates_to": {
1670 "m.in_reply_to": {
1671 "event_id": "$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU"
1672 }
1673 },
1674 "msgtype": "m.text"
1675 },
1676 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
1677 "origin_server_ts": 1666006933326,
1678 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
1679 "sender": "@deepbluev7:neko.dev",
1680 "type": "m.room.message",
1681 "unsigned": {
1682 "age": 568918
1683 }
1684 })"_json;
1685 auto text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
1686 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
1687 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
1688
1689 auto actions = evaluator.evaluate({text}, ctx, {});
1690
1691 auto notifies = [](const std::vector<mtx::pushrules::actions::Action> &a) {
1692 for (const auto &action : a) {
1693 if (action == mtx::pushrules::actions::Action{mtx::pushrules::actions::notify{}}) {
1694 return true;
1695 }
1696 }
1697 return false;
1698 };
1699
1700 EXPECT_TRUE(notifies(actions));
1701 }
1702
1703 TEST(Pushrules, ReactionDoesNotMatch)
1704 {
1705 json raw_rule = R"(
1706 {
1707 "global": {
1708 "content": [
1709 {
1710 "actions": [
1711 "notify",
1712 {
1713 "set_tweak": "highlight",
1714 "value": false
1715 }
1716 ],
1717 "pattern": "nheko",
1718 "rule_id": "nheko"
1719 },
1720 {
1721 "actions": [
1722 "notify",
1723 {
1724 "set_tweak": "highlight",
1725 "value": false
1726 }
1727 ],
1728 "pattern": "Nheko",
1729 "rule_id": "Nheko"
1730 },
1731 {
1732 "actions": [
1733 "notify",
1734 {
1735 "set_tweak": "highlight",
1736 "value": false
1737 }
1738 ],
1739 "pattern": "nico",
1740 "rule_id": "nico"
1741 },
1742 {
1743 "actions": [
1744 "notify",
1745 {
1746 "set_tweak": "highlight",
1747 "value": false
1748 }
1749 ],
1750 "pattern": "Nico",
1751 "rule_id": "Nico"
1752 },
1753 {
1754 "actions": [
1755 "notify",
1756 {
1757 "set_tweak": "highlight",
1758 "value": false
1759 }
1760 ],
1761 "pattern": "cc2",
1762 "rule_id": "cc2"
1763 }
1764 ],
1765 "override": [
1766 {
1767 "actions": [
1768 "dont_notify"
1769 ],
1770 "default": true,
1771 "enabled": false,
1772 "rule_id": ".m.rule.master"
1773 },
1774 {
1775 "actions": [
1776 "dont_notify"
1777 ],
1778 "conditions": [
1779 {
1780 "key": "content.msgtype",
1781 "kind": "event_match",
1782 "pattern": "m.notice"
1783 }
1784 ],
1785 "default": true,
1786 "rule_id": ".m.rule.suppress_notices"
1787 },
1788 {
1789 "actions": [
1790 "notify",
1791 {
1792 "set_tweak": "highlight",
1793 "value": false
1794 },
1795 {
1796 "set_tweak": "sound",
1797 "value": "default"
1798 }
1799 ],
1800 "conditions": [
1801 {
1802 "key": "type",
1803 "kind": "event_match",
1804 "pattern": "m.room.member"
1805 },
1806 {
1807 "key": "content.membership",
1808 "kind": "event_match",
1809 "pattern": "invite"
1810 },
1811 {
1812 "key": "state_key",
1813 "kind": "event_match",
1814 "pattern": "@deepbluev7:neko.dev"
1815 }
1816 ],
1817 "default": true,
1818 "rule_id": ".m.rule.invite_for_me"
1819 },
1820 {
1821 "actions": [
1822 "dont_notify"
1823 ],
1824 "conditions": [
1825 {
1826 "key": "type",
1827 "kind": "event_match",
1828 "pattern": "m.room.member"
1829 }
1830 ],
1831 "default": true,
1832 "rule_id": ".m.rule.member_event"
1833 },
1834 {
1835 "actions": [
1836 "notify",
1837 {
1838 "set_tweak": "highlight"
1839 },
1840 {
1841 "set_tweak": "sound",
1842 "value": "default"
1843 }
1844 ],
1845 "conditions": [
1846 {
1847 "key": "sender",
1848 "kind": "im.nheko.msc3664.related_event_match",
1849 "pattern": "@deepbluev7:neko.dev",
1850 "rel_type": "m.in_reply_to"
1851 }
1852 ],
1853 "default": true,
1854 "rule_id": ".im.nheko.msc3664.reply"
1855 },
1856 {
1857 "actions": [
1858 "notify",
1859 {
1860 "set_tweak": "highlight"
1861 },
1862 {
1863 "set_tweak": "sound",
1864 "value": "default"
1865 }
1866 ],
1867 "conditions": [
1868 {
1869 "kind": "contains_display_name"
1870 }
1871 ],
1872 "default": true,
1873 "rule_id": ".m.rule.contains_display_name"
1874 },
1875 {
1876 "actions": [
1877 "notify",
1878 {
1879 "set_tweak": "highlight"
1880 }
1881 ],
1882 "conditions": [
1883 {
1884 "key": "room",
1885 "kind": "sender_notification_permission"
1886 },
1887 {
1888 "key": "content.body",
1889 "kind": "event_match",
1890 "pattern": "@room"
1891 }
1892 ],
1893 "default": true,
1894 "rule_id": ".m.rule.roomnotif"
1895 },
1896 {
1897 "actions": [
1898 "notify",
1899 {
1900 "set_tweak": "highlight"
1901 }
1902 ],
1903 "conditions": [
1904 {
1905 "key": "type",
1906 "kind": "event_match",
1907 "pattern": "m.room.tombstone"
1908 },
1909 {
1910 "key": "state_key",
1911 "kind": "event_match"
1912 }
1913 ],
1914 "default": true,
1915 "rule_id": ".m.rule.tombstone"
1916 },
1917 {
1918 "actions": [
1919 "dont_notify"
1920 ],
1921 "conditions": [
1922 {
1923 "key": "type",
1924 "kind": "event_match",
1925 "pattern": "m.reaction"
1926 }
1927 ],
1928 "default": true,
1929 "rule_id": ".m.rule.reaction"
1930 },
1931 {
1932 "conditions": [
1933 {
1934 "key": "type",
1935 "kind": "event_match",
1936 "pattern": "m.room.server_acl"
1937 },
1938 {
1939 "key": "state_key",
1940 "kind": "event_match"
1941 }
1942 ],
1943 "default": true,
1944 "rule_id": ".m.rule.room.server_acl"
1945 }
1946 ],
1947 "room": [
1948 {
1949 "actions": [
1950 "notify",
1951 {
1952 "set_tweak": "highlight",
1953 "value": false
1954 },
1955 {
1956 "set_tweak": "sound",
1957 "value": "default"
1958 }
1959 ],
1960 "rule_id": "!gHDcupDIWdXmlHOhoN:neko.dev"
1961 }
1962 ],
1963 "sender": [
1964 ],
1965 "underride": [
1966 {
1967 "actions": [
1968 "notify",
1969 {
1970 "set_tweak": "sound",
1971 "value": "ring"
1972 },
1973 {
1974 "set_tweak": "highlight",
1975 "value": false
1976 }
1977 ],
1978 "conditions": [
1979 {
1980 "key": "type",
1981 "kind": "event_match",
1982 "pattern": "m.call.invite"
1983 }
1984 ],
1985 "default": true,
1986 "rule_id": ".m.rule.call"
1987 },
1988 {
1989 "actions": [
1990 "notify",
1991 {
1992 "set_tweak": "sound",
1993 "value": "default"
1994 },
1995 {
1996 "set_tweak": "highlight",
1997 "value": false
1998 }
1999 ],
2000 "conditions": [
2001 {
2002 "key": "type",
2003 "kind": "event_match",
2004 "pattern": "m.room.message"
2005 },
2006 {
2007 "is": "2",
2008 "kind": "room_member_count"
2009 }
2010 ],
2011 "default": true,
2012 "rule_id": ".m.rule.room_one_to_one"
2013 },
2014 {
2015 "actions": [
2016 "notify",
2017 {
2018 "set_tweak": "sound",
2019 "value": "default"
2020 },
2021 {
2022 "set_tweak": "highlight",
2023 "value": false
2024 }
2025 ],
2026 "conditions": [
2027 {
2028 "key": "type",
2029 "kind": "event_match",
2030 "pattern": "m.room.encrypted"
2031 },
2032 {
2033 "is": "2",
2034 "kind": "room_member_count"
2035 }
2036 ],
2037 "default": true,
2038 "rule_id": ".m.rule.encrypted_room_one_to_one"
2039 },
2040 {
2041 "actions": [
2042 "notify",
2043 {
2044 "set_tweak": "sound",
2045 "value": "default"
2046 },
2047 {
2048 "set_tweak": "highlight",
2049 "value": false
2050 }
2051 ],
2052 "conditions": [
2053 {
2054 "key": "type",
2055 "kind": "event_match",
2056 "pattern": "m.room.message"
2057 }
2058 ],
2059 "default": true,
2060 "rule_id": ".m.rule.message"
2061 },
2062 {
2063 "actions": [
2064 "notify",
2065 {
2066 "set_tweak": "sound",
2067 "value": "default"
2068 },
2069 {
2070 "set_tweak": "highlight",
2071 "value": false
2072 }
2073 ],
2074 "conditions": [
2075 {
2076 "key": "type",
2077 "kind": "event_match",
2078 "pattern": "m.room.encrypted"
2079 }
2080 ],
2081 "default": true,
2082 "rule_id": ".m.rule.encrypted"
2083 },
2084 {
2085 "actions": [
2086 "notify",
2087 {
2088 "set_tweak": "highlight",
2089 "value": false
2090 }
2091 ],
2092 "conditions": [
2093 {
2094 "key": "type",
2095 "kind": "event_match",
2096 "pattern": "im.vector.modular.widgets"
2097 },
2098 {
2099 "key": "content.type",
2100 "kind": "event_match",
2101 "pattern": "jitsi"
2102 },
2103 {
2104 "key": "state_key",
2105 "kind": "event_match",
2106 "pattern": "*"
2107 }
2108 ],
2109 "default": true,
2110 "rule_id": ".im.vector.jitsi"
2111 }
2112 ]
2113 }
2114 })"_json;
2115
2116 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2117
2118 json raw_event = R"(
2119 {
2120 "content": {
2121 "m.relates_to": {
2122 "event_id": "$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU",
2123 "key": "a",
2124 "rel_type": "m.annotation"
2125 }
2126 },
2127 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
2128 "origin_server_ts": 1666006933326,
2129 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
2130 "sender": "@deepbluev7:neko.dev",
2131 "type": "m.reaction",
2132 "unsigned": {
2133 "age": 568918
2134 }
2135 })"_json;
2136 auto text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Reaction>>();
2137 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
2138 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
2139
2140 auto actions = evaluator.evaluate({text}, ctx, {});
2141
2142 auto notifies = [](const std::vector<mtx::pushrules::actions::Action> &a) {
2143 for (const auto &action : a) {
2144 if (action == mtx::pushrules::actions::Action{mtx::pushrules::actions::notify{}}) {
2145 return true;
2146 }
2147 }
2148 return false;
2149 };
2150
2151 EXPECT_FALSE(notifies(actions));
2152 }
2153
2154 TEST(Pushrules, NormalMessageDoesNotHighlight)
2155 {
2156 json raw_rule =
2157 R"({
2158 "global": {
2159 "underride": [
2160 {
2161 "conditions": [
2162 {
2163 "kind": "event_match",
2164 "key": "type",
2165 "pattern": "m.call.invite"
2166 }
2167 ],
2168 "actions": [
2169 "notify",
2170 {
2171 "set_tweak": "sound",
2172 "value": "ring"
2173 },
2174 {
2175 "set_tweak": "highlight",
2176 "value": false
2177 }
2178 ],
2179 "rule_id": ".m.rule.call",
2180 "default": true,
2181 "enabled": true
2182 },
2183 {
2184 "conditions": [
2185 {
2186 "kind": "event_match",
2187 "key": "type",
2188 "pattern": "m.room.message"
2189 },
2190 {
2191 "kind": "room_member_count",
2192 "is": "2"
2193 }
2194 ],
2195 "actions": [
2196 "notify",
2197 {
2198 "set_tweak": "sound",
2199 "value": "default"
2200 },
2201 {
2202 "set_tweak": "highlight",
2203 "value": false
2204 }
2205 ],
2206 "rule_id": ".m.rule.room_one_to_one",
2207 "default": true,
2208 "enabled": true
2209 },
2210 {
2211 "conditions": [
2212 {
2213 "kind": "event_match",
2214 "key": "type",
2215 "pattern": "m.room.encrypted"
2216 },
2217 {
2218 "kind": "room_member_count",
2219 "is": "2"
2220 }
2221 ],
2222 "actions": [
2223 "notify",
2224 {
2225 "set_tweak": "sound",
2226 "value": "default"
2227 },
2228 {
2229 "set_tweak": "highlight",
2230 "value": false
2231 }
2232 ],
2233 "rule_id": ".m.rule.encrypted_room_one_to_one",
2234 "default": true,
2235 "enabled": true
2236 },
2237 {
2238 "conditions": [
2239 {
2240 "kind": "event_match",
2241 "key": "type",
2242 "pattern": "m.room.message"
2243 }
2244 ],
2245 "actions": [
2246 "notify",
2247 {
2248 "set_tweak": "highlight",
2249 "value": false
2250 }
2251 ],
2252 "rule_id": ".m.rule.message",
2253 "default": true,
2254 "enabled": true
2255 },
2256 {
2257 "conditions": [
2258 {
2259 "kind": "event_match",
2260 "key": "type",
2261 "pattern": "m.room.encrypted"
2262 }
2263 ],
2264 "actions": [
2265 "notify",
2266 {
2267 "set_tweak": "highlight",
2268 "value": false
2269 }
2270 ],
2271 "rule_id": ".m.rule.encrypted",
2272 "default": true,
2273 "enabled": true
2274 },
2275 {
2276 "conditions": [
2277 {
2278 "kind": "event_match",
2279 "key": "type",
2280 "pattern": "im.vector.modular.widgets"
2281 },
2282 {
2283 "kind": "event_match",
2284 "key": "content.type",
2285 "pattern": "jitsi"
2286 },
2287 {
2288 "kind": "event_match",
2289 "key": "state_key",
2290 "pattern": "*"
2291 }
2292 ],
2293 "actions": [
2294 "notify",
2295 {
2296 "set_tweak": "highlight",
2297 "value": false
2298 }
2299 ],
2300 "rule_id": ".im.vector.jitsi",
2301 "default": true,
2302 "enabled": true
2303 }
2304 ],
2305 "sender": [],
2306 "room": [
2307 ],
2308 "content": [
2309 {
2310 "actions": [
2311 "notify",
2312 {
2313 "set_tweak": "sound",
2314 "value": "default"
2315 },
2316 {
2317 "set_tweak": "highlight"
2318 }
2319 ],
2320 "pattern": "Stuart",
2321 "rule_id": "Stuart",
2322 "default": false,
2323 "enabled": true
2324 },
2325 {
2326 "actions": [
2327 "notify",
2328 {
2329 "set_tweak": "sound",
2330 "value": "default"
2331 },
2332 {
2333 "set_tweak": "highlight"
2334 }
2335 ],
2336 "pattern": "stuart",
2337 "rule_id": "stuart",
2338 "default": false,
2339 "enabled": true
2340 }
2341 ],
2342 "override": [
2343 {
2344 "conditions": [],
2345 "actions": [
2346 "dont_notify"
2347 ],
2348 "rule_id": ".m.rule.master",
2349 "default": true,
2350 "enabled": false
2351 },
2352 {
2353 "conditions": [
2354 {
2355 "kind": "event_match",
2356 "key": "content.msgtype",
2357 "pattern": "m.notice"
2358 }
2359 ],
2360 "actions": [
2361 "dont_notify"
2362 ],
2363 "rule_id": ".m.rule.suppress_notices",
2364 "default": true,
2365 "enabled": true
2366 },
2367 {
2368 "conditions": [
2369 {
2370 "kind": "event_match",
2371 "key": "type",
2372 "pattern": "m.room.member"
2373 },
2374 {
2375 "kind": "event_match",
2376 "key": "content.membership",
2377 "pattern": "invite"
2378 },
2379 {
2380 "kind": "event_match",
2381 "key": "state_key",
2382 "pattern": "@cadair:cadair.com"
2383 }
2384 ],
2385 "actions": [
2386 "notify",
2387 {
2388 "set_tweak": "highlight",
2389 "value": false
2390 },
2391 {
2392 "set_tweak": "sound",
2393 "value": "default"
2394 }
2395 ],
2396 "rule_id": ".m.rule.invite_for_me",
2397 "default": true,
2398 "enabled": true
2399 },
2400 {
2401 "conditions": [
2402 {
2403 "kind": "event_match",
2404 "key": "type",
2405 "pattern": "m.room.member"
2406 }
2407 ],
2408 "actions": [
2409 "dont_notify"
2410 ],
2411 "rule_id": ".m.rule.member_event",
2412 "default": true,
2413 "enabled": true
2414 },
2415 {
2416 "conditions": [
2417 {
2418 "kind": "contains_display_name"
2419 }
2420 ],
2421 "actions": [
2422 "notify",
2423 {
2424 "set_tweak": "highlight"
2425 },
2426 {
2427 "set_tweak": "sound",
2428 "value": "default"
2429 }
2430 ],
2431 "rule_id": ".m.rule.contains_display_name",
2432 "default": true,
2433 "enabled": true
2434 },
2435 {
2436 "conditions": [
2437 {
2438 "kind": "sender_notification_permission",
2439 "key": "room"
2440 },
2441 {
2442 "kind": "event_match",
2443 "key": "content.body",
2444 "pattern": "@room"
2445 }
2446 ],
2447 "actions": [
2448 "notify",
2449 {
2450 "set_tweak": "highlight"
2451 }
2452 ],
2453 "rule_id": ".m.rule.roomnotif",
2454 "default": true,
2455 "enabled": true
2456 },
2457 {
2458 "conditions": [
2459 {
2460 "kind": "event_match",
2461 "key": "type",
2462 "pattern": "m.room.tombstone"
2463 },
2464 {
2465 "kind": "event_match",
2466 "key": "state_key",
2467 "pattern": ""
2468 }
2469 ],
2470 "actions": [
2471 "notify",
2472 {
2473 "set_tweak": "highlight"
2474 }
2475 ],
2476 "rule_id": ".m.rule.tombstone",
2477 "default": true,
2478 "enabled": true
2479 },
2480 {
2481 "conditions": [
2482 {
2483 "kind": "event_match",
2484 "key": "type",
2485 "pattern": "m.reaction"
2486 }
2487 ],
2488 "actions": [
2489 "dont_notify"
2490 ],
2491 "rule_id": ".m.rule.reaction",
2492 "default": true,
2493 "enabled": true
2494 },
2495 {
2496 "conditions": [
2497 {
2498 "kind": "event_match",
2499 "key": "type",
2500 "pattern": "m.room.server_acl"
2501 },
2502 {
2503 "kind": "event_match",
2504 "key": "state_key",
2505 "pattern": ""
2506 }
2507 ],
2508 "actions": [],
2509 "rule_id": ".m.rule.room.server_acl",
2510 "default": true,
2511 "enabled": true
2512 }
2513 ]
2514 },
2515 "device": {}
2516 }
2517 )"_json;
2518
2519 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2520
2521 json raw_event = R"({
2522 "content": {
2523 "body": "You can hide them in the room settings",
2524 "msgtype": "m.text"
2525 },
2526 "event_id": "$4gnZ8Vop7BnXjpri2qH4wk2xZ7PRWzqMJWhDboe8Upc",
2527 "origin_server_ts": 1667410048371,
2528 "sender": "@deepbluev7:neko.dev",
2529 "type": "m.room.message",
2530 "unsigned": {
2531 "age": 69,
2532 "transaction_id": "mEfzSS17er68gvakLESSodiRsoDwvBqdk"
2533 }
2534 }
2535 )"_json;
2536 auto text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
2537 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
2538 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
2539
2540 auto actions = evaluator.evaluate({text}, ctx, {});
2541
2542 auto notifies = [](const std::vector<mtx::pushrules::actions::Action> &a) {
2543 for (const auto &action : a) {
2544 if (action == mtx::pushrules::actions::Action{mtx::pushrules::actions::notify{}}) {
2545 return true;
2546 }
2547 }
2548 return false;
2549 };
2550
2551 EXPECT_TRUE(notifies(actions));
2552 for (const auto &action : actions) {
2553 EXPECT_NE(action,
2554 mtx::pushrules::actions::Action{mtx::pushrules::actions::set_tweak_highlight{}});
2555 }
2556 }
2557
2558 TEST(Pushrules, RelatedEventMatch)
2559 {
2560 json raw_rule = R"(
2561 {
2562 "global": {
2563 "content": [
2564 ],
2565 "override": [
2566 {
2567 "actions": [
2568 "dont_notify"
2569 ],
2570 "default": true,
2571 "enabled": false,
2572 "rule_id": ".m.rule.master"
2573 },
2574 {
2575 "actions": [
2576 "notify",
2577 {
2578 "set_tweak": "highlight"
2579 },
2580 {
2581 "set_tweak": "sound",
2582 "value": "default"
2583 }
2584 ],
2585 "conditions": [
2586 {
2587 "key": "sender",
2588 "kind": "im.nheko.msc3664.related_event_match",
2589 "pattern": "@deepbluev7:neko.dev",
2590 "rel_type": "m.in_reply_to"
2591 }
2592 ],
2593 "default": true,
2594 "rule_id": ".im.nheko.msc3664.reply"
2595 }
2596 ],
2597 "room": [
2598 ],
2599 "sender": [
2600 ],
2601 "underride": [
2602 ]
2603 }
2604 })"_json;
2605
2606 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2607
2608 json raw_event = R"(
2609 {
2610 "content": {
2611 "body": "You can hide them in the room settings",
2612 "msgtype": "m.text",
2613 "m.relates_to": {
2614 "m.in_reply_to": {
2615 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de"
2616 },
2617 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de",
2618 "rel_type": "m.thread"
2619 }
2620 },
2621 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
2622 "origin_server_ts": 1666006933326,
2623 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
2624 "sender": "@nico:neko.dev",
2625 "type": "m.room.message"
2626 })"_json;
2627 json related_raw_event = R"(
2628 {
2629 "content": {
2630 "body": "You can hide them in the room settings",
2631 "msgtype": "m.text"
2632 },
2633 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de",
2634 "origin_server_ts": 1666006933326,
2635 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
2636 "sender": "@deepbluev7:neko.dev",
2637 "type": "m.room.message"
2638 })"_json;
2639
2640 auto text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
2641 auto related_text = related_raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
2642 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
2643 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
2644
2645 auto actions = evaluator.evaluate({text},
2646 ctx,
2647 {
2648 {text.content.relations.relations.at(0), {related_text}},
2649 {text.content.relations.relations.at(1), {related_text}},
2650 });
2651
2652 auto notifies = [](const std::vector<mtx::pushrules::actions::Action> &a) {
2653 for (const auto &action : a) {
2654 if (action == mtx::pushrules::actions::Action{mtx::pushrules::actions::notify{}}) {
2655 return true;
2656 }
2657 }
2658 return false;
2659 };
2660
2661 EXPECT_TRUE(
2662 notifies(evaluator.evaluate({text},
2663 ctx,
2664 {
2665 {text.content.relations.relations.at(0), {related_text}},
2666 {text.content.relations.relations.at(1), {related_text}},
2667 })));
2668 EXPECT_FALSE(notifies(evaluator.evaluate({text}, ctx, {})));
2669
2670 raw_rule = R"(
2671 {
2672 "global": {
2673 "content": [
2674 ],
2675 "override": [
2676 {
2677 "actions": [
2678 "dont_notify"
2679 ],
2680 "default": true,
2681 "enabled": false,
2682 "rule_id": ".m.rule.master"
2683 },
2684 {
2685 "actions": [
2686 "notify",
2687 {
2688 "set_tweak": "highlight"
2689 },
2690 {
2691 "set_tweak": "sound",
2692 "value": "default"
2693 }
2694 ],
2695 "conditions": [
2696 {
2697 "key": "sender",
2698 "kind": "im.nheko.msc3664.related_event_match",
2699 "pattern": "@deepbluev7:neko.dev",
2700 "rel_type": "m.thread"
2701 }
2702 ],
2703 "default": true,
2704 "rule_id": ".im.nheko.msc3664.reply"
2705 }
2706 ],
2707 "room": [
2708 ],
2709 "sender": [
2710 ],
2711 "underride": [
2712 ]
2713 }
2714 })"_json;
2715
2716 ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2717 mtx::pushrules::PushRuleEvaluator evaluator1{ruleset.global};
2718 EXPECT_TRUE(
2719 notifies(evaluator1.evaluate({text},
2720 ctx,
2721 {
2722 {text.content.relations.relations.at(0), {related_text}},
2723 {text.content.relations.relations.at(1), {related_text}},
2724 })));
2725 EXPECT_FALSE(notifies(evaluator1.evaluate({text}, ctx, {})));
2726
2727 raw_rule = R"(
2728 {
2729 "global": {
2730 "content": [
2731 ],
2732 "override": [
2733 {
2734 "actions": [
2735 "dont_notify"
2736 ],
2737 "default": true,
2738 "enabled": false,
2739 "rule_id": ".m.rule.master"
2740 },
2741 {
2742 "actions": [
2743 "notify",
2744 {
2745 "set_tweak": "highlight"
2746 },
2747 {
2748 "set_tweak": "sound",
2749 "value": "default"
2750 }
2751 ],
2752 "conditions": [
2753 {
2754 "kind": "im.nheko.msc3664.related_event_match",
2755 "rel_type": "m.thread"
2756 }
2757 ],
2758 "default": true,
2759 "rule_id": ".im.nheko.msc3664.reply"
2760 }
2761 ],
2762 "room": [
2763 ],
2764 "sender": [
2765 ],
2766 "underride": [
2767 ]
2768 }
2769 })"_json;
2770
2771 ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2772 mtx::pushrules::PushRuleEvaluator evaluator2{ruleset.global};
2773 EXPECT_TRUE(
2774 notifies(evaluator2.evaluate({text},
2775 ctx,
2776 {
2777 {text.content.relations.relations.at(0), {related_text}},
2778 {text.content.relations.relations.at(1), {related_text}},
2779 })));
2780 EXPECT_FALSE(notifies(evaluator2.evaluate({text}, ctx, {})));
2781
2782 raw_rule = R"(
2783 {
2784 "global": {
2785 "content": [
2786 ],
2787 "override": [
2788 {
2789 "actions": [
2790 "dont_notify"
2791 ],
2792 "default": true,
2793 "enabled": false,
2794 "rule_id": ".m.rule.master"
2795 },
2796 {
2797 "actions": [
2798 "notify",
2799 {
2800 "set_tweak": "highlight"
2801 },
2802 {
2803 "set_tweak": "sound",
2804 "value": "default"
2805 }
2806 ],
2807 "conditions": [
2808 {
2809 "key": "sender",
2810 "kind": "im.nheko.msc3664.related_event_match",
2811 "pattern": "@deepbluev8:neko.dev",
2812 "rel_type": "m.in_reply_to"
2813 }
2814 ],
2815 "default": true,
2816 "rule_id": ".im.nheko.msc3664.reply"
2817 }
2818 ],
2819 "room": [
2820 ],
2821 "sender": [
2822 ],
2823 "underride": [
2824 ]
2825 }
2826 })"_json;
2827
2828 ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2829 mtx::pushrules::PushRuleEvaluator evaluator3{ruleset.global};
2830 EXPECT_FALSE(
2831 notifies(evaluator3.evaluate({text},
2832 ctx,
2833 {
2834 {text.content.relations.relations.at(0), {related_text}},
2835 {text.content.relations.relations.at(1), {related_text}},
2836 })));
2837 EXPECT_FALSE(notifies(evaluator3.evaluate({text}, ctx, {})));
2838
2839 raw_event = R"(
2840 {
2841 "content": {
2842 "body": "You can hide them in the room settings",
2843 "msgtype": "m.text",
2844 "m.relates_to": {
2845 "m.in_reply_to": {
2846 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de"
2847 },
2848 "is_falling_back": true,
2849 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de",
2850 "rel_type": "m.thread"
2851 }
2852 },
2853 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
2854 "origin_server_ts": 1666006933326,
2855 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
2856 "sender": "@nico:neko.dev",
2857 "type": "m.room.message"
2858 })"_json;
2859 text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
2860 EXPECT_FALSE(
2861 notifies(evaluator.evaluate({text},
2862 ctx,
2863 {
2864 {text.content.relations.relations.at(0), {related_text}},
2865 {text.content.relations.relations.at(1), {related_text}},
2866 })));
2867 EXPECT_FALSE(notifies(evaluator.evaluate({text}, ctx, {})));
2868
2869 raw_rule = R"(
2870 {
2871 "global": {
2872 "content": [
2873 ],
2874 "override": [
2875 {
2876 "actions": [
2877 "dont_notify"
2878 ],
2879 "default": true,
2880 "enabled": false,
2881 "rule_id": ".m.rule.master"
2882 },
2883 {
2884 "actions": [
2885 "notify",
2886 {
2887 "set_tweak": "highlight"
2888 },
2889 {
2890 "set_tweak": "sound",
2891 "value": "default"
2892 }
2893 ],
2894 "conditions": [
2895 {
2896 "key": "sender",
2897 "kind": "im.nheko.msc3664.related_event_match",
2898 "pattern": "@deepbluev7:neko.dev",
2899 "rel_type": "m.in_reply_to",
2900 "include_fallback": true
2901 }
2902 ],
2903 "default": true,
2904 "rule_id": ".im.nheko.msc3664.reply"
2905 }
2906 ],
2907 "room": [
2908 ],
2909 "sender": [
2910 ],
2911 "underride": [
2912 ]
2913 }
2914 })"_json;
2915
2916 ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2917 mtx::pushrules::PushRuleEvaluator evaluator4{ruleset.global};
2918 EXPECT_TRUE(
2919 notifies(evaluator4.evaluate({text},
2920 ctx,
2921 {
2922 {text.content.relations.relations.at(0), {related_text}},
2923 {text.content.relations.relations.at(1), {related_text}},
2924 })));
2925 EXPECT_FALSE(notifies(evaluator4.evaluate({text}, ctx, {})));
2926
2927 raw_rule = R"(
2928 {
2929 "global": {
2930 "content": [
2931 ],
2932 "override": [
2933 {
2934 "actions": [
2935 "dont_notify"
2936 ],
2937 "default": true,
2938 "enabled": false,
2939 "rule_id": ".m.rule.master"
2940 },
2941 {
2942 "actions": [
2943 "notify",
2944 {
2945 "set_tweak": "highlight"
2946 },
2947 {
2948 "set_tweak": "sound",
2949 "value": "default"
2950 }
2951 ],
2952 "conditions": [
2953 {
2954 "key": "sender",
2955 "kind": "im.nheko.msc3664.related_event_match",
2956 "pattern": "@deepbluev7:neko.dev",
2957 "rel_type": "m.in_reply_to"
2958 }
2959 ],
2960 "default": true,
2961 "rule_id": ".im.nheko.msc3664.reply"
2962 }
2963 ],
2964 "room": [
2965 ],
2966 "sender": [
2967 ],
2968 "underride": [
2969 ]
2970 }
2971 })"_json;
2972
2973 raw_event = R"(
2974 {
2975 "content": {
2976 "body": "You can hide them in the room settings",
2977 "msgtype": "m.emote",
2978 "m.relates_to": {
2979 "m.in_reply_to": {
2980 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de"
2981 },
2982 "event_id": "$aaaaaaaaaaaaaaaaaaaa:bc.de",
2983 "rel_type": "m.thread"
2984 }
2985 },
2986 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
2987 "origin_server_ts": 1666006933326,
2988 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
2989 "sender": "@nico:neko.dev",
2990 "type": "m.room.message"
2991 })"_json;
2992 auto emote = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Emote>>();
2993
2994 ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
2995 mtx::pushrules::PushRuleEvaluator evaluator5{ruleset.global};
2996 EXPECT_TRUE(
2997 notifies(evaluator5.evaluate({emote},
2998 ctx,
2999 {
3000 {emote.content.relations.relations.at(0), {related_text}},
3001 {emote.content.relations.relations.at(1), {related_text}},
3002 })));
3003 EXPECT_FALSE(notifies(evaluator5.evaluate({emote}, ctx, {})));
3004 }
3005
3006 TEST(Pushrules, EmptyOrInvalidContentRulesDontMatch)
3007 {
3008 json raw_rule = R"(
3009 {
3010 "global": {
3011 "content": [
3012 {
3013 "actions": [
3014 "notify",
3015 {
3016 "set_tweak": "sound",
3017 "value": "default"
3018 },
3019 {
3020 "set_tweak": "highlight",
3021 "value": true
3022 }
3023 ],
3024 "conditions": [
3025 {
3026 "kind": "contains_user_mxid"
3027 }
3028 ],
3029 "default": true,
3030 "enabled": true,
3031 "rule_id": ".m.rule.contains_user_name"
3032 }
3033 ],
3034 "override": [
3035 {
3036 "actions": [
3037 "dont_notify"
3038 ],
3039 "conditions": [],
3040 "default": true,
3041 "enabled": false,
3042 "rule_id": ".m.rule.master"
3043 },
3044 {
3045 "actions": [
3046 "dont_notify"
3047 ],
3048 "conditions": [
3049 {
3050 "key": "content.msgtype",
3051 "kind": "event_match",
3052 "pattern": "m.notice"
3053 }
3054 ],
3055 "default": true,
3056 "enabled": true,
3057 "rule_id": ".m.rule.suppress_notices"
3058 },
3059 {
3060 "actions": [
3061 "notify",
3062 {
3063 "set_tweak": "sound",
3064 "value": "default"
3065 },
3066 {
3067 "set_tweak": "highlight",
3068 "value": false
3069 }
3070 ],
3071 "conditions": [
3072 {
3073 "key": "type",
3074 "kind": "event_match",
3075 "pattern": "m.room.member"
3076 },
3077 {
3078 "key": "content.membership",
3079 "kind": "event_match",
3080 "pattern": "invite"
3081 },
3082 {
3083 "kind": "state_key_user_mxid"
3084 }
3085 ],
3086 "default": true,
3087 "enabled": true,
3088 "rule_id": ".m.rule.invite_for_me"
3089 },
3090 {
3091 "actions": [
3092 "dont_notify"
3093 ],
3094 "conditions": [
3095 {
3096 "key": "type",
3097 "kind": "event_match",
3098 "pattern": "m.room.member"
3099 }
3100 ],
3101 "default": true,
3102 "enabled": true,
3103 "rule_id": ".m.rule.member_event"
3104 },
3105 {
3106 "actions": [
3107 "notify",
3108 {
3109 "set_tweak": "sound",
3110 "value": "default"
3111 },
3112 {
3113 "set_tweak": "highlight"
3114 }
3115 ],
3116 "conditions": [
3117 {
3118 "kind": "contains_display_name"
3119 }
3120 ],
3121 "default": true,
3122 "enabled": true,
3123 "rule_id": ".m.rule.contains_display_name"
3124 },
3125 {
3126 "actions": [
3127 "notify",
3128 {
3129 "set_tweak": "highlight",
3130 "value": true
3131 }
3132 ],
3133 "conditions": [
3134 {
3135 "key": "type",
3136 "kind": "event_match",
3137 "pattern": "m.room.tombstone"
3138 },
3139 {
3140 "key": "state_key",
3141 "kind": "event_match",
3142 "pattern": ""
3143 }
3144 ],
3145 "default": true,
3146 "enabled": true,
3147 "rule_id": ".m.rule.tombstone"
3148 },
3149 {
3150 "actions": [
3151 "notify",
3152 {
3153 "set_tweak": "highlight",
3154 "value": true
3155 }
3156 ],
3157 "conditions": [
3158 {
3159 "key": "content.body",
3160 "kind": "event_match",
3161 "pattern": "@room"
3162 },
3163 {
3164 "key": "room",
3165 "kind": "sender_notification_permission"
3166 }
3167 ],
3168 "default": true,
3169 "enabled": true,
3170 "rule_id": ".m.rule.roomnotif"
3171 },
3172 {
3173 "actions": [
3174 "dont_notify"
3175 ],
3176 "conditions": [
3177 {
3178 "key": "type",
3179 "kind": "event_match",
3180 "pattern": "m.reaction"
3181 }
3182 ],
3183 "default": true,
3184 "enabled": true,
3185 "rule_id": ".m.rule.reaction"
3186 },
3187 {
3188 "actions": [
3189 "dont_notify"
3190 ],
3191 "conditions": [
3192 {
3193 "key": "room_id",
3194 "kind": "event_match",
3195 "pattern": "!eMqocHMcvmMFuUpioO:matrix.org"
3196 }
3197 ],
3198 "default": false,
3199 "enabled": true,
3200 "rule_id": "!eMqocHMcvmMFuUpioO:matrix.org"
3201 },
3202 {
3203 "actions": [
3204 "dont_notify"
3205 ],
3206 "conditions": [
3207 {
3208 "key": "room_id",
3209 "kind": "event_match",
3210 "pattern": "!n6gxWBq2KfcWGcQ5IV:zemos.net"
3211 }
3212 ],
3213 "default": false,
3214 "enabled": true,
3215 "rule_id": "!n6gxWBq2KfcWGcQ5IV:zemos.net"
3216 }
3217 ],
3218 "room": [],
3219 "sender": [],
3220 "underride": [
3221 {
3222 "actions": [
3223 "notify",
3224 {
3225 "set_tweak": "sound",
3226 "value": "ring"
3227 },
3228 {
3229 "set_tweak": "highlight",
3230 "value": false
3231 }
3232 ],
3233 "conditions": [
3234 {
3235 "key": "type",
3236 "kind": "event_match",
3237 "pattern": "m.call.invite"
3238 }
3239 ],
3240 "default": true,
3241 "enabled": true,
3242 "rule_id": ".m.rule.call"
3243 },
3244 {
3245 "actions": [
3246 "notify",
3247 {
3248 "set_tweak": "sound",
3249 "value": "default"
3250 },
3251 {
3252 "set_tweak": "highlight",
3253 "value": false
3254 }
3255 ],
3256 "conditions": [
3257 {
3258 "key": "type",
3259 "kind": "event_match",
3260 "pattern": "m.room.encrypted"
3261 },
3262 {
3263 "is": "2",
3264 "kind": "room_member_count"
3265 }
3266 ],
3267 "default": true,
3268 "enabled": true,
3269 "rule_id": ".m.rule.encrypted_room_one_to_one"
3270 },
3271 {
3272 "actions": [
3273 "notify",
3274 {
3275 "set_tweak": "sound",
3276 "value": "default"
3277 },
3278 {
3279 "set_tweak": "highlight",
3280 "value": false
3281 }
3282 ],
3283 "conditions": [
3284 {
3285 "key": "type",
3286 "kind": "event_match",
3287 "pattern": "m.room.message"
3288 },
3289 {
3290 "is": "2",
3291 "kind": "room_member_count"
3292 }
3293 ],
3294 "default": true,
3295 "enabled": true,
3296 "rule_id": ".m.rule.room_one_to_one"
3297 },
3298 {
3299 "actions": [
3300 "notify",
3301 {
3302 "set_tweak": "highlight",
3303 "value": false
3304 }
3305 ],
3306 "conditions": [
3307 {
3308 "key": "type",
3309 "kind": "event_match",
3310 "pattern": "m.room.encrypted"
3311 }
3312 ],
3313 "default": true,
3314 "enabled": true,
3315 "rule_id": ".m.rule.encrypted"
3316 },
3317 {
3318 "actions": [
3319 "notify",
3320 {
3321 "set_tweak": "highlight",
3322 "value": false
3323 }
3324 ],
3325 "conditions": [
3326 {
3327 "key": "type",
3328 "kind": "event_match",
3329 "pattern": "m.room.message"
3330 }
3331 ],
3332 "default": true,
3333 "enabled": true,
3334 "rule_id": ".m.rule.message"
3335 }
3336 ]
3337 }
3338 }
3339 )"_json;
3340
3341 mtx::pushrules::GlobalRuleset ruleset = raw_rule.get<mtx::pushrules::GlobalRuleset>();
3342
3343 json raw_event = R"(
3344 {
3345 "content": {
3346 "body": "> <@lordmzte:mzte.de> btw, im still not getting notification on replies, only explicit mentions.\n\nprobably because of your username :p",
3347 "format": "org.matrix.custom.html",
3348 "formatted_body": "<mx-reply><blockquote><a href=\"https://matrix.to/#/!UbCmIlGTHNIgIRZcpt:nheko.im/$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU\">In reply to</a> <a href=\"https://matrix.to/#/@lordmzte:mzte.de\">@lordmzte:mzte.de</a><br/>btw, im still not getting notification on replies, only explicit mentions.</blockquote></mx-reply>probably because of your username :p",
3349 "im.nheko.relations.v1.relations": [
3350 {
3351 "event_id": "$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU",
3352 "rel_type": "im.nheko.relations.v1.in_reply_to"
3353 }
3354 ],
3355 "m.relates_to": {
3356 "m.in_reply_to": {
3357 "event_id": "$ifmL9zdEQjnec3LlgxX0Bqr7xVm0agynZBglt7q59AU"
3358 }
3359 },
3360 "msgtype": "m.text"
3361 },
3362 "event_id": "$mol6Bt546FLBNM7WgTj8mGTAieS8ZsX3JLZ2MH9qQwg",
3363 "origin_server_ts": 1666006933326,
3364 "room_id": "!UbCmIlGTHNIgIRZcpt:nheko.im",
3365 "sender": "@deepbluev7:neko.dev",
3366 "type": "m.room.message",
3367 "unsigned": {
3368 "age": 568918
3369 }
3370 })"_json;
3371 auto text = raw_event.get<mtx::events::RoomEvent<mtx::events::msg::Text>>();
3372 mtx::pushrules::PushRuleEvaluator evaluator{ruleset.global};
3373 mtx::pushrules::PushRuleEvaluator::RoomContext ctx{};
3374 ctx.user_display_name = "Jason";
3375
3376 auto actions = evaluator.evaluate({text}, ctx, {});
3377
3378 auto notifies = [](const std::vector<mtx::pushrules::actions::Action> &a) {
3379 for (const auto &action : a) {
3380 if (action ==
3381 mtx::pushrules::actions::Action{mtx::pushrules::actions::set_tweak_highlight{}}) {
3382 return true;
3383 }
3384 }
3385 return false;
3386 };
3387
3388 EXPECT_FALSE(notifies(actions));
3389 }