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 { MentionButton } from "./-components/mention";
import { MentionNote } from "@/components/note/mentions/note";
import { toast } from "sonner";
type EditorSearch = {
reply_to: string;

View File

@ -207,12 +207,10 @@ export class Ark {
global?: boolean,
) {
try {
let until: string = undefined;
const until: string = asOf && asOf > 0 ? asOf.toString() : undefined;
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", {
limit,
until,
@ -220,39 +218,31 @@ export class Ark {
global: isGlobal,
});
// remove duplicate event
for (const event of nostrEvents) {
if (event.kind === Kind.Repost) {
const repostId = event.tags.find((tag) => tag[0] === "e")?.[1];
seenIds.add(repostId);
const events = nostrEvents.filter((event) => {
const eTags = event.tags.filter((el) => el[0] === "e");
const ids = eTags.map((item) => item[1]);
const isDup = ids.some((id) => seens.has(id));
// Add found ids to seen list
for (const id of ids) {
seens.add(id);
}
const eventIds = event.tags
.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);
// Filter NSFW event
if (this.settings?.nsfw) {
return events.filter(
(event) =>
event.tags.filter((event) => event[0] === "content-warning")
.length > 0,
);
const wTags = event.tags.filter((t) => t[0] === "content-warning");
const isLewd = wTags.length > 0;
return !isDup && !isLewd;
}
// Filter duplicate event
return !isDup;
});
return events;
} catch (e) {
console.info(String(e));
console.error("[get_events] failed", String(e));
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) {
Ok(val) => match val {
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,
},
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);
if let Ok(events) = &client
match &client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
Ok(events) => {
if let Some(event) = events.first() {
Ok(event.as_json())
} 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("EventId is not valid".into())
Err(err) => Err(err.to_string()),
}
}
None => Err("Event ID is not valid.".into()),
}
}
@ -213,8 +223,6 @@ pub async fn search(
limit: usize,
state: State<'_, Nostr>,
) -> Result<Vec<Event>, String> {
println!("search: {}", content);
let client = &state.client;
let filter = Filter::new()
.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
let _ = client
.add_relay_with_opts(relay_url, opts)
.add_relay_with_opts(relay_url.clone(), opts)
.await
.unwrap_or_default();
// Connect relay
client
.connect_relay(item.0.to_string())
.await
.unwrap_or_default();
client.connect_relay(relay_url).await.unwrap_or_default();
}
}
}
@ -251,14 +248,10 @@ pub fn to_npub(hex: &str) -> Result<String, ()> {
Ok(npub.to_bech32().unwrap())
}
#[tauri::command(async)]
#[tauri::command]
pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, ()> {
let public_key = PublicKey::from_str(key).unwrap();
let status = nip05::verify(public_key, nip05, None).await;
if status.is_ok() {
Ok(true)
} else {
Ok(false)
}
Ok(status.is_ok())
}

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 {
Ok(_) => Ok(true),
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(
key: &str,
content: &str,