@@ -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 ,
@@ -322,27 +325,31 @@ impl BaseRoomInfo {
322
325
}
323
326
324
327
pub ( super ) fn handle_redaction ( & mut self , redacts : & EventId ) {
325
- let room_version = self . room_version ( ) . unwrap_or ( & ROOM_VERSION_FALLBACK ) . to_owned ( ) ;
328
+ let redaction_rules = self
329
+ . room_version ( )
330
+ . and_then ( |room_version| room_version. rules ( ) )
331
+ . unwrap_or ( ROOM_VERSION_RULES_FALLBACK )
332
+ . redaction ;
326
333
327
334
// FIXME: Use let chains once available to get rid of unwrap()s
328
335
if self . avatar . has_event_id ( redacts) {
329
- self . avatar . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
336
+ self . avatar . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
330
337
} else if self . canonical_alias . has_event_id ( redacts) {
331
- self . canonical_alias . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
338
+ self . canonical_alias . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
332
339
} else if self . create . has_event_id ( redacts) {
333
- self . create . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
340
+ self . create . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
334
341
} else if self . guest_access . has_event_id ( redacts) {
335
- self . guest_access . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
342
+ self . guest_access . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
336
343
} else if self . history_visibility . has_event_id ( redacts) {
337
- self . history_visibility . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
344
+ self . history_visibility . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
338
345
} else if self . join_rules . has_event_id ( redacts) {
339
- self . join_rules . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
346
+ self . join_rules . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
340
347
} else if self . name . has_event_id ( redacts) {
341
- self . name . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
348
+ self . name . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
342
349
} else if self . tombstone . has_event_id ( redacts) {
343
- self . tombstone . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
350
+ self . tombstone . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
344
351
} else if self . topic . has_event_id ( redacts) {
345
- self . topic . as_mut ( ) . unwrap ( ) . redact ( & room_version ) ;
352
+ self . topic . as_mut ( ) . unwrap ( ) . redact ( & redaction_rules ) ;
346
353
} else {
347
354
self . rtc_member_events
348
355
. retain ( |_, member_event| member_event. event_id ( ) != Some ( redacts) ) ;
@@ -451,11 +458,11 @@ pub struct RoomInfo {
451
458
/// room state.
452
459
pub ( crate ) base_info : Box < BaseRoomInfo > ,
453
460
454
- /// Did we already warn about an unknown room version in
455
- /// [`RoomInfo::room_version_or_default`]? This is done to avoid
456
- /// spamming about unknown room versions in the log for the same room.
461
+ /// Whether we already warned about unknown room version rules in
462
+ /// [`RoomInfo::room_version_rules_or_default`]. This is done to avoid
463
+ /// spamming about unknown room versions rules in the log for the same room.
457
464
#[ serde( skip) ]
458
- pub ( crate ) warned_about_unknown_room_version : Arc < AtomicBool > ,
465
+ pub ( crate ) warned_about_unknown_room_version_rules : Arc < AtomicBool > ,
459
466
460
467
/// Cached display name, useful for sync access.
461
468
///
@@ -502,7 +509,7 @@ impl RoomInfo {
502
509
latest_event : None ,
503
510
read_receipts : Default :: default ( ) ,
504
511
base_info : Box :: new ( BaseRoomInfo :: new ( ) ) ,
505
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
512
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
506
513
cached_display_name : None ,
507
514
cached_user_defined_notification_mode : None ,
508
515
recency_stamp : None ,
@@ -670,9 +677,9 @@ impl RoomInfo {
670
677
event : & SyncRoomRedactionEvent ,
671
678
_raw : & Raw < SyncRoomRedactionEvent > ,
672
679
) {
673
- let room_version = self . room_version_or_default ( ) ;
680
+ let redaction_rules = self . room_version_rules_or_default ( ) . redaction ;
674
681
675
- let Some ( redacts) = event. redacts ( & room_version ) else {
682
+ let Some ( redacts) = event. redacts ( & redaction_rules ) else {
676
683
info ! ( "Can't apply redaction, redacts field is missing" ) ;
677
684
return ;
678
685
} ;
@@ -681,7 +688,7 @@ impl RoomInfo {
681
688
if let Some ( latest_event) = & mut self . latest_event {
682
689
tracing:: trace!( "Checking if redaction applies to latest event" ) ;
683
690
if latest_event. event_id ( ) . as_deref ( ) == Some ( redacts) {
684
- match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & room_version ) {
691
+ match apply_redaction ( latest_event. event ( ) . raw ( ) , _raw, & redaction_rules ) {
685
692
Some ( redacted) => {
686
693
// Even if the original event was encrypted, redaction removes all its
687
694
// fields so it cannot possibly be successfully decrypted after redaction.
@@ -842,24 +849,26 @@ impl RoomInfo {
842
849
self . base_info . room_version ( )
843
850
}
844
851
845
- /// Get the room version of this room, or a sensible default.
852
+ /// Get the room version rules of this room, or a sensible default.
846
853
///
847
- /// Will warn (at most once) if the room creation event is missing from this
848
- /// [`RoomInfo`].
849
- pub fn room_version_or_default ( & self ) -> RoomVersionId {
854
+ /// Will warn (at most once) if the room create event is missing from this
855
+ /// [`RoomInfo`] or if the room version is unsupported .
856
+ pub fn room_version_rules_or_default ( & self ) -> RoomVersionRules {
850
857
use std:: sync:: atomic:: Ordering ;
851
858
852
- self . base_info . room_version ( ) . cloned ( ) . unwrap_or_else ( || {
853
- if self
854
- . warned_about_unknown_room_version
855
- . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
856
- . is_ok ( )
857
- {
858
- warn ! ( "Unknown room version, falling back to {ROOM_VERSION_FALLBACK}" ) ;
859
- }
859
+ self . base_info . room_version ( ) . and_then ( |room_version| room_version. rules ( ) ) . unwrap_or_else (
860
+ || {
861
+ if self
862
+ . warned_about_unknown_room_version_rules
863
+ . compare_exchange ( false , true , Ordering :: Relaxed , Ordering :: Relaxed )
864
+ . is_ok ( )
865
+ {
866
+ warn ! ( "Unable to get the room version rules, defaulting to rules for room version {ROOM_VERSION_FALLBACK}" ) ;
867
+ }
860
868
861
- ROOM_VERSION_FALLBACK
862
- } )
869
+ ROOM_VERSION_RULES_FALLBACK
870
+ } ,
871
+ )
863
872
}
864
873
865
874
/// Get the room type of this room.
@@ -1109,7 +1118,7 @@ pub(crate) enum SyncInfo {
1109
1118
pub fn apply_redaction (
1110
1119
event : & Raw < AnySyncTimelineEvent > ,
1111
1120
raw_redaction : & Raw < SyncRoomRedactionEvent > ,
1112
- room_version : & RoomVersionId ,
1121
+ rules : & RedactionRules ,
1113
1122
) -> Option < Raw < AnySyncTimelineEvent > > {
1114
1123
use ruma:: canonical_json:: { redact_in_place, RedactedBecause } ;
1115
1124
@@ -1129,7 +1138,7 @@ pub fn apply_redaction(
1129
1138
}
1130
1139
} ;
1131
1140
1132
- let redact_result = redact_in_place ( & mut event_json, room_version , Some ( redacted_because) ) ;
1141
+ let redact_result = redact_in_place ( & mut event_json, rules , Some ( redacted_because) ) ;
1133
1142
1134
1143
if let Err ( e) = redact_result {
1135
1144
warn ! ( "Failed to redact event: {e}" ) ;
@@ -1259,7 +1268,7 @@ mod tests {
1259
1268
assign ! ( BaseRoomInfo :: new( ) , { pinned_events: Some ( RoomPinnedEventsEventContent :: new( vec![ owned_event_id!( "$a" ) ] ) ) } ) ,
1260
1269
) ,
1261
1270
read_receipts : Default :: default ( ) ,
1262
- warned_about_unknown_room_version : Arc :: new ( false . into ( ) ) ,
1271
+ warned_about_unknown_room_version_rules : Arc :: new ( false . into ( ) ) ,
1263
1272
cached_display_name : None ,
1264
1273
cached_user_defined_notification_mode : None ,
1265
1274
recency_stamp : Some ( 42 ) ,
0 commit comments