From 6a08c1de102bc46c874bb5eae3e30cbfa5e40935 Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 7 Feb 2024 14:04:57 +0700 Subject: [PATCH] feat: add all nostr events command --- src-tauri/src/main.rs | 9 ++- src-tauri/src/nostr/event.rs | 120 +++++++++++++++++++++++++++++++- src-tauri/src/nostr/metadata.rs | 2 +- 3 files changed, 128 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3f03a370..d2742499 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -129,8 +129,15 @@ fn main() { nostr::keys::verify_signer, nostr::keys::event_to_bech32, nostr::keys::user_to_bech32, - nostr::metadata::get_metadata, + nostr::metadata::get_profile, nostr::event::get_event, + nostr::event::get_text_events, + nostr::event::get_event_thread, + nostr::event::publish, + nostr::event::reply_to, + nostr::event::repost, + nostr::event::upvote, + nostr::event::downvote, commands::secret::secure_save, commands::secret::secure_load, commands::secret::secure_remove, diff --git a/src-tauri/src/nostr/event.rs b/src-tauri/src/nostr/event.rs index 6fe37aa1..1180607d 100644 --- a/src-tauri/src/nostr/event.rs +++ b/src-tauri/src/nostr/event.rs @@ -1,6 +1,6 @@ use crate::Nostr; use nostr_sdk::prelude::*; -use std::time::Duration; +use std::{str::FromStr, time::Duration}; use tauri::State; #[tauri::command(async)] @@ -27,3 +27,121 @@ pub async fn get_event(id: &str, nostr: State<'_, Nostr>) -> Result Ok(event) } + +#[tauri::command(async)] +pub async fn get_text_events( + limit: usize, + until: Option, + nostr: State<'_, Nostr>, +) -> Result, ()> { + let client = &nostr.client; + let contact_list = &nostr.contact_list.clone().unwrap(); + + let authors: Vec = contact_list.into_iter().map(|x| x.pk).collect(); + let mut final_until = Timestamp::now(); + + if let Some(t) = until { + final_until = Timestamp::from_str(&t).unwrap(); + } + + let filter = Filter::new() + .kinds(vec![Kind::TextNote, Kind::Repost]) + .authors(authors) + .limit(limit) + .until(final_until); + + let events = client + .get_events_of(vec![filter], Some(Duration::from_secs(10))) + .await + .expect("Get event failed"); + + Ok(events) +} + +#[tauri::command(async)] +pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result, ()> { + let client = &nostr.client; + let event_id = EventId::from_hex(id).unwrap(); + let filter = Filter::new().kinds(vec![Kind::TextNote]).event(event_id); + + let events = client + .get_events_of(vec![filter], Some(Duration::from_secs(10))) + .await + .expect("Get event failed"); + + Ok(events) +} + +#[tauri::command(async)] +pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + + let event = client + .publish_text_note(content, []) + .await + .expect("Publish new text note failed"); + + Ok(event) +} + +#[tauri::command(async)] +pub async fn reply_to( + content: &str, + tags: Vec, + nostr: State<'_, Nostr>, +) -> Result { + let client = &nostr.client; + + if let Ok(event_tags) = Tag::parse(tags) { + let event = client + .publish_text_note(content, vec![event_tags]) + .await + .expect("Publish reply failed"); + + Ok(event) + } else { + Err("Reply failed".into()) + } +} + +#[tauri::command(async)] +pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + let public_key = XOnlyPublicKey::from_str(pubkey).unwrap(); + let event_id = EventId::from_hex(id).unwrap(); + + let event = client + .repost_event(event_id, public_key) + .await + .expect("Repost failed"); + + Ok(event) +} + +#[tauri::command(async)] +pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + let public_key = XOnlyPublicKey::from_str(pubkey).unwrap(); + let event_id = EventId::from_hex(id).unwrap(); + + let event = client + .like(event_id, public_key) + .await + .expect("Upvote failed"); + + Ok(event) +} + +#[tauri::command(async)] +pub async fn downvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + let public_key = XOnlyPublicKey::from_str(pubkey).unwrap(); + let event_id = EventId::from_hex(id).unwrap(); + + let event = client + .dislike(event_id, public_key) + .await + .expect("Downvote failed"); + + Ok(event) +} diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index 73b84cc5..5c381ff1 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -4,7 +4,7 @@ use std::{str::FromStr, time::Duration}; use tauri::State; #[tauri::command(async)] -pub async fn get_metadata(id: &str, nostr: State<'_, Nostr>) -> Result { +pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result { let client = &nostr.client; let public_key;