diff --git a/src-tauri/src/commands/account.rs b/src-tauri/src/commands/account.rs index 26eea44a..b71d6e6f 100644 --- a/src-tauri/src/commands/account.rs +++ b/src-tauri/src/commands/account.rs @@ -285,7 +285,7 @@ pub async fn login( // NIP-03: Get user's contact list let contact_list = { if let Ok(contacts) = client.get_contact_list(Some(Duration::from_secs(5))).await { - state.contact_list.lock().await.clone_from(&contacts); + state.contact_list.lock().unwrap().clone_from(&contacts); contacts } else { Vec::new() @@ -375,7 +375,7 @@ pub async fn login( .collect(); // Update app's state - state.trusted_list.lock().await.clone_from(&trusted_list); + state.trusted_list.lock().unwrap().clone_from(&trusted_list); let trusted_users: Vec = trusted_list.into_iter().collect(); println!("Total trusted users: {}", trusted_users.len()); diff --git a/src-tauri/src/commands/event.rs b/src-tauri/src/commands/event.rs index 0988e134..535d9e6c 100644 --- a/src-tauri/src/commands/event.rs +++ b/src-tauri/src/commands/event.rs @@ -73,71 +73,35 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result, ) -> Result { let client = &state.client; - let settings = state.settings.lock().await; let event_id = EventId::parse(&id).map_err(|err| err.to_string())?; let filter = Filter::new().id(event_id); - if !settings.use_relay_hint { - match client - .get_events_of( - vec![filter], - EventSource::both(Some(Duration::from_secs(5))), - ) - .await - { - Ok(events) => { - if let Some(event) = events.first() { - let raw = event.as_json(); - let parsed = if event.kind == Kind::TextNote { - Some(parse_event(&event.content).await) - } else { - None - }; - - Ok(RichEvent { raw, parsed }) + match client + .get_events_of( + vec![filter], + EventSource::both(Some(Duration::from_secs(5))), + ) + .await + { + Ok(events) => { + if let Some(event) = events.first() { + let raw = event.as_json(); + let parsed = if event.kind == Kind::TextNote { + Some(parse_event(&event.content).await) } else { - Err("Cannot found this event with current relay list".into()) - } + None + }; + + Ok(RichEvent { raw, parsed }) + } else { + Err("Cannot found this event with current relay list".into()) } - Err(err) => Err(err.to_string()), - } - } else { - // Add relay hint to relay pool - if let Err(e) = client.add_relay(&relay_hint).await { - return Err(e.to_string()); - } - - if let Err(e) = client.connect_relay(&relay_hint).await { - return Err(e.to_string()); - } - - match client - .get_events_of( - vec![Filter::new().id(event_id)], - EventSource::both(Some(Duration::from_secs(5))), - ) - .await - { - Ok(events) => { - if let Some(event) = events.first() { - let raw = event.as_json(); - let parsed = if event.kind == Kind::TextNote { - Some(parse_event(&event.content).await) - } else { - None - }; - - Ok(RichEvent { raw, parsed }) - } else { - Err("Cannot found this event with current relay list".into()) - } - } - Err(err) => Err(err.to_string()), } + Err(err) => Err(err.to_string()), } } diff --git a/src-tauri/src/commands/metadata.rs b/src-tauri/src/commands/metadata.rs index 62530a71..ae0e9359 100644 --- a/src-tauri/src/commands/metadata.rs +++ b/src-tauri/src/commands/metadata.rs @@ -107,8 +107,8 @@ pub async fn set_contact_list( #[tauri::command] #[specta::specta] -pub async fn get_contact_list(state: State<'_, Nostr>) -> Result, String> { - let contact_list = state.contact_list.lock().await.clone(); +pub fn get_contact_list(state: State<'_, Nostr>) -> Result, String> { + let contact_list = state.contact_list.lock().unwrap().clone(); let vec: Vec = contact_list .into_iter() .map(|f| f.public_key.to_hex()) @@ -152,8 +152,8 @@ pub async fn set_profile(profile: Profile, state: State<'_, Nostr>) -> Result) -> Result { - let contact_list = &state.contact_list.lock().await; +pub fn check_contact(id: String, state: State<'_, Nostr>) -> Result { + let contact_list = &state.contact_list.lock().unwrap(); let public_key = PublicKey::from_str(&id).map_err(|e| e.to_string())?; match contact_list.iter().position(|x| x.public_key == public_key) { @@ -577,8 +577,8 @@ pub async fn get_notifications(state: State<'_, Nostr>) -> Result, S #[tauri::command] #[specta::specta] -pub async fn get_user_settings(state: State<'_, Nostr>) -> Result { - Ok(state.settings.lock().await.clone()) +pub fn get_user_settings(state: State<'_, Nostr>) -> Result { + Ok(state.settings.lock().unwrap().clone()) } #[tauri::command] @@ -597,7 +597,7 @@ pub async fn set_user_settings( let parsed: Settings = serde_json::from_str(&settings).map_err(|e| e.to_string())?; // Update state - state.settings.lock().await.clone_from(&parsed); + state.settings.lock().unwrap().clone_from(&parsed); // Emit new changes to frontend NewSettings(parsed).emit(&handle).unwrap(); @@ -622,8 +622,8 @@ pub async fn verify_nip05(id: String, nip05: &str) -> Result { #[tauri::command] #[specta::specta] -pub async fn is_trusted_user(id: String, state: State<'_, Nostr>) -> Result { - let trusted_list = &state.trusted_list.lock().await; +pub fn is_trusted_user(id: String, state: State<'_, Nostr>) -> Result { + let trusted_list = &state.trusted_list.lock().unwrap(); let public_key = PublicKey::from_str(&id).map_err(|e| e.to_string())?; Ok(trusted_list.contains(&public_key)) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a5f1f71d..603f09de 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -16,13 +16,13 @@ use std::{ fs, io::{self, BufRead}, str::FromStr, + sync::Mutex, time::Duration, }; use tauri::{path::BaseDirectory, Emitter, EventTarget, Manager}; use tauri_plugin_decorum::WebviewWindowExt; use tauri_plugin_notification::{NotificationExt, PermissionState}; use tauri_specta::{collect_commands, collect_events, Builder, Event as TauriEvent}; -use tokio::sync::Mutex; pub mod commands; pub mod common; @@ -317,11 +317,7 @@ fn main() { } } None => { - let contact_list = client - .get_contact_list(Some(Duration::from_secs(5))) - .await - .unwrap(); - + let contact_list = state.contact_list.lock().unwrap().clone(); if !contact_list.is_empty() { let authors: Vec = contact_list.iter().map(|f| f.public_key).collect(); diff --git a/src/commands.gen.ts b/src/commands.gen.ts index cc8b0447..93e42409 100644 --- a/src/commands.gen.ts +++ b/src/commands.gen.ts @@ -270,7 +270,7 @@ async getNotifications() : Promise> { else return { status: "error", error: e as any }; } }, -async getUserSettings() : Promise> { +async getUserSettings() : Promise> { try { return { status: "ok", data: await TAURI_INVOKE("get_user_settings") }; } catch (e) { diff --git a/src/components/note/mentions/note.tsx b/src/components/note/mentions/note.tsx index f2705263..aa900d80 100644 --- a/src/components/note/mentions/note.tsx +++ b/src/components/note/mentions/note.tsx @@ -98,33 +98,47 @@ function Content({ text, className }: { text: string; className?: string }) { )); for (const word of nostr) { - const bech32 = word.replace("nostr:", ""); - const data = nip19.decode(bech32); + const bech32 = word.replace("nostr:", "").replace(/[^\w\s]/gi, ""); + try { + const data = nip19.decode(bech32); - switch (data.type) { - case "npub": - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - )); - break; - case "nprofile": - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - )); - break; - default: - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - {match} - - )); - break; + switch (data.type) { + case "npub": + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => , + ); + break; + case "nprofile": + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => ( + + ), + ); + break; + default: + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => ( + + {match} + + ), + ); + break; + } + } catch { + console.log(word); } } diff --git a/src/routes/columns/_layout/stories.lazy.tsx b/src/routes/columns/_layout/stories.lazy.tsx index 1fb086a9..29e3f53b 100644 --- a/src/routes/columns/_layout/stories.lazy.tsx +++ b/src/routes/columns/_layout/stories.lazy.tsx @@ -188,33 +188,48 @@ function Content({ text, className }: { text: string; className?: string }) { )); for (const word of nostr) { - const bech32 = word.replace("nostr:", ""); - const data = nip19.decode(bech32); + const bech32 = word.replace("nostr:", "").replace(/[^\w\s]/gi, ""); - switch (data.type) { - case "npub": - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - )); - break; - case "nprofile": - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - )); - break; - default: - replacedText = reactStringReplace(replacedText, word, (match, i) => ( - - {match} - - )); - break; + try { + const data = nip19.decode(bech32); + + switch (data.type) { + case "npub": + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => , + ); + break; + case "nprofile": + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => ( + + ), + ); + break; + default: + replacedText = reactStringReplace( + replacedText, + word, + (match, i) => ( + + {match} + + ), + ); + break; + } + } catch { + console.log(word); } }