@@ -19,7 +19,9 @@ use std::{
19
19
20
20
use bitflags:: bitflags;
21
21
use eyeball:: Subscriber ;
22
- use matrix_sdk_common:: { deserialized_responses:: TimelineEventKind , ROOM_VERSION_FALLBACK } ;
22
+ use matrix_sdk_common:: {
23
+ deserialized_responses:: TimelineEventKind , ROOM_VERSION_FALLBACK , ROOM_VERSION_RULES_FALLBACK ,
24
+ } ;
23
25
use ruma:: {
24
26
api:: client:: sync:: sync_events:: v3:: RoomSummary as RumaSummary ,
25
27
assign,
@@ -45,6 +47,7 @@ use ruma::{
45
47
RedactedStateEventContent , StateEventType , StaticStateEventContent , SyncStateEvent ,
46
48
} ,
47
49
room:: RoomType ,
50
+ room_version_rules:: { RedactionRules , RoomVersionRules } ,
48
51
serde:: Raw ,
49
52
EventId , MxcUri , OwnedEventId , OwnedMxcUri , OwnedRoomAliasId , OwnedRoomId , OwnedUserId ,
50
53
RoomAliasId , RoomId , RoomVersionId , UserId ,
@@ -310,27 +313,31 @@ impl BaseRoomInfo {
310
313
}
311
314
312
315
pub ( super ) fn handle_redaction ( & mut self , redacts : & EventId ) {
313
- let room_version = self . room_version ( ) . unwrap_or ( & ROOM_VERSION_FALLBACK ) . to_owned ( ) ;
316
+ let redaction_rules = self
317
+ . room_version ( )
318
+ . and_then ( |room_version| room_version. rules ( ) )
319
+ . unwrap_or ( ROOM_VERSION_RULES_FALLBACK )
320
+ . redaction ;
314
321
315
322
// FIXME: Use let chains once available to get rid of unwrap()s
316
323
if self . avatar . has_event_id ( redacts) {
317
- self . avatar . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
324
+ self . avatar . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
318
325
} else if self . canonical_alias . has_event_id ( redacts) {
319
- self . canonical_alias . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
326
+ self . canonical_alias . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
320
327
} else if self . create . has_event_id ( redacts) {
321
- self . create . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
328
+ self . create . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
322
329
} else if self . guest_access . has_event_id ( redacts) {
323
- self . guest_access . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
330
+ self . guest_access . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
324
331
} else if self . history_visibility . has_event_id ( redacts) {
325
- self . history_visibility . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
332
+ self . history_visibility . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
326
333
} else if self . join_rules . has_event_id ( redacts) {
327
- self . join_rules . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
334
+ self . join_rules . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
328
335
} else if self . name . has_event_id ( redacts) {
329
- self . name . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
336
+ self . name . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
330
337
} else if self . tombstone . has_event_id ( redacts) {
331
- self . tombstone . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
338
+ self . tombstone . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
332
339
} else if self . topic . has_event_id ( redacts) {
333
- self . topic . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
340
+ self . topic . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
334
341
} else {
335
342
self . rtc_member_events
336
343
. retain ( |_, member_event| member_event. event_id ( ) != Some ( redacts) ) ;
@@ -439,11 +446,11 @@ pub struct RoomInfo {
439
446
/// room state.
440
447
pub ( crate ) base_info : Box < BaseRoomInfo > ,
441
448
442
- /// Did we already warn about an unknown room version in
443
- /// [`RoomInfo::room_version_or_default`]? This is done to avoid
444
- /// spamming about unknown room versions in the log for the same room.
449
+ /// Whether we already warned about unknown room version rules in
450
+ /// [`RoomInfo::room_version_rules_or_default`]. This is done to avoid
451
+ /// spamming about unknown room versions rules in the log for the same room.
445
452
#[ serde( skip) ]
446
- pub ( crate ) warned_about_unknown_room_version : Arc < AtomicBool > ,
453
+ pub ( crate ) warned_about_unknown_room_version_rules : Arc < AtomicBool > ,
447
454
448
455
/// Cached display name, useful for sync access.
449
456
///
@@ -482,7 +489,7 @@ impl RoomInfo {
482
489
latest_event : None ,
483
490
read_receipts : Default :: default ( ) ,
484
491
base_info : Box :: new ( BaseRoomInfo :: new ( ) ) ,
485
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
492
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
486
493
cached_display_name : None ,
487
494
cached_user_defined_notification_mode : None ,
488
495
recency_stamp : None ,
@@ -643,9 +650,9 @@ impl RoomInfo {
643
650
event : & SyncRoomRedactionEvent ,
644
651
_raw : & Raw < SyncRoomRedactionEvent > ,
645
652
) {
646
- let room_version = self . room_version_or_default ( ) ;
653
+ let redaction_rules = self . room_version_rules_or_default ( ) . redaction ;
647
654
648
- let Some ( redacts) = event. redacts ( & room_version ) else {
655
+ let Some ( redacts) = event. redacts ( & redaction_rules ) else {
649
656
info ! ( "Can't apply redaction, redacts field is missing" ) ;
650
657
return ;
651
658
} ;
@@ -654,7 +661,7 @@ impl RoomInfo {
654
661
if let Some ( latest_event) = & mut self . latest_event {
655
662
tracing:: trace!( "Checking if redaction applies to latest event" ) ;
656
663
if latest_event. event_id ( ) . as_deref ( ) == Some ( redacts) {
657
- match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & room_version ) {
664
+ match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & redaction_rules ) {
658
665
Some ( redacted) => {
659
666
// Even if the original event was encrypted, redaction removes all its
660
667
// fields so it cannot possibly be successfully decrypted after redaction.
@@ -801,24 +808,26 @@ impl RoomInfo {
801
808
self . base_info . room_version ( )
802
809
}
803
810
804
- /// Get the room version of this room, or a sensible default.
811
+ /// Get the room version rules of this room, or a sensible default.
805
812
///
806
- /// Will warn (at most once) if the room creation event is missing from this
807
- /// [`RoomInfo`].
808
- pub fn room_version_or_default ( & self ) -> RoomVersionId {
813
+ /// Will warn (at most once) if the room create event is missing from this
814
+ /// [`RoomInfo`] or if the room version is unsupported .
815
+ pub fn room_version_rules_or_default ( & self ) -> RoomVersionRules {
809
816
use std:: sync:: atomic:: Ordering ;
810
817
811
- self . base_info . room_version ( ) . cloned ( ) . unwrap_or_else ( || {
812
- if self
813
- . warned_about_unknown_room_version
814
- . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
815
- . is_ok ( )
816
- {
817
- warn ! ( "Unknown room version, falling back to {ROOM_VERSION_FALLBACK}" ) ;
818
- }
818
+ self . base_info . room_version ( ) . and_then ( |room_version| room_version. rules ( ) ) . unwrap_or_else (
819
+ || {
820
+ if self
821
+ . warned_about_unknown_room_version_rules
822
+ . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
823
+ . is_ok ( )
824
+ {
825
+ warn ! ( "Unable to get the room version rules, defaulting to rules for room version {ROOM_VERSION_FALLBACK}" ) ;
826
+ }
819
827
820
- ROOM_VERSION_FALLBACK
821
- } )
828
+ ROOM_VERSION_RULES_FALLBACK
829
+ } ,
830
+ )
822
831
}
823
832
824
833
/// Get the room type of this room.
@@ -1068,7 +1077,7 @@ pub(crate) enum SyncInfo {
1068
1077
pub fn apply_redaction (
1069
1078
event : & Raw < AnySyncTimelineEvent > ,
1070
1079
raw_redaction : & Raw < SyncRoomRedactionEvent > ,
1071
- room_version : & RoomVersionId ,
1080
+ rules : & RedactionRules ,
1072
1081
) -> Option < Raw < AnySyncTimelineEvent > > {
1073
1082
use ruma:: canonical_json:: { redact_in_place, RedactedBecause } ;
1074
1083
@@ -1088,7 +1097,7 @@ pub fn apply_redaction(
1088
1097
}
1089
1098
} ;
1090
1099
1091
- let redact_result = redact_in_place ( & mut event_json, room_version , Some ( redacted_because) ) ;
1100
+ let redact_result = redact_in_place ( & mut event_json, rules , Some ( redacted_because) ) ;
1092
1101
1093
1102
if let Err ( e) = redact_result {
1094
1103
warn ! ( "Failed to redact event: {e}" ) ;
@@ -1217,7 +1226,7 @@ mod tests {
1217
1226
assign ! ( BaseRoomInfo :: new( ) , { pinned_events: Some ( RoomPinnedEventsEventContent :: new( vec![ owned_event_id!( "$a" ) ] ) ) } ) ,
1218
1227
) ,
1219
1228
read_receipts : Default :: default ( ) ,
1220
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
1229
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
1221
1230
cached_display_name : None ,
1222
1231
cached_user_defined_notification_mode : None ,
1223
1232
recency_stamp : Some ( 42 ) ,
0 commit comments