wip: refactor

This commit is contained in:
2025-02-01 08:30:24 +07:00
parent 82f18fc478
commit a61fd27b9d
25 changed files with 547 additions and 507 deletions

View File

@@ -0,0 +1,13 @@
[package]
name = "app_state"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
common = { path = "../common" }
state = { path = "../state" }
ui = { path = "../ui" }
gpui.workspace = true
nostr-sdk.workspace = true

View File

@@ -0,0 +1 @@
pub mod registry;

View File

@@ -0,0 +1,81 @@
use common::{
constants::{ALL_MESSAGES_SUB_ID, NEW_MESSAGE_SUB_ID},
profile::NostrProfile,
};
use gpui::{App, Entity, Global, WeakEntity};
use nostr_sdk::prelude::*;
use state::get_client;
use std::time::Duration;
use ui::Root;
pub struct AppRegistry {
root: WeakEntity<Root>,
user: Option<NostrProfile>,
}
impl Global for AppRegistry {}
impl AppRegistry {
pub fn set_global(root: WeakEntity<Root>, cx: &mut App) {
cx.observe_global::<Self>(|cx| {
if let Some(profile) = cx.global::<Self>().user() {
let client = get_client();
let public_key = profile.public_key();
cx.background_executor()
.spawn(async move {
let subscription = Filter::new()
.kind(Kind::ContactList)
.author(public_key)
.limit(1);
// Get contact list
_ = client.sync(subscription, &SyncOptions::default()).await;
let all_messages_sub_id = SubscriptionId::new(ALL_MESSAGES_SUB_ID);
let new_message_sub_id = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
// Create a filter for getting all gift wrapped events send to current user
let all_messages = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
// Subscription options
let opts = SubscribeAutoCloseOptions::default().exit_policy(
ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(5)),
);
// Create a filter for getting new message
let new_message = Filter::new()
.kind(Kind::GiftWrap)
.pubkey(public_key)
.limit(0);
// Subscribe for all messages
_ = client
.subscribe_with_id(all_messages_sub_id, all_messages, Some(opts))
.await;
// Subscribe for new message
_ = client
.subscribe_with_id(new_message_sub_id, new_message, None)
.await;
})
.detach();
}
})
.detach();
cx.set_global(Self { root, user: None });
}
pub fn set_user(&mut self, profile: Option<NostrProfile>) {
self.user = profile;
}
pub fn user(&self) -> Option<NostrProfile> {
self.user.clone()
}
pub fn root(&self) -> Option<Entity<Root>> {
self.root.upgrade()
}
}