ruma-common 0.17.1

Common types for other ruma crates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
use std::{
    collections::BTreeMap, future::Future, ops::RangeBounds, pin::Pin, str::FromStr, sync::Arc,
};

use js_int::{Int, UInt};
use regex::bytes::Regex;
#[cfg(feature = "unstable-msc3931")]
use ruma_macros::StringEnum;
use serde::{Deserialize, Serialize};
use serde_json::value::Value as JsonValue;
use wildmatch::WildMatch;

use crate::{
    EventId, OwnedRoomId, OwnedUserId, UserId,
    power_levels::{NotificationPowerLevels, NotificationPowerLevelsKey},
    room_version_rules::RoomPowerLevelsRules,
};
#[cfg(feature = "unstable-msc3931")]
use crate::{PrivOwnedStr, RoomVersionId};

mod flattened_json;
mod push_condition_serde;
mod room_member_count_is;

pub use self::{
    flattened_json::{FlattenedJson, FlattenedJsonValue, ScalarJsonValue},
    room_member_count_is::{ComparisonOperator, RoomMemberCountIs},
};

/// Features supported by room versions.
#[cfg(feature = "unstable-msc3931")]
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, StringEnum)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum RoomVersionFeature {
    /// m.extensible_events
    ///
    /// The room supports [extensible events].
    ///
    /// [extensible events]: https://github.com/matrix-org/matrix-spec-proposals/pull/1767
    #[cfg(feature = "unstable-msc3932")]
    #[ruma_enum(rename = "org.matrix.msc3932.extensible_events")]
    ExtensibleEvents,

    #[doc(hidden)]
    _Custom(PrivOwnedStr),
}

#[cfg(feature = "unstable-msc3931")]
impl RoomVersionFeature {
    /// Get the default features for the given room version.
    pub fn list_for_room_version(version: &RoomVersionId) -> Vec<Self> {
        match version {
            RoomVersionId::V1
            | RoomVersionId::V2
            | RoomVersionId::V3
            | RoomVersionId::V4
            | RoomVersionId::V5
            | RoomVersionId::V6
            | RoomVersionId::V7
            | RoomVersionId::V8
            | RoomVersionId::V9
            | RoomVersionId::V10
            | RoomVersionId::V11
            | RoomVersionId::V12
            | RoomVersionId::_Custom(_) => vec![],
            #[cfg(feature = "unstable-msc2870")]
            RoomVersionId::MSC2870 => vec![],
        }
    }
}

/// A condition that must apply for an associated push rule's action to be taken.
#[derive(Clone, Debug)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum PushCondition {
    /// A glob pattern match on a field of the event.
    EventMatch {
        /// The [dot-separated path] of the property of the event to match.
        ///
        /// [dot-separated path]: https://spec.matrix.org/latest/appendices/#dot-separated-property-paths
        key: String,

        /// The glob-style pattern to match against.
        ///
        /// Patterns with no special glob characters should be treated as having asterisks
        /// prepended and appended when testing the condition.
        pattern: String,
    },

    /// Matches unencrypted messages where `content.body` contains the owner's display name in that
    /// room.
    #[deprecated]
    ContainsDisplayName,

    /// Matches the current number of members in the room.
    RoomMemberCount {
        /// The condition on the current number of members in the room.
        is: RoomMemberCountIs,
    },

    /// Takes into account the current power levels in the room, ensuring the sender of the event
    /// has high enough power to trigger the notification.
    SenderNotificationPermission {
        /// The field in the power level event the user needs a minimum power level for.
        ///
        /// Fields must be specified under the `notifications` property in the power level event's
        /// `content`.
        key: NotificationPowerLevelsKey,
    },

    /// Apply the rule only to rooms that support a given feature.
    #[cfg(feature = "unstable-msc3931")]
    RoomVersionSupports {
        /// The feature the room must support for the push rule to apply.
        feature: RoomVersionFeature,
    },

    /// Exact value match on a property of the event.
    EventPropertyIs {
        /// The [dot-separated path] of the property of the event to match.
        ///
        /// [dot-separated path]: https://spec.matrix.org/latest/appendices/#dot-separated-property-paths
        key: String,

        /// The value to match against.
        value: ScalarJsonValue,
    },

    /// Exact value match on a value in an array property of the event.
    EventPropertyContains {
        /// The [dot-separated path] of the property of the event to match.
        ///
        /// [dot-separated path]: https://spec.matrix.org/latest/appendices/#dot-separated-property-paths
        key: String,

        /// The value to match against.
        value: ScalarJsonValue,
    },

    /// Matches a thread event based on the user's thread subscription status, as defined by
    /// [MSC4306].
    ///
    /// [MSC4306]: https://github.com/matrix-org/matrix-spec-proposals/pull/4306
    #[cfg(feature = "unstable-msc4306")]
    ThreadSubscription {
        /// Whether the user must be subscribed (`true`) or unsubscribed (`false`) to the thread
        /// for the condition to match.
        subscribed: bool,
    },

    #[doc(hidden)]
    _Custom(_CustomPushCondition),
}

