Skip to content

Conversation

@jgmontoya
Copy link
Contributor

@jgmontoya jgmontoya commented Aug 8, 2025

Closes #297

Summary by CodeRabbit

  • Refactor

    • Simplified relay selection by introducing a unified method to retrieve relays by type, improving consistency in relay handling.
  • Tests

    • Added tests to verify accurate relay retrieval for various relay types and fallback to default relays when none are set.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 8, 2025

Walkthrough

A new method, get_relays_of_type, was added to the Contact struct to centralize relay selection logic with fallback to default relays. The method is now used in place of inline relay selection logic in the group management code. Associated tests were added to verify correct relay retrieval and fallback behavior.

Changes

Cohort / File(s) Change Summary
Contact relay selection method & tests
src/whitenoise/accounts/contacts.rs
Added get_relays_of_type to encapsulate relay retrieval with fallback; introduced tests for this logic.
Relay selection refactor in groups
src/whitenoise/accounts/groups.rs
Replaced explicit relay selection logic with calls to get_relays_of_type, removing duplicated fallback checks.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Contact
    participant Account

    Caller->>Contact: get_relays_of_type(relay_type)
    alt Relay set is not empty
        Contact-->>Caller: Return cloned relay set
    else Relay set is empty
        Contact->>Account: default_relays()
        Account-->>Contact: Default relays
        Contact-->>Caller: Return default relays
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Assessment against linked issues

Objective Addressed Explanation
Add a relay fallback method (#297)
Use the relay fallback method in place of inline fallback logic (#297)
Add tests for relay fallback and retrieval (#297)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes were found.

Poem

A bunny with code in its paw,
Hopped in and saw relay law—
"Choose with a fallback,
No more relay whack!"
Now fetching is simple, without a flaw.
🐇✨

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e5fc97 and 11c0856.

📒 Files selected for processing (2)
  • src/whitenoise/accounts/contacts.rs (2 hunks)
  • src/whitenoise/accounts/groups.rs (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/whitenoise/accounts/groups.rs
  • src/whitenoise/accounts/contacts.rs
⏰ 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)
  • GitHub Check: check (ubuntu-latest, native)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/contact-get-relays-of-type

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 (2)
src/whitenoise/accounts/contacts.rs (2)

73-87: Centralized relay fallback method: solid; add docs to lock semantics in

The implementation looks correct and preserves prior behavior while centralizing fallback logic. Please document the fallback to Account::default_relays and the fact that this returns an owned clone.

Apply this doc comment for clarity:

 impl Contact {
-    pub fn get_relays_of_type(&self, relay_type: RelayType) -> DashSet<RelayUrl> {
+    /// Returns the relays for the given `RelayType`.
+    ///
+    /// Behavior:
+    /// - If the contact has non-empty relays of that type, returns a cloned DashSet of those relays.
+    /// - If empty, returns `Account::default_relays()` as a fallback.
+    ///
+    /// Note: This returns an owned DashSet because downstream callers expect ownership.
+    pub fn get_relays_of_type(&self, relay_type: RelayType) -> DashSet<RelayUrl> {
         let relays = match relay_type {
             RelayType::Nostr => &self.nip65_relays,
             RelayType::Inbox => &self.inbox_relays,
             RelayType::KeyPackage => &self.key_package_relays,
         };
 
         if relays.is_empty() {
             Account::default_relays()
         } else {
             relays.clone()
         }
     }
 }

720-771: Tests cover all relay types; minor portability nit with DashSet::from_iter

Good coverage for the three RelayType variants and equality checks. To avoid relying on an associated constructor that may differ across dashmap versions, prefer explicit set construction for expected values.

Apply these localized changes:

-        assert!(Whitenoise::relayurl_dashset_eq(
-            nip65_relays,
-            DashSet::from_iter([nip65_relay])
-        ));
+        let mut expected_nip65 = DashSet::new();
+        expected_nip65.insert(nip65_relay);
+        assert!(Whitenoise::relayurl_dashset_eq(nip65_relays, expected_nip65));
-        assert!(Whitenoise::relayurl_dashset_eq(
-            inbox_relays,
-            DashSet::from_iter([inbox_relay])
-        ));
+        let mut expected_inbox = DashSet::new();
+        expected_inbox.insert(inbox_relay);
+        assert!(Whitenoise::relayurl_dashset_eq(inbox_relays, expected_inbox));
-        assert!(Whitenoise::relayurl_dashset_eq(
-            key_package_relays,
-            DashSet::from_iter([key_package_relay])
-        ));
+        let mut expected_key_pkg = DashSet::new();
+        expected_key_pkg.insert(key_package_relay);
+        assert!(Whitenoise::relayurl_dashset_eq(key_package_relays, expected_key_pkg));
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f344105 and dc01c2d.

📒 Files selected for processing (2)
  • src/whitenoise/accounts/contacts.rs (2 hunks)
  • src/whitenoise/accounts/groups.rs (5 hunks)
🔇 Additional comments (5)
src/whitenoise/accounts/contacts.rs (1)

774-808: No stray inline fallbacks remain

All active code now consistently uses get_relays_of_type. The only remaining .is_empty() check is in the src/_OLD/OLD_commands/groups/create_group.rs legacy file, which isn’t part of the current codepath—no further changes needed.

src/whitenoise/accounts/groups.rs (4)

4-4: Import for RelayType: LGTM

Needed for the new get_relays_of_type calls.


121-129: Inbox relay selection for welcome messages centralized

Using contact.get_relays_of_type(RelayType::Inbox) here matches prior behavior and the new abstraction.


348-357: Inbox relay selection during add_members refactored

Same centralization as above; behavior preserved. Good simplification.


48-52: Residual ad-hoc key-package fallback remains in mod.rs

We’ve centralized all inline get_relays_of_type(RelayType::KeyPackage) checks except for one remaining manual empty-check in the active code. Please replace it with the new common relay-selection logic.

• File: src/whitenoise/accounts/mod.rs Line 661

- if key_package_relays.is_empty() {
-     // legacy fallback…
- }
+ // use `contact.get_relays_of_type(RelayType::KeyPackage)` +
+ // common fetch_key_package_event_from(...) logic here

Likely an incorrect or invalid review comment.

@jgmontoya jgmontoya force-pushed the refactor/contact-get-relays-of-type branch from dc01c2d to 4e5fc97 Compare August 8, 2025 12:24
@jgmontoya jgmontoya requested a review from erskingardner August 8, 2025 12:27
@jgmontoya jgmontoya force-pushed the refactor/contact-get-relays-of-type branch from 4e5fc97 to 11c0856 Compare August 8, 2025 12:28
@erskingardner erskingardner merged commit 1a91e22 into master Aug 8, 2025
4 checks passed
@erskingardner erskingardner deleted the refactor/contact-get-relays-of-type branch August 8, 2025 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a relay fallback method

3 participants