Continue redesign for the v1 stable release (#5)
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m32s
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m32s
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use anyhow::{anyhow, Error};
|
||||
use anyhow::{anyhow, Context as AnyhowContext, Error};
|
||||
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -47,8 +47,8 @@ setting_accessors! {
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum AuthMode {
|
||||
#[default]
|
||||
Manual,
|
||||
Auto,
|
||||
Manual,
|
||||
}
|
||||
|
||||
/// Signer kind
|
||||
@@ -121,7 +121,7 @@ pub struct AppSettings {
|
||||
_subscriptions: SmallVec<[Subscription; 1]>,
|
||||
|
||||
/// Background tasks
|
||||
_tasks: SmallVec<[Task<()>; 1]>,
|
||||
tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
|
||||
}
|
||||
|
||||
impl AppSettings {
|
||||
@@ -136,7 +136,7 @@ impl AppSettings {
|
||||
}
|
||||
|
||||
fn new(cx: &mut Context<Self>) -> Self {
|
||||
let load_settings = Self::get_from_database(false, cx);
|
||||
let load_settings = Self::get_from_database(cx);
|
||||
|
||||
let mut tasks = smallvec![];
|
||||
let mut subscriptions = smallvec![];
|
||||
@@ -151,28 +151,33 @@ impl AppSettings {
|
||||
tasks.push(
|
||||
// Load the initial settings
|
||||
cx.spawn(async move |this, cx| {
|
||||
if let Ok(settings) = load_settings.await {
|
||||
this.update(cx, |this, cx| {
|
||||
this.values = settings;
|
||||
cx.notify();
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
let settings = load_settings.await.unwrap_or(Settings::default());
|
||||
log::info!("Settings: {settings:?}");
|
||||
|
||||
// Update the settings state
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_settings(settings, cx);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
|
||||
Self {
|
||||
values: Settings::default(),
|
||||
tasks,
|
||||
_subscriptions: subscriptions,
|
||||
_tasks: tasks,
|
||||
}
|
||||
}
|
||||
|
||||
/// Update settings
|
||||
fn set_settings(&mut self, settings: Settings, cx: &mut Context<Self>) {
|
||||
self.values = settings;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Get settings from the database
|
||||
///
|
||||
/// If `current_user` is true, the settings will be retrieved for current user.
|
||||
/// Otherwise, Coop will load the latest settings from the database.
|
||||
fn get_from_database(current_user: bool, cx: &App) -> Task<Result<Settings, Error>> {
|
||||
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
@@ -183,16 +188,16 @@ impl AppSettings {
|
||||
.identifier(SETTINGS_IDENTIFIER)
|
||||
.limit(1);
|
||||
|
||||
if current_user {
|
||||
let signer = client.signer().await?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
// Push author to the filter
|
||||
filter = filter.author(public_key);
|
||||
// If the signer is available, get settings belonging to the current user
|
||||
if let Some(signer) = client.signer() {
|
||||
if let Ok(public_key) = signer.get_public_key().await {
|
||||
// Push author to the filter
|
||||
filter = filter.author(public_key);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(event) = client.database().query(filter).await?.first_owned() {
|
||||
Ok(serde_json::from_str(&event.content).unwrap_or(Settings::default()))
|
||||
Ok(serde_json::from_str(&event.content)?)
|
||||
} else {
|
||||
Err(anyhow!("Not found"))
|
||||
}
|
||||
@@ -201,18 +206,18 @@ impl AppSettings {
|
||||
|
||||
/// Load settings
|
||||
pub fn load(&mut self, cx: &mut Context<Self>) {
|
||||
let task = Self::get_from_database(true, cx);
|
||||
let task = Self::get_from_database(cx);
|
||||
|
||||
self._tasks.push(
|
||||
self.tasks.push(
|
||||
// Run task in the background
|
||||
cx.spawn(async move |this, cx| {
|
||||
if let Ok(settings) = task.await {
|
||||
this.update(cx, |this, cx| {
|
||||
this.values = settings;
|
||||
cx.notify();
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
let settings = task.await?;
|
||||
// Update settings
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_settings(settings, cx);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -221,35 +226,36 @@ impl AppSettings {
|
||||
pub fn save(&mut self, cx: &mut Context<Self>) {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
let settings = self.values.clone();
|
||||
|
||||
if let Ok(content) = serde_json::to_string(&self.values) {
|
||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||
let signer = client.signer().await?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
self.tasks.push(cx.background_spawn(async move {
|
||||
let signer = client.signer().context("Signer not found")?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
let content = serde_json::to_string(&settings)?;
|
||||
|
||||
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
||||
.tag(Tag::identifier(SETTINGS_IDENTIFIER))
|
||||
.build(public_key)
|
||||
.sign(&Keys::generate())
|
||||
.await?;
|
||||
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
||||
.tag(Tag::identifier(SETTINGS_IDENTIFIER))
|
||||
.build(public_key)
|
||||
.sign(&Keys::generate())
|
||||
.await?;
|
||||
|
||||
client.database().save_event(&event).await?;
|
||||
// Save event to the local database only
|
||||
client.database().save_event(&event).await?;
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
task.detach();
|
||||
}
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
||||
/// Check if the given relay is trusted
|
||||
pub fn is_trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool {
|
||||
self.values.trusted_relays.contains(url)
|
||||
/// Check if the given relay is already authenticated
|
||||
pub fn trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool {
|
||||
self.values.trusted_relays.iter().any(|relay| {
|
||||
relay.as_str_without_trailing_slash() == url.as_str_without_trailing_slash()
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a relay to the trusted list
|
||||
pub fn add_trusted_relay(&mut self, url: RelayUrl, cx: &mut Context<Self>) {
|
||||
self.values.trusted_relays.insert(url);
|
||||
pub fn add_trusted_relay(&mut self, url: &RelayUrl, cx: &mut Context<Self>) {
|
||||
self.values.trusted_relays.insert(url.clone());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user