feat: automatically load inbox on startup
This commit is contained in:
@@ -20,7 +20,7 @@ use nostr_sdk::{
|
|||||||
};
|
};
|
||||||
use nostr_sdk::{prelude::NostrEventsDatabaseExt, FromBech32, SubscriptionId};
|
use nostr_sdk::{prelude::NostrEventsDatabaseExt, FromBech32, SubscriptionId};
|
||||||
use smol::Timer;
|
use smol::Timer;
|
||||||
use state::{get_client, initialize_client};
|
use state::get_client;
|
||||||
use std::{collections::HashSet, mem, sync::Arc, time::Duration};
|
use std::{collections::HashSet, mem, sync::Arc, time::Duration};
|
||||||
use ui::{theme::Theme, Root};
|
use ui::{theme::Theme, Root};
|
||||||
use views::{app, onboarding, startup};
|
use views::{app, onboarding, startup};
|
||||||
@@ -49,7 +49,7 @@ fn main() {
|
|||||||
let (batch_tx, batch_rx) = smol::channel::bounded::<Vec<PublicKey>>(100);
|
let (batch_tx, batch_rx) = smol::channel::bounded::<Vec<PublicKey>>(100);
|
||||||
|
|
||||||
// Initialize nostr client
|
// Initialize nostr client
|
||||||
let client = initialize_client();
|
let client = get_client();
|
||||||
|
|
||||||
// Initialize application
|
// Initialize application
|
||||||
let app = Application::new()
|
let app = Application::new()
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ pub fn init(
|
|||||||
) -> Result<Arc<Entity<Chat>>, anyhow::Error> {
|
) -> Result<Arc<Entity<Chat>>, anyhow::Error> {
|
||||||
if let Some(chats) = ChatRegistry::global(cx) {
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
if let Some(room) = chats.read(cx).get(id, cx) {
|
if let Some(room) = chats.read(cx).get(id, cx) {
|
||||||
Ok(Arc::new(Chat::new(id, &room, window, cx)))
|
Ok(Arc::new(Chat::new(id, room, window, cx)))
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!("Chat room is not exist"))
|
Err(anyhow!("Chat room is not exist"))
|
||||||
}
|
}
|
||||||
@@ -102,7 +102,7 @@ pub struct Chat {
|
|||||||
// Chat Room
|
// Chat Room
|
||||||
room: WeakEntity<Room>,
|
room: WeakEntity<Room>,
|
||||||
messages: Entity<Vec<Message>>,
|
messages: Entity<Vec<Message>>,
|
||||||
new_messages: WeakEntity<Vec<Event>>,
|
new_messages: Option<WeakEntity<Vec<Event>>>,
|
||||||
list_state: ListState,
|
list_state: ListState,
|
||||||
subscriptions: Vec<Subscription>,
|
subscriptions: Vec<Subscription>,
|
||||||
// New Message
|
// New Message
|
||||||
@@ -113,9 +113,15 @@ pub struct Chat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Chat {
|
impl Chat {
|
||||||
pub fn new(id: &u64, model: &Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<Self> {
|
pub fn new(
|
||||||
let room = model.downgrade();
|
id: &u64,
|
||||||
let new_messages = model.read(cx).new_messages.downgrade();
|
room: WeakEntity<Room>,
|
||||||
|
window: &mut Window,
|
||||||
|
cx: &mut App,
|
||||||
|
) -> Entity<Self> {
|
||||||
|
let new_messages = room
|
||||||
|
.read_with(cx, |this, _| this.new_messages.downgrade())
|
||||||
|
.ok();
|
||||||
|
|
||||||
cx.new(|cx| {
|
cx.new(|cx| {
|
||||||
let messages = cx.new(|_| vec![Message::placeholder()]);
|
let messages = cx.new(|_| vec![Message::placeholder()]);
|
||||||
@@ -371,7 +377,7 @@ impl Chat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn load_new_messages(&mut self, cx: &mut Context<Self>) {
|
fn load_new_messages(&mut self, cx: &mut Context<Self>) {
|
||||||
let Some(model) = self.new_messages.upgrade() else {
|
let Some(Some(model)) = self.new_messages.as_ref().map(|state| state.upgrade()) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use common::utils::{compare, room_hash, signer_public_key};
|
use common::utils::{compare, room_hash, signer_public_key};
|
||||||
use gpui::{App, AppContext, Context, Entity, Global};
|
use gpui::{App, AppContext, Context, Entity, Global, WeakEntity};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use state::get_client;
|
use state::get_client;
|
||||||
@@ -29,7 +29,13 @@ impl ChatRegistry {
|
|||||||
|
|
||||||
pub fn register(cx: &mut App) -> Entity<Self> {
|
pub fn register(cx: &mut App) -> Entity<Self> {
|
||||||
Self::global(cx).unwrap_or_else(|| {
|
Self::global(cx).unwrap_or_else(|| {
|
||||||
let entity = cx.new(Self::new);
|
let entity = cx.new(|cx| {
|
||||||
|
let mut this = Self::new(cx);
|
||||||
|
// Automatically load chat rooms the database when the registry is created
|
||||||
|
this.load_chat_rooms(cx);
|
||||||
|
|
||||||
|
this
|
||||||
|
});
|
||||||
|
|
||||||
// Set global state
|
// Set global state
|
||||||
cx.set_global(GlobalChatRegistry(entity.clone()));
|
cx.set_global(GlobalChatRegistry(entity.clone()));
|
||||||
@@ -111,6 +117,7 @@ impl ChatRegistry {
|
|||||||
|
|
||||||
cx.spawn(|this, cx| async move {
|
cx.spawn(|this, cx| async move {
|
||||||
if let Ok(events) = rx.await {
|
if let Ok(events) = rx.await {
|
||||||
|
if !events.is_empty() {
|
||||||
_ = cx.update(|cx| {
|
_ = cx.update(|cx| {
|
||||||
_ = this.update(cx, |this, cx| {
|
_ = this.update(cx, |this, cx| {
|
||||||
let current_rooms = this.current_rooms_ids(cx);
|
let current_rooms = this.current_rooms_ids(cx);
|
||||||
@@ -134,6 +141,7 @@ impl ChatRegistry {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
@@ -146,11 +154,11 @@ impl ChatRegistry {
|
|||||||
self.is_loading
|
self.is_loading
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: &u64, cx: &App) -> Option<Entity<Room>> {
|
pub fn get(&self, id: &u64, cx: &App) -> Option<WeakEntity<Room>> {
|
||||||
self.rooms
|
self.rooms
|
||||||
.iter()
|
.iter()
|
||||||
.find(|model| &model.read(cx).id == id)
|
.find(|model| model.read(cx).id == *id)
|
||||||
.cloned()
|
.map(|room| room.downgrade())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_room(&mut self, room: Room, cx: &mut Context<Self>) -> Result<(), anyhow::Error> {
|
pub fn new_room(&mut self, room: Room, cx: &mut Context<Self>) -> Result<(), anyhow::Error> {
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ use std::{fs, sync::OnceLock, time::Duration};
|
|||||||
|
|
||||||
static CLIENT: OnceLock<Client> = OnceLock::new();
|
static CLIENT: OnceLock<Client> = OnceLock::new();
|
||||||
|
|
||||||
pub fn initialize_client() -> &'static Client {
|
pub fn get_client() -> &'static Client {
|
||||||
|
CLIENT.get_or_init(|| {
|
||||||
// Setup app data folder
|
// Setup app data folder
|
||||||
let config_dir = config_dir().expect("Config directory not found");
|
let config_dir = config_dir().expect("Config directory not found");
|
||||||
let app_dir = config_dir.join("Coop/");
|
let app_dir = config_dir.join("Coop/");
|
||||||
@@ -23,12 +24,6 @@ pub fn initialize_client() -> &'static Client {
|
|||||||
.max_avg_latency(Duration::from_millis(800));
|
.max_avg_latency(Duration::from_millis(800));
|
||||||
|
|
||||||
// Setup Nostr Client
|
// Setup Nostr Client
|
||||||
let client = ClientBuilder::default().database(lmdb).opts(opts).build();
|
ClientBuilder::default().database(lmdb).opts(opts).build()
|
||||||
|
})
|
||||||
CLIENT.set(client).expect("Client is already initialized!");
|
|
||||||
CLIENT.get().expect("Client is NOT initialized!")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_client() -> &'static Client {
|
|
||||||
CLIENT.get().expect("Client is NOT initialized!")
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user