diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs index e404ef4ac..7002b5968 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs @@ -26,8 +26,9 @@ use crate::{ error::AsyncErrorDeps, event_cache_store::{ serializer::indexed_types::{ - IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey, IndexedEventRelationKey, - IndexedEventRoomKey, IndexedGapIdKey, IndexedLeaseIdKey, IndexedNextChunkIdKey, + IndexedChunk, IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey, + IndexedEventRelationKey, IndexedEventRoomKey, IndexedGapIdKey, IndexedLeaseIdKey, + IndexedNextChunkIdKey, }, types::{Chunk, ChunkType, Event, Gap, Lease, Position}, }, @@ -248,9 +249,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> { /// Add a chunk and ensure that the next and previous /// chunks are properly linked to the chunk being added. If a chunk with /// the same identifier already exists, the given chunk will be - /// rejected. - pub async fn add_chunk(&self, chunk: &Chunk) -> Result<(), TransactionError> { - self.add_item(chunk).await?; + /// rejected. When the item is successfully added, the + /// function returns the intermediary type [`IndexedChunk`] in case + /// inspection is needed. + pub async fn add_chunk(&self, chunk: &Chunk) -> Result { + let indexed = self.add_item(chunk).await?; if let Some(previous) = chunk.previous { let previous_identifier = ChunkIdentifier::new(previous); if let Some(mut previous_chunk) = @@ -269,7 +272,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> { self.put_item(&next_chunk).await?; } } - Ok(()) + Ok(indexed) } /// Delete chunk that matches the given id and the given linked chunk id and diff --git a/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs index 8d895148b..e9b6efb7d 100644 --- a/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs +++ b/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs @@ -24,11 +24,12 @@ use ruma::MxcUri; use crate::{ media_store::{ serializer::indexed_types::{ - IndexedCoreIdKey, IndexedLeaseIdKey, IndexedMediaContentIdKey, - IndexedMediaContentSizeKey, IndexedMediaIdKey, IndexedMediaLastAccessKey, - IndexedMediaMetadataContentSizeKey, IndexedMediaMetadataIdKey, - IndexedMediaMetadataLastAccessKey, IndexedMediaMetadataRetentionKey, - IndexedMediaMetadataUriKey, IndexedMediaRetentionMetadataKey, IndexedMediaUriKey, + IndexedCoreIdKey, IndexedLeaseIdKey, IndexedMedia, IndexedMediaContent, + IndexedMediaContentIdKey, IndexedMediaContentSizeKey, IndexedMediaIdKey, + IndexedMediaLastAccessKey, IndexedMediaMetadata, IndexedMediaMetadataContentSizeKey, + IndexedMediaMetadataIdKey, IndexedMediaMetadataLastAccessKey, + IndexedMediaMetadataRetentionKey, IndexedMediaMetadataUriKey, + IndexedMediaRetentionMetadataKey, IndexedMediaUriKey, }, types::{Lease, Media, MediaCleanupTime, MediaContent, MediaMetadata, UnixTime}, }, @@ -223,8 +224,10 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> { } /// Adds [`Media`] to IndexedDB. If an item with the same key already - /// exists, it will be rejected. - pub async fn add_media(&self, media: &Media) -> Result<(), TransactionError> { + /// exists, it will be rejected. When the item is successfully added, the + /// function returns the intermediary type [`IndexedMedia`] in case + /// inspection is needed. + pub async fn add_media(&self, media: &Media) -> Result { self.add_item(media).await } @@ -448,11 +451,13 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> { } /// Adds [`MediaMetadata`] to IndexedDB. If an item with the same key - /// already exists, it will be rejected. + /// already exists, it will be rejected. When the item is successfully + /// added, the function returns the intermediary type + /// [`IndexedMediaMetadata`] in case inspection is needed. pub async fn add_media_metadata( &self, media_metadata: &MediaMetadata, - ) -> Result<(), TransactionError> { + ) -> Result { self.add_item(media_metadata).await } @@ -603,8 +608,13 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> { } /// Adds [`MediaContent`] to IndexedDB. If an item with the same key already - /// exists, it will be rejected. - pub async fn add_media_content(&self, content: &MediaContent) -> Result<(), TransactionError> { + /// exists, it will be rejected. When the item is successfully added, the + /// function returns the intermediary type [`IndexedMediaContent`] in case + /// inspection is needed. + pub async fn add_media_content( + &self, + content: &MediaContent, + ) -> Result { self.add_item(content).await } diff --git a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs index 4604c8241..078e96537 100644 --- a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs @@ -359,23 +359,21 @@ impl<'a> Transaction<'a> { /// Adds an item to the corresponding IndexedDB object /// store, i.e., `T::OBJECT_STORE`. If an item with the same key already - /// exists, it will be rejected. - pub async fn add_item(&self, item: &T) -> Result<(), TransactionError> + /// exists, it will be rejected. When the item is successfully added, the + /// function returns the intermediary type [`Indexed::IndexedType`] in case + /// inspection is needed. + pub async fn add_item(&self, item: &T) -> Result where T: Indexed + Serialize, T::IndexedType: Serialize, T::Error: AsyncErrorDeps, { - self.transaction - .object_store(T::OBJECT_STORE)? - .add( - self.serializer - .serialize(item) - .map_err(|e| TransactionError::Serialization(Box::new(e)))? - .value, - ) - .await - .map_err(Into::into) + let output = self + .serializer + .serialize(item) + .map_err(|e| TransactionError::Serialization(Box::new(e)))?; + self.transaction.object_store(T::OBJECT_STORE)?.add(output.value).await?; + Ok(output.indexed) } /// Puts an item in the corresponding IndexedDB object