pub(super) fn check_event_match(
    event: &FlattenedJson,
    key: &str,
    pattern: &str,
    context: &PushConditionRoomCtx,
) -> bool {
    let value = match key {
        "room_id" => context.room_id.as_str(),
        _ => match event.get_str(key) {
            Some(v) => v,
            None => return false,
        },
    };

    value.matches_pattern(pattern, key == "content.body")
}

impl PushCondition {
    /// Check if this condition applies to the event.
    ///
    /// # Arguments
    ///
    /// * `event` - The flattened JSON representation of a room message event.
    /// * `context` - The context of the room at the time of the event. If the power levels context
    ///   is missing from it, conditions that depend on it will never apply.
    pub async fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool {
        if event.get_str("sender").is_some_and(|sender| sender == context.user_id) {
            return false;
        }

        match self {
            Self::EventMatch { key, pattern } => check_event_match(event, key, pattern, context),
            #[allow(deprecated)]
            Self::ContainsDisplayName => {
                let Some(value) = event.get_str("content.body") else { return false };
                value.matches_pattern(&context.user_display_name, true)
            }
            Self::RoomMemberCount { is } => is.contains(&context.member_count),
            Self::SenderNotificationPermission { key } => {
                let Some(power_levels) = &context.power_levels else { return false };
                let Some(sender_id) = event.get_str("sender") else { return false };
                let Ok(sender_id) = <&UserId>::try_from(sender_id) else { return false };

                power_levels.has_sender_notification_permission(sender_id, key)
            }
            #[cfg(feature = "unstable-msc3931")]
            Self::RoomVersionSupports { feature } => match feature {
                RoomVersionFeature::ExtensibleEvents => {
                    context.supported_features.contains(&RoomVersionFeature::ExtensibleEvents)
                }
                RoomVersionFeature::_Custom(_) => false,
            },
            Self::EventPropertyIs { key, value } => event.get(key).is_some_and(|v| v == value),
            Self::EventPropertyContains { key, value } => event
                .get(key)
                .and_then(FlattenedJsonValue::as_array)
                .is_some_and(|a| a.contains(value)),
            #[cfg(feature = "unstable-msc4306")]
            Self::ThreadSubscription { subscribed: must_be_subscribed } => {
                let Some(has_thread_subscription_fn) = &context.has_thread_subscription_fn else {
                    // If we don't have a function to check thread subscriptions, we can't
                    // determine if the condition applies.
                    return false;
                };

                // The event must have a relation of type `m.thread`.
                if event.get_str("content.m\\.relates_to.rel_type") != Some("m.thread") {
                    return false;
                }

                // Retrieve the thread root event ID.
                let Some(Ok(thread_root)) =
                    event.get_str("content.m\\.relates_to.event_id").map(<&EventId>::try_from)
                else {
                    return false;
                };

                let is_subscribed = has_thread_subscription_fn(thread_root).await;

                *must_be_subscribed == is_subscribed
            }
            Self::_Custom(_) => false,
        }
    }
}

/// An unknown push condition.
#[doc(hidden)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[allow(clippy::exhaustive_structs)]
pub struct _CustomPushCondition {
    /// The kind of the condition.
    kind: String,

    /// The additional fields that the condition contains.
    #[serde(flatten)]
    data: BTreeMap<String, JsonValue>,
}

/// The context of the room associated to an event to be able to test all push conditions.
#[derive(Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PushConditionRoomCtx {
    /// The ID of the room.
    pub room_id: OwnedRoomId,

    /// The number of members in the room.
    pub member_count: UInt,

    /// The user's matrix ID.
    pub user_id: OwnedUserId,

    /// The display name of the current user in the room.
    pub user_display_name: String,

    /// The room power levels context for the room.
    ///
    /// If this is missing, push rules that require this will never match.
    pub power_levels: Option<PushConditionPowerLevelsCtx>,

    /// The list of features this room's version or the room itself supports.
    #[cfg(feature = "unstable-msc3931")]
    pub supported_features: Vec<RoomVersionFeature>,

    /// A closure that returns a future indicating if the given thread (represented by its thread
    /// root event id) is subscribed to by the current user, where subscriptions are defined as per
    /// [MSC4306].
    ///
    /// [MSC4306]: https://github.com/matrix-org/matrix-spec-proposals/pull/4306
    #[cfg(feature = "unstable-msc4306")]
    has_thread_subscription_fn: Option<Arc<HasThreadSubscriptionFn>>,

    /// When the `unstable-msc4306` feature is enabled with the field above, it changes the auto
    /// trait implementations of the struct to `!RefUnwindSafe` and `!UnwindSafe`. So we use
    /// `PhantomData` to keep the same bounds on the field when the feature is disabled, to always
    /// have the same auto trait implementations.
    #[cfg(not(feature = "unstable-msc4306"))]
    has_thread_subscription_fn: std::marker::PhantomData<Arc<HasThreadSubscriptionFn>>,
}

#[cfg(not(target_family = "wasm"))]
type HasThreadSubscriptionFuture<'a> = Pin<Box<dyn Future<Output = bool> + Send + 'a>>;

#[cfg(target_family = "wasm")]
type HasThreadSubscriptionFuture<'a> = Pin<Box<dyn Future<Output = bool> + 'a>>;

#[cfg(not(target_family = "wasm"))]
type HasThreadSubscriptionFn =
    dyn for<'a> Fn(&'a EventId) -> HasThreadSubscriptionFuture<'a> + Send + Sync;

#[cfg(target_family = "wasm")]
type HasThreadSubscriptionFn = dyn for<'a> Fn(&'a EventId) -> HasThreadSubscriptionFuture<'a>;

impl std::fmt::Debug for PushConditionRoomCtx {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut debug_struct = f.debug_struct("PushConditionRoomCtx");

        debug_struct
            .field("room_id", &self.room_id)
            .field("member_count", &self.member_count)
            .field("user_id", &self.user_id)
            .field("user_display_name", &self.user_display_name)
            .field("power_levels", &self.power_levels);

        #[cfg(feature = "unstable-msc3931")]
        debug_struct.field("supported_features", &self.supported_features);

        debug_struct.finish_non_exhaustive()
    }
}

impl PushConditionRoomCtx {
    /// Create a new `PushConditionRoomCtx`.
    pub fn new(
        room_id: OwnedRoomId,
        member_count: UInt,
        user_id: OwnedUserId,
        user_display_name: String,
    ) -> Self {
        Self {
            room_id,
            member_count,
            user_id,
            user_display_name,
            power_levels: None,
            #[cfg(feature = "unstable-msc3931")]
            supported_features: Vec::new(),
            has_thread_subscription_fn: Default::default(),
        }
    }

    /// Set a function to check if the user is subscribed to a thread, so as to define the push
    /// rules defined in [MSC4306].
    ///
    /// [MSC4306]: https://github.com/matrix-org/matrix-spec-proposals/pull/4306
    #[cfg(feature = "unstable-msc4306")]
    pub fn with_has_thread_subscription_fn(
        self,
        #[cfg(not(target_family = "wasm"))]
        has_thread_subscription_fn: impl for<'a> Fn(
            &'a EventId,
        ) -> HasThreadSubscriptionFuture<'a>
        + Send
        + Sync
        + 'static,
        #[cfg(target_family = "wasm")]
        has_thread_subscription_fn: impl for<'a> Fn(
            &'a EventId,
        ) -> HasThreadSubscriptionFuture<'a>
        + 'static,
    ) -> Self {
        Self { has_thread_subscription_fn: Some(Arc::new(has_thread_subscription_fn)), ..self }
    }

    /// Add the given power levels context to this `PushConditionRoomCtx`.
    pub fn with_power_levels(self, power_levels: PushConditionPowerLevelsCtx) -> Self {
        Self { power_levels: Some(power_levels), ..self }
    }
}

/// The room power levels context to be able to test the corresponding push conditions.
///
/// Should be constructed using `From<RoomPowerLevels>`.
#[derive(Clone, Debug)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PushConditionPowerLevelsCtx {
    /// The power levels of the users of the room.
    pub users: BTreeMap<OwnedUserId, Int>,

    /// The default power level of the users of the room.
    pub users_default: Int,

    /// The notification power levels of the room.
    pub notifications: NotificationPowerLevels,

    /// The tweaks for determining the power level of a user.
    pub rules: RoomPowerLevelsRules,
}

impl PushConditionPowerLevelsCtx {
    /// Create a new `PushConditionPowerLevelsCtx`.
    pub fn new(
        users: BTreeMap<OwnedUserId, Int>,
        users_default: Int,
        notifications: NotificationPowerLevels,
        rules: RoomPowerLevelsRules,
    ) -> Self {
        Self { users, users_default, notifications, rules }
    }

    /// Whether the given user has the permission to notify for the given key.
    pub fn has_sender_notification_permission(
        &self,
        user_id: &UserId,
        key: &NotificationPowerLevelsKey,
    ) -> bool {
        let Some(notification_power_level) = self.notifications.get(key) else {
            // We don't know the required power level for the key.
            return false;
        };

        if self
            .rules
            .privileged_creators
            .as_ref()
            .is_some_and(|creators| creators.contains(user_id))
        {
            return true;
        }

        let user_power_level = self.users.get(user_id).unwrap_or(&self.users_default);

        user_power_level >= notification_power_level
    }
}

/// Additional functions for character matching.
trait CharExt {
    /// Whether or not this char can be part of a word.
    fn is_word_char(&self) -> bool;
}

impl CharExt for char {
    fn is_word_char(&self) -> bool {
        self.is_ascii_alphanumeric() || *self == '_'
    }
}

