-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor relay methods #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b0d46c
Update relay methods and fix some account setup issues
erskingardner 74e5a1a
Fix integration test
erskingardner 9eb040c
Formatting
erskingardner 790b1c1
Add back testing and convenience methods
erskingardner a708596
Improve condiitonal
erskingardner ca1ac9a
Review comments
erskingardner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ use crate::whitenoise::accounts::Account; | |
| use crate::whitenoise::error::{Result, WhitenoiseError}; | ||
| use crate::whitenoise::Whitenoise; | ||
| use nostr_mls::prelude::*; | ||
| use std::time::Duration; | ||
|
|
||
| impl Whitenoise { | ||
| /// Creates a new MLS group with the specified members and settings | ||
|
|
@@ -254,8 +255,13 @@ impl Whitenoise { | |
| // Fetch key packages for all members | ||
| for pk in members.iter() { | ||
| let contact = self.load_contact(pk).await?; | ||
| let relays_to_use = if contact.key_package_relays.is_empty() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! |
||
| Account::default_relays() | ||
| } else { | ||
| contact.key_package_relays.clone() | ||
| }; | ||
| let some_event = self | ||
| .fetch_key_package_event_from(contact.key_package_relays.clone(), *pk) | ||
| .fetch_key_package_event_from(relays_to_use, *pk) | ||
| .await?; | ||
| let event = some_event.ok_or(WhitenoiseError::NostrMlsError( | ||
| nostr_mls::Error::KeyPackage("Does not exist".to_owned()), | ||
|
|
@@ -300,52 +306,73 @@ impl Whitenoise { | |
| ))); | ||
| } | ||
|
|
||
| let result = self | ||
| .nostr | ||
| .publish_event_to(evolution_event, &group_relays) | ||
| .await; | ||
|
|
||
| match result { | ||
| Ok(_event_id) => { | ||
| // Evolution event published successfully | ||
| // Fan out the welcome message to all members | ||
| for (welcome_rumor, contact) in welcome_rumors.iter().zip(contacts) { | ||
| // Get the public key of the member from the key package event | ||
| let key_package_event_id = | ||
| welcome_rumor | ||
| .tags | ||
| .event_ids() | ||
| .next() | ||
| .ok_or(WhitenoiseError::Other(anyhow::anyhow!( | ||
| "No event ID found in welcome rumor" | ||
| )))?; | ||
|
|
||
| let member_pubkey = key_package_events | ||
| .iter() | ||
| .find(|event| event.id == *key_package_event_id) | ||
| .map(|event| event.pubkey) | ||
| .ok_or(WhitenoiseError::Other(anyhow::anyhow!( | ||
| "No public key found in key package event" | ||
| )))?; | ||
|
|
||
| // Create a timestamp 1 month in the future | ||
| use std::ops::Add; | ||
| let one_month_future = Timestamp::now().add(30 * 24 * 60 * 60); | ||
| self.nostr | ||
| .publish_gift_wrap_with_signer( | ||
| &member_pubkey, | ||
| welcome_rumor.clone(), | ||
| vec![Tag::expiration(one_month_future)], | ||
| contact.inbox_relays, | ||
| keys.clone(), | ||
| ) | ||
| .await | ||
| .map_err(WhitenoiseError::from)?; | ||
| } | ||
| } | ||
| Err(e) => { | ||
| return Err(WhitenoiseError::NostrManager(e)); | ||
| // Check if we have any relays to publish to and publish the evolution event | ||
| if group_relays.is_empty() { | ||
| tracing::warn!( | ||
| target: "whitenoise::add_members_to_group", | ||
| "Group has no relays configured, using account's default relays" | ||
| ); | ||
| // Use the account's default relays as fallback | ||
| let fallback_relays: std::collections::BTreeSet<RelayUrl> = account | ||
| .nip65_relays | ||
| .iter() | ||
| .map(|relay_ref| relay_ref.clone()) | ||
| .collect(); | ||
| if fallback_relays.is_empty() { | ||
| return Err(WhitenoiseError::Other(anyhow::anyhow!( | ||
| "No relays available for publishing evolution event - both group relays and account relays are empty" | ||
| ))); | ||
| } | ||
| self.nostr | ||
| .publish_event_to(evolution_event, &fallback_relays) | ||
| .await?; | ||
| } else { | ||
| self.nostr | ||
| .publish_event_to(evolution_event, &group_relays) | ||
| .await?; | ||
| } | ||
|
|
||
| // Evolution event published successfully | ||
| // Fan out the welcome message to all members | ||
| for (welcome_rumor, contact) in welcome_rumors.iter().zip(contacts) { | ||
erskingardner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Get the public key of the member from the key package event | ||
| let key_package_event_id = | ||
| welcome_rumor | ||
| .tags | ||
| .event_ids() | ||
| .next() | ||
| .ok_or(WhitenoiseError::Other(anyhow::anyhow!( | ||
| "No event ID found in welcome rumor" | ||
| )))?; | ||
|
|
||
| let member_pubkey = key_package_events | ||
| .iter() | ||
| .find(|event| event.id == *key_package_event_id) | ||
| .map(|event| event.pubkey) | ||
| .ok_or(WhitenoiseError::Other(anyhow::anyhow!( | ||
| "No public key found in key package event" | ||
| )))?; | ||
|
|
||
| // Create a timestamp 1 month in the future | ||
| let one_month_future = Timestamp::now() + Duration::from_secs(30 * 24 * 60 * 60); | ||
|
|
||
| // Use fallback relays if contact has no inbox relays configured | ||
| let relays_to_use = if contact.inbox_relays.is_empty() { | ||
| Account::default_relays() | ||
| } else { | ||
| contact.inbox_relays | ||
| }; | ||
erskingardner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| self.nostr | ||
| .publish_gift_wrap_with_signer( | ||
| &member_pubkey, | ||
| welcome_rumor.clone(), | ||
| vec![Tag::expiration(one_month_future)], | ||
| relays_to_use, | ||
| keys.clone(), | ||
| ) | ||
| .await | ||
| .map_err(WhitenoiseError::from)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
|
|
@@ -420,22 +447,28 @@ mod tests { | |
|
|
||
| #[tokio::test] | ||
| async fn test_create_group() { | ||
| let whitenoise = test_get_whitenoise().await; | ||
| let (whitenoise, _data_temp, _logs_temp) = create_mock_whitenoise().await; | ||
|
|
||
| // Setup creator account | ||
| let (creator_account, _creator_keys) = setup_login_account(whitenoise).await; | ||
| let creator_account = whitenoise.create_identity().await.unwrap(); | ||
|
|
||
| // Setup member accounts | ||
| let member_accounts = setup_multiple_test_accounts(whitenoise, &creator_account, 2).await; | ||
| let member_pubkeys: Vec<PublicKey> = | ||
| member_accounts.iter().map(|(acc, _)| acc.pubkey).collect(); | ||
| let mut member_pubkeys = Vec::new(); | ||
| for _ in 0..2 { | ||
| let member_account = whitenoise.create_identity().await.unwrap(); | ||
| whitenoise | ||
| .add_contact(&creator_account, member_account.pubkey) | ||
| .await | ||
| .unwrap(); | ||
| member_pubkeys.push(member_account.pubkey); | ||
| } | ||
|
|
||
| // Setup admin accounts (creator + one member as admin) | ||
| let admin_pubkeys = vec![creator_account.pubkey, member_pubkeys[0]]; | ||
|
|
||
| // Test for success case | ||
| case_create_group_success( | ||
| whitenoise, | ||
| &whitenoise, | ||
| &creator_account, | ||
| member_pubkeys.clone(), | ||
| admin_pubkeys.clone(), | ||
|
|
@@ -445,7 +478,7 @@ mod tests { | |
| // // Test case: Key package fetch fails (invalid member) | ||
| // let invalid_member_pubkey = create_test_keys().public_key(); | ||
| // case_create_group_key_package_fetch_fails( | ||
| // whitenoise, | ||
| // &whitenoise, | ||
| // &creator_account, | ||
| // vec![invalid_member_pubkey], | ||
| // admin_pubkeys.clone(), | ||
|
|
@@ -454,7 +487,7 @@ mod tests { | |
|
|
||
| // Test case: Empty admin list | ||
| case_create_group_empty_admin_list( | ||
| whitenoise, | ||
| &whitenoise, | ||
| &creator_account, | ||
| member_pubkeys.clone(), | ||
| vec![], // Empty admin list | ||
|
|
@@ -464,7 +497,7 @@ mod tests { | |
| // Test case: Invalid admin pubkey (not a member) | ||
| let non_member_pubkey = create_test_keys().public_key(); | ||
| case_create_group_invalid_admin_pubkey( | ||
| whitenoise, | ||
| &whitenoise, | ||
| &creator_account, | ||
| member_pubkeys.clone(), | ||
| vec![creator_account.pubkey, non_member_pubkey], | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.