wip: refactor
This commit is contained in:
@@ -28,3 +28,4 @@ rust-embed.workspace = true
|
||||
smol.workspace = true
|
||||
|
||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
||||
flume = "0.11.1"
|
||||
|
||||
@@ -10,13 +10,14 @@ use std::{
|
||||
sync::{Arc, OnceLock},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::{
|
||||
sync::{broadcast, mpsc},
|
||||
time::sleep,
|
||||
};
|
||||
use tokio::{sync::mpsc, time::sleep};
|
||||
|
||||
use constants::{ALL_MESSAGES_SUB_ID, APP_NAME, FAKE_SIG, METADATA_DELAY, NEW_MESSAGE_SUB_ID};
|
||||
use states::{account::AccountRegistry, chat::ChatRegistry, signal::SignalRegistry};
|
||||
use states::{
|
||||
account::AccountRegistry,
|
||||
chat::ChatRegistry,
|
||||
metadata::{MetadataRegistry, Signal},
|
||||
};
|
||||
use views::app::AppView;
|
||||
|
||||
pub mod asset;
|
||||
@@ -74,23 +75,24 @@ async fn main() {
|
||||
// Connect to all relays
|
||||
_ = client.connect().await;
|
||||
|
||||
// Channel for metadata signal
|
||||
let (signal_tx, mut signal_rx) = mpsc::channel::<PublicKey>(1000); // TODO: adjust?
|
||||
// Channel for EOSE
|
||||
// When receive EOSE from relay(s) -> Load all rooms and push it into UI.
|
||||
let (eose_tx, mut eose_rx) = mpsc::channel::<SubscriptionId>(200);
|
||||
|
||||
// Channel for new chat
|
||||
let (new_chat_tx, mut new_chat_rx) = mpsc::channel::<Event>(1000); // TODO: adjust?
|
||||
// Channel for new message
|
||||
// Push new message to chat panel or create new chat room if not exist.
|
||||
let (message_tx, message_rx) = flume::unbounded::<Event>();
|
||||
let message_rx_clone = message_rx.clone();
|
||||
|
||||
// Channel for all chats
|
||||
// When receive EOSE from relay(s). Reload UI
|
||||
let (all_chats_tx, mut all_chats_rx) = mpsc::channel::<i32>(1);
|
||||
|
||||
// Channel for metadata request queue
|
||||
let (queue_tx, mut queue_rx) = broadcast::channel::<PublicKey>(100);
|
||||
// Channel for signal
|
||||
// Merge all metadata requests into single one.
|
||||
// Notify to reload element if receive new metadata.
|
||||
let (signal_tx, mut signal_rx) = mpsc::channel::<Signal>(5000);
|
||||
let signal_tx_clone = signal_tx.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let sig = Signature::from_str(FAKE_SIG).unwrap();
|
||||
let all_messages_sub_id = SubscriptionId::new(ALL_MESSAGES_SUB_ID);
|
||||
let new_message_sub_id = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
||||
let new_message = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
||||
|
||||
while let Ok(notification) = notifications.recv().await {
|
||||
#[allow(clippy::collapsible_match)]
|
||||
@@ -121,69 +123,42 @@ async fn main() {
|
||||
);
|
||||
|
||||
// Save rumor to database to further query
|
||||
_ = client.database().save_event(&ev).await;
|
||||
if let Err(e) = client.database().save_event(&ev).await {
|
||||
println!("Save error: {}", e);
|
||||
}
|
||||
|
||||
// Send event back to channel
|
||||
if subscription_id == new_message_sub_id {
|
||||
if let Err(e) = new_chat_tx.send(ev).await {
|
||||
if subscription_id == new_message {
|
||||
if let Err(e) = message_tx.send_async(ev).await {
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if event.kind == Kind::Metadata {
|
||||
_ = signal_tx.send(event.pubkey).await;
|
||||
if let Err(e) = signal_tx.send(Signal::DONE(event.pubkey)).await {
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
}
|
||||
} else if let RelayMessage::EndOfStoredEvents(subscription_id) = message {
|
||||
if all_messages_sub_id == subscription_id {
|
||||
_ = all_chats_tx.send(1).await;
|
||||
if let Err(e) = eose_tx.send(subscription_id).await {
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut queue: HashSet<PublicKey> = HashSet::new();
|
||||
|
||||
while let Ok(public_key) = queue_rx.recv().await {
|
||||
queue.insert(public_key);
|
||||
|
||||
// Wait for METADATA_DELAY
|
||||
sleep(Duration::from_millis(METADATA_DELAY)).await;
|
||||
|
||||
if !queue.is_empty() {
|
||||
let authors: Vec<PublicKey> = queue.iter().copied().collect();
|
||||
let total = authors.len();
|
||||
|
||||
let filter = Filter::new()
|
||||
.authors(authors)
|
||||
.kind(Kind::Metadata)
|
||||
.limit(total);
|
||||
|
||||
let opts = SubscribeAutoCloseOptions::default()
|
||||
.filter(FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(2)));
|
||||
|
||||
// Clear queue
|
||||
queue.clear();
|
||||
|
||||
if let Err(e) = client.subscribe(vec![filter], Some(opts)).await {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
App::new()
|
||||
.with_assets(Assets)
|
||||
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
|
||||
.run(move |cx| {
|
||||
// Account state
|
||||
AccountRegistry::set_global(cx);
|
||||
// Metadata state
|
||||
MetadataRegistry::set_global(cx, signal_tx_clone);
|
||||
// Chat state
|
||||
ChatRegistry::set_global(cx);
|
||||
// Hold all metadata requests and merged it
|
||||
SignalRegistry::set_global(cx, Arc::new(queue_tx));
|
||||
ChatRegistry::set_global(cx, message_rx);
|
||||
|
||||
// Initialize components
|
||||
coop_ui::init(cx);
|
||||
@@ -192,16 +167,55 @@ async fn main() {
|
||||
cx.on_action(quit);
|
||||
|
||||
cx.spawn(|async_cx| async move {
|
||||
while let Some(public_key) = signal_rx.recv().await {
|
||||
_ = async_cx.update_global::<SignalRegistry, _>(|state, _cx| {
|
||||
state.push(public_key);
|
||||
});
|
||||
let mut queue: HashSet<PublicKey> = HashSet::new();
|
||||
|
||||
while let Some(signal) = signal_rx.recv().await {
|
||||
match signal {
|
||||
Signal::REQ(public_key) => {
|
||||
queue.insert(public_key);
|
||||
|
||||
// Wait for METADATA_DELAY
|
||||
sleep(Duration::from_millis(METADATA_DELAY)).await;
|
||||
|
||||
if !queue.is_empty() {
|
||||
let authors: Vec<PublicKey> = queue.iter().copied().collect();
|
||||
let total = authors.len();
|
||||
|
||||
let filter = Filter::new()
|
||||
.authors(authors)
|
||||
.kind(Kind::Metadata)
|
||||
.limit(total);
|
||||
|
||||
let opts = SubscribeAutoCloseOptions::default().filter(
|
||||
FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(2)),
|
||||
);
|
||||
|
||||
queue.clear();
|
||||
|
||||
async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Err(e) =
|
||||
client.subscribe(vec![filter], Some(opts)).await
|
||||
{
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
Signal::DONE(public_key) => {
|
||||
_ = async_cx.update_global::<MetadataRegistry, _>(|state, _| {
|
||||
state.seen(public_key);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|async_cx| async move {
|
||||
while let Some(event) = new_chat_rx.recv().await {
|
||||
while let Ok(event) = message_rx_clone.recv_async().await {
|
||||
_ = async_cx.update_global::<ChatRegistry, _>(|state, cx| {
|
||||
state.push(event, cx);
|
||||
});
|
||||
@@ -210,10 +224,14 @@ async fn main() {
|
||||
.detach();
|
||||
|
||||
cx.spawn(|async_cx| async move {
|
||||
while let Some(_n) = all_chats_rx.recv().await {
|
||||
_ = async_cx.update_global::<ChatRegistry, _>(|state, cx| {
|
||||
state.load(cx);
|
||||
});
|
||||
let all_messages = SubscriptionId::new(ALL_MESSAGES_SUB_ID);
|
||||
|
||||
while let Some(subscription_id) = eose_rx.recv().await {
|
||||
if subscription_id == all_messages {
|
||||
_ = async_cx.update_global::<ChatRegistry, _>(|state, cx| {
|
||||
state.load(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
@@ -58,6 +58,10 @@ impl AccountRegistry {
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn get(&self) -> Option<PublicKey> {
|
||||
self.public_key
|
||||
}
|
||||
|
||||
pub fn set_user(&mut self, public_key: Option<PublicKey>) {
|
||||
self.public_key = public_key
|
||||
}
|
||||
|
||||
@@ -1,75 +1,92 @@
|
||||
use flume::Receiver;
|
||||
use gpui::*;
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
pub struct ChatRegistry {
|
||||
events: Model<Option<Vec<Event>>>,
|
||||
chats: Model<Option<Vec<Event>>>,
|
||||
is_initialized: bool,
|
||||
// Use for receive new message
|
||||
pub(crate) receiver: Receiver<Event>,
|
||||
}
|
||||
|
||||
impl Global for ChatRegistry {}
|
||||
|
||||
impl ChatRegistry {
|
||||
pub fn set_global(cx: &mut AppContext) {
|
||||
let events = cx.new_model(|_| None);
|
||||
pub fn set_global(cx: &mut AppContext, receiver: Receiver<Event>) {
|
||||
let chats = cx.new_model(|_| None);
|
||||
|
||||
cx.set_global(Self::new(events));
|
||||
cx.set_global(Self::new(chats, receiver));
|
||||
}
|
||||
|
||||
pub fn load(&self, cx: &mut AppContext) {
|
||||
pub fn load(&mut self, cx: &mut AppContext) {
|
||||
let mut async_cx = cx.to_async();
|
||||
let async_events = self.events.clone();
|
||||
let async_chats = self.chats.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
if !self.is_initialized {
|
||||
self.is_initialized = true;
|
||||
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||
events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.pubkey != public_key) // Filter messages from current user
|
||||
.unique_by(|ev| ev.pubkey) // Get unique list
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at)) // Sort by created at
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||
events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.pubkey != public_key) // Filter all messages from current user
|
||||
.unique_by(|ev| ev.pubkey) // Get unique list
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
async_cx.update_model(&async_chats, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
})
|
||||
.await;
|
||||
|
||||
async_cx.update_model(&async_events, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
})
|
||||
})
|
||||
.detach();
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&self, event: Event, cx: &mut AppContext) {
|
||||
cx.update_model(&self.events, |a, b| {
|
||||
if let Some(events) = a {
|
||||
events.push(event);
|
||||
b.notify();
|
||||
cx.update_model(&self.chats, |a, b| {
|
||||
if let Some(chats) = a {
|
||||
if let Some(index) = chats.iter().position(|c| c.pubkey == event.pubkey) {
|
||||
chats.swap_remove(index);
|
||||
chats.push(event);
|
||||
|
||||
b.notify();
|
||||
} else {
|
||||
chats.push(event);
|
||||
b.notify();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get(&self, cx: &WindowContext) -> Option<Vec<Event>> {
|
||||
self.events.read(cx).clone()
|
||||
self.chats.read(cx).clone()
|
||||
}
|
||||
|
||||
fn new(events: Model<Option<Vec<Event>>>) -> Self {
|
||||
Self { events }
|
||||
fn new(chats: Model<Option<Vec<Event>>>, receiver: Receiver<Event>) -> Self {
|
||||
Self {
|
||||
chats,
|
||||
receiver,
|
||||
is_initialized: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
41
crates/app/src/states/metadata.rs
Normal file
41
crates/app/src/states/metadata.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Signal {
|
||||
/// Send
|
||||
DONE(PublicKey),
|
||||
/// Receive
|
||||
REQ(PublicKey),
|
||||
}
|
||||
|
||||
pub struct MetadataRegistry {
|
||||
seens: Vec<PublicKey>,
|
||||
pub reqs: Sender<Signal>,
|
||||
}
|
||||
|
||||
impl Global for MetadataRegistry {}
|
||||
|
||||
impl MetadataRegistry {
|
||||
pub fn set_global(cx: &mut AppContext, reqs: Sender<Signal>) {
|
||||
cx.set_global(Self::new(reqs));
|
||||
}
|
||||
|
||||
pub fn contains(&self, public_key: PublicKey) -> bool {
|
||||
self.seens.contains(&public_key)
|
||||
}
|
||||
|
||||
pub fn seen(&mut self, public_key: PublicKey) {
|
||||
if !self.seens.contains(&public_key) {
|
||||
self.seens.push(public_key);
|
||||
}
|
||||
}
|
||||
|
||||
fn new(reqs: Sender<Signal>) -> Self {
|
||||
Self {
|
||||
seens: Vec::new(),
|
||||
reqs,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod account;
|
||||
pub mod chat;
|
||||
pub mod signal;
|
||||
pub mod metadata;
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast::Sender;
|
||||
|
||||
pub struct SignalRegistry {
|
||||
public_keys: Vec<PublicKey>,
|
||||
pub queue: Arc<Sender<PublicKey>>,
|
||||
}
|
||||
|
||||
impl Global for SignalRegistry {}
|
||||
|
||||
impl SignalRegistry {
|
||||
pub fn set_global(cx: &mut AppContext, queue: Arc<Sender<PublicKey>>) {
|
||||
cx.set_global(Self::new(queue));
|
||||
}
|
||||
|
||||
pub fn contains(&self, public_key: PublicKey) -> bool {
|
||||
self.public_keys.contains(&public_key)
|
||||
}
|
||||
|
||||
pub fn push(&mut self, public_key: PublicKey) {
|
||||
self.public_keys.push(public_key);
|
||||
}
|
||||
|
||||
pub fn add_to_queue(&mut self, public_key: PublicKey) {
|
||||
if let Err(e) = self.queue.send(public_key) {
|
||||
println!("Dropped: {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
fn new(queue: Arc<Sender<PublicKey>>) -> Self {
|
||||
Self {
|
||||
public_keys: Vec::new(),
|
||||
queue,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ pub fn ago(time: u64) -> String {
|
||||
"now".to_owned()
|
||||
} else if diff < 24 {
|
||||
let duration = now.signed_duration_since(input_time);
|
||||
format!("{} ago", duration.num_hours())
|
||||
format!("{} hours ago", duration.num_hours())
|
||||
} else {
|
||||
input_time.format("%b %d").to_string()
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use coop_ui::{
|
||||
button::{Button, ButtonVariants},
|
||||
dock::{DockArea, DockItem, DockPlacement},
|
||||
theme::{ActiveTheme, Theme},
|
||||
Root, TitleBar,
|
||||
theme::{ActiveTheme, Theme, ThemeMode},
|
||||
IconName, Root, Sizable, TitleBar,
|
||||
};
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use prelude::FluentBuilder;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -62,6 +64,18 @@ impl AppView {
|
||||
AppView { onboarding, dock }
|
||||
}
|
||||
|
||||
fn change_theme_mode(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
|
||||
let mode = match cx.theme().mode.is_dark() {
|
||||
true => ThemeMode::Light,
|
||||
false => ThemeMode::Dark,
|
||||
};
|
||||
|
||||
// Change theme
|
||||
Theme::change(mode, cx);
|
||||
// Rerender
|
||||
cx.refresh();
|
||||
}
|
||||
|
||||
fn init_layout(dock_area: WeakView<DockArea>, cx: &mut WindowContext) {
|
||||
let left = DockItem::panel(Arc::new(LeftDock::new(cx)));
|
||||
let center = Self::init_dock_items(&dock_area, cx);
|
||||
@@ -119,7 +133,33 @@ impl Render for AppView {
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(TitleBar::new())
|
||||
.child(
|
||||
TitleBar::new()
|
||||
// Left side
|
||||
.child(div())
|
||||
// Right side
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_end()
|
||||
.px_2()
|
||||
.gap_2()
|
||||
.child(
|
||||
Button::new("theme-mode")
|
||||
.map(|this| {
|
||||
if cx.theme().mode.is_dark() {
|
||||
this.icon(IconName::Sun)
|
||||
} else {
|
||||
this.icon(IconName::Moon)
|
||||
}
|
||||
})
|
||||
.small()
|
||||
.ghost()
|
||||
.on_click(cx.listener(Self::change_theme_mode)),
|
||||
),
|
||||
),
|
||||
)
|
||||
.child(self.dock.clone())
|
||||
} else {
|
||||
content = content.size_full().child(self.onboarding.clone())
|
||||
|
||||
91
crates/app/src/views/dock/chat/form.rs
Normal file
91
crates/app/src/views/dock/chat/form.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use coop_ui::{
|
||||
button::{Button, ButtonVariants},
|
||||
input::{InputEvent, TextInput},
|
||||
theme::ActiveTheme,
|
||||
Icon, IconName,
|
||||
};
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
pub struct Form {
|
||||
to: PublicKey,
|
||||
input: View<TextInput>,
|
||||
}
|
||||
|
||||
impl Form {
|
||||
pub fn new(to: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let input = cx.new_view(|cx| {
|
||||
TextInput::new(cx)
|
||||
.appearance(false)
|
||||
.text_size(coop_ui::Size::Small)
|
||||
.placeholder("Message...")
|
||||
.cleanable()
|
||||
});
|
||||
|
||||
cx.subscribe(&input, move |form, text_input, input_event, cx| {
|
||||
if let InputEvent::PressEnter = input_event {
|
||||
let content = text_input.read(cx).text().to_string();
|
||||
// TODO: clean up content
|
||||
|
||||
form.send_message(content, cx);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self { to, input }
|
||||
}
|
||||
|
||||
fn send_message(&mut self, content: String, cx: &mut ViewContext<Self>) {
|
||||
let send_to = self.to;
|
||||
let content_clone = content.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
match client.send_private_msg(send_to, content, vec![]).await {
|
||||
Ok(_) => {
|
||||
// Send a copy to yourself
|
||||
if let Err(_e) = client
|
||||
.send_private_msg(public_key, content_clone, vec![])
|
||||
.await
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Err(_) => todo!(),
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Form {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.h_12()
|
||||
.flex_shrink_0()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.px_2()
|
||||
.child(
|
||||
Button::new("upload")
|
||||
.icon(Icon::new(IconName::Upload))
|
||||
.ghost(),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.flex()
|
||||
.bg(cx.theme().muted)
|
||||
.rounded(px(cx.theme().radius))
|
||||
.px_2()
|
||||
.child(self.input.clone()),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,26 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::get_client;
|
||||
use crate::{get_client, states::chat::ChatRegistry};
|
||||
|
||||
pub struct Messages {
|
||||
pub struct MessageList {
|
||||
member: PublicKey,
|
||||
messages: Model<Option<Events>>,
|
||||
}
|
||||
|
||||
impl Messages {
|
||||
impl MessageList {
|
||||
pub fn new(from: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let messages = cx.new_model(|_| None);
|
||||
let async_messages = messages.clone();
|
||||
|
||||
Self {
|
||||
member: from,
|
||||
messages,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&self, cx: &mut ViewContext<Self>) {
|
||||
let messages = self.messages.clone();
|
||||
let member = self.member;
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
@@ -20,40 +30,45 @@ impl Messages {
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
let recv_filter = Filter::new()
|
||||
let recv = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(from)
|
||||
.author(member)
|
||||
.pubkey(public_key);
|
||||
|
||||
let sender_filter = Filter::new()
|
||||
let send = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key)
|
||||
.pubkey(from);
|
||||
.pubkey(member);
|
||||
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
client
|
||||
.database()
|
||||
.query(vec![recv_filter, sender_filter])
|
||||
.await
|
||||
})
|
||||
.spawn(async move { client.database().query(vec![recv, send]).await })
|
||||
.await;
|
||||
|
||||
if let Ok(events) = events {
|
||||
_ = async_cx.update_model(&async_messages, |a, b| {
|
||||
_ = async_cx.update_model(&messages, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
Self { messages }
|
||||
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
||||
let receiver = cx.global::<ChatRegistry>().receiver.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
while let Ok(event) = receiver.recv_async().await {
|
||||
println!("New message: {}", event.as_json())
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Messages {
|
||||
impl Render for MessageList {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div().size_full().flex().flex_col().justify_end();
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
use coop_ui::{
|
||||
button::Button,
|
||||
button_group::ButtonGroup,
|
||||
dock::{DockItemState, Panel, PanelEvent, TitleStyle},
|
||||
input::TextInput,
|
||||
popup_menu::PopupMenu,
|
||||
Sizable,
|
||||
};
|
||||
use form::Form;
|
||||
use gpui::*;
|
||||
use messages::Messages;
|
||||
use list::MessageList;
|
||||
use nostr_sdk::*;
|
||||
|
||||
pub mod messages;
|
||||
pub mod form;
|
||||
pub mod list;
|
||||
|
||||
pub struct ChatPanel {
|
||||
// Panel
|
||||
@@ -19,17 +18,21 @@ pub struct ChatPanel {
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Chat Room
|
||||
messages: View<Messages>,
|
||||
input: View<TextInput>,
|
||||
list: View<MessageList>,
|
||||
form: View<Form>,
|
||||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(from: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
let input = cx.new_view(TextInput::new);
|
||||
let messages = cx.new_view(|cx| Messages::new(from, cx));
|
||||
let form = cx.new_view(|cx| Form::new(from, cx));
|
||||
let list = cx.new_view(|cx| {
|
||||
let list = MessageList::new(from, cx);
|
||||
// Load messages from database
|
||||
list.init(cx);
|
||||
// Subscribe for new message
|
||||
list.subscribe(cx);
|
||||
|
||||
input.update(cx, |input, _cx| {
|
||||
input.set_placeholder("Message");
|
||||
list
|
||||
});
|
||||
|
||||
cx.new_view(|cx| Self {
|
||||
@@ -37,8 +40,8 @@ impl ChatPanel {
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
messages,
|
||||
input,
|
||||
list,
|
||||
form,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -91,22 +94,7 @@ impl Render for ChatPanel {
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(self.messages.clone())
|
||||
.child(
|
||||
div()
|
||||
.flex_shrink_0()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.px_2()
|
||||
.h_11()
|
||||
.child(self.input.clone())
|
||||
.child(
|
||||
ButtonGroup::new("actions")
|
||||
.large()
|
||||
.child(Button::new("upload").label("Upload"))
|
||||
.child(Button::new("send").label("Send")),
|
||||
),
|
||||
)
|
||||
.child(self.list.clone())
|
||||
.child(self.form.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use prelude::FluentBuilder;
|
||||
|
||||
use crate::{
|
||||
get_client,
|
||||
states::signal::SignalRegistry,
|
||||
states::metadata::{MetadataRegistry, Signal},
|
||||
utils::{ago, show_npub},
|
||||
views::app::AddPanel,
|
||||
};
|
||||
@@ -153,6 +153,55 @@ impl Chat {
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
let client = get_client();
|
||||
let signal = cx.global::<MetadataRegistry>();
|
||||
|
||||
if !signal.contains(public_key) {
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let query = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = query {
|
||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
||||
*a = metadata;
|
||||
b.notify();
|
||||
});
|
||||
};
|
||||
})
|
||||
.detach();
|
||||
} else {
|
||||
let reqs = signal.reqs.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
if let Err(e) = reqs.send(Signal::REQ(public_key)).await {
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.observe_global::<MetadataRegistry>(|view, cx| {
|
||||
view.profile(cx);
|
||||
})
|
||||
.detach();
|
||||
};
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
last_seen,
|
||||
metadata,
|
||||
title: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn profile(&self, cx: &mut ViewContext<Self>) {
|
||||
let public_key = self.public_key;
|
||||
let async_metadata = self.metadata.clone();
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
@@ -169,47 +218,6 @@ impl Chat {
|
||||
};
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.update_global::<SignalRegistry, _>(|state, _cx| {
|
||||
state.add_to_queue(public_key);
|
||||
});
|
||||
|
||||
cx.observe_global::<SignalRegistry>(|chat, cx| {
|
||||
chat.load_profile(cx);
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
last_seen,
|
||||
metadata,
|
||||
title: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn load_profile(&self, cx: &mut ViewContext<Self>) {
|
||||
let public_key = self.public_key;
|
||||
let async_metadata = self.metadata.clone();
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
if cx.global::<SignalRegistry>().contains(self.public_key) {
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let query = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = query {
|
||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
||||
*a = metadata;
|
||||
b.notify();
|
||||
});
|
||||
};
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use chat::Chat;
|
||||
use coop_ui::{theme::ActiveTheme, v_flex, Collapsible, Icon, IconName, StyledExt};
|
||||
use gpui::*;
|
||||
|
||||
use itertools::Itertools;
|
||||
use prelude::FluentBuilder;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
use crate::states::chat::ChatRegistry;
|
||||
use crate::states::{account::AccountRegistry, chat::ChatRegistry};
|
||||
|
||||
pub mod chat;
|
||||
|
||||
@@ -19,7 +20,6 @@ impl Inbox {
|
||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let chats = cx.new_model(|_| None);
|
||||
|
||||
// Reload UI if global state changes
|
||||
cx.observe_global::<ChatRegistry>(|inbox, cx| {
|
||||
inbox.load(cx);
|
||||
})
|
||||
@@ -39,17 +39,22 @@ impl Inbox {
|
||||
|
||||
// Read global chat registry
|
||||
let events = cx.global::<ChatRegistry>().get(cx);
|
||||
let current_user = cx.global::<AccountRegistry>().get();
|
||||
|
||||
if let Some(events) = events {
|
||||
let chats: Vec<View<Chat>> = events
|
||||
.into_iter()
|
||||
.map(|event| cx.new_view(|cx| Chat::new(event, cx)))
|
||||
.collect();
|
||||
if let Some(public_key) = current_user {
|
||||
if let Some(events) = events {
|
||||
let chats: Vec<View<Chat>> = events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.pubkey != public_key)
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.map(|ev| cx.new_view(|cx| Chat::new(ev, cx)))
|
||||
.collect();
|
||||
|
||||
cx.update_model(&self.chats, |a, b| {
|
||||
*a = Some(chats);
|
||||
b.notify();
|
||||
});
|
||||
cx.update_model(&self.chats, |a, b| {
|
||||
*a = Some(chats);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user