/// Additional functions for string matching.
trait StrExt {
    /// Get the length of the char at `index`. The byte index must correspond to
    /// the start of a char boundary.
    fn char_len(&self, index: usize) -> usize;

    /// Get the char at `index`. The byte index must correspond to the start of
    /// a char boundary.
    fn char_at(&self, index: usize) -> char;

    /// Get the index of the char that is before the char at `index`. The byte index
    /// must correspond to a char boundary.
    ///
    /// Returns `None` if there's no previous char. Otherwise, returns the char.
    fn find_prev_char(&self, index: usize) -> Option<char>;

    /// Matches this string against `pattern`.
    ///
    /// The pattern can be a glob with wildcards `*` and `?`.
    ///
    /// The match is case insensitive.
    ///
    /// If `match_words` is `true`, checks that the pattern is separated from other words.
    fn matches_pattern(&self, pattern: &str, match_words: bool) -> bool;

    /// Matches this string against `pattern`, with word boundaries.
    ///
    /// The pattern can be a glob with wildcards `*` and `?`.
    ///
    /// A word boundary is defined as the start or end of the value, or any character not in the
    /// sets `[A-Z]`, `[a-z]`, `[0-9]` or `_`.
    ///
    /// The match is case sensitive.
    fn matches_word(&self, pattern: &str) -> bool;

    /// Translate the wildcards in `self` to a regex syntax.
    ///
    /// `self` must only contain wildcards.
    fn wildcards_to_regex(&self) -> String;
}

impl StrExt for str {
    fn char_len(&self, index: usize) -> usize {
        let mut len = 1;
        while !self.is_char_boundary(index + len) {
            len += 1;
        }
        len
    }

    fn char_at(&self, index: usize) -> char {
        let end = index + self.char_len(index);
        let char_str = &self[index..end];
        char::from_str(char_str)
            .unwrap_or_else(|_| panic!("Could not convert str '{char_str}' to char"))
    }

    fn find_prev_char(&self, index: usize) -> Option<char> {
        if index == 0 {
            return None;
        }

        let mut pos = index - 1;
        while !self.is_char_boundary(pos) {
            pos -= 1;
        }
        Some(self.char_at(pos))
    }

    fn matches_pattern(&self, pattern: &str, match_words: bool) -> bool {
        let value = &self.to_lowercase();
        let pattern = &pattern.to_lowercase();

        if match_words {
            value.matches_word(pattern)
        } else {
            WildMatch::new(pattern).matches(value)
        }
    }

    fn matches_word(&self, pattern: &str) -> bool {
        if self == pattern {
            return true;
        }
        if pattern.is_empty() {
            return false;
        }

        let has_wildcards = pattern.contains(['?', '*']);

        if has_wildcards {
            let mut chunks: Vec<String> = vec![];
            let mut prev_wildcard = false;
            let mut chunk_start = 0;

            for (i, c) in pattern.char_indices() {
                if matches!(c, '?' | '*') && !prev_wildcard {
                    if i != 0 {
                        chunks.push(regex::escape(&pattern[chunk_start..i]));
                        chunk_start = i;
                    }

                    prev_wildcard = true;
                } else if prev_wildcard {
                    let chunk = &pattern[chunk_start..i];
                    chunks.push(chunk.wildcards_to_regex());

                    chunk_start = i;
                    prev_wildcard = false;
                }
            }

            let len = pattern.len();
            if !prev_wildcard {
                chunks.push(regex::escape(&pattern[chunk_start..len]));
            } else if prev_wildcard {
                let chunk = &pattern[chunk_start..len];
                chunks.push(chunk.wildcards_to_regex());
            }

            // The word characters in ASCII compatible mode (with the `-u` flag) match the
            // definition in the spec: any character not in the set `[A-Za-z0-9_]`.
            let regex = format!(r"(?-u:^|\W|\b){}(?-u:\b|\W|$)", chunks.concat());
            let re = Regex::new(&regex).expect("regex construction should succeed");
            re.is_match(self.as_bytes())
        } else {
            match self.find(pattern) {
                Some(start) => {
                    let end = start + pattern.len();

                    // Look if the match has word boundaries.
                    let word_boundary_start = !self.char_at(start).is_word_char()
                        || !self.find_prev_char(start).is_some_and(|c| c.is_word_char());

                    if word_boundary_start {
                        let word_boundary_end = end == self.len()
                            || !self.find_prev_char(end).unwrap().is_word_char()
                            || !self.char_at(end).is_word_char();

                        if word_boundary_end {
                            return true;
                        }
                    }

                    // Find next word.
                    let non_word_str = &self[start..];
                    let Some(non_word) = non_word_str.find(|c: char| !c.is_word_char()) else {
                        return false;
                    };

                    let word_str = &non_word_str[non_word..];
                    let Some(word) = word_str.find(|c: char| c.is_word_char()) else {
                        return false;
                    };

                    word_str[word..].matches_word(pattern)
                }
                None => false,
            }
        }
    }

