wip: refactor
This commit is contained in:
@@ -102,37 +102,39 @@ async fn main() {
|
||||
} = message
|
||||
{
|
||||
if event.kind == Kind::GiftWrap {
|
||||
if let Ok(UnwrappedGift { rumor, .. }) =
|
||||
client.unwrap_gift_wrap(&event).await
|
||||
{
|
||||
let mut rumor_clone = rumor.clone();
|
||||
match client.unwrap_gift_wrap(&event).await {
|
||||
Ok(UnwrappedGift { rumor, .. }) => {
|
||||
let mut rumor_clone = rumor.clone();
|
||||
|
||||
// Compute event id if not exist
|
||||
rumor_clone.ensure_id();
|
||||
// Compute event id if not exist
|
||||
rumor_clone.ensure_id();
|
||||
|
||||
if let Some(id) = rumor_clone.id {
|
||||
let ev = Event::new(
|
||||
id,
|
||||
rumor_clone.pubkey,
|
||||
rumor_clone.created_at,
|
||||
rumor_clone.kind,
|
||||
rumor_clone.tags,
|
||||
rumor_clone.content,
|
||||
sig,
|
||||
);
|
||||
if let Some(id) = rumor_clone.id {
|
||||
let ev = Event::new(
|
||||
id,
|
||||
rumor_clone.pubkey,
|
||||
rumor_clone.created_at,
|
||||
rumor_clone.kind,
|
||||
rumor_clone.tags,
|
||||
rumor_clone.content,
|
||||
sig,
|
||||
);
|
||||
|
||||
// Save rumor to database to further query
|
||||
if let Err(e) = client.database().save_event(&ev).await {
|
||||
println!("Save error: {}", e);
|
||||
}
|
||||
// Save rumor to database to further query
|
||||
if let Err(e) = client.database().save_event(&ev).await {
|
||||
println!("Save error: {}", e);
|
||||
}
|
||||
|
||||
// Send event back to channel
|
||||
if subscription_id == new_message {
|
||||
if let Err(e) = signal_tx.send(Signal::RecvEvent(ev)).await {
|
||||
println!("Error: {}", e)
|
||||
// Send event back to channel
|
||||
if subscription_id == new_message {
|
||||
if let Err(e) = signal_tx.send(Signal::RecvEvent(ev)).await
|
||||
{
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
} else if event.kind == Kind::Metadata {
|
||||
if let Err(e) = signal_tx.send(Signal::RecvMetadata(event.pubkey)).await {
|
||||
|
||||
@@ -24,12 +24,9 @@ impl Form {
|
||||
.cleanable()
|
||||
});
|
||||
|
||||
cx.subscribe(&input, move |form, text_input, input_event, cx| {
|
||||
cx.subscribe(&input, move |form, _, 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);
|
||||
form.send_message(cx);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
@@ -37,28 +34,45 @@ impl Form {
|
||||
Self { to, input }
|
||||
}
|
||||
|
||||
fn send_message(&mut self, content: String, cx: &mut ViewContext<Self>) {
|
||||
fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
||||
let send_to = self.to;
|
||||
let content = self.input.read(cx).text().to_string();
|
||||
let content_clone = content.clone();
|
||||
|
||||
let async_input = self.input.clone();
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
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![])
|
||||
async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
// Send message to all members
|
||||
if client
|
||||
.send_private_msg(send_to, content, vec![])
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
todo!()
|
||||
// Send a copy to yourself
|
||||
_ = client
|
||||
.send_private_msg(
|
||||
public_key,
|
||||
content_clone,
|
||||
vec![Tag::public_key(send_to)],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
Err(_) => todo!(),
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
_ = async_cx.update_view(&async_input, |input, cx| {
|
||||
input.set_text("", cx);
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
@@ -67,8 +81,11 @@ impl Form {
|
||||
impl Render for Form {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.h_12()
|
||||
.flex_shrink_0()
|
||||
.w_full()
|
||||
.h_12()
|
||||
.border_t_1()
|
||||
.border_color(cx.theme().border.opacity(0.7))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use prelude::FluentBuilder;
|
||||
|
||||
use crate::get_client;
|
||||
use crate::{get_client, states::chat::ChatRegistry};
|
||||
|
||||
pub struct MessageList {
|
||||
member: PublicKey,
|
||||
@@ -56,28 +57,38 @@ impl MessageList {
|
||||
}
|
||||
|
||||
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
||||
/*
|
||||
let receiver = cx.global::<ChatRegistry>().receiver.clone();
|
||||
let messages = self.messages.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
while let Ok(event) = receiver.recv_async().await {
|
||||
println!("New message: {}", event.as_json())
|
||||
cx.observe_global::<ChatRegistry>(move |_, cx| {
|
||||
let state = cx.global::<ChatRegistry>();
|
||||
let events = state.new_messages.clone();
|
||||
|
||||
cx.update_model(&messages, |a, b| {
|
||||
if let Some(m) = a {
|
||||
m.extend(events);
|
||||
b.notify();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
*/
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if let Some(messages) = self.messages.read(cx).as_ref() {
|
||||
content = content.children(messages.clone().into_iter().map(|m| div().child(m.content)))
|
||||
}
|
||||
|
||||
div().flex_1().child(content)
|
||||
div()
|
||||
.h_full()
|
||||
.flex()
|
||||
.flex_col_reverse()
|
||||
.justify_end()
|
||||
.when_some(self.messages.read(cx).as_ref(), |this, messages| {
|
||||
this.children(messages.clone().into_iter().map(|m| {
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(m.pubkey.to_hex())
|
||||
.child(m.content)
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use coop_ui::{
|
||||
button::Button,
|
||||
dock::{Panel, PanelEvent, PanelState, TitleStyle},
|
||||
popup_menu::PopupMenu,
|
||||
v_flex,
|
||||
};
|
||||
use form::Form;
|
||||
use gpui::*;
|
||||
@@ -90,11 +91,9 @@ impl FocusableView for ChatPanel {
|
||||
|
||||
impl Render for ChatPanel {
|
||||
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
v_flex()
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(self.list.clone())
|
||||
.child(div().flex_1().min_h_0().child(self.list.clone()))
|
||||
.child(self.form.clone())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user