diff --git a/crates/matrix-sdk-base/src/store/memory_store.rs b/crates/matrix-sdk-base/src/store/memory_store.rs index e531b6a11..f166475fc 100644 --- a/crates/matrix-sdk-base/src/store/memory_store.rs +++ b/crates/matrix-sdk-base/src/store/memory_store.rs @@ -45,7 +45,9 @@ use super::{ use crate::{ MinimalRoomMemberEvent, RoomMemberships, StateStoreDataKey, StateStoreDataValue, deserialized_responses::{DisplayName, RawAnySyncOrStrippedState}, - store::{QueueWedgeError, StoredThreadSubscription}, + store::{ + QueueWedgeError, StoredThreadSubscription, traits::compare_thread_subscription_bump_stamps, + }, }; #[derive(Debug, Default)] @@ -978,22 +980,8 @@ impl StateStore for MemoryStore { if *previous == new { return Ok(()); } - - match (previous.bump_stamp, new.bump_stamp) { - // If the previous subscription had a bump stamp, and the new one - // doesn't, keep the previous one. - (Some(prev_bump), None) => { - new.bump_stamp = Some(prev_bump); - } - - // If the previous bump stamp is newer than the new one, don't store the value at - // all. - (Some(prev_bump), Some(new_bump)) if new_bump <= prev_bump => { - return Ok(()); - } - - // In all other cases, keep the new bumpstamp. - _ => {} + if !compare_thread_subscription_bump_stamps(previous.bump_stamp, &mut new.bump_stamp) { + return Ok(()); } } diff --git a/crates/matrix-sdk-base/src/store/mod.rs b/crates/matrix-sdk-base/src/store/mod.rs index b2cafb1be..a28625c67 100644 --- a/crates/matrix-sdk-base/src/store/mod.rs +++ b/crates/matrix-sdk-base/src/store/mod.rs @@ -66,6 +66,7 @@ use ruma::{ use serde::de::DeserializeOwned; use tokio::sync::{Mutex, RwLock, broadcast}; use tracing::warn; +pub use traits::compare_thread_subscription_bump_stamps; use crate::{ MinimalRoomMemberEvent, Room, RoomCreateWithCreatorEventContent, RoomStateFilter, SessionMeta, diff --git a/crates/matrix-sdk-base/src/store/traits.rs b/crates/matrix-sdk-base/src/store/traits.rs index b212f03d7..783dc2dab 100644 --- a/crates/matrix-sdk-base/src/store/traits.rs +++ b/crates/matrix-sdk-base/src/store/traits.rs @@ -1296,6 +1296,37 @@ impl StateStoreDataKey<'_> { pub const SEEN_KNOCK_REQUESTS: &'static str = "seen_knock_requests"; } +/// Compare two thread subscription changes bump stamps, given a fixed room and +/// thread root event id pair. +/// +/// May update the newer one to keep the previous one if needed, under some +/// conditions. +/// +/// Returns true if the new subscription should be stored, or false if the new +/// subscription should be ignored. +pub fn compare_thread_subscription_bump_stamps( + previous: Option, + new: &mut Option, +) -> bool { + match (previous, &new) { + // If the previous subscription had a bump stamp, and the new one doesn't, keep the + // previous one; it should be updated soon via sync anyways. + (Some(prev_bump), None) => { + *new = Some(prev_bump); + } + + // If the previous bump stamp is newer than the new one, don't store the value at all. + (Some(prev_bump), Some(new_bump)) if *new_bump <= prev_bump => { + return false; + } + + // In all other cases, keep the new bumpstamp. + _ => {} + } + + true +} + #[cfg(test)] mod tests { use super::{ServerInfo, now_timestamp_ms}; diff --git a/crates/matrix-sdk-indexeddb/src/state_store/mod.rs b/crates/matrix-sdk-indexeddb/src/state_store/mod.rs index fd1c85916..4d43d0ece 100644 --- a/crates/matrix-sdk-indexeddb/src/state_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/state_store/mod.rs @@ -26,10 +26,10 @@ use indexed_db_futures::prelude::*; use matrix_sdk_base::{ deserialized_responses::{DisplayName, RawAnySyncOrStrippedState}, store::{ - ChildTransactionId, ComposerDraft, DependentQueuedRequest, DependentQueuedRequestKind, - QueuedRequest, QueuedRequestKind, RoomLoadSettings, SentRequestKey, - SerializableEventContent, ServerInfo, StateChanges, StateStore, StoreError, - StoredThreadSubscription, ThreadSubscriptionStatus, + compare_thread_subscription_bump_stamps, ChildTransactionId, ComposerDraft, + DependentQueuedRequest, DependentQueuedRequestKind, QueuedRequest, QueuedRequestKind, + RoomLoadSettings, SentRequestKey, SerializableEventContent, ServerInfo, StateChanges, + StateStore, StoreError, StoredThreadSubscription, ThreadSubscriptionStatus, }, MinimalRoomMemberEvent, RoomInfo, RoomMemberships, StateStoreDataKey, StateStoreDataValue, ROOM_VERSION_FALLBACK, ROOM_VERSION_RULES_FALLBACK, @@ -1832,22 +1832,8 @@ impl_state_store!({ if new == previous { return Ok(()); } - - match (previous.bump_stamp, new.bump_stamp) { - // If the previous subscription had a bump stamp, and the new one - // doesn't, keep the previous one. - (Some(prev_bump), None) => { - new.bump_stamp = Some(prev_bump); - } - - // If the previous bump stamp is newer than the new one, don't store the value at - // all. - (Some(prev_bump), Some(new_bump)) if new_bump <= prev_bump => { - return Ok(()); - } - - // In all other cases, keep the new bumpstamp. - _ => {} + if !compare_thread_subscription_bump_stamps(previous.bump_stamp, &mut new.bump_stamp) { + return Ok(()); } } diff --git a/crates/matrix-sdk-sqlite/src/state_store.rs b/crates/matrix-sdk-sqlite/src/state_store.rs index 6bbb6acdc..1633c9aa4 100644 --- a/crates/matrix-sdk-sqlite/src/state_store.rs +++ b/crates/matrix-sdk-sqlite/src/state_store.rs @@ -12,9 +12,10 @@ use deadpool_sqlite::{Object as SqliteAsyncConn, Pool as SqlitePool, Runtime}; use matrix_sdk_base::{ deserialized_responses::{DisplayName, RawAnySyncOrStrippedState, SyncOrStrippedState}, store::{ - migration_helpers::RoomInfoV1, ChildTransactionId, DependentQueuedRequest, - DependentQueuedRequestKind, QueueWedgeError, QueuedRequest, QueuedRequestKind, - RoomLoadSettings, SentRequestKey, StoredThreadSubscription, ThreadSubscriptionStatus, + compare_thread_subscription_bump_stamps, migration_helpers::RoomInfoV1, ChildTransactionId, + DependentQueuedRequest, DependentQueuedRequestKind, QueueWedgeError, QueuedRequest, + QueuedRequestKind, RoomLoadSettings, SentRequestKey, StoredThreadSubscription, + ThreadSubscriptionStatus, }, MinimalRoomMemberEvent, RoomInfo, RoomMemberships, RoomState, StateChanges, StateStore, StateStoreDataKey, StateStoreDataValue, ROOM_VERSION_FALLBACK, ROOM_VERSION_RULES_FALLBACK, @@ -2126,22 +2127,8 @@ impl StateStore for SqliteStateStore { // No need to update anything. return Ok(()); } - - match (previous.bump_stamp, new.bump_stamp) { - // If the previous subscription had a bump stamp, and the new one - // doesn't, keep the previous one. - (Some(prev_bump), None) => { - new.bump_stamp = Some(prev_bump); - } - - // If the previous bump stamp is newer than the new one, don't store the value at - // all. - (Some(prev_bump), Some(new_bump)) if new_bump <= prev_bump => { - return Ok(()); - } - - // In all other cases, keep the new bumpstamp. - _ => {} + if !compare_thread_subscription_bump_stamps(previous.bump_stamp, &mut new.bump_stamp) { + return Ok(()); } }