30
30
use std:: sync:: LazyLock ;
31
31
32
32
use matrix_sdk_base:: {
33
- event_cache:: store:: media:: MediaRetentionPolicy ,
33
+ event_cache:: store:: media:: { IgnoreMediaRetentionPolicy , MediaRetentionPolicy } ,
34
34
linked_chunk:: { ChunkIdentifier , LinkedChunkId } ,
35
+ media:: { MediaRequestParameters , UniqueKey } ,
35
36
} ;
36
37
use matrix_sdk_crypto:: CryptoStoreError ;
37
38
use ruma:: { events:: relation:: RelationType , EventId , OwnedEventId , RoomId } ;
@@ -45,7 +46,7 @@ use crate::{
45
46
Indexed , IndexedKey , IndexedKeyBounds , IndexedKeyComponentBounds ,
46
47
IndexedPrefixKeyBounds , IndexedPrefixKeyComponentBounds ,
47
48
} ,
48
- types:: { Chunk , Event , Gap , Lease , Position } ,
49
+ types:: { Chunk , Event , Gap , Lease , Media , Position } ,
49
50
} ,
50
51
serializer:: { IndexeddbSerializer , MaybeEncrypted } ,
51
52
} ;
@@ -281,6 +282,14 @@ pub type IndexedGapContent = MaybeEncrypted;
281
282
/// A (possibly) encrypted representation of a [`MediaRetentionPolicy`]
282
283
pub type IndexedMediaRetentionPolicyContent = MaybeEncrypted ;
283
284
285
+ /// A (possibly) encrypted representation of a [`MediaMetadata`][1]
286
+ ///
287
+ /// [1]: crate::event_cache_store::types::MediaMetadata
288
+ pub type IndexedMediaMetadata = MaybeEncrypted ;
289
+
290
+ /// A (possibly) encrypted representation of [`Media::content`]
291
+ pub type IndexedMediaContent = Vec < u8 > ;
292
+
284
293
/// Represents the [`LEASES`][1] object store.
285
294
///
286
295
/// [1]: crate::event_cache_store::migrations::v1::create_lease_object_store
@@ -883,3 +892,77 @@ impl IndexedKey<MediaRetentionPolicy> for IndexedCoreIdKey {
883
892
serializer. encode_key_as_string ( keys:: CORE , keys:: MEDIA_RETENTION_POLICY_KEY )
884
893
}
885
894
}
895
+
896
+ /// Represents the [`MEDIA`][1] object store.
897
+ ///
898
+ /// [1]: crate::event_cache_store::migrations::v1::create_media_object_store
899
+ #[ derive( Debug , Serialize , Deserialize ) ]
900
+ pub struct IndexedMedia {
901
+ /// The primary key of the object store
902
+ pub id : IndexedMediaIdKey ,
903
+ /// The (possibly) encrypted metadata - i.e., [`MediaMetadata`][1]
904
+ ///
905
+ /// [1]: crate::event_cache_store::types::MediaMetadata
906
+ pub metadata : IndexedMediaMetadata ,
907
+ /// The (possibly) encrypted content - i.e., [`Media::content`]
908
+ pub content : IndexedMediaContent ,
909
+ }
910
+
911
+ #[ derive( Debug , Error ) ]
912
+ pub enum IndexedMediaError {
913
+ #[ error( "crypto store: {0}" ) ]
914
+ CryptoStore ( #[ from] CryptoStoreError ) ,
915
+ #[ error( "serialization: {0}" ) ]
916
+ Serialization ( #[ from] rmp_serde:: encode:: Error ) ,
917
+ #[ error( "deserialization: {0}" ) ]
918
+ Deserialization ( #[ from] rmp_serde:: decode:: Error ) ,
919
+ }
920
+
921
+ impl Indexed for Media {
922
+ const OBJECT_STORE : & ' static str = keys:: MEDIA ;
923
+
924
+ type IndexedType = IndexedMedia ;
925
+ type Error = IndexedMediaError ;
926
+
927
+ fn to_indexed (
928
+ & self ,
929
+ serializer : & IndexeddbSerializer ,
930
+ ) -> Result < Self :: IndexedType , Self :: Error > {
931
+ let content = rmp_serde:: to_vec_named ( & serializer. maybe_encrypt_value ( & self . content ) ?) ?;
932
+ Ok ( Self :: IndexedType {
933
+ id : <IndexedMediaIdKey as IndexedKey < Self > >:: encode (
934
+ & self . metadata . request_parameters ,
935
+ serializer,
936
+ ) ,
937
+ metadata : serializer. maybe_encrypt_value ( & self . metadata ) ?,
938
+ content,
939
+ } )
940
+ }
941
+
942
+ fn from_indexed (
943
+ indexed : Self :: IndexedType ,
944
+ serializer : & IndexeddbSerializer ,
945
+ ) -> Result < Self , Self :: Error > {
946
+ Ok ( Self {
947
+ metadata : serializer. maybe_decrypt_value ( indexed. metadata ) ?,
948
+ content : serializer. maybe_decrypt_value ( rmp_serde:: from_slice ( & indexed. content ) ?) ?,
949
+ } )
950
+ }
951
+ }
952
+
953
+ /// The primary key of the [`MEDIA`][1] object store, which is constructed from:
954
+ ///
955
+ /// - The (possibly) hashed value returned by
956
+ /// [`MediaRequestParameters::unique_key`]
957
+ ///
958
+ /// [1]: crate::event_cache_store::migrations::v1::create_media_object_store
959
+ #[ derive( Debug , Serialize , Deserialize ) ]
960
+ pub struct IndexedMediaIdKey ( String ) ;
961
+
962
+ impl IndexedKey < Media > for IndexedMediaIdKey {
963
+ type KeyComponents < ' a > = & ' a MediaRequestParameters ;
964
+
965
+ fn encode ( components : Self :: KeyComponents < ' _ > , serializer : & IndexeddbSerializer ) -> Self {
966
+ Self ( serializer. encode_key_as_string ( keys:: MEDIA , components. unique_key ( ) ) )
967
+ }
968
+ }
0 commit comments