11//! Memory-based storage implementation of the NostrMlsStorageProvider trait for Nostr MLS groups
22
33use nostr:: PublicKey ;
4- use nostr_mls_storage:: groups:: error:: GroupError ;
4+ use nostr_mls_storage:: groups:: error:: { GroupError , InvalidGroupState } ;
55use nostr_mls_storage:: groups:: types:: * ;
66use nostr_mls_storage:: groups:: GroupStorage ;
77use nostr_mls_storage:: messages:: types:: Message ;
88
99use crate :: NostrMlsMemoryStorage ;
1010
1111impl GroupStorage for NostrMlsMemoryStorage {
12- fn save_group ( & self , group : Group ) -> Result < Group , GroupError > {
12+ fn save_group ( & self , group : Group ) -> Result < ( ) , GroupError > {
1313 // Store in the MLS group ID cache
1414 {
1515 let mut cache = self . groups_cache . write ( ) ;
@@ -19,10 +19,10 @@ impl GroupStorage for NostrMlsMemoryStorage {
1919 // Store in the Nostr group ID cache
2020 {
2121 let mut cache = self . groups_by_nostr_id_cache . write ( ) ;
22- cache. put ( group. nostr_group_id . clone ( ) , group. clone ( ) ) ;
22+ cache. put ( group. nostr_group_id . clone ( ) , group) ;
2323 }
2424
25- Ok ( group )
25+ Ok ( ( ) )
2626 }
2727
2828 fn all_groups ( & self ) -> Result < Vec < Group > , GroupError > {
@@ -32,92 +32,73 @@ impl GroupStorage for NostrMlsMemoryStorage {
3232 Ok ( groups)
3333 }
3434
35- fn find_group_by_mls_group_id ( & self , mls_group_id : & [ u8 ] ) -> Result < Group , GroupError > {
35+ fn find_group_by_mls_group_id ( & self , mls_group_id : & [ u8 ] ) -> Result < Option < Group > , GroupError > {
3636 let cache = self . groups_cache . read ( ) ;
37- if let Some ( group) = cache. peek ( mls_group_id) {
38- // Return a clone of the found group
39- return Ok ( group. clone ( ) ) ;
40- }
41-
42- Err ( GroupError :: NotFound )
37+ Ok ( cache. peek ( mls_group_id) . cloned ( ) )
4338 }
4439
45- fn find_group_by_nostr_group_id ( & self , nostr_group_id : & str ) -> Result < Group , GroupError > {
40+ fn find_group_by_nostr_group_id (
41+ & self ,
42+ nostr_group_id : & str ,
43+ ) -> Result < Option < Group > , GroupError > {
4644 let cache = self . groups_by_nostr_id_cache . read ( ) ;
47- if let Some ( group) = cache. peek ( nostr_group_id) {
48- // Return a clone of the found group
49- return Ok ( group. clone ( ) ) ;
50- }
51-
52- Err ( GroupError :: NotFound )
45+ Ok ( cache. peek ( nostr_group_id) . cloned ( ) )
5346 }
5447
5548 fn messages ( & self , mls_group_id : & [ u8 ] ) -> Result < Vec < Message > , GroupError > {
5649 // Check if the group exists first
5750 self . find_group_by_mls_group_id ( mls_group_id) ?;
5851
5952 let cache = self . messages_by_group_cache . read ( ) ;
60- if let Some ( messages) = cache. peek ( mls_group_id) {
61- return Ok ( messages. clone ( ) ) ;
53+ match cache. peek ( mls_group_id) . cloned ( ) {
54+ Some ( messages) => Ok ( messages) ,
55+ // If not in cache but group exists, return empty vector
56+ None => Ok ( Vec :: new ( ) ) ,
6257 }
63-
64- // If not in cache but group exists, return empty vector
65- Ok ( Vec :: new ( ) )
6658 }
6759
6860 fn admins ( & self , mls_group_id : & [ u8 ] ) -> Result < Vec < PublicKey > , GroupError > {
69- // Find the group first
70- if let Ok ( group) = self . find_group_by_mls_group_id ( mls_group_id) {
71- // Return the admin pubkeys from the group
72- return Ok ( group. admin_pubkeys ) ;
61+ match self . find_group_by_mls_group_id ( mls_group_id) ? {
62+ Some ( group) => Ok ( group. admin_pubkeys ) ,
63+ None => Err ( GroupError :: InvalidState ( InvalidGroupState :: NoAdmins ) ) ,
7364 }
74-
75- Err ( GroupError :: NotFound )
7665 }
7766
7867 fn group_relays ( & self , mls_group_id : & [ u8 ] ) -> Result < Vec < GroupRelay > , GroupError > {
7968 // Check if the group exists first
8069 self . find_group_by_mls_group_id ( mls_group_id) ?;
8170
8271 let cache = self . group_relays_cache . read ( ) ;
83- if let Some ( relays) = cache. peek ( mls_group_id) {
84- return Ok ( relays. clone ( ) ) ;
72+ match cache. peek ( mls_group_id) . cloned ( ) {
73+ Some ( relays) => Ok ( relays) ,
74+ None => Err ( GroupError :: InvalidState ( InvalidGroupState :: NoRelays ) ) ,
8575 }
86-
87- // If not in cache but group exists, return empty vector
88- Ok ( Vec :: new ( ) )
8976 }
9077
91- fn save_group_relay ( & self , group_relay : GroupRelay ) -> Result < GroupRelay , GroupError > {
92- let mls_group_id = group_relay. mls_group_id . clone ( ) ;
93-
78+ fn save_group_relay ( & self , group_relay : GroupRelay ) -> Result < ( ) , GroupError > {
9479 // Check if the group exists first
95- self . find_group_by_mls_group_id ( & mls_group_id) ?;
96-
97- let group_relay_clone = group_relay. clone ( ) ;
80+ self . find_group_by_mls_group_id ( & group_relay. mls_group_id ) ?;
9881
99- {
100- let mut cache = self . group_relays_cache . write ( ) ;
101- // Get existing relays or create new vector
102- let relays = match cache. get ( & mls_group_id) {
103- Some ( existing_relays) => {
104- let mut new_relays = existing_relays. clone ( ) ;
105- // Add the new relay if it doesn't already exist
106- if !new_relays
107- . iter ( )
108- . any ( |r| r. relay_url == group_relay. relay_url )
109- {
110- new_relays. push ( group_relay_clone) ;
111- }
112- new_relays
113- }
114- None => vec ! [ group_relay_clone] ,
115- } ;
82+ let mut cache = self . group_relays_cache . write ( ) ;
11683
117- // Update the cache with the new vector
118- cache. put ( mls_group_id, relays) ;
119- }
84+ // Try to get the existing relays for the group
85+ match cache. get_mut ( & group_relay. mls_group_id ) {
86+ // If the group exists, add the new relay to the vector
87+ Some ( existing_relays) => {
88+ // TODO: the time complexity here is O(n). The number of relays is likely to be small, but it's probably better to use a BTreeSet anyway.
12089
121- Ok ( group_relay)
90+ // Add the new relay if it doesn't already exist
91+ if !existing_relays. contains ( & group_relay) {
92+ existing_relays. push ( group_relay) ;
93+ }
94+ }
95+ // If the group doesn't exist, create a new vector with the new relay
96+ None => {
97+ // Update the cache with the new vector
98+ cache. put ( group_relay. mls_group_id . clone ( ) , vec ! [ group_relay] ) ;
99+ }
100+ } ;
101+
102+ Ok ( ( ) )
122103 }
123104}
0 commit comments