    fn wildcards_to_regex(&self) -> String {
        // Simplify pattern to avoid performance issues:
        // - The glob `?**?**?` is equivalent to the glob `???*`
        // - The glob `???*` is equivalent to the regex `.{3,}`
        let question_marks = self.matches('?').count();

        if self.contains('*') {
            format!(".{{{question_marks},}}")
        } else {
            format!(".{{{question_marks}}}")
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use assert_matches2::assert_matches;
    use js_int::{Int, int, uint};
    use macro_rules_attribute::apply;
    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
    use smol_macros::test;

    use super::{
        FlattenedJson, PushCondition, PushConditionPowerLevelsCtx, PushConditionRoomCtx,
        RoomMemberCountIs, StrExt,
    };
    use crate::{
        OwnedUserId, owned_room_id, owned_user_id,
        power_levels::{NotificationPowerLevels, NotificationPowerLevelsKey},
        room_version_rules::{AuthorizationRules, RoomPowerLevelsRules},
    };

    #[test]
    fn serialize_event_match_condition() {
        let json_data = json!({
            "key": "content.msgtype",
            "kind": "event_match",
            "pattern": "m.notice"
        });
        assert_eq!(
            to_json_value(PushCondition::EventMatch {
                key: "content.msgtype".into(),
                pattern: "m.notice".into(),
            })
            .unwrap(),
            json_data
        );
    }

    #[test]
    #[allow(deprecated)]
    fn serialize_contains_display_name_condition() {
        assert_eq!(
            to_json_value(PushCondition::ContainsDisplayName).unwrap(),
            json!({ "kind": "contains_display_name" })
        );
    }

    #[test]
    fn serialize_room_member_count_condition() {
        let json_data = json!({
            "is": "2",
            "kind": "room_member_count"
        });
        assert_eq!(
            to_json_value(PushCondition::RoomMemberCount { is: RoomMemberCountIs::from(uint!(2)) })
                .unwrap(),
            json_data
        );
    }

    #[test]
    fn serialize_sender_notification_permission_condition() {
        let json_data = json!({
            "key": "room",
            "kind": "sender_notification_permission"
        });
        assert_eq!(
            json_data,
            to_json_value(PushCondition::SenderNotificationPermission { key: "room".into() })
                .unwrap()
        );
    }

    #[test]
    fn deserialize_event_match_condition() {
        let json_data = json!({
            "key": "content.msgtype",
            "kind": "event_match",
            "pattern": "m.notice"
        });
        assert_matches!(
            from_json_value::<PushCondition>(json_data).unwrap(),
            PushCondition::EventMatch { key, pattern }
        );
        assert_eq!(key, "content.msgtype");
        assert_eq!(pattern, "m.notice");
    }

    #[test]
    #[allow(deprecated)]
    fn deserialize_contains_display_name_condition() {
        assert_matches!(
            from_json_value::<PushCondition>(json!({ "kind": "contains_display_name" })).unwrap(),
            PushCondition::ContainsDisplayName
        );
    }

    #[test]
    fn deserialize_room_member_count_condition() {
        let json_data = json!({
            "is": "2",
            "kind": "room_member_count"
        });
        assert_matches!(
            from_json_value::<PushCondition>(json_data).unwrap(),
            PushCondition::RoomMemberCount { is }
        );
        assert_eq!(is, RoomMemberCountIs::from(uint!(2)));
    }

    #[test]
    fn deserialize_sender_notification_permission_condition() {
        let json_data = json!({
            "key": "room",
            "kind": "sender_notification_permission"
        });
        assert_matches!(
            from_json_value::<PushCondition>(json_data).unwrap(),
            PushCondition::SenderNotificationPermission { key }
        );
        assert_eq!(key, NotificationPowerLevelsKey::Room);
    }

    #[test]
    fn words_match() {
        assert!("foo bar".matches_word("foo"));
        assert!(!"Foo bar".matches_word("foo"));
        assert!(!"foobar".matches_word("foo"));
        assert!("foobar foo".matches_word("foo"));
        assert!(!"foobar foobar".matches_word("foo"));
        assert!(!"foobar bar".matches_word("bar bar"));
        assert!("foobar bar bar".matches_word("bar bar"));
        assert!(!"foobar bar barfoo".matches_word("bar bar"));
        assert!("conduit ⚡️".matches_word("conduit ⚡️"));
        assert!("conduit ⚡️".matches_word("conduit"));
        assert!("conduit ⚡️".matches_word("⚡️"));
        assert!("conduit⚡️".matches_word("conduit"));
        assert!("conduit⚡️".matches_word("⚡️"));
        assert!("⚡️conduit".matches_word("conduit"));
        assert!("⚡️conduit".matches_word("⚡️"));
        assert!("Ruma Dev👩‍💻".matches_word("Dev"));
        assert!("Ruma Dev👩‍💻".matches_word("👩‍💻"));
        assert!("Ruma Dev👩‍💻".matches_word("Dev👩‍💻"));

        // Regex syntax is escaped
        assert!(!"matrix".matches_word(r"\w*"));
        assert!(r"\w".matches_word(r"\w*"));
        assert!(!"matrix".matches_word("[a-z]*"));
        assert!("[a-z] and [0-9]".matches_word("[a-z]*"));
        assert!(!"m".matches_word("[[:alpha:]]?"));
        assert!("[[:alpha:]]!".matches_word("[[:alpha:]]?"));

        // From the spec: <https://spec.matrix.org/v1.17/client-server-api/#conditions-1>
        assert!("An example event.".matches_word("ex*ple"));
        assert!("exple".matches_word("ex*ple"));
        assert!("An exciting triple-whammy".matches_word("ex*ple"));
    }

    #[test]
    fn patterns_match() {
        // Word matching without glob
        assert!("foo bar".matches_pattern("foo", true));
        assert!("Foo bar".matches_pattern("foo", true));
        assert!(!"foobar".matches_pattern("foo", true));
        assert!("".matches_pattern("", true));
        assert!(!"foo".matches_pattern("", true));
        assert!("foo bar".matches_pattern("foo bar", true));
        assert!(" foo bar ".matches_pattern("foo bar", true));
        assert!("baz foo bar baz".matches_pattern("foo bar", true));
        assert!("foo baré".matches_pattern("foo bar", true));
        assert!(!"bar foo".matches_pattern("foo bar", true));
        assert!("foo bar".matches_pattern("foo ", true));
        assert!("foo ".matches_pattern("foo ", true));
        assert!("foo  ".matches_pattern("foo ", true));
        assert!(" foo  ".matches_pattern("foo ", true));

        // Word matching with glob
        assert!("foo bar".matches_pattern("foo*", true));
        assert!("foo bar".matches_pattern("foo b?r", true));
        assert!(" foo bar ".matches_pattern("foo b?r", true));
        assert!("baz foo bar baz".matches_pattern("foo b?r", true));
        assert!("foo baré".matches_pattern("foo b?r", true));
        assert!(!"bar foo".matches_pattern("foo b?r", true));
        assert!("foo bar".matches_pattern("f*o ", true));
        assert!("foo ".matches_pattern("f*o ", true));
        assert!("foo  ".matches_pattern("f*o ", true));
        assert!(" foo  ".matches_pattern("f*o ", true));

        // Glob matching
        assert!(!"foo bar".matches_pattern("foo", false));
        assert!("foo".matches_pattern("foo", false));
        assert!("foo".matches_pattern("foo*", false));
        assert!("foobar".matches_pattern("foo*", false));
        assert!("foo bar".matches_pattern("foo*", false));
        assert!(!"foo".matches_pattern("foo?", false));
        assert!("fooo".matches_pattern("foo?", false));
        assert!("FOO".matches_pattern("foo", false));
        assert!("".matches_pattern("", false));
        assert!("".matches_pattern("*", false));
        assert!(!"foo".matches_pattern("", false));

        // From the spec: <https://spec.matrix.org/v1.17/client-server-api/#conditions-1>
        assert!("Lunch plans".matches_pattern("lunc?*", false));
        assert!("LUNCH".matches_pattern("lunc?*", false));
        assert!(!" lunch".matches_pattern("lunc?*", false));
        assert!(!"lunc".matches_pattern("lunc?*", false));
    }

    fn sender() -> OwnedUserId {
        owned_user_id!("@worthy_whale:server.name")
    }

    fn push_context() -> PushConditionRoomCtx {
        let mut users = BTreeMap::new();
        users.insert(sender(), int!(25));

        let power_levels = PushConditionPowerLevelsCtx {
            users,
            users_default: int!(50),
            notifications: NotificationPowerLevels { room: int!(50) },
            rules: RoomPowerLevelsRules::new(&AuthorizationRules::V1, None),
        };

        let mut ctx = PushConditionRoomCtx::new(
            owned_room_id!("!room:server.name"),
            uint!(3),
            owned_user_id!("@gorilla:server.name"),
            "Groovy Gorilla".into(),
        );
        ctx.power_levels = Some(power_levels);
        ctx
    }

    fn first_flattened_event() -> FlattenedJson {
        FlattenedJson::from_value(json!({
            "sender": "@worthy_whale:server.name",
            "content": {
                "msgtype": "m.text",
                "body": "@room Give a warm welcome to Groovy Gorilla",
            },
        }))
    }

    fn second_flattened_event() -> FlattenedJson {
        FlattenedJson::from_value(json!({
            "sender": "@party_bot:server.name",
            "content": {
                "msgtype": "m.notice",
                "body": "Everybody come to party!",
            },
        }))
    }

    #[apply(test!)]
    async fn event_match_applies() {
        let context = push_context();
        let first_event = first_flattened_event();
        let second_event = second_flattened_event();

        let correct_room = PushCondition::EventMatch {
            key: "room_id".into(),
            pattern: "!room:server.name".into(),
        };
        let incorrect_room = PushCondition::EventMatch {
            key: "room_id".into(),
            pattern: "!incorrect:server.name".into(),
        };

        assert!(correct_room.applies(&first_event, &context).await);
        assert!(!incorrect_room.applies(&first_event, &context).await);

        let keyword =
            PushCondition::EventMatch { key: "content.body".into(), pattern: "come".into() };

        assert!(!keyword.applies(&first_event, &context).await);
        assert!(keyword.applies(&second_event, &context).await);

        let msgtype =
            PushCondition::EventMatch { key: "content.msgtype".into(), pattern: "m.notice".into() };

        assert!(!msgtype.applies(&first_event, &context).await);
        assert!(msgtype.applies(&second_event, &context).await);
    }

    #[apply(test!)]
    async fn room_member_count_is_applies() {
        let context = push_context();
        let event = first_flattened_event();

        let member_count_eq =
            PushCondition::RoomMemberCount { is: RoomMemberCountIs::from(uint!(3)) };
        let member_count_gt =
            PushCondition::RoomMemberCount { is: RoomMemberCountIs::from(uint!(2)..) };
        let member_count_lt =
            PushCondition::RoomMemberCount { is: RoomMemberCountIs::from(..uint!(3)) };

        assert!(member_count_eq.applies(&event, &context).await);
        assert!(member_count_gt.applies(&event, &context).await);
        assert!(!member_count_lt.applies(&event, &context).await);
    }

    #[apply(test!)]
    #[allow(deprecated)]
    async fn contains_display_name_applies() {
        let context = push_context();
        let first_event = first_flattened_event();
        let second_event = second_flattened_event();

        let contains_display_name = PushCondition::ContainsDisplayName;

        assert!(contains_display_name.applies(&first_event, &context).await);
        assert!(!contains_display_name.applies(&second_event, &context).await);
    }

    #[apply(test!)]
    async fn sender_notification_permission_applies() {
        let context = push_context();
        let first_event = first_flattened_event();
        let second_event = second_flattened_event();

        let sender_notification_permission =
            PushCondition::SenderNotificationPermission { key: "room".into() };

        assert!(!sender_notification_permission.applies(&first_event, &context).await);
        assert!(sender_notification_permission.applies(&second_event, &context).await);
    }

    #[cfg(feature = "unstable-msc3932")]
    #[apply(test!)]
    async fn room_version_supports_applies() {
        use assign::assign;

        let context_not_matching = push_context();
        let context_matching = assign!(
            PushConditionRoomCtx::new(
                owned_room_id!("!room:server.name"),
                uint!(3),
                owned_user_id!("@gorilla:server.name"),
                "Groovy Gorilla".into(),
            ), {
                power_levels: context_not_matching.power_levels.clone(),
                supported_features: vec![super::RoomVersionFeature::ExtensibleEvents],
            }
        );

        let simple_event = FlattenedJson::from_value(json!({
            "sender": "@worthy_whale:server.name",
            "content": {
                "msgtype": "org.matrix.msc3932.extensible_events",
                "body": "@room Give a warm welcome to Groovy Gorilla",
            },
        }));

        let room_version_condition = PushCondition::RoomVersionSupports {
            feature: super::RoomVersionFeature::ExtensibleEvents,
        };

        assert!(room_version_condition.applies(&simple_event, &context_matching).await);
        assert!(!room_version_condition.applies(&simple_event, &context_not_matching).await);
    }

    #[apply(test!)]
    async fn event_property_is_applies() {
        use crate::push::condition::ScalarJsonValue;

        let context = push_context();
        let event = FlattenedJson::from_value(json!({
            "sender": "@worthy_whale:server.name",
            "content": {
                "msgtype": "m.text",
                "body": "Boom!",
                "org.fake.boolean": false,
                "org.fake.number": 13,
                "org.fake.null": null,
            },
        }));

        let string_match = PushCondition::EventPropertyIs {
            key: "content.body".to_owned(),
            value: "Boom!".into(),
        };
        assert!(string_match.applies(&event, &context).await);

        let string_no_match =
            PushCondition::EventPropertyIs { key: "content.body".to_owned(), value: "Boom".into() };
        assert!(!string_no_match.applies(&event, &context).await);

        let wrong_type =
            PushCondition::EventPropertyIs { key: "content.body".to_owned(), value: false.into() };
        assert!(!wrong_type.applies(&event, &context).await);

        let bool_match = PushCondition::EventPropertyIs {
            key: r"content.org\.fake\.boolean".to_owned(),
            value: false.into(),
        };
        assert!(bool_match.applies(&event, &context).await);

        let bool_no_match = PushCondition::EventPropertyIs {
            key: r"content.org\.fake\.boolean".to_owned(),
            value: true.into(),
        };
        assert!(!bool_no_match.applies(&event, &context).await);

        let int_match = PushCondition::EventPropertyIs {
            key: r"content.org\.fake\.number".to_owned(),
            value: int!(13).into(),
        };
        assert!(int_match.applies(&event, &context).await);

        let int_no_match = PushCondition::EventPropertyIs {
            key: r"content.org\.fake\.number".to_owned(),
            value: int!(130).into(),
        };
        assert!(!int_no_match.applies(&event, &context).await);

        let null_match = PushCondition::EventPropertyIs {
            key: r"content.org\.fake\.null".to_owned(),
            value: ScalarJsonValue::Null,
        };
        assert!(null_match.applies(&event, &context).await);
    }

    #[apply(test!)]
    async fn event_property_contains_applies() {
        use crate::push::condition::ScalarJsonValue;

        let context = push_context();
        let event = FlattenedJson::from_value(json!({
            "sender": "@worthy_whale:server.name",
            "content": {
                "org.fake.array": ["Boom!", false, 13, null],
            },
        }));

        let wrong_key =
            PushCondition::EventPropertyContains { key: "send".to_owned(), value: false.into() };
        assert!(!wrong_key.applies(&event, &context).await);

        let string_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: "Boom!".into(),
        };
        assert!(string_match.applies(&event, &context).await);

        let string_no_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: "Boom".into(),
        };
        assert!(!string_no_match.applies(&event, &context).await);

        let bool_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: false.into(),
        };
        assert!(bool_match.applies(&event, &context).await);

