refactor: app settings

This commit is contained in:
2024-10-30 10:57:43 +07:00
parent 11dcef4e87
commit 618a45d349
33 changed files with 617 additions and 629 deletions

View File

@@ -5,7 +5,7 @@ use specta::Type;
use std::{str::FromStr, time::Duration};
use tauri::{Emitter, State};
use crate::{common::get_all_accounts, Nostr};
use crate::{common::get_all_accounts, Nostr, Settings};
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
struct Account {
@@ -249,3 +249,21 @@ pub async fn set_signer(
}
}
}
#[tauri::command]
#[specta::specta]
pub async fn get_app_settings(state: State<'_, Nostr>) -> Result<Settings, String> {
let settings = state.settings.read().await.clone();
Ok(settings)
}
#[tauri::command]
#[specta::specta]
pub async fn set_app_settings(settings: String, state: State<'_, Nostr>) -> Result<(), String> {
let parsed: Settings = serde_json::from_str(&settings).map_err(|e| e.to_string())?;
let mut settings = state.settings.write().await;
// Update state
settings.clone_from(&parsed);
Ok(())
}

View File

@@ -36,6 +36,7 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,
Ok(RichEvent { raw, parsed })
} else {
let filter = Filter::new().id(event_id);
let mut rich_event = RichEvent {
raw: "".to_string(),
parsed: None,

View File

@@ -7,21 +7,9 @@ use tauri::{Emitter, Manager, State};
use crate::{
common::{get_latest_event, process_event},
Nostr, RichEvent, Settings,
Nostr, RichEvent,
};
#[derive(Clone, Serialize, Deserialize, Type)]
pub struct Profile {
name: String,
display_name: String,
about: Option<String>,
picture: String,
banner: Option<String>,
nip05: Option<String>,
lud16: Option<String>,
website: Option<String>,
}
#[derive(Clone, Serialize, Deserialize, Type)]
pub struct Mention {
pubkey: String,
@@ -116,30 +104,9 @@ pub async fn get_contact_list(id: String, state: State<'_, Nostr>) -> Result<Vec
#[tauri::command]
#[specta::specta]
pub async fn set_profile(profile: Profile, state: State<'_, Nostr>) -> Result<String, String> {
pub async fn set_profile(new_profile: String, state: State<'_, Nostr>) -> Result<String, String> {
let client = &state.client;
let mut metadata = Metadata::new()
.name(profile.name)
.display_name(profile.display_name)
.about(profile.about.unwrap_or_default())
.nip05(profile.nip05.unwrap_or_default())
.lud16(profile.lud16.unwrap_or_default());
if let Ok(url) = Url::parse(&profile.picture) {
metadata = metadata.picture(url)
}
if let Some(b) = profile.banner {
if let Ok(url) = Url::parse(&b) {
metadata = metadata.banner(url)
}
}
if let Some(w) = profile.website {
if let Ok(url) = Url::parse(&w) {
metadata = metadata.website(url)
}
}
let metadata = Metadata::from_json(new_profile).map_err(|e| e.to_string())?;
match client.set_metadata(&metadata).await {
Ok(id) => Ok(id.to_string()),
@@ -612,21 +579,6 @@ pub async fn get_notifications(id: String, state: State<'_, Nostr>) -> Result<Ve
}
}
#[tauri::command]
#[specta::specta]
pub async fn get_user_settings(state: State<'_, Nostr>) -> Result<Settings, String> {
Ok(state.settings.lock().await.clone())
}
#[tauri::command]
#[specta::specta]
pub async fn set_user_settings(settings: String, state: State<'_, Nostr>) -> Result<(), String> {
let parsed: Settings = serde_json::from_str(&settings).map_err(|e| e.to_string())?;
state.settings.lock().await.clone_from(&parsed);
Ok(())
}
#[tauri::command]
#[specta::specta]
pub async fn verify_nip05(id: String, nip05: &str) -> Result<bool, String> {

View File

@@ -22,7 +22,7 @@ use tauri::{path::BaseDirectory, Emitter, EventTarget, Listener, Manager};
use tauri_plugin_decorum::WebviewWindowExt;
use tauri_plugin_notification::{NotificationExt, PermissionState};
use tauri_specta::{collect_commands, Builder};
use tokio::{sync::Mutex, sync::RwLock, time::sleep};
use tokio::{sync::RwLock, time::sleep};
pub mod commands;
pub mod common;
@@ -30,7 +30,7 @@ pub mod common;
pub struct Nostr {
client: Client,
queue: RwLock<HashSet<PublicKey>>,
settings: Mutex<Settings>,
settings: RwLock<Settings>,
}
#[derive(Serialize, Deserialize, Debug)]
@@ -40,37 +40,30 @@ pub struct Payload {
#[derive(Clone, Serialize, Deserialize, Type)]
pub struct Settings {
proxy: Option<String>,
image_resize_service: Option<String>,
use_relay_hint: bool,
resize_service: bool,
content_warning: bool,
trusted_only: bool,
display_avatar: bool,
display_zap_button: bool,
display_repost_button: bool,
display_media: bool,
transparent: bool,
}
impl Default for Settings {
fn default() -> Self {
Self {
proxy: None,
image_resize_service: Some("https://wsrv.nl".to_string()),
use_relay_hint: true,
content_warning: true,
trusted_only: false,
resize_service: true,
display_avatar: true,
display_zap_button: true,
display_repost_button: true,
display_media: true,
transparent: true,
}
}
}
pub const DEFAULT_DIFFICULTY: u8 = 0;
pub const FETCH_LIMIT: usize = 50;
pub const QUEUE_DELAY: u64 = 300;
pub const NOTIFICATION_SUB_ID: &str = "lume_notification";
fn main() {
@@ -113,8 +106,6 @@ fn main() {
zap_event,
copy_friend,
get_notifications,
get_user_settings,
set_user_settings,
verify_nip05,
get_meta_from_event,
get_event,
@@ -138,6 +129,8 @@ fn main() {
close_column,
close_all_columns,
open_window,
get_app_settings,
set_app_settings,
]);
#[cfg(debug_assertions)]
@@ -182,7 +175,7 @@ fn main() {
// Config
let opts = Options::new()
.gossip(true)
.gossip(false)
.max_avg_latency(Duration::from_millis(300))
.automatic_authentication(true)
.connection_timeout(Some(Duration::from_secs(5)))
@@ -238,7 +231,7 @@ fn main() {
app.manage(Nostr {
client,
queue: RwLock::new(HashSet::new()),
settings: Mutex::new(Settings::default()),
settings: RwLock::new(Settings::default()),
});
// Listen for request metadata
@@ -250,22 +243,27 @@ fn main() {
tauri::async_runtime::spawn(async move {
let state = handle.state::<Nostr>();
let client = &state.client;
let mut write_queue = state.queue.write().await;
if let Ok(public_key) = PublicKey::parse(parsed_payload.id) {
let mut write_queue = state.queue.write().await;
write_queue.insert(public_key);
}
sleep(Duration::from_millis(300)).await;
sleep(Duration::from_millis(QUEUE_DELAY)).await;
let read_queue = state.queue.read().await;
let filter_opts = FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(2));
let opts = SubscribeAutoCloseOptions::default().filter(filter_opts);
let limit = read_queue.len() * 2;
let authors: Vec<PublicKey> = read_queue.iter().copied().collect();
let filter = Filter::new().authors(authors).kind(Kind::Metadata);
let opts = SubscribeAutoCloseOptions::default()
.filter(FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(3)));
let filter = Filter::new()
.authors(authors)
.kind(Kind::Metadata)
.limit(limit);
if client.subscribe(vec![filter], Some(opts)).await.is_ok() {
let mut write_queue = state.queue.write().await;
write_queue.clear();
}
});