Skip to content

Commit

Permalink
Partial implementation of EventCacheMediaStore
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Feb 26, 2025
1 parent a8082ba commit 8906c94
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 8 additions & 1 deletion crates/matrix-sdk-indexeddb/src/event_cache_store/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::event_cache_store::{
indexeddb_serializer::IndexeddbSerializer, migrations::open_and_upgrade_db,
};

use matrix_sdk_base::event_cache::store::media::{MediaRetentionPolicy, MediaService};
use matrix_sdk_store_encryption::StoreCipher;
use std::sync::Arc;

Expand Down Expand Up @@ -48,7 +49,13 @@ impl IndexeddbEventCacheStoreBuilder {
let serializer = IndexeddbSerializer::new(self.store_cipher);
let inner = open_and_upgrade_db(&name, &serializer).await?;

let store = IndexeddbEventCacheStore { inner, serializer };
let media_service = MediaService::new();
let media_retention_policy: Option<MediaRetentionPolicy> =
media_retention_policy(&inner).await?;
media_service.restore(policy);

let store =
IndexeddbEventCacheStore { inner, serializer, media_service: Arc::new(media_service) };
Ok(store)
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/matrix-sdk-indexeddb/src/event_cache_store/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use indexed_db_futures::{
request::{IdbOpenDbRequestLike, OpenDbRequest},
IdbDatabase, IdbKeyPath, IdbVersionChangeEvent,
};
use matrix_sdk_base::event_cache::store::media::MediaRetentionPolicy;
use wasm_bindgen::JsValue;

const CURRENT_DB_VERSION: u32 = 2;
Expand Down Expand Up @@ -62,3 +63,15 @@ async fn setup_db(db: IdbDatabase, version: u32) -> Result<IdbDatabase> {

Ok(db)
}

async fn media_retention_policy(db: IdbDatabase) -> Option<MediaRetentionPolicy> {
let store = db
.transaction_on_one_with_mode(keys::CORE, web_sys::IdbTransactionMode::Readonly)?
.object_store(keys::CORE)?;

let policy = store.get_owned(&keys::MEDIA_RETENTION_POLICY.into()).await?;

// let policy = serde_json::from_str(&policy).ok()?;

Some(policy)
}
10 changes: 6 additions & 4 deletions crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use matrix_sdk_base::{
// EventCacheStoreMedia,
IgnoreMediaRetentionPolicy,
MediaRetentionPolicy,
// MediaService,
MediaService,
},
EventCacheStore,
},
Expand Down Expand Up @@ -71,13 +71,13 @@ pub use error::IndexeddbEventCacheStoreError;

mod keys {
pub const CORE: &str = "core";
// Entries in Key-value store
// pub const MEDIA_RETENTION_POLICY: &str = "media_retention_policy";

pub const EVENTS: &str = "events";
pub const LINKED_CHUNKS: &str = "linked_chunks";
pub const GAPS: &str = "gaps";
pub const MEDIA: &str = "media";

// Used as a single key to store/retrieve the media retention policy
pub const MEDIA_RETENTION_POLICY: &str = "media_retention_policy";
}

pub const KEY_SEPARATOR: &str = "\u{001D}";
Expand All @@ -87,9 +87,11 @@ const CHUNK_TYPE_EVENT_TYPE_STRING: &str = "E";
/// The string used to identify a chunk of type gap, in the `type` field in the
/// database.
const CHUNK_TYPE_GAP_TYPE_STRING: &str = "G";

pub struct IndexeddbEventCacheStore {
pub(crate) inner: IdbDatabase,
pub(crate) serializer: IndexeddbSerializer,
pub(crate) media_service: Arc<MediaService>,
}

#[cfg(not(tarpaulin_include))]
Expand Down

0 comments on commit 8906c94

Please sign in to comment.