wip: refactor
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::{get_client, states::chat::ChatRegistry};
|
||||
use crate::get_client;
|
||||
|
||||
pub struct MessageList {
|
||||
member: PublicKey,
|
||||
@@ -56,6 +56,7 @@ impl MessageList {
|
||||
}
|
||||
|
||||
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
||||
/*
|
||||
let receiver = cx.global::<ChatRegistry>().receiver.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
@@ -65,6 +66,7 @@ impl MessageList {
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use prelude::FluentBuilder;
|
||||
|
||||
use crate::{
|
||||
get_client,
|
||||
states::metadata::{MetadataRegistry, Signal},
|
||||
states::{metadata::MetadataRegistry, signal::SignalRegistry},
|
||||
utils::{ago, show_npub},
|
||||
views::app::AddPanel,
|
||||
};
|
||||
@@ -138,66 +138,41 @@ impl RenderOnce for ChatItem {
|
||||
|
||||
pub struct Chat {
|
||||
title: Option<String>,
|
||||
public_key: PublicKey,
|
||||
metadata: Model<Option<Metadata>>,
|
||||
last_seen: Timestamp,
|
||||
pub(crate) public_key: PublicKey,
|
||||
}
|
||||
|
||||
impl Chat {
|
||||
pub fn new(event: Event, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let public_key = event.pubkey;
|
||||
let last_seen = event.created_at;
|
||||
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
||||
tag.content().map(|s| s.to_string())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let metadata = cx.new_model(|_| None);
|
||||
let async_metadata = metadata.clone();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
// Request metadata
|
||||
_ = cx.global::<SignalRegistry>().tx.send(public_key);
|
||||
|
||||
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();
|
||||
};
|
||||
// Reload when received metadata
|
||||
cx.observe_global::<MetadataRegistry>(|chat, cx| {
|
||||
chat.load_metadata(cx);
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
last_seen,
|
||||
metadata,
|
||||
title: None,
|
||||
title,
|
||||
}
|
||||
}
|
||||
|
||||
fn profile(&self, cx: &mut ViewContext<Self>) {
|
||||
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
||||
let public_key = self.public_key;
|
||||
let async_metadata = self.metadata.clone();
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
@@ -1,67 +1,148 @@
|
||||
use chat::Chat;
|
||||
use coop_ui::{theme::ActiveTheme, v_flex, Collapsible, Icon, IconName, StyledExt};
|
||||
use coop_ui::{
|
||||
skeleton::Skeleton, theme::ActiveTheme, v_flex, Collapsible, Icon, IconName, StyledExt,
|
||||
};
|
||||
use gpui::*;
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use prelude::FluentBuilder;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
use crate::states::{account::AccountRegistry, chat::ChatRegistry};
|
||||
use crate::{get_client, states::chat::ChatRegistry};
|
||||
|
||||
pub mod chat;
|
||||
|
||||
pub struct Inbox {
|
||||
label: SharedString,
|
||||
chats: Model<Option<Vec<View<Chat>>>>,
|
||||
events: Model<Option<Vec<Event>>>,
|
||||
chats: Model<Vec<View<Chat>>>,
|
||||
is_loading: bool,
|
||||
is_fetching: bool,
|
||||
is_collapsed: bool,
|
||||
}
|
||||
|
||||
impl Inbox {
|
||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let chats = cx.new_model(|_| None);
|
||||
let chats = cx.new_model(|_| Vec::new());
|
||||
let events = cx.new_model(|_| None);
|
||||
|
||||
cx.observe_global::<ChatRegistry>(|inbox, cx| {
|
||||
inbox.load(cx);
|
||||
let state = cx.global::<ChatRegistry>();
|
||||
|
||||
if state.reload || (state.is_initialized && state.new_messages.is_empty()) {
|
||||
inbox.load(cx);
|
||||
} else {
|
||||
let new_messages = state.new_messages.clone();
|
||||
|
||||
for message in new_messages.into_iter() {
|
||||
cx.update_model(&inbox.events, |model, b| {
|
||||
if let Some(events) = model {
|
||||
if !events.iter().any(|ev| ev.pubkey == message.pubkey) {
|
||||
events.push(message);
|
||||
b.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.observe(&events, |inbox, model, cx| {
|
||||
// Show fetching indicator
|
||||
inbox.set_fetching(cx);
|
||||
|
||||
let events: Option<Vec<Event>> = model.read(cx).clone();
|
||||
|
||||
if let Some(events) = events {
|
||||
let views = inbox.chats.read(cx);
|
||||
let public_keys: Vec<PublicKey> =
|
||||
views.iter().map(|v| v.read(cx).public_key).collect();
|
||||
|
||||
for event in events
|
||||
.into_iter()
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
{
|
||||
if !public_keys.contains(&event.pubkey) {
|
||||
let view = cx.new_view(|cx| Chat::new(event, cx));
|
||||
|
||||
cx.update_model(&inbox.chats, |a, b| {
|
||||
a.push(view);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Hide fetching indicator
|
||||
inbox.set_fetching(cx);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.observe_new_views::<Chat>(|chat, cx| {
|
||||
chat.load_metadata(cx);
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
events,
|
||||
chats,
|
||||
label: "Inbox".into(),
|
||||
is_loading: true,
|
||||
is_fetching: false,
|
||||
is_collapsed: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn load(&mut self, cx: &mut ViewContext<Self>) {
|
||||
// Stop loading indicator;
|
||||
pub fn load(&mut self, cx: &mut ViewContext<Self>) {
|
||||
// Hide loading indicator
|
||||
self.set_loading(cx);
|
||||
|
||||
// Read global chat registry
|
||||
let events = cx.global::<ChatRegistry>().get(cx);
|
||||
let current_user = cx.global::<AccountRegistry>().get();
|
||||
let async_events = self.events.clone();
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
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.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
cx.update_model(&self.chats, |a, b| {
|
||||
*a = Some(chats);
|
||||
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)
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
async_cx.update_model(&async_events, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn set_loading(&mut self, cx: &mut ViewContext<Self>) {
|
||||
self.is_loading = false;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn set_fetching(&mut self, cx: &mut ViewContext<Self>) {
|
||||
self.is_fetching = !self.is_fetching;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Collapsible for Inbox {
|
||||
@@ -78,14 +159,31 @@ impl Collapsible for Inbox {
|
||||
impl Render for Inbox {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div();
|
||||
let chats = self.chats.read(cx);
|
||||
|
||||
if let Some(chats) = self.chats.read(cx).as_ref() {
|
||||
content = content.children(chats.clone())
|
||||
if self.is_loading {
|
||||
content = content.children((0..5).map(|_| {
|
||||
div()
|
||||
.h_8()
|
||||
.px_1()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.child(Skeleton::new().flex_shrink_0().size_6().rounded_full())
|
||||
.child(Skeleton::new().w_20().h_3().rounded_sm())
|
||||
}))
|
||||
} else {
|
||||
match self.is_loading {
|
||||
true => content = content.child("Loading..."),
|
||||
false => content = content.child("Empty"),
|
||||
}
|
||||
content = content
|
||||
.children(chats.clone())
|
||||
.when(self.is_fetching, |this| {
|
||||
this.h_8()
|
||||
.px_1()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.child(Skeleton::new().flex_shrink_0().size_6().rounded_full())
|
||||
.child(Skeleton::new().w_20().h_3().rounded_sm())
|
||||
});
|
||||
}
|
||||
|
||||
v_flex()
|
||||
|
||||
Reference in New Issue
Block a user