@@ -130,6 +130,7 @@ use storable::discrepancy_counter::{DiscrepancyType, StorableDiscrepancyCounter}
130130use storable:: fixed_anchor:: StorableFixedAnchor ;
131131use storable:: openid_credential:: StorableOpenIdCredential ;
132132use storable:: openid_credential_key:: StorableOpenIdCredentialKey ;
133+ use storable:: smtp:: { StorableEmail , StorableEmailAddress , StorableEmailList } ;
133134use storable:: storable_persistent_state:: StorablePersistentState ;
134135
135136pub mod anchor;
@@ -178,6 +179,7 @@ const LOOKUP_APPLICATION_WITH_ORIGIN_MEMORY_INDEX: u8 = 19u8;
178179const STABLE_ANCHOR_APPLICATION_CONFIG_MEMORY_INDEX : u8 = 20u8 ;
179180const LOOKUP_ANCHOR_WITH_RECOVERY_PHRASE_PRINCIPAL_MEMORY_INDEX : u8 = 21u8 ;
180181const LOOKUP_ANCHOR_WITH_PASSKEY_PUBKEY_HASH_MEMORY_INDEX : u8 = 22u8 ;
182+ const SMTP_POSTBOX_MEMORY_INDEX : u8 = 23u8 ;
181183
182184const ANCHOR_MEMORY_ID : MemoryId = MemoryId :: new ( ANCHOR_MEMORY_INDEX ) ;
183185const ARCHIVE_BUFFER_MEMORY_ID : MemoryId = MemoryId :: new ( ARCHIVE_BUFFER_MEMORY_INDEX ) ;
@@ -215,6 +217,8 @@ const LOOKUP_ANCHOR_WITH_RECOVERY_PHRASE_PRINCIPAL_MEMORY_ID: MemoryId =
215217const LOOKUP_ANCHOR_WITH_PASSKEY_PUBKEY_HASH_MEMORY_ID : MemoryId =
216218 MemoryId :: new ( LOOKUP_ANCHOR_WITH_PASSKEY_PUBKEY_HASH_MEMORY_INDEX ) ;
217219
220+ const SMTP_POSTBOX_MEMORY_ID : MemoryId = MemoryId :: new ( SMTP_POSTBOX_MEMORY_INDEX ) ;
221+
218222// The bucket size 128 is relatively low, to avoid wasting memory when using
219223// multiple virtual memories for smaller amounts of data.
220224// This value results in 256 GB of total managed memory, which should be enough
@@ -327,6 +331,9 @@ pub struct Storage<M: Memory> {
327331 lookup_anchor_with_passkey_pubkey_hash_memory_wrapper : MemoryWrapper < ManagedMemory < M > > ,
328332 pub ( crate ) lookup_anchor_with_passkey_pubkey_hash_memory :
329333 StableBTreeMap < Principal , StorableAnchorNumber , ManagedMemory < M > > ,
334+
335+ smtp_postbox_memory_wrapper : MemoryWrapper < ManagedMemory < M > > ,
336+ smtp_postbox : StableBTreeMap < StorableEmailAddress , StorableEmailList , ManagedMemory < M > > ,
330337}
331338
332339#[ repr( C , packed) ]
@@ -410,6 +417,7 @@ impl<M: Memory + Clone> Storage<M> {
410417 memory_manager. get ( LOOKUP_ANCHOR_WITH_RECOVERY_PHRASE_PRINCIPAL_MEMORY_ID ) ;
411418 let lookup_anchor_with_passkey_pubkey_hash_memory =
412419 memory_manager. get ( LOOKUP_ANCHOR_WITH_PASSKEY_PUBKEY_HASH_MEMORY_ID ) ;
420+ let smtp_postbox_memory = memory_manager. get ( SMTP_POSTBOX_MEMORY_ID ) ;
413421
414422 let registration_rates = RegistrationRates :: new (
415423 MinHeap :: init ( registration_ref_rate_memory. clone ( ) )
@@ -510,6 +518,8 @@ impl<M: Memory + Clone> Storage<M> {
510518 lookup_anchor_with_passkey_pubkey_hash_memory : StableBTreeMap :: init (
511519 lookup_anchor_with_passkey_pubkey_hash_memory,
512520 ) ,
521+ smtp_postbox_memory_wrapper : MemoryWrapper :: new ( smtp_postbox_memory. clone ( ) ) ,
522+ smtp_postbox : StableBTreeMap :: init ( smtp_postbox_memory) ,
513523 }
514524 }
515525
@@ -1829,6 +1839,28 @@ impl<M: Memory + Clone> Storage<M> {
18291839 self . header . version
18301840 }
18311841
1842+ pub fn store_email ( & mut self , recipient : String , email : StorableEmail ) {
1843+ use internet_identity_interface:: internet_identity:: types:: smtp:: MAX_EMAILS_PER_USER ;
1844+
1845+ let key = StorableEmailAddress ( recipient) ;
1846+ let mut list = self
1847+ . smtp_postbox
1848+ . get ( & key)
1849+ . unwrap_or ( StorableEmailList {
1850+ emails : Vec :: new ( ) ,
1851+ } ) ;
1852+
1853+ list. emails . push ( email) ;
1854+
1855+ // Keep only the most recent emails
1856+ if list. emails . len ( ) > MAX_EMAILS_PER_USER {
1857+ let start = list. emails . len ( ) - MAX_EMAILS_PER_USER ;
1858+ list. emails = list. emails . split_off ( start) ;
1859+ }
1860+
1861+ self . smtp_postbox . insert ( key, list) ;
1862+ }
1863+
18321864 pub fn memory_sizes ( & self ) -> HashMap < String , u64 > {
18331865 HashMap :: from_iter ( vec ! [
18341866 ( "header" . to_string( ) , self . header_memory. size( ) ) ,
@@ -1905,6 +1937,10 @@ impl<M: Memory + Clone> Storage<M> {
19051937 self . lookup_anchor_with_passkey_pubkey_hash_memory_wrapper
19061938 . size( ) ,
19071939 ) ,
1940+ (
1941+ "smtp_postbox" . to_string( ) ,
1942+ self . smtp_postbox_memory_wrapper. size( ) ,
1943+ ) ,
19081944 ] )
19091945 }
19101946}
0 commit comments