diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs index 34db417bf..c70e70ddd 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs @@ -81,7 +81,9 @@ impl From for EventCacheStoreError { match value { DomException { .. } => Self::InvalidData { details: value.to_string() }, Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())), - ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() }, + ItemIsNotUnique | ItemNotFound | NumericalOverflow => { + Self::InvalidData { details: value.to_string() } + } Backend(e) => GenericError::from(e.to_string()).into(), } } diff --git a/crates/matrix-sdk-indexeddb/src/media_store/error.rs b/crates/matrix-sdk-indexeddb/src/media_store/error.rs index 728c37a9f..31e07d696 100644 --- a/crates/matrix-sdk-indexeddb/src/media_store/error.rs +++ b/crates/matrix-sdk-indexeddb/src/media_store/error.rs @@ -71,7 +71,9 @@ impl From for MediaStoreError { match value { DomException { .. } => Self::InvalidData { details: value.to_string() }, Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())), - ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() }, + ItemIsNotUnique | ItemNotFound | NumericalOverflow => { + Self::InvalidData { details: value.to_string() } + } Backend(e) => GenericError::from(e.to_string()).into(), } } diff --git a/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs index 30f45f6e1..8332d0ec3 100644 --- a/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs +++ b/crates/matrix-sdk-indexeddb/src/media_store/transaction.rs @@ -348,6 +348,25 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> { self.get_item_by_key_components::(id).await } + /// Query IndexedDB for the maximum [`IndexedMediaContentIdKey`] associated + /// with a [`MediaContent`] + pub async fn get_max_media_content_key_by_id( + &self, + ) -> Result, TransactionError> { + self.get_max_key::(IndexedKeyRange::all( + self.serializer().inner(), + )) + .await + } + + /// Query IndexedDB for the next available [`MediaContent::id`] + pub async fn get_next_media_content_id(&self) -> Result { + Ok(match self.get_max_media_content_key_by_id().await? { + Some(key) => key.checked_add(1).ok_or(TransactionError::NumericalOverflow)?, + None => 0, + }) + } + /// 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> { diff --git a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs index e9668cef4..7cb3a6217 100644 --- a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs @@ -45,6 +45,8 @@ pub enum TransactionError { ItemIsNotUnique, #[error("item not found")] ItemNotFound, + #[error("a numerical operation overflowed")] + NumericalOverflow, #[error("backend: {0}")] Backend(Box), }