-
Notifications
You must be signed in to change notification settings - Fork 36
Fix tag reference #305
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
Fix tag reference #305
Conversation
WalkthroughThe changes update the logic for handling relay tags in Nostr events and relay list publishing. Tag filtering and creation are now differentiated based on event or relay type, using single-letter "r" tags for certain kinds and "relay" tags for others. Extensive asynchronous tests were added to verify correct tag usage and extraction. Changes
Sequence Diagram(s)sequenceDiagram
participant Account
participant RelayManager
participant Event
participant Database
Account->>RelayManager: publish_relay_list_for_account()
RelayManager->>RelayManager: For each relay, create tag ("r" for Nostr, "relay" for others)
RelayManager->>Event: Construct event with tags
RelayManager->>Database: Store event
Note over RelayManager,Event: Tests verify correct tag types per event kind
sequenceDiagram
participant Test
participant NostrManager
participant Event
Test->>Event: Create event with tags (varied kinds)
Test->>NostrManager: relay_urls_from_event(event)
NostrManager->>NostrManager: Filter tags by kind ("r" or "relay" depending on event)
NostrManager->>Test: Return extracted relay URLs
Note over Test,NostrManager: Tests assert correct extraction for all cases
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~15–25 minutes Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
src/whitenoise/accounts/relays.rs (3)
275-286: Minor allocation nit: avoid cloning the entire DashSet when building tagsYou can iterate by reference and avoid cloning the whole set.
- RelayType::Nostr => relays_to_publish - .clone() - .into_iter() - .map(|url| Tag::reference(url.to_string())) - .collect(), + RelayType::Nostr => relays_to_publish + .iter() + .map(|url| Tag::reference(url.to_string())) + .collect(), - RelayType::Inbox | RelayType::KeyPackage => relays_to_publish - .clone() - .into_iter() - .map(|url| Tag::custom(TagKind::Relay, [url.to_string()])) - .collect(), + RelayType::Inbox | RelayType::KeyPackage => relays_to_publish + .iter() + .map(|url| Tag::custom(TagKind::Relay, [url.to_string()])) + .collect(),
269-273: Confirm intended publish target set for non-Nostr relay typesWhen target_relays is None, events (including Inbox/KeyPackage relay lists) are published only to account.nip65_relays. If an account has only Inbox or KeyPackage relays (and zero NIP-65 relays), publication might go to an empty set. If you want broader propagation, consider publishing to the union of nip65_relays and the specific relays for the relay_type.
Example change:
let relays_to_use = match target_relays.as_ref() { Some(relays) => relays.clone(), None => { let mut union = DashSet::new(); union.extend(account.nip65_relays.clone()); union.extend(match relay_type { RelayType::Nostr => account.nip65_relays.clone(), RelayType::Inbox => account.inbox_relays.clone(), RelayType::KeyPackage => account.key_package_relays.clone(), }); union } };
716-884: Great end-to-end verification of published tag types; consider reducing flakinessThe tests thoroughly verify that Kind::RelayList contains only "r" tags and that Inbox/KeyPackage relay events contain only "relay" tags. The fixed sleep can be flaky; prefer polling with a timeout until events appear in the DB.
A sketch to replace the fixed sleep:
use tokio::time::{sleep, timeout}; use std::time::Duration; // wait up to 2s for at least one event timeout(Duration::from_secs(2), async { loop { let events = whitenoise.nostr.client.database().query(relay_list_filter.clone()).await.unwrap(); if !events.is_empty() { break; } sleep(Duration::from_millis(50)).await; } }).await.expect("timed out waiting for relay list event");src/nostr_manager/mod.rs (1)
491-514: Micro-optimization: avoid boxing a closureYou can inline the match in the filter to eliminate the Box allocation.
- let tag_filter: Box<dyn Fn(&Tag) -> bool> = match event.kind { - Kind::RelayList => { - Box::new(|tag| { - tag.kind() == TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::R)) - }) - } - Kind::InboxRelays | Kind::MlsKeyPackageRelays => { - Box::new(|tag| tag.kind() == TagKind::Relay) - } - _ => { - Box::new(|tag| { - tag.kind() == TagKind::Relay - || tag.kind() - == TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::R)) - }) - } - }; - - event - .tags - .into_iter() - .filter(|tag| tag_filter(tag)) + event + .tags + .into_iter() + .filter(|tag| match event.kind { + Kind::RelayList => { + tag.kind() == TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::R)) + } + Kind::InboxRelays | Kind::MlsKeyPackageRelays => tag.kind() == TagKind::Relay, + _ => { + tag.kind() == TagKind::Relay + || tag.kind() + == TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::R)) + } + })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/nostr_manager/mod.rs(2 hunks)src/whitenoise/accounts/relays.rs(2 hunks)
🔇 Additional comments (9)
src/whitenoise/accounts/relays.rs (2)
275-286: Correct tag-kind selection for each relay typeUsing Tag::reference for Nostr (produces "r" tags) and Tag::custom(TagKind::Relay, ...) for Inbox/KeyPackage is consistent and matches the parsing logic in NostrManager.
692-714: Unit test correctly asserts tag kinds created by the helpersThe assertions validate that Tag::reference yields the "r" single-letter tag and Tag::custom(TagKind::Relay, ...) yields "relay" tags. Solid coverage of the new behavior.
src/nostr_manager/mod.rs (7)
491-514: Clear, kind-aware tag filtering (RelayList uses "r"; Inbox/KeyPackage use "relay")This logic aligns with the updated publishing behavior and preserves backward compatibility for unknown kinds.
1042-1069: Test: RelayList with mixed tags parses only "r" entriesValidates the filter excludes TagKind::Relay for Kind::RelayList. Good guard against regressions.
1070-1097: Test: InboxRelays parses only "relay" tags and ignores "r"Accurately captures the intended behavior for Kind::InboxRelays.
1098-1125: Test: KeyPackageRelays parses only "relay" tags and ignores "r"Mirrors the inbox test; confirms correct behavior for Kind::MlsKeyPackageRelays.
1126-1151: Test: Unknown kind supports both tag forms (backward compatibility)Good coverage to ensure legacy events continue to work.
1153-1180: Test: Invalid URLs are filtered outEnsures robustness against malformed tag contents.
1181-1201: Test: No relay tags yields empty resultSanity check looks good; helps prevent accidental inclusion of unrelated tags.
Summary by CodeRabbit