Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/matrix-sdk-indexeddb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ js-sys.workspace = true
matrix-sdk-base = { workspace = true, features = ["js"], optional = true }
matrix-sdk-crypto = { workspace = true, features = ["js"], optional = true }
matrix-sdk-store-encryption.workspace = true
rmp-serde.workspace = true
ruma.workspace = true
serde.workspace = true
serde-wasm-bindgen = "0.6.5"
Expand Down
39 changes: 39 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 @@ -131,6 +131,16 @@ pub mod v1 {
pub const GAPS: &str = "gaps";
pub const GAPS_KEY_PATH: &str = "id";
pub const MEDIA_RETENTION_POLICY_KEY: &str = "media_retention_policy";
pub const MEDIA: &str = "media";
pub const MEDIA_KEY_PATH: &str = "id";
pub const MEDIA_SOURCE: &str = "media_source";
pub const MEDIA_SOURCE_KEY_PATH: &str = "source";
pub const MEDIA_CONTENT_SIZE: &str = "media_content_size";
pub const MEDIA_CONTENT_SIZE_KEY_PATH: &str = "content_size";
pub const MEDIA_LAST_ACCESS: &str = "media_last_access";
pub const MEDIA_LAST_ACCESS_KEY_PATH: &str = "last_access";
pub const MEDIA_RETENTION_METADATA: &str = "media_retention_metadata";
pub const MEDIA_RETENTION_METADATA_KEY_PATH: &str = "retention_metadata";
}

/// Create all object stores and indices for v1 database
Expand All @@ -140,6 +150,7 @@ pub mod v1 {
create_linked_chunks_object_store(db)?;
create_events_object_store(db)?;
create_gaps_object_store(db)?;
create_media_object_store(db)?;
Ok(())
}

Expand Down Expand Up @@ -218,4 +229,32 @@ pub mod v1 {
let _ = db.create_object_store_with_params(keys::GAPS, &object_store_params)?;
Ok(())
}

/// Create an object store for tracking information about media.
///
/// * Primary Key - `id`
/// * Index - `source` - tracks the [`MediaSource`][1] of the associated
/// media
/// * Index - `content_size` - tracks the size of the media content and
/// whether to ignore the [`MediaRetentionPolicy`][2]
/// * Index - `last_access` - tracks the last time the associated media was
/// accessed
/// * Index - `retention_metadata` - tracks all retention metadata - i.e.,
/// joins `content_size` and `last_access`
///
/// [1]: ruma::events::room::MediaSource
/// [2]: matrix_sdk_base::event_cache::store::media::MediaRetentionPolicy
fn create_media_object_store(db: &IdbDatabase) -> Result<(), DomException> {
let mut object_store_params = IdbObjectStoreParameters::new();
object_store_params.key_path(Some(&keys::MEDIA_KEY_PATH.into()));
let media = db.create_object_store_with_params(keys::MEDIA, &object_store_params)?;
media.create_index(keys::MEDIA_SOURCE, &keys::MEDIA_SOURCE_KEY_PATH.into())?;
media.create_index(keys::MEDIA_CONTENT_SIZE, &keys::MEDIA_CONTENT_SIZE_KEY_PATH.into())?;
media.create_index(keys::MEDIA_LAST_ACCESS, &keys::MEDIA_LAST_ACCESS_KEY_PATH.into())?;
media.create_index(
keys::MEDIA_RETENTION_METADATA,
&keys::MEDIA_RETENTION_METADATA_KEY_PATH.into(),
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2025 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

pub mod ignore_media_retention_policy {
//! This module contains a foreign implementation of [`serde::Serialize`]
//! and [`serde::Deserialize`] for [`IgnoreMediaRetentionPolicy`]. These
//! implementations can be injected with the proper macros, i.e.,
//! `#[serde(with = "path::to::this::module")]`.
//!
//! This is necessary, as [`IgnoreMediaRetentionPolicy`] does not implement
//! these traits directly.

use matrix_sdk_base::event_cache::store::media::IgnoreMediaRetentionPolicy;
use serde::{Deserializer, Serializer};

/// Serializes an [`IgnoreMediaRetentionPolicy`] as a `u8`, where
/// [`IgnoreMediaRetentionPolicy::No`] is `0`
/// and [`IgnoreMediaRetentionPolicy::Yes`] is `1`.
///
/// Note that this is not serialized as a `bool` because boolean values are
/// not supported as IndexedDB keys.
pub fn serialize<S>(ignore_policy: &IgnoreMediaRetentionPolicy, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_u8(match ignore_policy {
IgnoreMediaRetentionPolicy::Yes => 1,
IgnoreMediaRetentionPolicy::No => 0,
})
}

/// Deserializes a `u8` into an [`IgnoreMediaRetentionPolicy`] where `0` is
/// [`IgnoreMediaRetentionPolicy::No`] and anything else is
/// [`IgnoreMediaRetentionPolicy::Yes`].
pub fn deserialize<'de, D>(d: D) -> Result<IgnoreMediaRetentionPolicy, D::Error>
where
D: Deserializer<'de>,
{
Ok(match serde::de::Deserialize::deserialize(d)? {
0u8 => IgnoreMediaRetentionPolicy::No,
_ => IgnoreMediaRetentionPolicy::Yes,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
serializer::IndexeddbSerializer,
};

pub mod foreign;
pub mod traits;
pub mod types;

Expand Down
Loading
Loading