diff --git a/crates/matrix-sdk-base/src/event_cache/store/memory_store.rs b/crates/matrix-sdk-base/src/event_cache/store/memory_store.rs index 8b8b14356..c7cf9f750 100644 --- a/crates/matrix-sdk-base/src/event_cache/store/memory_store.rs +++ b/crates/matrix-sdk-base/src/event_cache/store/memory_store.rs @@ -20,7 +20,7 @@ use matrix_sdk_common::{ ring_buffer::RingBuffer, store_locks::memory_store_helper::try_take_leased_lock, }; -use ruma::{MxcUri, OwnedMxcUri}; +use ruma::{MxcUri, OwnedMxcUri, RoomId}; use super::{EventCacheStore, EventCacheStoreError, Result}; use crate::{ @@ -75,9 +75,10 @@ impl EventCacheStore for MemoryStore { async fn handle_linked_chunk_updates( &self, + room_id: &RoomId, updates: &[Update], ) -> Result<(), Self::Error> { - self.events.write().unwrap().apply_updates(updates); + self.events.write().unwrap().apply_updates(room_id, updates); Ok(()) } diff --git a/crates/matrix-sdk-base/src/event_cache/store/traits.rs b/crates/matrix-sdk-base/src/event_cache/store/traits.rs index dde808309..2584b8881 100644 --- a/crates/matrix-sdk-base/src/event_cache/store/traits.rs +++ b/crates/matrix-sdk-base/src/event_cache/store/traits.rs @@ -16,7 +16,7 @@ use std::{fmt, sync::Arc}; use async_trait::async_trait; use matrix_sdk_common::{linked_chunk::Update, AsyncTraitDeps}; -use ruma::MxcUri; +use ruma::{MxcUri, RoomId}; use super::EventCacheStoreError; use crate::{ @@ -45,6 +45,7 @@ pub trait EventCacheStore: AsyncTraitDeps { /// in-memory. This method aims at forwarding this update inside this store. async fn handle_linked_chunk_updates( &self, + room_id: &RoomId, updates: &[Update], ) -> Result<(), Self::Error>; @@ -144,9 +145,10 @@ impl EventCacheStore for EraseEventCacheStoreError { async fn handle_linked_chunk_updates( &self, + room_id: &RoomId, updates: &[Update], ) -> Result<(), Self::Error> { - self.0.handle_linked_chunk_updates(updates).await.map_err(Into::into) + self.0.handle_linked_chunk_updates(room_id, updates).await.map_err(Into::into) } async fn add_media_content( diff --git a/crates/matrix-sdk-common/src/linked_chunk/relational.rs b/crates/matrix-sdk-common/src/linked_chunk/relational.rs index f4d528bff..d410b2fa8 100644 --- a/crates/matrix-sdk-common/src/linked_chunk/relational.rs +++ b/crates/matrix-sdk-common/src/linked_chunk/relational.rs @@ -15,6 +15,8 @@ //! Implementation for a _relational linked chunk_, see //! [`RelationalLinkedChunk`]. +use ruma::RoomId; + use crate::linked_chunk::{ChunkIdentifier, Position, Update}; /// A row of the [`RelationalLinkedChunk::chunks`]. @@ -76,7 +78,7 @@ impl RelationalLinkedChunk { /// Apply [`Update`]s. That's the only way to write data inside this /// relational linked chunk. - pub fn apply_updates(&mut self, updates: &[Update]) + pub fn apply_updates(&mut self, _room_id: &RoomId, updates: &[Update]) where Item: Clone, Gap: Clone, @@ -239,22 +241,28 @@ impl Default for RelationalLinkedChunk { #[cfg(test)] mod tests { + use ruma::room_id; + use super::{ChunkIdentifier as CId, *}; #[test] fn test_new_items_chunk() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // 0 - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // 1 after 0 - Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, - // 2 before 0 - Update::NewItemsChunk { previous: None, new: CId(2), next: Some(CId(0)) }, - // 3 between 2 and 0 - Update::NewItemsChunk { previous: Some(CId(2)), new: CId(3), next: Some(CId(0)) }, - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // 0 + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // 1 after 0 + Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, + // 2 before 0 + Update::NewItemsChunk { previous: None, new: CId(2), next: Some(CId(0)) }, + // 3 between 2 and 0 + Update::NewItemsChunk { previous: Some(CId(2)), new: CId(3), next: Some(CId(0)) }, + ], + ); // Chunks are correctly linked. assert_eq!( @@ -272,16 +280,20 @@ mod tests { #[test] fn test_new_gap_chunk() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // 0 - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // 1 after 0 - Update::NewGapChunk { previous: Some(CId(0)), new: CId(1), next: None, gap: () }, - // 2 after 1 - Update::NewItemsChunk { previous: Some(CId(1)), new: CId(2), next: None }, - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // 0 + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // 1 after 0 + Update::NewGapChunk { previous: Some(CId(0)), new: CId(1), next: None, gap: () }, + // 2 after 1 + Update::NewItemsChunk { previous: Some(CId(1)), new: CId(2), next: None }, + ], + ); // Chunks are correctly links. assert_eq!( @@ -301,18 +313,22 @@ mod tests { #[test] fn test_remove_chunk() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // 0 - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // 1 after 0 - Update::NewGapChunk { previous: Some(CId(0)), new: CId(1), next: None, gap: () }, - // 2 after 1 - Update::NewItemsChunk { previous: Some(CId(1)), new: CId(2), next: None }, - // remove 1 - Update::RemoveChunk(CId(1)), - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // 0 + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // 1 after 0 + Update::NewGapChunk { previous: Some(CId(0)), new: CId(1), next: None, gap: () }, + // 2 after 1 + Update::NewItemsChunk { previous: Some(CId(1)), new: CId(2), next: None }, + // remove 1 + Update::RemoveChunk(CId(1)), + ], + ); // Chunks are correctly links. assert_eq!( @@ -328,20 +344,24 @@ mod tests { #[test] fn test_push_items() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // new chunk (this is not mandatory for this test, but let's try to be realistic) - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // new items on 0 - Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c'] }, - // new chunk (to test new items are pushed in the correct chunk) - Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, - // new items on 1 - Update::PushItems { at: Position(CId(1), 0), items: vec!['x', 'y', 'z'] }, - // new items on 0 again - Update::PushItems { at: Position(CId(0), 3), items: vec!['d', 'e'] }, - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // new chunk (this is not mandatory for this test, but let's try to be realistic) + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // new items on 0 + Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c'] }, + // new chunk (to test new items are pushed in the correct chunk) + Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, + // new items on 1 + Update::PushItems { at: Position(CId(1), 0), items: vec!['x', 'y', 'z'] }, + // new items on 0 again + Update::PushItems { at: Position(CId(0), 3), items: vec!['d', 'e'] }, + ], + ); // Chunks are correctly links. assert_eq!( @@ -369,18 +389,22 @@ mod tests { #[test] fn test_remove_item() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // new chunk (this is not mandatory for this test, but let's try to be realistic) - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // new items on 0 - Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c', 'd', 'e'] }, - // remove an item: 'a' - Update::RemoveItem { at: Position(CId(0), 0) }, - // remove an item: 'd' - Update::RemoveItem { at: Position(CId(0), 2) }, - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // new chunk (this is not mandatory for this test, but let's try to be realistic) + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // new items on 0 + Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c', 'd', 'e'] }, + // remove an item: 'a' + Update::RemoveItem { at: Position(CId(0), 0) }, + // remove an item: 'd' + Update::RemoveItem { at: Position(CId(0), 2) }, + ], + ); // Chunks are correctly links. assert_eq!( @@ -400,20 +424,24 @@ mod tests { #[test] fn test_detach_last_items() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); - relational_linked_chunk.apply_updates(&[ - // new chunk - Update::NewItemsChunk { previous: None, new: CId(0), next: None }, - // new chunk - Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, - // new items on 0 - Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c', 'd', 'e'] }, - // new items on 1 - Update::PushItems { at: Position(CId(1), 0), items: vec!['x', 'y', 'z'] }, - // detach last items on 0 - Update::DetachLastItems { at: Position(CId(0), 2) }, - ]); + relational_linked_chunk.apply_updates( + room_id, + &[ + // new chunk + Update::NewItemsChunk { previous: None, new: CId(0), next: None }, + // new chunk + Update::NewItemsChunk { previous: Some(CId(0)), new: CId(1), next: None }, + // new items on 0 + Update::PushItems { at: Position(CId(0), 0), items: vec!['a', 'b', 'c', 'd', 'e'] }, + // new items on 1 + Update::PushItems { at: Position(CId(1), 0), items: vec!['x', 'y', 'z'] }, + // detach last items on 0 + Update::DetachLastItems { at: Position(CId(0), 2) }, + ], + ); // Chunks are correctly links. assert_eq!( @@ -438,10 +466,11 @@ mod tests { #[test] fn test_start_and_end_reattach_items() { + let room_id = room_id!("!r0:matrix.org"); let mut relational_linked_chunk = RelationalLinkedChunk::::new(); relational_linked_chunk - .apply_updates(&[Update::StartReattachItems, Update::EndReattachItems]); + .apply_updates(room_id, &[Update::StartReattachItems, Update::EndReattachItems]); // Nothing happened. assert!(relational_linked_chunk.chunks.is_empty()); diff --git a/crates/matrix-sdk-sqlite/src/event_cache_store.rs b/crates/matrix-sdk-sqlite/src/event_cache_store.rs index dc74999e2..f6b62e762 100644 --- a/crates/matrix-sdk-sqlite/src/event_cache_store.rs +++ b/crates/matrix-sdk-sqlite/src/event_cache_store.rs @@ -8,7 +8,7 @@ use matrix_sdk_base::{ media::{MediaRequestParameters, UniqueKey}, }; use matrix_sdk_store_encryption::StoreCipher; -use ruma::MilliSecondsSinceUnixEpoch; +use ruma::{MilliSecondsSinceUnixEpoch, RoomId}; use rusqlite::OptionalExtension; use tokio::fs; use tracing::debug; @@ -185,6 +185,7 @@ impl EventCacheStore for SqliteEventCacheStore { async fn handle_linked_chunk_updates( &self, + _room_id: &RoomId, _updates: &[Update], ) -> Result<(), Self::Error> { todo!()