wip: multi accounts

This commit is contained in:
2024-02-19 09:58:27 +07:00
parent 0f06522c28
commit bfe35ad885
13 changed files with 537 additions and 465 deletions

View File

@@ -1,68 +1,85 @@
use crate::Nostr;
use crate::NostrClient;
use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command]
pub async fn get_event(id: &str, nostr: State<'_, Nostr>) -> Result<String, String> {
let client = &nostr.client;
let event_id: EventId = match Nip19::from_bech32(id) {
pub async fn get_event(id: &str, state: State<'_, NostrClient>) -> Result<String, String> {
let client = state.0.lock().await;
let event_id: Option<EventId> = match Nip19::from_bech32(id) {
Ok(val) => match val {
Nip19::EventId(id) => id,
Nip19::Event(event) => event.event_id,
_ => panic!("not nip19"),
Nip19::EventId(id) => Some(id),
Nip19::Event(event) => Some(event.event_id),
_ => None,
},
Err(_) => match EventId::from_hex(id) {
Ok(val) => Some(val),
Err(_) => None,
},
Err(_) => EventId::from_hex(id).unwrap(),
};
let filter = Filter::new().id(event_id);
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get event failed");
if let Some(id) = event_id {
let filter = Filter::new().id(id);
if let Some(event) = events.first() {
Ok(event.as_json())
if let Ok(events) = &client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
if let Some(event) = events.first() {
Ok(event.as_json())
} else {
Err("Event not found with current relay list".into())
}
} else {
Err("Event not found with current relay list".into())
}
} else {
Err("Event not found".into())
Err("EventId is not valid".into())
}
}
#[tauri::command]
pub async fn get_text_events(
limit: usize,
until: Option<String>,
nostr: State<'_, Nostr>,
until: Option<&str>,
contact_list: Option<Vec<&str>>,
state: State<'_, NostrClient>,
) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
let contact_list = &nostr.contact_list.clone().unwrap();
let client = state.0.lock().await;
let authors: Vec<PublicKey> = contact_list.into_iter().map(|x| x.public_key).collect();
let mut final_until = Timestamp::now();
if let Some(list) = contact_list {
let authors: Vec<PublicKey> = list
.into_iter()
.map(|x| PublicKey::from_str(x).unwrap())
.collect();
let mut final_until = Timestamp::now();
if let Some(t) = until {
final_until = Timestamp::from_str(&t).unwrap();
}
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 filter = Filter::new()
.kinds(vec![Kind::TextNote, Kind::Repost])
.authors(authors)
.limit(limit)
.until(final_until);
if let Ok(events) = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
Ok(events)
if let Ok(events) = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
Ok(events)
} else {
Err(())
}
} else {
Err(())
}
}
#[tauri::command]
pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
pub async fn get_event_thread(id: &str, state: State<'_, NostrClient>) -> Result<Vec<Event>, ()> {
let client = state.0.lock().await;
let event_id = EventId::from_hex(id).unwrap();
let filter = Filter::new().kinds(vec![Kind::TextNote]).event(event_id);
@@ -77,9 +94,8 @@ pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<E
}
#[tauri::command]
pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn publish(content: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let event = client
.publish_text_note(content, [])
.await
@@ -92,10 +108,9 @@ pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId,
pub async fn reply_to(
content: &str,
tags: Vec<String>,
nostr: State<'_, Nostr>,
state: State<'_, NostrClient>,
) -> Result<EventId, String> {
let client = &nostr.client;
let client = state.0.lock().await;
if let Ok(event_tags) = Tag::parse(tags) {
let event = client
.publish_text_note(content, vec![event_tags])
@@ -109,8 +124,8 @@ pub async fn reply_to(
}
#[tauri::command]
pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn repost(id: &str, pubkey: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
@@ -123,8 +138,8 @@ pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
}
#[tauri::command]
pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn upvote(id: &str, pubkey: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
@@ -137,8 +152,12 @@ pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
}
#[tauri::command]
pub async fn downvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn downvote(
id: &str,
pubkey: &str,
state: State<'_, NostrClient>,
) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();