|
| 1 | +//! Memory-based storage implementation of the NostrMlsStorageProvider trait for Nostr MLS groups |
| 2 | +
|
| 3 | +use nostr::PublicKey; |
| 4 | +use nostr_mls_storage::groups::error::GroupError; |
| 5 | +use nostr_mls_storage::groups::types::*; |
| 6 | +use nostr_mls_storage::groups::GroupStorage; |
| 7 | +use nostr_mls_storage::messages::types::Message; |
| 8 | + |
| 9 | +use crate::NostrMlsMemoryStorage; |
| 10 | + |
| 11 | +impl GroupStorage for NostrMlsMemoryStorage { |
| 12 | + fn save_group(&self, group: Group) -> Result<Group, GroupError> { |
| 13 | + // Store in the MLS group ID cache |
| 14 | + { |
| 15 | + let mut cache = self.groups_cache.write(); |
| 16 | + cache.put(group.mls_group_id.clone(), group.clone()); |
| 17 | + } |
| 18 | + |
| 19 | + // Store in the Nostr group ID cache |
| 20 | + { |
| 21 | + let mut cache = self.groups_by_nostr_id_cache.write(); |
| 22 | + cache.put(group.nostr_group_id.clone(), group.clone()); |
| 23 | + } |
| 24 | + |
| 25 | + Ok(group) |
| 26 | + } |
| 27 | + |
| 28 | + fn all_groups(&self) -> Result<Vec<Group>, GroupError> { |
| 29 | + let cache = self.groups_cache.read(); |
| 30 | + // Convert the values from the cache to a Vec |
| 31 | + let groups: Vec<Group> = cache.iter().map(|(_, v)| v.clone()).collect(); |
| 32 | + Ok(groups) |
| 33 | + } |
| 34 | + |
| 35 | + fn find_group_by_mls_group_id(&self, mls_group_id: &[u8]) -> Result<Group, GroupError> { |
| 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) |
| 43 | + } |
| 44 | + |
| 45 | + fn find_group_by_nostr_group_id(&self, nostr_group_id: &str) -> Result<Group, GroupError> { |
| 46 | + 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) |
| 53 | + } |
| 54 | + |
| 55 | + fn messages(&self, mls_group_id: &[u8]) -> Result<Vec<Message>, GroupError> { |
| 56 | + // Check if the group exists first |
| 57 | + self.find_group_by_mls_group_id(mls_group_id)?; |
| 58 | + |
| 59 | + let cache = self.messages_by_group_cache.read(); |
| 60 | + if let Some(messages) = cache.peek(mls_group_id) { |
| 61 | + return Ok(messages.clone()); |
| 62 | + } |
| 63 | + |
| 64 | + // If not in cache but group exists, return empty vector |
| 65 | + Ok(Vec::new()) |
| 66 | + } |
| 67 | + |
| 68 | + 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); |
| 73 | + } |
| 74 | + |
| 75 | + Err(GroupError::NotFound) |
| 76 | + } |
| 77 | + |
| 78 | + fn group_relays(&self, mls_group_id: &[u8]) -> Result<Vec<GroupRelay>, GroupError> { |
| 79 | + // Check if the group exists first |
| 80 | + self.find_group_by_mls_group_id(mls_group_id)?; |
| 81 | + |
| 82 | + let cache = self.group_relays_cache.read(); |
| 83 | + if let Some(relays) = cache.peek(mls_group_id) { |
| 84 | + return Ok(relays.clone()); |
| 85 | + } |
| 86 | + |
| 87 | + // If not in cache but group exists, return empty vector |
| 88 | + Ok(Vec::new()) |
| 89 | + } |
| 90 | + |
| 91 | + fn save_group_relay(&self, group_relay: GroupRelay) -> Result<GroupRelay, GroupError> { |
| 92 | + let mls_group_id = group_relay.mls_group_id.clone(); |
| 93 | + |
| 94 | + // 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(); |
| 98 | + |
| 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 | + }; |
| 116 | + |
| 117 | + // Update the cache with the new vector |
| 118 | + cache.put(mls_group_id, relays); |
| 119 | + } |
| 120 | + |
| 121 | + Ok(group_relay) |
| 122 | + } |
| 123 | +} |
0 commit comments