refactor(sdk): avoid duplicating the comparison of bumpstamps

This commit is contained in:
Benjamin Bouvier
2025-08-29 11:25:48 +02:00
parent 7a762035f1
commit 9f22f550bf
5 changed files with 49 additions and 56 deletions

View File

@@ -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(());
}
}

View File

@@ -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,

View File

@@ -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<u64>,
new: &mut Option<u64>,
) -> 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};

View File

@@ -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(());
}
}

View File

@@ -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(());
}
}