From e1d136aa6ef214ebc1f0e9fa6f8afcd72b156ec9 Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Mon, 25 Aug 2025 17:54:40 -0400 Subject: [PATCH] refactor(indexeddb): add indexed type to represent media retention policy Signed-off-by: Michael Goldenberg --- .../src/event_cache_store/migrations.rs | 1 + .../src/event_cache_store/serializer/types.rs | 61 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs index 9978ea906..9db8da0ee 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs @@ -130,6 +130,7 @@ pub mod v1 { pub const EVENTS_RELATION_RELATION_TYPES: &str = "events_relation_relation_type"; pub const GAPS: &str = "gaps"; pub const GAPS_KEY_PATH: &str = "id"; + pub const MEDIA_RETENTION_POLICY_KEY: &str = "media_retention_policy"; } /// Create all object stores and indices for v1 database diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs index fcb67a648..2af9b046e 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs @@ -29,7 +29,10 @@ use std::sync::LazyLock; -use matrix_sdk_base::linked_chunk::{ChunkIdentifier, LinkedChunkId}; +use matrix_sdk_base::{ + event_cache::store::media::MediaRetentionPolicy, + linked_chunk::{ChunkIdentifier, LinkedChunkId}, +}; use matrix_sdk_crypto::CryptoStoreError; use ruma::{events::relation::RelationType, EventId, OwnedEventId, RoomId}; use serde::{Deserialize, Serialize}; @@ -231,6 +234,13 @@ impl From for IndexedKeyRange { } } +/// A representation of the primary key of the [`CORE`][1] object store. +/// The key may or may not be hashed depending on the +/// provided [`IndexeddbSerializer`]. +/// +/// [1]: crate::event_cache_store::migrations::v1::create_core_object_store +pub type IndexedCoreIdKey = String; + /// A (possibly) encrypted representation of a [`Lease`] pub type IndexedLeaseContent = MaybeEncrypted; @@ -268,6 +278,9 @@ pub type IndexedEventContent = MaybeEncrypted; /// A (possibly) encrypted representation of a [`Gap`] pub type IndexedGapContent = MaybeEncrypted; +/// A (possibly) encrypted representation of a [`MediaRetentionPolicy`] +pub type IndexedMediaRetentionPolicyContent = MaybeEncrypted; + /// Represents the [`LEASES`][1] object store. /// /// [1]: crate::event_cache_store::migrations::v1::create_lease_object_store @@ -291,7 +304,7 @@ impl Indexed for Lease { serializer: &IndexeddbSerializer, ) -> Result { Ok(IndexedLease { - id: IndexedLeaseIdKey::encode(&self.key, serializer), + id: >::encode(&self.key, serializer), content: serializer.maybe_encrypt_value(self)?, }) } @@ -826,3 +839,47 @@ impl<'a> IndexedPrefixKeyComponentBounds<'a, Gap, LinkedChunkId<'a>> for Indexed ) } } + +/// Represents the [`MediaRetentionPolicy`] record in the [`CORE`][1] object +/// store. +/// +/// [1]: crate::event_cache_store::migrations::v1::create_core_object_store +#[derive(Debug, Serialize, Deserialize)] +pub struct IndexedMediaRetentionPolicy { + /// The primary key of the object store. + pub id: IndexedCoreIdKey, + /// The (possibly) encrypted content - i.e., a [`MediaRetentionPolicy`]. + pub content: IndexedMediaRetentionPolicyContent, +} + +impl Indexed for MediaRetentionPolicy { + const OBJECT_STORE: &'static str = keys::CORE; + + type IndexedType = IndexedMediaRetentionPolicy; + type Error = CryptoStoreError; + + fn to_indexed( + &self, + serializer: &IndexeddbSerializer, + ) -> Result { + Ok(Self::IndexedType { + id: >::encode((), serializer), + content: serializer.maybe_encrypt_value(self)?, + }) + } + + fn from_indexed( + indexed: Self::IndexedType, + serializer: &IndexeddbSerializer, + ) -> Result { + serializer.maybe_decrypt_value(indexed.content) + } +} + +impl IndexedKey for IndexedCoreIdKey { + type KeyComponents<'a> = (); + + fn encode(components: Self::KeyComponents<'_>, serializer: &IndexeddbSerializer) -> Self { + serializer.encode_key_as_string(keys::CORE, keys::MEDIA_RETENTION_POLICY_KEY) + } +}