Skip to content

Commit

Permalink
Add member pubkeys command and check that group messages are from a g…
Browse files Browse the repository at this point in the history
…roup member
  • Loading branch information
erskingardner committed Jan 16, 2025
1 parent b47d01d commit 7973064
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src-tauri/src/commands/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ pub async fn fetch_mls_messages(
let json_str = json_value.to_string();
let json_event = UnsignedEvent::from_json(&json_str).unwrap();

if !group.members(&wn).unwrap().contains(&json_event.pubkey) {
tracing::error!(
target: "whitenoise::commands::groups::fetch_mls_messages",
"Message from non-member: {:?}",
json_event.pubkey
);
continue;
}

group
.add_message(json_event.clone(), &wn)
.map_err(|e| e.to_string())?;
Expand All @@ -647,3 +656,32 @@ pub async fn fetch_mls_messages(

Ok(())
}

/// Gets the list of members in an MLS group
///
/// # Arguments
/// * `group_id` - Hex-encoded MLS group ID
/// * `wn` - Whitenoise state handle
///
/// # Returns
/// * `Ok(Vec<String>)` - List of member public keys if successful
/// * `Err(String)` - Error message if operation fails
///
/// # Errors
/// * If no active account is found
/// * If group ID cannot be decoded from hex
/// * If group cannot be found
/// * If members cannot be retrieved
#[tauri::command]
pub fn get_group_members(
group_id: &str,
wn: tauri::State<'_, Whitenoise>,
) -> Result<Vec<PublicKey>, String> {
let account = Account::get_active(&wn).map_err(|e| e.to_string())?;
let mls_group_id =
hex::decode(group_id).map_err(|e| format!("Error decoding group id: {}", e))?;
let group = Group::find_by_mls_group_id(&account.pubkey, &mls_group_id, &wn)
.map_err(|e| format!("Error fetching group: {}", e))?;
let members = group.members(&wn).map_err(|e| e.to_string())?;
Ok(members)
}
35 changes: 30 additions & 5 deletions src-tauri/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::database::DatabaseError;
use crate::messages::Message;
use crate::utils::is_valid_hex_pubkey;
use crate::Whitenoise;
use nostr_openmls::groups::GroupError as NostrMlsError;
use nostr_openmls::nostr_group_data_extension::NostrGroupDataExtension;
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -61,6 +62,12 @@ pub enum GroupError {

#[error("Database error: {0}")]
DatabaseError(#[from] DatabaseError),

#[error("MLS error: {0}")]
MlsError(#[from] NostrMlsError),

#[error("Key error: {0}")]
KeyError(#[from] nostr_sdk::key::Error),
}

pub type Result<T> = std::result::Result<T, GroupError>;
Expand Down Expand Up @@ -353,10 +360,28 @@ impl Group {
Ok(messages.into_iter().map(|(_, event)| event).collect())
}

pub fn members(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<Vec<PublicKey>> {
let nostr_mls = wn.nostr_mls.lock().unwrap();
let member_pubkeys = nostr_mls
.member_pubkeys(self.mls_group_id.clone())
.map_err(|e| GroupError::MlsError(e))?;
member_pubkeys
.iter()
.try_fold(Vec::with_capacity(member_pubkeys.len()), |mut acc, pk| {
acc.push(PublicKey::parse(pk)?);
Ok(acc)
})
}

pub fn admins(&self) -> Result<Vec<PublicKey>> {
self.admin_pubkeys.iter().try_fold(
Vec::with_capacity(self.admin_pubkeys.len()),
|mut acc, pk| {
acc.push(PublicKey::parse(pk)?);
Ok(acc)
},
)
}

// pub fn remove(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<()> {}
// pub fn members(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<Vec<PublicKey>> {}
// pub fn admins(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<Vec<PublicKey>> {}
// pub fn chats(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<Vec<Chat>> {}
// pub fn send_chat(&self, chat: &str, wn: &tauri::State<'_, Whitenoise>) -> Result<()> {}
// pub fn fetch_chats_from_relays(&self, wn: &tauri::State<'_, Whitenoise>) -> Result<()> {}
}
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub fn run() {
update_account_onboarding,
get_group,
get_group_and_messages,
get_group_members,
get_invite,
accept_invite,
decline_invite,
Expand Down

0 comments on commit 7973064

Please sign in to comment.