Skip to content

Commit 0b2daa7

Browse files
committed
refactor(indexeddb): add migrations and types for media source index
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent e2b3178 commit 0b2daa7

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub mod v1 {
133133
pub const MEDIA_RETENTION_POLICY_KEY: &str = "media_retention_policy";
134134
pub const MEDIA: &str = "media";
135135
pub const MEDIA_KEY_PATH: &str = "id";
136+
pub const MEDIA_SOURCE: &str = "media_source";
137+
pub const MEDIA_SOURCE_KEY_PATH: &str = "source";
136138
pub const MEDIA_CONTENT_SIZE: &str = "media_content_size";
137139
pub const MEDIA_CONTENT_SIZE_KEY_PATH: &str = "content_size";
138140
}
@@ -227,14 +229,18 @@ pub mod v1 {
227229
/// Create an object store for tracking information about media.
228230
///
229231
/// * Primary Key - `id`
232+
/// * Index - `source` - tracks the [`MediaSource`][1] of the associated
233+
/// media
230234
/// * Index - `content_size` - tracks the size of the media content and
231-
/// whether to ignore the [`MediaRetentionPolicy`][1]
235+
/// whether to ignore the [`MediaRetentionPolicy`][2]
232236
///
233-
/// [1]: matrix_sdk_base::event_cache::store::media::MediaRetentionPolicy
237+
/// [1]: ruma::events::room::MediaSource
238+
/// [2]: matrix_sdk_base::event_cache::store::media::MediaRetentionPolicy
234239
fn create_media_object_store(db: &IdbDatabase) -> Result<(), DomException> {
235240
let mut object_store_params = IdbObjectStoreParameters::new();
236241
object_store_params.key_path(Some(&keys::MEDIA_KEY_PATH.into()));
237242
let media = db.create_object_store_with_params(keys::MEDIA, &object_store_params)?;
243+
media.create_index(keys::MEDIA_SOURCE, &keys::MEDIA_SOURCE_KEY_PATH.into())?;
238244
media.create_index(keys::MEDIA_CONTENT_SIZE, &keys::MEDIA_CONTENT_SIZE_KEY_PATH.into())?;
239245
Ok(())
240246
}

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ use matrix_sdk_base::{
3535
media::{MediaRequestParameters, UniqueKey},
3636
};
3737
use matrix_sdk_crypto::CryptoStoreError;
38-
use ruma::{events::relation::RelationType, EventId, OwnedEventId, RoomId};
38+
use ruma::{
39+
events::{relation::RelationType, room::MediaSource},
40+
EventId, OwnedEventId, RoomId,
41+
};
3942
use serde::{Deserialize, Serialize};
4043
use thiserror::Error;
4144

@@ -907,6 +910,9 @@ impl IndexedKey<MediaRetentionPolicy> for IndexedCoreIdKey {
907910
pub struct IndexedMedia {
908911
/// The primary key of the object store
909912
pub id: IndexedMediaIdKey,
913+
/// The (possibly) hashed source of the media derived from
914+
/// [`MediaSource::unique_key`]
915+
pub source: IndexedMediaSourceKey,
910916
/// The size (in bytes) of the media content and whether to ignore the
911917
/// [`MediaRetentionPolicy`]
912918
pub content_size: IndexedMediaContentSizeKey,
@@ -944,6 +950,10 @@ impl Indexed for Media {
944950
&self.metadata.request_parameters,
945951
serializer,
946952
),
953+
source: <IndexedMediaSourceKey as IndexedKey<Self>>::encode(
954+
&self.metadata.request_parameters.source,
955+
serializer,
956+
),
947957
content_size: IndexedMediaContentSizeKey::encode(
948958
(self.metadata.ignore_policy, content.len()),
949959
serializer,
@@ -981,6 +991,23 @@ impl IndexedKey<Media> for IndexedMediaIdKey {
981991
}
982992
}
983993

994+
/// The value associated with the [`source`](IndexedMedia::source) index of the
995+
/// [`MEDIA`][1] object store, which is constructed from:
996+
///
997+
/// - The (possibly) hashed value returned by [`MediaSource::unique_key`]
998+
///
999+
/// [1]: crate::event_cache_store::migrations::v1::create_media_object_store
1000+
#[derive(Debug, Serialize, Deserialize)]
1001+
pub struct IndexedMediaSourceKey(String);
1002+
1003+
impl IndexedKey<Media> for IndexedMediaSourceKey {
1004+
type KeyComponents<'a> = &'a MediaSource;
1005+
1006+
fn encode(components: Self::KeyComponents<'_>, serializer: &IndexeddbSerializer) -> Self {
1007+
Self(serializer.encode_key_as_string(keys::MEDIA_SOURCE, components.unique_key()))
1008+
}
1009+
}
1010+
9841011
/// The value associated with the [`content_size`](IndexedMedia::content_size)
9851012
/// index of the [`MEDIA`][1] object store, which is constructed from:
9861013
///

0 commit comments

Comments
 (0)