feat: improve tauri commands

This commit is contained in:
reya 2024-05-15 13:58:03 +07:00
parent 8ea2335225
commit 7724eccd72
15 changed files with 701 additions and 711 deletions

View File

@ -25,6 +25,7 @@ import { MediaButton } from "./-components/media";
import { NsfwToggle } from "./-components/nsfw"; import { NsfwToggle } from "./-components/nsfw";
import { MentionButton } from "./-components/mention"; import { MentionButton } from "./-components/mention";
import { MentionNote } from "@/components/note/mentions/note"; import { MentionNote } from "@/components/note/mentions/note";
import { toast } from "sonner";
type EditorSearch = { type EditorSearch = {
reply_to: string; reply_to: string;

View File

@ -207,12 +207,10 @@ export class Ark {
global?: boolean, global?: boolean,
) { ) {
try { try {
let until: string = undefined; const until: string = asOf && asOf > 0 ? asOf.toString() : undefined;
const isGlobal = global ?? false; const isGlobal = global ?? false;
const seens = new Set<string>();
if (asOf && asOf > 0) until = asOf.toString();
const seenIds = new Set<string>();
const nostrEvents: Event[] = await invoke("get_events", { const nostrEvents: Event[] = await invoke("get_events", {
limit, limit,
until, until,
@ -220,39 +218,31 @@ export class Ark {
global: isGlobal, global: isGlobal,
}); });
// remove duplicate event const events = nostrEvents.filter((event) => {
for (const event of nostrEvents) { const eTags = event.tags.filter((el) => el[0] === "e");
if (event.kind === Kind.Repost) { const ids = eTags.map((item) => item[1]);
const repostId = event.tags.find((tag) => tag[0] === "e")?.[1]; const isDup = ids.some((id) => seens.has(id));
seenIds.add(repostId);
// Add found ids to seen list
for (const id of ids) {
seens.add(id);
} }
const eventIds = event.tags // Filter NSFW event
.filter((el) => el[0] === "e")
?.map((item) => item[1]);
if (eventIds?.length) {
for (const id of eventIds) {
seenIds.add(id);
}
}
}
const events = nostrEvents
.filter((event) => !seenIds.has(event.id))
.sort((a, b) => b.created_at - a.created_at);
if (this.settings?.nsfw) { if (this.settings?.nsfw) {
return events.filter( const wTags = event.tags.filter((t) => t[0] === "content-warning");
(event) => const isLewd = wTags.length > 0;
event.tags.filter((event) => event[0] === "content-warning")
.length > 0, return !isDup && !isLewd;
);
} }
// Filter duplicate event
return !isDup;
});
return events; return events;
} catch (e) { } catch (e) {
console.info(String(e)); console.error("[get_events] failed", String(e));
return []; return [];
} }
} }

View File

@ -9,7 +9,15 @@ pub async fn get_event(id: &str, state: State<'_, Nostr>) -> Result<String, Stri
let event_id: Option<EventId> = match Nip19::from_bech32(id) { let event_id: Option<EventId> = match Nip19::from_bech32(id) {
Ok(val) => match val { Ok(val) => match val {
Nip19::EventId(id) => Some(id), Nip19::EventId(id) => Some(id),
Nip19::Event(event) => Some(event.event_id), Nip19::Event(event) => {
let relays = event.relays;
for relay in relays.into_iter() {
let url = Url::from_str(&relay).unwrap();
let _ = client.add_relay(url.clone()).await.unwrap_or_default();
client.connect_relay(url).await.unwrap_or_default();
}
Some(event.event_id)
}
_ => None, _ => None,
}, },
Err(_) => match EventId::from_hex(id) { Err(_) => match EventId::from_hex(id) {
@ -18,23 +26,25 @@ pub async fn get_event(id: &str, state: State<'_, Nostr>) -> Result<String, Stri
}, },
}; };
if let Some(id) = event_id { match event_id {
Some(id) => {
let filter = Filter::new().id(id); let filter = Filter::new().id(id);
if let Ok(events) = &client match &client
.get_events_of(vec![filter], Some(Duration::from_secs(10))) .get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await .await
{ {
Ok(events) => {
if let Some(event) = events.first() { if let Some(event) = events.first() {
Ok(event.as_json()) Ok(event.as_json())
} else { } else {
Err("Event not found with current relay list".into()) Err("Cannot found this event with current relay list".into())
} }
} else {
Err("Event not found with current relay list".into())
} }
} else { Err(err) => Err(err.to_string()),
Err("EventId is not valid".into()) }
}
None => Err("Event ID is not valid.".into()),
} }
} }
@ -213,8 +223,6 @@ pub async fn search(
limit: usize, limit: usize,
state: State<'_, Nostr>, state: State<'_, Nostr>,
) -> Result<Vec<Event>, String> { ) -> Result<Vec<Event>, String> {
println!("search: {}", content);
let client = &state.client; let client = &state.client;
let filter = Filter::new() let filter = Filter::new()
.kinds(vec![Kind::TextNote, Kind::Metadata]) .kinds(vec![Kind::TextNote, Kind::Metadata])

View File

@ -206,15 +206,12 @@ pub async fn load_selected_account(npub: &str, state: State<'_, Nostr>) -> Resul
// Add relay to relay pool // Add relay to relay pool
let _ = client let _ = client
.add_relay_with_opts(relay_url, opts) .add_relay_with_opts(relay_url.clone(), opts)
.await .await
.unwrap_or_default(); .unwrap_or_default();
// Connect relay // Connect relay
client client.connect_relay(relay_url).await.unwrap_or_default();
.connect_relay(item.0.to_string())
.await
.unwrap_or_default();
} }
} }
} }
@ -251,14 +248,10 @@ pub fn to_npub(hex: &str) -> Result<String, ()> {
Ok(npub.to_bech32().unwrap()) Ok(npub.to_bech32().unwrap())
} }
#[tauri::command(async)] #[tauri::command]
pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, ()> { pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, ()> {
let public_key = PublicKey::from_str(key).unwrap(); let public_key = PublicKey::from_str(key).unwrap();
let status = nip05::verify(public_key, nip05, None).await; let status = nip05::verify(public_key, nip05, None).await;
if status.is_ok() { Ok(status.is_ok())
Ok(true)
} else {
Ok(false)
}
} }

View File

@ -104,8 +104,6 @@ pub async fn friend_to_friend(npub: &str, state: State<'_, Nostr>) -> Result<boo
} }
} }
println!("contact list: {}", contact_list.len());
match client.set_contact_list(contact_list).await { match client.set_contact_list(contact_list).await {
Ok(_) => Ok(true), Ok(_) => Ok(true),
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string()),
@ -289,7 +287,7 @@ pub async fn unfollow(id: &str, state: State<'_, Nostr>) -> Result<EventId, Stri
} }
} }
#[tauri::command(async)] #[tauri::command]
pub async fn set_nstore( pub async fn set_nstore(
key: &str, key: &str,
content: &str, content: &str,