        let bool_no_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: true.into(),
        };
        assert!(!bool_no_match.applies(&event, &context).await);

        let int_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: int!(13).into(),
        };
        assert!(int_match.applies(&event, &context).await);

        let int_no_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: int!(130).into(),
        };
        assert!(!int_no_match.applies(&event, &context).await);

        let null_match = PushCondition::EventPropertyContains {
            key: r"content.org\.fake\.array".to_owned(),
            value: ScalarJsonValue::Null,
        };
        assert!(null_match.applies(&event, &context).await);
    }

    #[apply(test!)]
    async fn room_creators_always_have_notification_permission() {
        let mut context = push_context();
        context.power_levels = Some(PushConditionPowerLevelsCtx {
            users: BTreeMap::new(),
            users_default: Int::MIN,
            notifications: NotificationPowerLevels { room: Int::MAX },
            rules: RoomPowerLevelsRules::new(&AuthorizationRules::V12, Some(sender())),
        });

        let first_event = first_flattened_event();

        let sender_notification_permission =
            PushCondition::SenderNotificationPermission { key: NotificationPowerLevelsKey::Room };

        assert!(sender_notification_permission.applies(&first_event, &context).await);
    }

    #[cfg(feature = "unstable-msc4306")]
    #[apply(test!)]
    async fn thread_subscriptions_match() {
        use crate::{EventId, event_id};

        let context = push_context().with_has_thread_subscription_fn(|event_id: &EventId| {
            Box::pin(async move {
                // Simulate thread subscriptions for testing.
                event_id == event_id!("$subscribed_thread")
            })
        });

        let subscribed_thread_event = FlattenedJson::from_value(json!({
            "event_id": "$thread_response",
            "sender": "@worthy_whale:server.name",
            "content": {
                "msgtype": "m.text",
                "body": "response in thread $subscribed_thread",
                "m.relates_to": {
                    "rel_type": "m.thread",
                    "event_id": "$subscribed_thread",
                    "is_falling_back": true,
                    "m.in_reply_to": {
                        "event_id": "$prev_event",
                    },
                },
            },
        }));

        let unsubscribed_thread_event = FlattenedJson::from_value(json!({
            "event_id": "$thread_response2",
            "sender": "@worthy_whale:server.name",
            "content": {
                "msgtype": "m.text",
                "body": "response in thread $unsubscribed_thread",
                "m.relates_to": {
                    "rel_type": "m.thread",
                    "event_id": "$unsubscribed_thread",
                    "is_falling_back": true,
                    "m.in_reply_to": {
                        "event_id": "$prev_event2",
                    },
                },
            },
        }));

        let non_thread_related_event = FlattenedJson::from_value(json!({
            "event_id": "$thread_response2",
            "sender": "@worthy_whale:server.name",
            "content": {
                "m.relates_to": {
                    "rel_type": "m.reaction",
                    "event_id": "$subscribed_thread",
                    "key": "👍",
                },
            },
        }));

        let subscribed_thread_condition = PushCondition::ThreadSubscription { subscribed: true };
        assert!(subscribed_thread_condition.applies(&subscribed_thread_event, &context).await);
        assert!(!subscribed_thread_condition.applies(&unsubscribed_thread_event, &context).await);
        assert!(!subscribed_thread_condition.applies(&non_thread_related_event, &context).await);

        let unsubscribed_thread_condition = PushCondition::ThreadSubscription { subscribed: false };
        assert!(unsubscribed_thread_condition.applies(&unsubscribed_thread_event, &context).await);
        assert!(!unsubscribed_thread_condition.applies(&subscribed_thread_event, &context).await);
        assert!(!unsubscribed_thread_condition.applies(&non_thread_related_event, &context).await);
    }
}