refactor chats (#15)

* refactor

* update

* update

* update

* remove nostrprofile struct

* update

* refactor contacts

* prevent double login
This commit is contained in:
reya
2025-04-10 08:10:53 +07:00
committed by GitHub
parent f7610cc9c9
commit 3246abace1
27 changed files with 1166 additions and 909 deletions

View File

@@ -1,7 +1,6 @@
use std::time::Duration;
use anyhow::Error;
use common::profile::NostrProfile;
use global::{
constants::{ALL_MESSAGES_SUB_ID, NEW_MESSAGE_SUB_ID},
get_client,
@@ -15,12 +14,19 @@ struct GlobalAccount(Entity<Account>);
impl Global for GlobalAccount {}
pub fn init(cx: &mut App) {
Account::set_global(cx.new(|_| Account { profile: None }), cx);
Account::set_global(
cx.new(|_| Account {
profile: None,
loading: false,
}),
cx,
);
}
#[derive(Debug, Clone)]
pub struct Account {
pub profile: Option<NostrProfile>,
pub profile: Option<Profile>,
loading: bool,
}
impl Account {
@@ -36,7 +42,13 @@ impl Account {
where
S: NostrSigner + 'static,
{
let task: Task<Result<NostrProfile, Error>> = cx.background_spawn(async move {
if self.loading {
return;
}
self.set_loading(true, cx);
let task: Task<Result<Profile, Error>> = cx.background_spawn(async move {
let client = get_client();
// Use user's signer for main signer
_ = client.set_signer(signer).await;
@@ -44,6 +56,7 @@ impl Account {
// Verify nostr signer and get public key
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
log::info!("Logged in with public key: {:?}", public_key);
// Fetch user's metadata
let metadata = client
@@ -51,7 +64,7 @@ impl Account {
.await?
.unwrap_or_default();
Ok(NostrProfile::new(public_key, metadata))
Ok(Profile::new(public_key, metadata))
});
cx.spawn_in(window, async move |this, cx| match task.await {
@@ -59,6 +72,7 @@ impl Account {
cx.update(|_, cx| {
this.update(cx, |this, cx| {
this.profile = Some(profile);
this.set_loading(false, cx);
this.subscribe(cx);
cx.notify();
})
@@ -79,14 +93,14 @@ impl Account {
let client = get_client();
let keys = Keys::generate();
let task: Task<Result<NostrProfile, Error>> = cx.background_spawn(async move {
let task: Task<Result<Profile, Error>> = cx.background_spawn(async move {
let public_key = keys.public_key();
// Update signer
client.set_signer(keys).await;
// Set metadata
client.set_metadata(&metadata).await?;
Ok(NostrProfile::new(public_key, metadata))
Ok(Profile::new(public_key, metadata))
});
cx.spawn_in(window, async move |this, cx| {
@@ -115,7 +129,7 @@ impl Account {
};
let client = get_client();
let user = profile.public_key;
let user = profile.public_key();
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let metadata = Filter::new()
@@ -164,4 +178,9 @@ impl Account {
})
.detach();
}
fn set_loading(&mut self, loading: bool, cx: &mut Context<Self>) {
self.loading = loading;
cx.notify();
}
}