Skip to content

Commit 06732ca

Browse files
authored
refactor(common): Use a constant for the room version to use as a fallback
It avoids using different versions in several places for consistency. It also allows to be able to change it in a single place when needed. This also bumps the fallback to v11 everywhere, since it is the default version for new rooms since Matrix 1.14 and it has the sanest redaction rules. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 115c757 commit 06732ca

File tree

8 files changed

+43
-33
lines changed

8 files changed

+43
-33
lines changed

crates/matrix-sdk-base/src/response_processors/timeline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ruma::{
2424
StateEventType,
2525
},
2626
push::{Action, PushConditionRoomCtx},
27-
RoomVersionId, UInt, UserId,
27+
UInt, UserId,
2828
};
2929
use tracing::{instrument, trace, warn};
3030

@@ -76,9 +76,9 @@ pub async fn build<'notification, 'e2ee>(
7676
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomRedaction(
7777
redaction_event,
7878
)) => {
79-
let room_version = room_info.room_version().unwrap_or(&RoomVersionId::V1);
79+
let room_version = room_info.room_version_or_default();
8080

81-
if let Some(redacts) = redaction_event.redacts(room_version) {
81+
if let Some(redacts) = redaction_event.redacts(&room_version) {
8282
room_info
8383
.handle_redaction(redaction_event, timeline_event.raw().cast_ref());
8484

crates/matrix-sdk-base/src/room/room_info.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919

2020
use bitflags::bitflags;
2121
use eyeball::Subscriber;
22-
use matrix_sdk_common::deserialized_responses::TimelineEventKind;
22+
use matrix_sdk_common::{deserialized_responses::TimelineEventKind, ROOM_VERSION_FALLBACK};
2323
use ruma::{
2424
api::client::sync::sync_events::v3::RoomSummary as RumaSummary,
2525
assign,
@@ -310,7 +310,7 @@ impl BaseRoomInfo {
310310
}
311311

312312
pub(super) fn handle_redaction(&mut self, redacts: &EventId) {
313-
let room_version = self.room_version().unwrap_or(&RoomVersionId::V1).to_owned();
313+
let room_version = self.room_version().unwrap_or(&ROOM_VERSION_FALLBACK).to_owned();
314314

315315
// FIXME: Use let chains once available to get rid of unwrap()s
316316
if self.avatar.has_event_id(redacts) {
@@ -643,9 +643,9 @@ impl RoomInfo {
643643
event: &SyncRoomRedactionEvent,
644644
_raw: &Raw<SyncRoomRedactionEvent>,
645645
) {
646-
let room_version = self.base_info.room_version().unwrap_or(&RoomVersionId::V1);
646+
let room_version = self.room_version_or_default();
647647

648-
let Some(redacts) = event.redacts(room_version) else {
648+
let Some(redacts) = event.redacts(&room_version) else {
649649
info!("Can't apply redaction, redacts field is missing");
650650
return;
651651
};
@@ -654,7 +654,7 @@ impl RoomInfo {
654654
if let Some(latest_event) = &mut self.latest_event {
655655
tracing::trace!("Checking if redaction applies to latest event");
656656
if latest_event.event_id().as_deref() == Some(redacts) {
657-
match apply_redaction(latest_event.event().raw(), _raw, room_version) {
657+
match apply_redaction(latest_event.event().raw(), _raw, &room_version) {
658658
Some(redacted) => {
659659
// Even if the original event was encrypted, redaction removes all its
660660
// fields so it cannot possibly be successfully decrypted after redaction.
@@ -814,10 +814,10 @@ impl RoomInfo {
814814
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
815815
.is_ok()
816816
{
817-
warn!("Unknown room version, falling back to v10");
817+
warn!("Unknown room version, falling back to {ROOM_VERSION_FALLBACK}");
818818
}
819819

820-
RoomVersionId::V10
820+
ROOM_VERSION_FALLBACK
821821
})
822822
}
823823

crates/matrix-sdk-base/src/store/memory_store.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::{
1919

2020
use async_trait::async_trait;
2121
use growable_bloom_filter::GrowableBloom;
22+
use matrix_sdk_common::ROOM_VERSION_FALLBACK;
2223
use ruma::{
2324
canonical_json::{redact, RedactedBecause},
2425
events::{
@@ -31,7 +32,7 @@ use ruma::{
3132
serde::Raw,
3233
time::Instant,
3334
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedMxcUri,
34-
OwnedRoomId, OwnedTransactionId, OwnedUserId, RoomId, RoomVersionId, TransactionId, UserId,
35+
OwnedRoomId, OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UserId,
3536
};
3637
use tracing::{debug, instrument, warn};
3738

@@ -439,12 +440,13 @@ impl StateStore for MemoryStore {
439440
}
440441

441442
let make_room_version = |room_info: &HashMap<OwnedRoomId, RoomInfo>, room_id| {
442-
room_info.get(room_id).and_then(|info| info.room_version().cloned()).unwrap_or_else(
443-
|| {
444-
warn!(?room_id, "Unable to find the room version, assuming version 9");
445-
RoomVersionId::V9
446-
},
447-
)
443+
room_info.get(room_id).map(|info| info.room_version_or_default()).unwrap_or_else(|| {
444+
warn!(
445+
?room_id,
446+
"Unable to find the room version, assuming {ROOM_VERSION_FALLBACK}"
447+
);
448+
ROOM_VERSION_FALLBACK
449+
})
448450
};
449451

450452
let inner = &mut *inner;

crates/matrix-sdk-common/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
66

77
## [Unreleased] - ReleaseDate
88

9+
### Features
10+
11+
- Expose the `ROOM_VERSION_FALLBACK` that should be used when the version of a
12+
room is unknown.
13+
([#5306](https://github.com/matrix-org/matrix-rust-sdk/pull/5306))
14+
915
## [0.12.0] - 2025-06-10
1016

1117
No notable changes in this release.
@@ -53,5 +59,3 @@ No notable changes in this release.
5359
### Refactor
5460

5561
- Move `linked_chunk` from `matrix-sdk` to `matrix-sdk-common`.
56-
57-

crates/matrix-sdk-common/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod ttl_cache;
4242
#[cfg(all(target_family = "wasm", not(tarpaulin_include)))]
4343
pub mod js_tracing;
4444

45+
use ruma::RoomVersionId;
4546
pub use store_locks::LEASE_DURATION_MS;
4647

4748
/// Alias for `Send` on non-wasm, empty trait (implemented by everything) on
@@ -104,3 +105,6 @@ pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
104105

105106
#[cfg(feature = "uniffi")]
106107
uniffi::setup_scaffolding!();
108+
109+
/// The room version to use as a fallback when the version of a room is unknown.
110+
pub const ROOM_VERSION_FALLBACK: RoomVersionId = RoomVersionId::V11;

crates/matrix-sdk-indexeddb/src/state_store/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use matrix_sdk_base::{
3030
SerializableEventContent, ServerInfo, StateChanges, StateStore, StoreError,
3131
},
3232
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, StateStoreDataKey, StateStoreDataValue,
33+
ROOM_VERSION_FALLBACK,
3334
};
3435
use matrix_sdk_store_encryption::{Error as EncryptionError, StoreCipher};
3536
use ruma::{
@@ -45,7 +46,7 @@ use ruma::{
4546
},
4647
serde::Raw,
4748
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedMxcUri,
48-
OwnedRoomId, OwnedTransactionId, OwnedUserId, RoomId, RoomVersionId, TransactionId, UserId,
49+
OwnedRoomId, OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UserId,
4950
};
5051
use serde::{de::DeserializeOwned, Deserialize, Serialize};
5152
use tracing::{debug, warn};
@@ -929,10 +930,10 @@ impl_state_store!({
929930
.get(&self.encode_key(keys::ROOM_INFOS, room_id))?
930931
.await?
931932
.and_then(|f| self.deserialize_value::<RoomInfo>(&f).ok())
932-
.and_then(|info| info.room_version().cloned())
933+
.map(|info| info.room_version_or_default())
933934
.unwrap_or_else(|| {
934-
warn!(?room_id, "Unable to find the room version, assume version 9");
935-
RoomVersionId::V9
935+
warn!(?room_id, "Unable to find the room version, assuming {ROOM_VERSION_FALLBACK}");
936+
ROOM_VERSION_FALLBACK
936937
})
937938
);
938939
}

crates/matrix-sdk-sqlite/src/state_store.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use matrix_sdk_base::{
1616
RoomLoadSettings, SentRequestKey,
1717
},
1818
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, RoomState, StateChanges, StateStore,
19-
StateStoreDataKey, StateStoreDataValue,
19+
StateStoreDataKey, StateStoreDataValue, ROOM_VERSION_FALLBACK,
2020
};
2121
use matrix_sdk_store_encryption::StoreCipher;
2222
use ruma::{
@@ -33,7 +33,7 @@ use ruma::{
3333
},
3434
serde::Raw,
3535
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId,
36-
OwnedTransactionId, OwnedUserId, RoomId, RoomVersionId, TransactionId, UInt, UserId,
36+
OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
3737
};
3838
use rusqlite::{OptionalExtension, Transaction};
3939
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -1319,13 +1319,13 @@ impl StateStore for SqliteStateStore {
13191319
.ok()
13201320
.flatten()
13211321
.and_then(|v| this.deserialize_json::<RoomInfo>(&v).ok())
1322-
.and_then(|info| info.room_version().cloned())
1322+
.map(|info| info.room_version_or_default())
13231323
.unwrap_or_else(|| {
13241324
warn!(
13251325
?room_id,
1326-
"Unable to find the room version, assume version 9"
1326+
"Unable to find the room version, assuming {ROOM_VERSION_FALLBACK}"
13271327
);
1328-
RoomVersionId::V9
1328+
ROOM_VERSION_FALLBACK
13291329
})
13301330
};
13311331

crates/matrix-sdk/src/event_cache/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ use matrix_sdk_base::{
4242
linked_chunk::lazy_loader::LazyLoaderError,
4343
store_locks::LockStoreError,
4444
sync::RoomUpdates,
45+
ROOM_VERSION_FALLBACK,
4546
};
4647
use matrix_sdk_common::executor::{spawn, JoinHandle};
4748
use room::RoomEventCacheState;
48-
use ruma::{
49-
events::AnySyncEphemeralRoomEvent, serde::Raw, OwnedEventId, OwnedRoomId, RoomId, RoomVersionId,
50-
};
49+
use ruma::{events::AnySyncEphemeralRoomEvent, serde::Raw, OwnedEventId, OwnedRoomId, RoomId};
5150
use tokio::sync::{
5251
broadcast::{channel, error::RecvError, Receiver, Sender},
5352
mpsc, Mutex, RwLock,
@@ -554,8 +553,8 @@ impl EventCacheInner {
554553
.as_ref()
555554
.map(|room| room.clone_info().room_version_or_default())
556555
.unwrap_or_else(|| {
557-
warn!("unknown room version for {room_id}, using default V1");
558-
RoomVersionId::V1
556+
warn!("unknown room version for {room_id}, using default {ROOM_VERSION_FALLBACK}");
557+
ROOM_VERSION_FALLBACK
559558
});
560559

561560
let room_state = RoomEventCacheState::new(

0 commit comments

Comments
 (0)