1
1
//! Memory-based storage implementation of the NostrMlsStorageProvider trait for Nostr MLS groups
2
2
3
3
use nostr:: PublicKey ;
4
- use nostr_mls_storage:: groups:: error:: GroupError ;
4
+ use nostr_mls_storage:: groups:: error:: { GroupError , InvalidGroupState } ;
5
5
use nostr_mls_storage:: groups:: types:: * ;
6
6
use nostr_mls_storage:: groups:: GroupStorage ;
7
7
use nostr_mls_storage:: messages:: types:: Message ;
8
8
9
9
use crate :: NostrMlsMemoryStorage ;
10
10
11
11
impl GroupStorage for NostrMlsMemoryStorage {
12
- fn save_group ( & self , group : Group ) -> Result < Group , GroupError > {
12
+ fn save_group ( & self , group : Group ) -> Result < ( ) , GroupError > {
13
13
// Store in the MLS group ID cache
14
14
{
15
15
let mut cache = self . groups_cache . write ( ) ;
@@ -19,10 +19,10 @@ impl GroupStorage for NostrMlsMemoryStorage {
19
19
// Store in the Nostr group ID cache
20
20
{
21
21
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) ;
23
23
}
24
24
25
- Ok ( group )
25
+ Ok ( ( ) )
26
26
}
27
27
28
28
fn all_groups ( & self ) -> Result < Vec < Group > , GroupError > {
@@ -32,92 +32,73 @@ impl GroupStorage for NostrMlsMemoryStorage {
32
32
Ok ( groups)
33
33
}
34
34
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 > {
36
36
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 ( ) )
43
38
}
44
39
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 > {
46
44
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 ( ) )
53
46
}
54
47
55
48
fn messages ( & self , mls_group_id : & [ u8 ] ) -> Result < Vec < Message > , GroupError > {
56
49
// Check if the group exists first
57
50
self . find_group_by_mls_group_id ( mls_group_id) ?;
58
51
59
52
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 ( ) ) ,
62
57
}
63
-
64
- // If not in cache but group exists, return empty vector
65
- Ok ( Vec :: new ( ) )
66
58
}
67
59
68
60
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 ) ) ,
73
64
}
74
-
75
- Err ( GroupError :: NotFound )
76
65
}
77
66
78
67
fn group_relays ( & self , mls_group_id : & [ u8 ] ) -> Result < Vec < GroupRelay > , GroupError > {
79
68
// Check if the group exists first
80
69
self . find_group_by_mls_group_id ( mls_group_id) ?;
81
70
82
71
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 ) ) ,
85
75
}
86
-
87
- // If not in cache but group exists, return empty vector
88
- Ok ( Vec :: new ( ) )
89
76
}
90
77
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 > {
94
79
// 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 ) ?;
98
81
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 ( ) ;
116
83
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.
120
89
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 ( ( ) )
122
103
}
123
104
}
0 commit comments