@@ -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 , MilliSecondsSinceUnixEpoch , MxcUri , OwnedEventId , OwnedMxcUri , OwnedRoomAliasId ,
50
53
OwnedRoomId , OwnedUserId , 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
///
@@ -490,7 +497,7 @@ impl RoomInfo {
490
497
latest_event : None ,
491
498
read_receipts : Default :: default ( ) ,
492
499
base_info : Box :: new ( BaseRoomInfo :: new ( ) ) ,
493
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
500
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
494
501
cached_display_name : None ,
495
502
cached_user_defined_notification_mode : None ,
496
503
recency_stamp : None ,
@@ -652,9 +659,9 @@ impl RoomInfo {
652
659
event : & SyncRoomRedactionEvent ,
653
660
_raw : & Raw < SyncRoomRedactionEvent > ,
654
661
) {
655
- let room_version = self . room_version_or_default ( ) ;
662
+ let redaction_rules = self . room_version_rules_or_default ( ) . redaction ;
656
663
657
- let Some ( redacts) = event. redacts ( & room_version ) else {
664
+ let Some ( redacts) = event. redacts ( & redaction_rules ) else {
658
665
info ! ( "Can't apply redaction, redacts field is missing" ) ;
659
666
return ;
660
667
} ;
@@ -663,7 +670,7 @@ impl RoomInfo {
663
670
if let Some ( latest_event) = & mut self . latest_event {
664
671
tracing:: trace!( "Checking if redaction applies to latest event" ) ;
665
672
if latest_event. event_id ( ) . as_deref ( ) == Some ( redacts) {
666
- match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & room_version ) {
673
+ match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & redaction_rules ) {
667
674
Some ( redacted) => {
668
675
// Even if the original event was encrypted, redaction removes all its
669
676
// fields so it cannot possibly be successfully decrypted after redaction.
@@ -826,24 +833,26 @@ impl RoomInfo {
826
833
self . base_info . room_version ( )
827
834
}
828
835
829
- /// Get the room version of this room, or a sensible default.
836
+ /// Get the room version rules of this room, or a sensible default.
830
837
///
831
- /// Will warn (at most once) if the room creation event is missing from this
832
- /// [`RoomInfo`].
833
- pub fn room_version_or_default ( & self ) -> RoomVersionId {
838
+ /// Will warn (at most once) if the room create event is missing from this
839
+ /// [`RoomInfo`] or if the room version is unsupported .
840
+ pub fn room_version_rules_or_default ( & self ) -> RoomVersionRules {
834
841
use std:: sync:: atomic:: Ordering ;
835
842
836
- self . base_info . room_version ( ) . cloned ( ) . unwrap_or_else ( || {
837
- if self
838
- . warned_about_unknown_room_version
839
- . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
840
- . is_ok ( )
841
- {
842
- warn ! ( "Unknown room version, falling back to {ROOM_VERSION_FALLBACK}" ) ;
843
- }
843
+ self . base_info . room_version ( ) . and_then ( |room_version| room_version. rules ( ) ) . unwrap_or_else (
844
+ || {
845
+ if self
846
+ . warned_about_unknown_room_version_rules
847
+ . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
848
+ . is_ok ( )
849
+ {
850
+ warn ! ( "Unable to get the room version rules, defaulting to rules for room version {ROOM_VERSION_FALLBACK}" ) ;
851
+ }
844
852
845
- ROOM_VERSION_FALLBACK
846
- } )
853
+ ROOM_VERSION_RULES_FALLBACK
854
+ } ,
855
+ )
847
856
}
848
857
849
858
/// Get the room type of this room.
@@ -1093,7 +1102,7 @@ pub(crate) enum SyncInfo {
1093
1102
pub fn apply_redaction (
1094
1103
event : & Raw < AnySyncTimelineEvent > ,
1095
1104
raw_redaction : & Raw < SyncRoomRedactionEvent > ,
1096
- room_version : & RoomVersionId ,
1105
+ rules : & RedactionRules ,
1097
1106
) -> Option < Raw < AnySyncTimelineEvent > > {
1098
1107
use ruma:: canonical_json:: { redact_in_place, RedactedBecause } ;
1099
1108
@@ -1113,7 +1122,7 @@ pub fn apply_redaction(
1113
1122
}
1114
1123
} ;
1115
1124
1116
- let redact_result = redact_in_place ( & mut event_json, room_version , Some ( redacted_because) ) ;
1125
+ let redact_result = redact_in_place ( & mut event_json, rules , Some ( redacted_because) ) ;
1117
1126
1118
1127
if let Err ( e) = redact_result {
1119
1128
warn ! ( "Failed to redact event: {e}" ) ;
@@ -1243,7 +1252,7 @@ mod tests {
1243
1252
assign ! ( BaseRoomInfo :: new( ) , { pinned_events: Some ( RoomPinnedEventsEventContent :: new( vec![ owned_event_id!( "$a" ) ] ) ) } ) ,
1244
1253
) ,
1245
1254
read_receipts : Default :: default ( ) ,
1246
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
1255
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
1247
1256
cached_display_name : None ,
1248
1257
cached_user_defined_notification_mode : None ,
1249
1258
recency_stamp : Some ( 42 ) ,
0 commit comments