Skip to content
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

Groupable queries #595

Open
reyamir opened this issue Oct 28, 2024 · 1 comment
Open

Groupable queries #595

reyamir opened this issue Oct 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@reyamir
Copy link
Contributor

reyamir commented Oct 28, 2024

Describe the enhancement
Same as NDK https://github.com/nostr-dev-kit/ndk#groupable-queries

Use case
Prevent rate-limited by relays when load user's metadata or other metadata events from difference UI at the same time.

@reyamir reyamir added the enhancement New feature or request label Oct 28, 2024
@reyamir
Copy link
Contributor Author

reyamir commented Oct 29, 2024

In case anyone need a example for grouped query for Tauri app, I've made a simple function like this

app.listen_any("request_metadata", move |event| {
    let payload = event.payload();
    let parsed_payload: Payload = serde_json::from_str(payload).expect("Parse failed");
    let handle = handle_clone_child.clone();

    tauri::async_runtime::spawn(async move {
        let state = handle.state::<Nostr>();
        let client = &state.client;

        if let Ok(public_key) = PublicKey::parse(parsed_payload.id) {
            let mut write_queue = state.queue.write().await;
            write_queue.insert(public_key);
        };

        // Wait for [QUEUE_DELAY]
        sleep(Duration::from_millis(QUEUE_DELAY)).await;

        let read_queue = state.queue.read().await;

        if !read_queue.is_empty() {
            let authors: HashSet<PublicKey> = read_queue.iter().copied().collect();

            let filter = Filter::new()
                .authors(authors)
                .kind(Kind::Metadata)
                .limit(200);

            let opts = SubscribeAutoCloseOptions::default()
                .filter(FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(3)));

            // Drop queue, you don't need it at this time anymore
            drop(read_queue);
            // Clear queue
            let mut write_queue = state.queue.write().await;
            write_queue.clear();

            if let Ok(output) = client.subscribe(vec![filter], Some(opts)).await {
                println!("Subscribe success: {:?}", output.success);
            }
        }
    });
});

// Handle notification
...
if let Err(e) = handle_clone.emit("metadata", event.as_json()) {
    println!("Emit error: {}", e)
}
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant