chore: refactor chat registry
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -901,7 +901,7 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chat_state"
|
name = "chats"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
@@ -1136,7 +1136,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"cargo-packager-updater",
|
"cargo-packager-updater",
|
||||||
"chat_state",
|
"chats",
|
||||||
"common",
|
"common",
|
||||||
"dirs 5.0.1",
|
"dirs 5.0.1",
|
||||||
"gpui",
|
"gpui",
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ path = "src/main.rs"
|
|||||||
ui = { path = "../ui" }
|
ui = { path = "../ui" }
|
||||||
common = { path = "../common" }
|
common = { path = "../common" }
|
||||||
state = { path = "../state" }
|
state = { path = "../state" }
|
||||||
chat_state = { path = "../chat_state" }
|
chats = { path = "../chats" }
|
||||||
|
|
||||||
gpui.workspace = true
|
gpui.workspace = true
|
||||||
gpui_tokio.workspace = true
|
gpui_tokio.workspace = true
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use asset::Assets;
|
use asset::Assets;
|
||||||
use async_utility::task::spawn;
|
use async_utility::task::spawn;
|
||||||
use chat_state::registry::ChatRegistry;
|
use chats::registry::ChatRegistry;
|
||||||
use common::{
|
use common::{
|
||||||
constants::{
|
constants::{
|
||||||
ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, FAKE_SIG, KEYRING_SERVICE, NEW_MESSAGE_SUB_ID,
|
ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, FAKE_SIG, KEYRING_SERVICE, NEW_MESSAGE_SUB_ID,
|
||||||
@@ -8,16 +8,15 @@ use common::{
|
|||||||
profile::NostrProfile,
|
profile::NostrProfile,
|
||||||
};
|
};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, point, px, size, App, AppContext, Application, AsyncApp, BorrowAppContext, Bounds,
|
actions, point, px, size, App, AppContext, Application, AsyncApp, Bounds, KeyBinding, Menu,
|
||||||
KeyBinding, Menu, MenuItem, SharedString, TitlebarOptions, WindowBounds, WindowKind,
|
MenuItem, SharedString, TitlebarOptions, WindowBounds, WindowKind, WindowOptions,
|
||||||
WindowOptions,
|
|
||||||
};
|
};
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use gpui::{WindowBackgroundAppearance, WindowDecorations};
|
use gpui::{WindowBackgroundAppearance, WindowDecorations};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use state::{get_client, initialize_client};
|
use state::{get_client, initialize_client};
|
||||||
use std::{borrow::Cow, collections::HashSet, ops::Deref, str::FromStr, sync::Arc, time::Duration};
|
use std::{borrow::Cow, collections::HashSet, str::FromStr, sync::Arc, time::Duration};
|
||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
use ui::{theme::Theme, Root};
|
use ui::{theme::Theme, Root};
|
||||||
use views::{app, onboarding, startup};
|
use views::{app, onboarding, startup};
|
||||||
@@ -247,11 +246,9 @@ fn main() {
|
|||||||
|
|
||||||
app.run(move |cx| {
|
app.run(move |cx| {
|
||||||
// Initialize chat global state
|
// Initialize chat global state
|
||||||
ChatRegistry::set_global(cx);
|
chats::registry::init(cx);
|
||||||
|
|
||||||
// Initialize components
|
// Initialize components
|
||||||
ui::init(cx);
|
ui::init(cx);
|
||||||
|
|
||||||
// Bring the app to the foreground
|
// Bring the app to the foreground
|
||||||
cx.activate(true);
|
cx.activate(true);
|
||||||
// Register the `quit` function
|
// Register the `quit` function
|
||||||
@@ -284,8 +281,7 @@ fn main() {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let window = cx
|
cx.open_window(opts, |window, cx| {
|
||||||
.open_window(opts, |window, cx| {
|
|
||||||
window.set_window_title(APP_NAME);
|
window.set_window_title(APP_NAME);
|
||||||
window.set_app_id(APP_ID);
|
window.set_app_id(APP_ID);
|
||||||
window
|
window
|
||||||
@@ -294,16 +290,15 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
let window_handle = window.window_handle();
|
let handle = window.window_handle();
|
||||||
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
|
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
|
||||||
|
|
||||||
let task = cx.read_credentials(KEYRING_SERVICE);
|
let task = cx.read_credentials(KEYRING_SERVICE);
|
||||||
|
let (tx, rx) = oneshot::channel::<Option<NostrProfile>>();
|
||||||
|
|
||||||
cx.spawn(|mut cx| async move {
|
// Read credential in OS Keyring
|
||||||
if let Ok(Some((npub, secret))) = task.await {
|
cx.background_spawn(async {
|
||||||
let (tx, rx) = oneshot::channel::<NostrProfile>();
|
let profile = if let Ok(Some((npub, secret))) = task.await {
|
||||||
|
|
||||||
cx.background_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
let public_key = PublicKey::from_bech32(&npub).unwrap();
|
let public_key = PublicKey::from_bech32(&npub).unwrap();
|
||||||
let secret_hex = String::from_utf8(secret).unwrap();
|
let secret_hex = String::from_utf8(secret).unwrap();
|
||||||
let keys = Keys::parse(&secret_hex).unwrap();
|
let keys = Keys::parse(&secret_hex).unwrap();
|
||||||
@@ -312,27 +307,32 @@ fn main() {
|
|||||||
_ = client.set_signer(keys).await;
|
_ = client.set_signer(keys).await;
|
||||||
|
|
||||||
// Get user's metadata
|
// Get user's metadata
|
||||||
let metadata = if let Ok(Some(metadata)) =
|
let metadata =
|
||||||
client.database().metadata(public_key).await
|
if let Ok(Some(metadata)) = client.database().metadata(public_key).await {
|
||||||
{
|
|
||||||
metadata
|
metadata
|
||||||
} else {
|
} else {
|
||||||
Metadata::new()
|
Metadata::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
_ = tx.send(NostrProfile::new(public_key, metadata));
|
Some(NostrProfile::new(public_key, metadata))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
_ = tx.send(profile)
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
if let Ok(profile) = rx.await {
|
// Set root view based on credential status
|
||||||
_ = cx.update_window(window_handle, |_, window, cx| {
|
cx.spawn(|mut cx| async move {
|
||||||
|
if let Ok(Some(profile)) = rx.await {
|
||||||
|
_ = cx.update_window(handle, |_, window, cx| {
|
||||||
window.replace_root(cx, |window, cx| {
|
window.replace_root(cx, |window, cx| {
|
||||||
Root::new(app::init(profile, window, cx).into(), window, cx)
|
Root::new(app::init(profile, window, cx).into(), window, cx)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
_ = cx.update_window(window_handle, |_, window, cx| {
|
_ = cx.update_window(handle, |_, window, cx| {
|
||||||
window.replace_root(cx, |window, cx| {
|
window.replace_root(cx, |window, cx| {
|
||||||
Root::new(onboarding::init(window, cx).into(), window, cx)
|
Root::new(onboarding::init(window, cx).into(), window, cx)
|
||||||
});
|
});
|
||||||
@@ -341,36 +341,32 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
root
|
|
||||||
})
|
|
||||||
.expect("System error. Please re-open the app.");
|
|
||||||
|
|
||||||
// Listen for messages from the Nostr thread
|
// Listen for messages from the Nostr thread
|
||||||
cx.spawn(|mut cx| async move {
|
cx.spawn(|cx| async move {
|
||||||
while let Some(signal) = signal_rx.recv().await {
|
while let Some(signal) = signal_rx.recv().await {
|
||||||
match signal {
|
match signal {
|
||||||
Signal::Eose => {
|
Signal::Eose => {
|
||||||
if let Err(e) = cx.update_window(*window.deref(), |_this, window, cx| {
|
_ = cx.update(|cx| {
|
||||||
cx.update_global::<ChatRegistry, _>(|this, cx| {
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
this.load(window, cx);
|
chats.update(cx, |this, cx| this.load_chat_rooms(cx))
|
||||||
});
|
|
||||||
}) {
|
|
||||||
error!("Error: {}", e)
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Signal::Event(event) => {
|
Signal::Event(event) => {
|
||||||
if let Err(e) = cx.update_window(*window.deref(), |_this, window, cx| {
|
_ = cx.update(|cx| {
|
||||||
cx.update_global::<ChatRegistry, _>(|this, cx| {
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
this.new_room_message(event, window, cx);
|
chats.update(cx, |this, cx| this.push_message(event, cx))
|
||||||
});
|
|
||||||
}) {
|
|
||||||
error!("Error: {}", e)
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
|
root
|
||||||
|
})
|
||||||
|
.expect("System error. Please re-open the app.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use cargo_packager_updater::{check_update, semver::Version, url::Url};
|
use cargo_packager_updater::{check_update, semver::Version, url::Url};
|
||||||
use chat_state::registry::ChatRegistry;
|
|
||||||
use common::{
|
use common::{
|
||||||
constants::{UPDATER_PUBKEY, UPDATER_URL},
|
constants::{UPDATER_PUBKEY, UPDATER_URL},
|
||||||
profile::NostrProfile,
|
profile::NostrProfile,
|
||||||
@@ -217,16 +216,12 @@ impl AppView {
|
|||||||
fn on_panel_action(&mut self, action: &AddPanel, window: &mut Window, cx: &mut Context<Self>) {
|
fn on_panel_action(&mut self, action: &AddPanel, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
match &action.panel {
|
match &action.panel {
|
||||||
PanelKind::Room(id) => {
|
PanelKind::Room(id) => {
|
||||||
if let Some(weak_room) = cx.global::<ChatRegistry>().get_room(id, cx) {
|
if let Ok(panel) = chat::init(id, window, cx) {
|
||||||
if let Some(room) = weak_room.upgrade() {
|
|
||||||
let panel = Arc::new(chat::init(&room, window, cx));
|
|
||||||
|
|
||||||
self.dock.update(cx, |dock_area, cx| {
|
self.dock.update(cx, |dock_area, cx| {
|
||||||
dock_area.add_panel(panel, action.position, window, cx);
|
dock_area.add_panel(panel, action.position, window, cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
PanelKind::Profile => {
|
PanelKind::Profile => {
|
||||||
let panel = Arc::new(profile::init(self.account.clone(), window, cx));
|
let panel = Arc::new(profile::init(self.account.clone(), window, cx));
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::anyhow;
|
||||||
use async_utility::task::spawn;
|
use async_utility::task::spawn;
|
||||||
use chat_state::room::{LastSeen, Room};
|
use chats::registry::ChatRegistry;
|
||||||
|
use chats::room::{LastSeen, Room};
|
||||||
use common::{
|
use common::{
|
||||||
constants::IMAGE_SERVICE,
|
constants::IMAGE_SERVICE,
|
||||||
profile::NostrProfile,
|
profile::NostrProfile,
|
||||||
@@ -28,8 +32,20 @@ use ui::{
|
|||||||
|
|
||||||
mod message;
|
mod message;
|
||||||
|
|
||||||
pub fn init(room: &Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<Chat> {
|
pub fn init(
|
||||||
Chat::new(room, window, cx)
|
id: &u64,
|
||||||
|
window: &mut Window,
|
||||||
|
cx: &mut App,
|
||||||
|
) -> Result<Arc<Entity<Chat>>, anyhow::Error> {
|
||||||
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
|
if let Some(room) = chats.read(cx).get(id, cx) {
|
||||||
|
Ok(Arc::new(Chat::new(&room, window, cx)))
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Chat room is not exist"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Chat Registry is not initialized"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use chat_state::registry::ChatRegistry;
|
use chats::registry::ChatRegistry;
|
||||||
use common::{
|
use common::{
|
||||||
constants::FAKE_SIG,
|
constants::FAKE_SIG,
|
||||||
profile::NostrProfile,
|
profile::NostrProfile,
|
||||||
@@ -6,8 +6,8 @@ use common::{
|
|||||||
};
|
};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, img, impl_internal_actions, prelude::FluentBuilder, px, relative, uniform_list, App,
|
div, img, impl_internal_actions, prelude::FluentBuilder, px, relative, uniform_list, App,
|
||||||
AppContext, BorrowAppContext, Context, Entity, FocusHandle, InteractiveElement, IntoElement,
|
AppContext, Context, Entity, FocusHandle, InteractiveElement, IntoElement, ParentElement,
|
||||||
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, TextAlign, Window,
|
Render, SharedString, StatefulInteractiveElement, Styled, TextAlign, Window,
|
||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -212,9 +212,11 @@ impl Compose {
|
|||||||
|
|
||||||
if let Ok(event) = rx.await {
|
if let Ok(event) = rx.await {
|
||||||
_ = cx.update_window(window_handle, |_, window, cx| {
|
_ = cx.update_window(window_handle, |_, window, cx| {
|
||||||
cx.update_global::<ChatRegistry, _>(|this, cx| {
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
this.new_room_message(event, window, cx);
|
chats.update(cx, |this, cx| {
|
||||||
|
this.push_message(event, cx);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Stop loading spinner
|
// Stop loading spinner
|
||||||
_ = this.update(cx, |this, cx| {
|
_ = this.update(cx, |this, cx| {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::views::app::{AddPanel, PanelKind};
|
use crate::views::app::{AddPanel, PanelKind};
|
||||||
use chat_state::registry::ChatRegistry;
|
use chats::registry::ChatRegistry;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, img, percentage, prelude::FluentBuilder, px, relative, Context, InteractiveElement,
|
div, img, percentage, prelude::FluentBuilder, px, relative, Context, InteractiveElement,
|
||||||
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
|
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
|
||||||
@@ -39,15 +39,14 @@ impl Inbox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render_item(&self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
fn render_item(&self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
let weak_model = cx.global::<ChatRegistry>().inbox();
|
if let Some(chats) = ChatRegistry::global(cx) {
|
||||||
|
|
||||||
if let Some(model) = weak_model.upgrade() {
|
|
||||||
div().map(|this| {
|
div().map(|this| {
|
||||||
let inbox = model.read(cx);
|
let state = chats.read(cx);
|
||||||
|
let rooms = state.rooms();
|
||||||
|
|
||||||
if inbox.is_loading {
|
if state.is_loading() {
|
||||||
this.children(self.render_skeleton(5))
|
this.children(self.render_skeleton(5))
|
||||||
} else if inbox.rooms.is_empty() {
|
} else if rooms.is_empty() {
|
||||||
this.px_1()
|
this.px_1()
|
||||||
.w_full()
|
.w_full()
|
||||||
.h_20()
|
.h_20()
|
||||||
@@ -72,7 +71,7 @@ impl Inbox {
|
|||||||
.child("Recent chats will appear here."),
|
.child("Recent chats will appear here."),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
this.children(inbox.rooms.iter().map(|model| {
|
this.children(rooms.iter().map(|model| {
|
||||||
let room = model.read(cx);
|
let room = model.read(cx);
|
||||||
let room_id: SharedString = room.id.to_string().into();
|
let room_id: SharedString = room.id.to_string().into();
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
use gpui::{Context, Entity};
|
|
||||||
|
|
||||||
use crate::room::Room;
|
|
||||||
|
|
||||||
pub struct Inbox {
|
|
||||||
pub rooms: Vec<Entity<Room>>,
|
|
||||||
pub is_loading: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Inbox {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
rooms: vec![],
|
|
||||||
is_loading: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ids(&self, cx: &Context<Self>) -> Vec<u64> {
|
|
||||||
self.rooms.iter().map(|room| room.read(cx).id).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Inbox {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
use async_utility::tokio::sync::oneshot;
|
|
||||||
use common::utils::{compare, room_hash, signer_public_key};
|
|
||||||
use gpui::{App, AppContext, Entity, Global, WeakEntity, Window};
|
|
||||||
use itertools::Itertools;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use state::get_client;
|
|
||||||
use std::cmp::Reverse;
|
|
||||||
|
|
||||||
use crate::{inbox::Inbox, room::Room};
|
|
||||||
|
|
||||||
pub struct ChatRegistry {
|
|
||||||
inbox: Entity<Inbox>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Global for ChatRegistry {}
|
|
||||||
|
|
||||||
impl ChatRegistry {
|
|
||||||
pub fn set_global(cx: &mut App) {
|
|
||||||
let inbox = cx.new(|_| Inbox::default());
|
|
||||||
|
|
||||||
cx.observe_new::<Room>(|this, _window, cx| {
|
|
||||||
// Get all pubkeys to load metadata
|
|
||||||
let pubkeys = this.pubkeys();
|
|
||||||
|
|
||||||
cx.spawn(|this, mut cx| async move {
|
|
||||||
let (tx, rx) = oneshot::channel::<Vec<(PublicKey, Metadata)>>();
|
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
|
||||||
let client = get_client();
|
|
||||||
let mut profiles = Vec::new();
|
|
||||||
|
|
||||||
for public_key in pubkeys.into_iter() {
|
|
||||||
if let Ok(metadata) = client.database().metadata(public_key).await {
|
|
||||||
profiles.push((public_key, metadata.unwrap_or_default()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = tx.send(profiles);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
if let Ok(profiles) = rx.await {
|
|
||||||
if let Some(room) = this.upgrade() {
|
|
||||||
_ = cx.update_entity(&room, |this, cx| {
|
|
||||||
for profile in profiles.into_iter() {
|
|
||||||
this.set_metadata(profile.0, profile.1);
|
|
||||||
}
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
cx.set_global(Self { inbox });
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load(&mut self, window: &mut Window, cx: &mut App) {
|
|
||||||
let window_handle = window.window_handle();
|
|
||||||
let inbox = self.inbox.downgrade();
|
|
||||||
|
|
||||||
cx.spawn(|mut cx| async move {
|
|
||||||
let (tx, rx) = oneshot::channel::<Vec<Event>>();
|
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
|
||||||
let client = get_client();
|
|
||||||
|
|
||||||
if let Ok(public_key) = signer_public_key(client).await {
|
|
||||||
let filter = Filter::new()
|
|
||||||
.kind(Kind::PrivateDirectMessage)
|
|
||||||
.author(public_key);
|
|
||||||
|
|
||||||
// Get all DM events from database
|
|
||||||
if let Ok(events) = client.database().query(filter).await {
|
|
||||||
let result: Vec<Event> = events
|
|
||||||
.into_iter()
|
|
||||||
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
|
||||||
.unique_by(room_hash)
|
|
||||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
_ = tx.send(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
if let Ok(events) = rx.await {
|
|
||||||
_ = cx.update_window(window_handle, |_, _, cx| {
|
|
||||||
_ = inbox.update(cx, |this, cx| {
|
|
||||||
let current_rooms = this.ids(cx);
|
|
||||||
let items: Vec<Entity<Room>> = events
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|ev| {
|
|
||||||
let new = room_hash(&ev);
|
|
||||||
// Filter all seen events
|
|
||||||
if !current_rooms.iter().any(|this| this == &new) {
|
|
||||||
Some(cx.new(|_| Room::parse(&ev)))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
this.rooms.extend(items);
|
|
||||||
this.is_loading = false;
|
|
||||||
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn inbox(&self) -> WeakEntity<Inbox> {
|
|
||||||
self.inbox.downgrade()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_room(&self, id: &u64, cx: &App) -> Option<WeakEntity<Room>> {
|
|
||||||
self.inbox
|
|
||||||
.read(cx)
|
|
||||||
.rooms
|
|
||||||
.iter()
|
|
||||||
.find(|model| &model.read(cx).id == id)
|
|
||||||
.map(|model| model.downgrade())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_room(&mut self, room: Room, cx: &mut App) {
|
|
||||||
let room = cx.new(|_| room);
|
|
||||||
|
|
||||||
self.inbox.update(cx, |this, cx| {
|
|
||||||
if !this.rooms.iter().any(|r| r.read(cx) == room.read(cx)) {
|
|
||||||
this.rooms.insert(0, room);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_room_message(&mut self, event: Event, window: &mut Window, cx: &mut App) {
|
|
||||||
let window_handle = window.window_handle();
|
|
||||||
// Get all pubkeys from event's tags for comparision
|
|
||||||
let mut pubkeys: Vec<_> = event.tags.public_keys().copied().collect();
|
|
||||||
pubkeys.push(event.pubkey);
|
|
||||||
|
|
||||||
if let Some(room) = self
|
|
||||||
.inbox
|
|
||||||
.read(cx)
|
|
||||||
.rooms
|
|
||||||
.iter()
|
|
||||||
.find(|room| compare(&room.read(cx).pubkeys(), &pubkeys))
|
|
||||||
{
|
|
||||||
let this = room.downgrade();
|
|
||||||
|
|
||||||
cx.spawn(|mut cx| async move {
|
|
||||||
_ = cx.update_window(window_handle, |_, _, cx| {
|
|
||||||
_ = this.update(cx, |this, cx| {
|
|
||||||
this.last_seen.set(event.created_at);
|
|
||||||
this.new_messages.push(event);
|
|
||||||
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
} else {
|
|
||||||
let room = cx.new(|_| Room::parse(&event));
|
|
||||||
|
|
||||||
self.inbox.update(cx, |this, cx| {
|
|
||||||
this.rooms.insert(0, room);
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "chat_state"
|
name = "chats"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
@@ -1,3 +1,2 @@
|
|||||||
pub mod inbox;
|
|
||||||
pub mod registry;
|
pub mod registry;
|
||||||
pub mod room;
|
pub mod room;
|
||||||
175
crates/chats/src/registry.rs
Normal file
175
crates/chats/src/registry.rs
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
use async_utility::tokio::sync::oneshot;
|
||||||
|
use common::utils::{compare, room_hash, signer_public_key};
|
||||||
|
use gpui::{App, AppContext, Context, Entity, Global};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use state::get_client;
|
||||||
|
use std::cmp::Reverse;
|
||||||
|
|
||||||
|
use crate::room::Room;
|
||||||
|
|
||||||
|
pub fn init(cx: &mut App) {
|
||||||
|
ChatRegistry::register(cx);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct GlobalChatRegistry(Entity<ChatRegistry>);
|
||||||
|
|
||||||
|
impl Global for GlobalChatRegistry {}
|
||||||
|
|
||||||
|
pub struct ChatRegistry {
|
||||||
|
rooms: Vec<Entity<Room>>,
|
||||||
|
is_loading: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChatRegistry {
|
||||||
|
pub fn global(cx: &mut App) -> Option<Entity<Self>> {
|
||||||
|
cx.try_global::<GlobalChatRegistry>()
|
||||||
|
.map(|global| global.0.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register(cx: &mut App) -> Entity<Self> {
|
||||||
|
Self::global(cx).unwrap_or_else(|| {
|
||||||
|
let entity = cx.new(Self::new);
|
||||||
|
// Set global state
|
||||||
|
cx.set_global(GlobalChatRegistry(entity.clone()));
|
||||||
|
// Observe and load metadata for any new rooms
|
||||||
|
cx.observe_new::<Room>(|this, _window, cx| {
|
||||||
|
let client = get_client();
|
||||||
|
let pubkeys = this.pubkeys();
|
||||||
|
let (tx, rx) = oneshot::channel::<Vec<(PublicKey, Metadata)>>();
|
||||||
|
|
||||||
|
cx.background_spawn(async move {
|
||||||
|
let mut profiles = Vec::new();
|
||||||
|
|
||||||
|
for public_key in pubkeys.into_iter() {
|
||||||
|
if let Ok(metadata) = client.database().metadata(public_key).await {
|
||||||
|
profiles.push((public_key, metadata.unwrap_or_default()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = tx.send(profiles);
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
if let Ok(profiles) = rx.await {
|
||||||
|
if let Some(room) = this.upgrade() {
|
||||||
|
_ = cx.update_entity(&room, |this, cx| {
|
||||||
|
for profile in profiles.into_iter() {
|
||||||
|
this.set_metadata(profile.0, profile.1);
|
||||||
|
}
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
entity
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(_cx: &mut Context<Self>) -> Self {
|
||||||
|
Self {
|
||||||
|
rooms: Vec::with_capacity(5),
|
||||||
|
is_loading: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_rooms_ids(&self, cx: &mut Context<Self>) -> Vec<u64> {
|
||||||
|
self.rooms.iter().map(|room| room.read(cx).id).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_chat_rooms(&mut self, cx: &mut Context<Self>) {
|
||||||
|
let client = get_client();
|
||||||
|
let (tx, rx) = oneshot::channel::<Vec<Event>>();
|
||||||
|
|
||||||
|
cx.background_spawn(async move {
|
||||||
|
if let Ok(public_key) = signer_public_key(client).await {
|
||||||
|
let filter = Filter::new()
|
||||||
|
.kind(Kind::PrivateDirectMessage)
|
||||||
|
.author(public_key);
|
||||||
|
|
||||||
|
// Get all DM events from database
|
||||||
|
if let Ok(events) = client.database().query(filter).await {
|
||||||
|
let result: Vec<Event> = events
|
||||||
|
.into_iter()
|
||||||
|
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
||||||
|
.unique_by(room_hash)
|
||||||
|
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
_ = tx.send(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
cx.spawn(|this, cx| async move {
|
||||||
|
if let Ok(events) = rx.await {
|
||||||
|
_ = cx.update(|cx| {
|
||||||
|
_ = this.update(cx, |this, cx| {
|
||||||
|
let current_rooms = this.current_rooms_ids(cx);
|
||||||
|
let items: Vec<Entity<Room>> = events
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|ev| {
|
||||||
|
let new = room_hash(&ev);
|
||||||
|
// Filter all seen events
|
||||||
|
if !current_rooms.iter().any(|this| this == &new) {
|
||||||
|
Some(cx.new(|_| Room::parse(&ev)))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
this.rooms.extend(items);
|
||||||
|
this.is_loading = false;
|
||||||
|
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rooms(&self) -> &Vec<Entity<Room>> {
|
||||||
|
&self.rooms
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_loading(&self) -> bool {
|
||||||
|
self.is_loading
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, id: &u64, cx: &App) -> Option<Entity<Room>> {
|
||||||
|
self.rooms
|
||||||
|
.iter()
|
||||||
|
.find(|model| &model.read(cx).id == id)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_message(&mut self, event: Event, cx: &mut Context<Self>) {
|
||||||
|
// Get all pubkeys from event's tags for comparision
|
||||||
|
let mut pubkeys: Vec<_> = event.tags.public_keys().copied().collect();
|
||||||
|
pubkeys.push(event.pubkey);
|
||||||
|
|
||||||
|
if let Some(room) = self
|
||||||
|
.rooms
|
||||||
|
.iter()
|
||||||
|
.find(|room| compare(&room.read(cx).pubkeys(), &pubkeys))
|
||||||
|
{
|
||||||
|
room.update(cx, |this, cx| {
|
||||||
|
this.last_seen.set(event.created_at);
|
||||||
|
this.new_messages.push(event);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
let room = cx.new(|_| Room::parse(&event));
|
||||||
|
self.rooms.insert(0, room);
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user