feat: add all nostr events command

This commit is contained in:
reya 2024-02-07 14:04:57 +07:00
parent 3c4bd39384
commit 6a08c1de10
3 changed files with 128 additions and 3 deletions

View File

@ -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,

View File

@ -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<String, ()>
Ok(event)
}
#[tauri::command(async)]
pub async fn get_text_events(
limit: usize,
until: Option<String>,
nostr: State<'_, Nostr>,
) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
let contact_list = &nostr.contact_list.clone().unwrap();
let authors: Vec<XOnlyPublicKey> = 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<Vec<Event>, ()> {
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<EventId, ()> {
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<String>,
nostr: State<'_, Nostr>,
) -> Result<EventId, String> {
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<EventId, ()> {
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<EventId, ()> {
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<EventId, ()> {
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)
}

View File

@ -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<Metadata, ()> {
pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
let client = &nostr.client;
let public_key;