1use std::{collections::BTreeMap, fmt, hash::Hash, iter};
18
19pub use matrix_sdk_common::deserialized_responses::*;
20use once_cell::sync::Lazy;
21use regex::Regex;
22use ruma::{
23 events::{
24 room::{
25 member::{MembershipState, RoomMemberEvent, RoomMemberEventContent},
26 power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
27 },
28 AnyStrippedStateEvent, AnySyncStateEvent, AnySyncTimelineEvent, EventContentFromType,
29 PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent,
30 StateEventContent, StaticStateEventContent, StrippedStateEvent, SyncStateEvent,
31 },
32 serde::Raw,
33 EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, UInt, UserId,
34};
35use serde::Serialize;
36use unicode_normalization::UnicodeNormalization;
37
38#[derive(Clone, Debug)]
41#[non_exhaustive]
42pub struct AmbiguityChange {
43 pub member_id: OwnedUserId,
46 pub member_ambiguous: bool,
49 pub disambiguated_member: Option<OwnedUserId>,
51 pub ambiguated_member: Option<OwnedUserId>,
53}
54
55impl AmbiguityChange {
56 pub fn user_ids(&self) -> impl Iterator<Item = &UserId> {
58 iter::once(&*self.member_id)
59 .chain(self.disambiguated_member.as_deref())
60 .chain(self.ambiguated_member.as_deref())
61 }
62}
63
64#[derive(Clone, Debug, Default)]
66#[non_exhaustive]
67pub struct AmbiguityChanges {
68 pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
71}
72
73static MXID_REGEX: Lazy<Regex> = Lazy::new(|| {
74 Regex::new(DisplayName::MXID_PATTERN)
75 .expect("We should be able to create a regex from our static MXID pattern")
76});
77static LEFT_TO_RIGHT_REGEX: Lazy<Regex> = Lazy::new(|| {
78 Regex::new(DisplayName::LEFT_TO_RIGHT_PATTERN)
79 .expect("We should be able to create a regex from our static left-to-right pattern")
80});
81static HIDDEN_CHARACTERS_REGEX: Lazy<Regex> = Lazy::new(|| {
82 Regex::new(DisplayName::HIDDEN_CHARACTERS_PATTERN)
83 .expect("We should be able to create a regex from our static hidden characters pattern")
84});
85
86static I_REGEX: Lazy<Regex> = Lazy::new(|| {
91 Regex::new("[i]").expect("We should be able to create a regex from our uppercase I pattern")
92});
93
94static ZERO_REGEX: Lazy<Regex> = Lazy::new(|| {
99 Regex::new("[0]").expect("We should be able to create a regex from our zero pattern")
100});
101
102static DOT_REGEX: Lazy<Regex> = Lazy::new(|| {
107 Regex::new("[.\u{1d16d}]").expect("We should be able to create a regex from our dot pattern")
108});
109
110#[derive(Debug, Clone, Eq)]
136pub struct DisplayName {
137 raw: String,
138 decancered: Option<String>,
139}
140
141impl Hash for DisplayName {
142 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
143 if let Some(decancered) = &self.decancered {
144 decancered.hash(state);
145 } else {
146 self.raw.hash(state);
147 }
148 }
149}
150
151impl PartialEq for DisplayName {
152 fn eq(&self, other: &Self) -> bool {
153 match (self.decancered.as_deref(), other.decancered.as_deref()) {
154 (None, None) => self.raw == other.raw,
155 (None, Some(_)) | (Some(_), None) => false,
156 (Some(this), Some(other)) => this == other,
157 }
158 }
159}
160
161impl DisplayName {
162 const MXID_PATTERN: &'static str = "@.+[:.].+";
164
165 const LEFT_TO_RIGHT_PATTERN: &'static str = "[\u{202a}-\u{202f}\u{200e}\u{200f}]";
169
170 const HIDDEN_CHARACTERS_PATTERN: &'static str =
180 "[\u{2000}-\u{200D}\u{300}-\u{036f}\u{2062}-\u{2063}\u{2800}\u{061c}\u{feff}]";
181
182 pub fn new(raw: &str) -> Self {
191 let normalized = raw.nfd().collect::<String>();
192 let replaced = DOT_REGEX.replace_all(&normalized, ":");
193 let replaced = HIDDEN_CHARACTERS_REGEX.replace_all(&replaced, "");
194
195 let decancered = decancer::cure!(&replaced).ok().map(|cured| {
196 let removed_left_to_right = LEFT_TO_RIGHT_REGEX.replace_all(cured.as_ref(), "");
197 let replaced = I_REGEX.replace_all(&removed_left_to_right, "l");
198 let replaced = DOT_REGEX.replace_all(&replaced, ":");
201 let replaced = ZERO_REGEX.replace_all(&replaced, "o");
202
203 replaced.to_string()
204 });
205
206 Self { raw: raw.to_owned(), decancered }
207 }
208
209 pub fn is_inherently_ambiguous(&self) -> bool {
214 self.looks_like_an_mxid() || self.has_hidden_characters() || self.decancered.is_none()
216 }
217
218 pub fn as_raw_str(&self) -> &str {
221 &self.raw
222 }
223
224 pub fn as_normalized_str(&self) -> Option<&str> {
230 self.decancered.as_deref()
231 }
232
233 fn has_hidden_characters(&self) -> bool {
234 HIDDEN_CHARACTERS_REGEX.is_match(&self.raw)
235 }
236
237 fn looks_like_an_mxid(&self) -> bool {
238 self.decancered
239 .as_deref()
240 .map(|d| MXID_REGEX.is_match(d))
241 .unwrap_or_else(|| MXID_REGEX.is_match(&self.raw))
242 }
243}
244
245#[derive(Clone, Debug, Default)]
249pub struct MembersResponse {
250 pub chunk: Vec<RoomMemberEvent>,
252 pub ambiguity_changes: AmbiguityChanges,
254}
255
256#[derive(Clone, Debug, Serialize)]
258#[serde(untagged)]
259pub enum RawAnySyncOrStrippedTimelineEvent {
260 Sync(Raw<AnySyncTimelineEvent>),
262 Stripped(Raw<AnyStrippedStateEvent>),
264}
265
266#[derive(Clone, Debug, Serialize)]
268#[serde(untagged)]
269pub enum RawAnySyncOrStrippedState {
270 Sync(Raw<AnySyncStateEvent>),
272 Stripped(Raw<AnyStrippedStateEvent>),
274}
275
276impl RawAnySyncOrStrippedState {
277 pub fn deserialize(&self) -> serde_json::Result<AnySyncOrStrippedState> {
279 match self {
280 Self::Sync(raw) => Ok(AnySyncOrStrippedState::Sync(raw.deserialize()?)),
281 Self::Stripped(raw) => Ok(AnySyncOrStrippedState::Stripped(raw.deserialize()?)),
282 }
283 }
284
285 pub fn cast<C>(self) -> RawSyncOrStrippedState<C>
288 where
289 C: StaticStateEventContent + RedactContent,
290 C::Redacted: RedactedStateEventContent,
291 {
292 match self {
293 Self::Sync(raw) => RawSyncOrStrippedState::Sync(raw.cast()),
294 Self::Stripped(raw) => RawSyncOrStrippedState::Stripped(raw.cast()),
295 }
296 }
297}
298
299#[derive(Clone, Debug)]
301pub enum AnySyncOrStrippedState {
302 Sync(AnySyncStateEvent),
304 Stripped(AnyStrippedStateEvent),
306}
307
308impl AnySyncOrStrippedState {
309 pub fn as_sync(&self) -> Option<&AnySyncStateEvent> {
312 match self {
313 Self::Sync(ev) => Some(ev),
314 Self::Stripped(_) => None,
315 }
316 }
317
318 pub fn as_stripped(&self) -> Option<&AnyStrippedStateEvent> {
321 match self {
322 Self::Sync(_) => None,
323 Self::Stripped(ev) => Some(ev),
324 }
325 }
326}
327
328#[derive(Clone, Debug, Serialize)]
330#[serde(untagged)]
331pub enum RawSyncOrStrippedState<C>
332where
333 C: StaticStateEventContent + RedactContent,
334 C::Redacted: RedactedStateEventContent,
335{
336 Sync(Raw<SyncStateEvent<C>>),
338 Stripped(Raw<StrippedStateEvent<C::PossiblyRedacted>>),
340}
341
342impl<C> RawSyncOrStrippedState<C>
343where
344 C: StaticStateEventContent + RedactContent,
345 C::Redacted: RedactedStateEventContent + fmt::Debug + Clone,
346{
347 pub fn deserialize(&self) -> serde_json::Result<SyncOrStrippedState<C>>
349 where
350 C: StaticStateEventContent + EventContentFromType + RedactContent,
351 C::Redacted: RedactedStateEventContent<StateKey = C::StateKey> + EventContentFromType,
352 C::PossiblyRedacted: PossiblyRedactedStateEventContent + EventContentFromType,
353 {
354 match self {
355 Self::Sync(ev) => Ok(SyncOrStrippedState::Sync(ev.deserialize()?)),
356 Self::Stripped(ev) => Ok(SyncOrStrippedState::Stripped(ev.deserialize()?)),
357 }
358 }
359}
360
361pub type RawMemberEvent = RawSyncOrStrippedState<RoomMemberEventContent>;
363
364#[derive(Clone, Debug)]
366pub enum SyncOrStrippedState<C>
367where
368 C: StaticStateEventContent + RedactContent,
369 C::Redacted: RedactedStateEventContent + fmt::Debug + Clone,
370{
371 Sync(SyncStateEvent<C>),
373 Stripped(StrippedStateEvent<C::PossiblyRedacted>),
375}
376
377impl<C> SyncOrStrippedState<C>
378where
379 C: StaticStateEventContent + RedactContent,
380 C::Redacted: RedactedStateEventContent<StateKey = C::StateKey> + fmt::Debug + Clone,
381 C::PossiblyRedacted: PossiblyRedactedStateEventContent<StateKey = C::StateKey>,
382{
383 pub fn as_sync(&self) -> Option<&SyncStateEvent<C>> {
385 match self {
386 Self::Sync(ev) => Some(ev),
387 Self::Stripped(_) => None,
388 }
389 }
390
391 pub fn as_stripped(&self) -> Option<&StrippedStateEvent<C::PossiblyRedacted>> {
394 match self {
395 Self::Sync(_) => None,
396 Self::Stripped(ev) => Some(ev),
397 }
398 }
399
400 pub fn sender(&self) -> &UserId {
402 match self {
403 Self::Sync(e) => e.sender(),
404 Self::Stripped(e) => &e.sender,
405 }
406 }
407
408 pub fn event_id(&self) -> Option<&EventId> {
410 match self {
411 Self::Sync(e) => Some(e.event_id()),
412 Self::Stripped(_) => None,
413 }
414 }
415
416 pub fn origin_server_ts(&self) -> Option<MilliSecondsSinceUnixEpoch> {
418 match self {
419 Self::Sync(e) => Some(e.origin_server_ts()),
420 Self::Stripped(_) => None,
421 }
422 }
423
424 pub fn state_key(&self) -> &C::StateKey {
426 match self {
427 Self::Sync(e) => e.state_key(),
428 Self::Stripped(e) => &e.state_key,
429 }
430 }
431}
432
433impl<C> SyncOrStrippedState<C>
434where
435 C: StaticStateEventContent<PossiblyRedacted = C>
436 + RedactContent
437 + PossiblyRedactedStateEventContent,
438 C::Redacted: RedactedStateEventContent<StateKey = <C as StateEventContent>::StateKey>
439 + fmt::Debug
440 + Clone,
441{
442 pub fn original_content(&self) -> Option<&C> {
444 match self {
445 Self::Sync(e) => e.as_original().map(|e| &e.content),
446 Self::Stripped(e) => Some(&e.content),
447 }
448 }
449}
450
451pub type MemberEvent = SyncOrStrippedState<RoomMemberEventContent>;
453
454impl MemberEvent {
455 pub fn membership(&self) -> &MembershipState {
457 match self {
458 MemberEvent::Sync(e) => e.membership(),
459 MemberEvent::Stripped(e) => &e.content.membership,
460 }
461 }
462
463 pub fn user_id(&self) -> &UserId {
465 self.state_key()
466 }
467
468 pub fn display_name(&self) -> DisplayName {
473 DisplayName::new(
474 self.original_content()
475 .and_then(|c| c.displayname.as_deref())
476 .unwrap_or_else(|| self.user_id().localpart()),
477 )
478 }
479
480 pub fn reason(&self) -> Option<&str> {
482 match self {
483 MemberEvent::Sync(SyncStateEvent::Original(c)) => c.content.reason.as_deref(),
484 MemberEvent::Stripped(e) => e.content.reason.as_deref(),
485 _ => None,
486 }
487 }
488
489 pub fn timestamp(&self) -> Option<UInt> {
491 match self {
492 MemberEvent::Sync(SyncStateEvent::Original(c)) => Some(c.origin_server_ts.0),
493 _ => None,
494 }
495 }
496}
497
498impl SyncOrStrippedState<RoomPowerLevelsEventContent> {
499 pub fn power_levels(&self) -> RoomPowerLevels {
501 match self {
502 Self::Sync(e) => e.power_levels(),
503 Self::Stripped(e) => e.power_levels(),
504 }
505 }
506}
507
508#[cfg(test)]
509mod test {
510 macro_rules! assert_display_name_eq {
511 ($left:expr, $right:expr $(, $desc:expr)?) => {{
512 let left = crate::deserialized_responses::DisplayName::new($left);
513 let right = crate::deserialized_responses::DisplayName::new($right);
514
515 similar_asserts::assert_eq!(
516 left,
517 right
518 $(, $desc)?
519 );
520 }};
521 }
522
523 macro_rules! assert_display_name_ne {
524 ($left:expr, $right:expr $(, $desc:expr)?) => {{
525 let left = crate::deserialized_responses::DisplayName::new($left);
526 let right = crate::deserialized_responses::DisplayName::new($right);
527
528 assert_ne!(
529 left,
530 right
531 $(, $desc)?
532 );
533 }};
534 }
535
536 macro_rules! assert_ambiguous {
537 ($name:expr) => {
538 let name = crate::deserialized_responses::DisplayName::new($name);
539
540 assert!(
541 name.is_inherently_ambiguous(),
542 "The display {:?} should be considered amgibuous",
543 name
544 );
545 };
546 }
547
548 macro_rules! assert_not_ambiguous {
549 ($name:expr) => {
550 let name = crate::deserialized_responses::DisplayName::new($name);
551
552 assert!(
553 !name.is_inherently_ambiguous(),
554 "The display {:?} should not be considered amgibuous",
555 name
556 );
557 };
558 }
559
560 #[test]
561 fn test_display_name_inherently_ambiguous() {
562 assert_not_ambiguous!("Alice");
565 assert_not_ambiguous!("Carol");
566 assert_not_ambiguous!("Car0l");
567 assert_not_ambiguous!("Ivan");
568 assert_not_ambiguous!("𝒮𝒶𝒽𝒶𝓈𝓇𝒶𝒽𝓁𝒶");
569 assert_not_ambiguous!("Ⓢⓐⓗⓐⓢⓡⓐⓗⓛⓐ");
570 assert_not_ambiguous!("🅂🄰🄷🄰🅂🅁🄰🄷🄻🄰");
571 assert_not_ambiguous!("Sahasrahla");
572 assert_not_ambiguous!("\u{202e}alharsahas");
574
575 assert_ambiguous!("Sa̴hasrahla");
577 assert_ambiguous!("Sahas\u{200D}rahla");
578 }
579
580 #[test]
581 fn test_display_name_equality_capitalization() {
582 assert_display_name_eq!("Alice", "alice");
584 }
585
586 #[test]
587 fn test_display_name_equality_different_names() {
588 assert_display_name_ne!("Alice", "Carol");
590 }
591
592 #[test]
593 fn test_display_name_equality_capital_l() {
594 assert_display_name_eq!("Hello", "HeIlo");
596 }
597
598 #[test]
599 fn test_display_name_equality_confusable_zero() {
600 assert_display_name_eq!("Carol", "Car0l");
602 }
603
604 #[test]
605 fn test_display_name_equality_cyrillic() {
606 assert_display_name_eq!("alice", "аlice");
608 }
609
610 #[test]
611 fn test_display_name_equality_scriptures() {
612 assert_display_name_eq!("Sahasrahla", "𝒮𝒶𝒽𝒶𝓈𝓇𝒶𝒽𝓁𝒶");
614 }
615
616 #[test]
617 fn test_display_name_equality_frakturs() {
618 assert_display_name_eq!("Sahasrahla", "𝔖𝔞𝔥𝔞𝔰𝔯𝔞𝔥𝔩𝔞");
620 }
621
622 #[test]
623 fn test_display_name_equality_circled() {
624 assert_display_name_eq!("Sahasrahla", "Ⓢⓐⓗⓐⓢⓡⓐⓗⓛⓐ");
626 }
627
628 #[test]
629 fn test_display_name_equality_squared() {
630 assert_display_name_eq!("Sahasrahla", "🅂🄰🄷🄰🅂🅁🄰🄷🄻🄰");
632 }
633
634 #[test]
635 fn test_display_name_equality_big_unicode() {
636 assert_display_name_eq!("Sahasrahla", "Sahasrahla");
638 }
639
640 #[test]
641 fn test_display_name_equality_left_to_right() {
642 assert_display_name_eq!("Sahasrahla", "\u{202e}alharsahas");
644 }
645
646 #[test]
647 fn test_display_name_equality_diacritical() {
648 assert_display_name_eq!("Sahasrahla", "Sa̴hasrahla");
650 }
651
652 #[test]
653 fn test_display_name_equality_zero_width_joiner() {
654 assert_display_name_eq!("Sahasrahla", "Sahas\u{200B}rahla");
656 }
657
658 #[test]
659 fn test_display_name_equality_zero_width_space() {
660 assert_display_name_eq!("Sahasrahla", "Sahas\u{200D}rahla");
662 }
663
664 #[test]
665 fn test_display_name_equality_ligatures() {
666 assert_display_name_eq!("ff", "\u{FB00}");
668 }
669
670 #[test]
671 fn test_display_name_confusable_mxid_colon() {
672 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0589}domain.tld");
673 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{05c3}domain.tld");
674 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0703}domain.tld");
675 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0a83}domain.tld");
676 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{16ec}domain.tld");
677 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{205a}domain.tld");
678 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{2236}domain.tld");
679 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe13}domain.tld");
680 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe52}domain.tld");
681 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe30}domain.tld");
682 assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{ff1a}domain.tld");
683
684 assert_ambiguous!("@mxid\u{0589}domain.tld");
686 assert_ambiguous!("@mxid\u{05c3}domain.tld");
687 assert_ambiguous!("@mxid\u{0703}domain.tld");
688 assert_ambiguous!("@mxid\u{0a83}domain.tld");
689 assert_ambiguous!("@mxid\u{16ec}domain.tld");
690 assert_ambiguous!("@mxid\u{205a}domain.tld");
691 assert_ambiguous!("@mxid\u{2236}domain.tld");
692 assert_ambiguous!("@mxid\u{fe13}domain.tld");
693 assert_ambiguous!("@mxid\u{fe52}domain.tld");
694 assert_ambiguous!("@mxid\u{fe30}domain.tld");
695 assert_ambiguous!("@mxid\u{ff1a}domain.tld");
696 }
697
698 #[test]
699 fn test_display_name_confusable_mxid_dot() {
700 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{0701}tld");
701 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{0702}tld");
702 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{2024}tld");
703 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{fe52}tld");
704 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{ff0e}tld");
705 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{1d16d}tld");
706
707 assert_ambiguous!("@mxid:domain\u{0701}tld");
709 assert_ambiguous!("@mxid:domain\u{0702}tld");
710 assert_ambiguous!("@mxid:domain\u{2024}tld");
711 assert_ambiguous!("@mxid:domain\u{fe52}tld");
712 assert_ambiguous!("@mxid:domain\u{ff0e}tld");
713 assert_ambiguous!("@mxid:domain\u{1d16d}tld");
714 }
715
716 #[test]
717 fn test_display_name_confusable_mxid_replacing_a() {
718 assert_display_name_eq!("@mxid:domain.tld", "@mxid:dom\u{1d44e}in.tld");
719 assert_display_name_eq!("@mxid:domain.tld", "@mxid:dom\u{0430}in.tld");
720
721 assert_ambiguous!("@mxid:dom\u{1d44e}in.tld");
723 assert_ambiguous!("@mxid:dom\u{0430}in.tld");
724 }
725
726 #[test]
727 fn test_display_name_confusable_mxid_replacing_l() {
728 assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain.tId");
729 assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{217c}d");
730 assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{ff4c}d");
731 assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{1d5f9}d");
732 assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{1d695}d");
733 assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{2223}d");
734
735 assert_ambiguous!("@mxid:domain.tId");
737 assert_ambiguous!("@mxid:domain.t\u{217c}d");
738 assert_ambiguous!("@mxid:domain.t\u{ff4c}d");
739 assert_ambiguous!("@mxid:domain.t\u{1d5f9}d");
740 assert_ambiguous!("@mxid:domain.t\u{1d695}d");
741 assert_ambiguous!("@mxid:domain.t\u{2223}d");
742 }
743}