wip: refactor
This commit is contained in:
@@ -56,6 +56,7 @@ impl Room {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
|
pub room_id: SharedString,
|
||||||
pub event: Event,
|
pub event: Event,
|
||||||
pub metadata: Option<Metadata>,
|
pub metadata: Option<Metadata>,
|
||||||
}
|
}
|
||||||
@@ -82,10 +83,15 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, event: Event, metadata: Option<Metadata>) {
|
pub fn push(&mut self, event: Event, metadata: Option<Metadata>) {
|
||||||
self.new_messages
|
let pubkeys: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
||||||
.write()
|
let room_id = get_room_id(&event.pubkey, &pubkeys);
|
||||||
.unwrap()
|
let message = Message {
|
||||||
.push(Message { event, metadata });
|
room_id: room_id.into(),
|
||||||
|
event,
|
||||||
|
metadata,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.new_messages.write().unwrap().push(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use super::message::RoomMessage;
|
|||||||
use crate::{
|
use crate::{
|
||||||
get_client,
|
get_client,
|
||||||
states::{
|
states::{
|
||||||
|
account::AccountRegistry,
|
||||||
chat::{ChatRegistry, Room},
|
chat::{ChatRegistry, Room},
|
||||||
metadata::MetadataRegistry,
|
metadata::MetadataRegistry,
|
||||||
},
|
},
|
||||||
@@ -25,6 +26,7 @@ pub struct Messages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct RoomPanel {
|
pub struct RoomPanel {
|
||||||
|
id: SharedString,
|
||||||
owner: PublicKey,
|
owner: PublicKey,
|
||||||
members: Arc<[PublicKey]>,
|
members: Arc<[PublicKey]>,
|
||||||
// Form
|
// Form
|
||||||
@@ -36,6 +38,7 @@ pub struct RoomPanel {
|
|||||||
|
|
||||||
impl RoomPanel {
|
impl RoomPanel {
|
||||||
pub fn new(room: &Arc<Room>, cx: &mut ViewContext<'_, Self>) -> Self {
|
pub fn new(room: &Arc<Room>, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
|
let id = room.id.clone();
|
||||||
let members: Arc<[PublicKey]> = room.members.clone().into();
|
let members: Arc<[PublicKey]> = room.members.clone().into();
|
||||||
let owner = room.owner;
|
let owner = room.owner;
|
||||||
|
|
||||||
@@ -83,6 +86,7 @@ impl RoomPanel {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
id,
|
||||||
owner,
|
owner,
|
||||||
members,
|
members,
|
||||||
input,
|
input,
|
||||||
@@ -148,18 +152,19 @@ impl RoomPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
||||||
|
let room_id = self.id.clone();
|
||||||
let messages = self.messages.clone();
|
let messages = self.messages.clone();
|
||||||
|
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
||||||
|
|
||||||
cx.observe_global::<ChatRegistry>(move |_, cx| {
|
cx.observe_global::<ChatRegistry>(move |_, cx| {
|
||||||
let state = cx.global::<ChatRegistry>();
|
let state = cx.global::<ChatRegistry>();
|
||||||
// let mut metadata = state.metadata.clone();
|
let new_messages = state.new_messages.read().unwrap().clone();
|
||||||
|
let filter = new_messages
|
||||||
|
.into_iter()
|
||||||
|
.filter(|m| m.room_id == room_id && m.event.pubkey != current_user)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// TODO: filter messages
|
let items: Vec<RoomMessage> = filter
|
||||||
let items: Vec<RoomMessage> = state
|
|
||||||
.new_messages
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|m| {
|
.map(|m| {
|
||||||
RoomMessage::new(
|
RoomMessage::new(
|
||||||
@@ -171,22 +176,24 @@ impl RoomPanel {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
cx.update_model(&messages, |a, b| {
|
cx.update_model(&messages, |model, cx| {
|
||||||
a.items.extend(items);
|
model.items.extend(items);
|
||||||
a.count = a.items.len();
|
model.count = model.items.len();
|
||||||
b.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: support chat room
|
|
||||||
pub fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
pub fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
let owner = self.owner;
|
let owner = self.owner;
|
||||||
|
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
||||||
let content = self.input.read(cx).text().to_string();
|
let content = self.input.read(cx).text().to_string();
|
||||||
let content_clone = content.clone();
|
let content2 = content.clone();
|
||||||
|
let content3 = content2.clone();
|
||||||
|
|
||||||
let async_input = self.input.clone();
|
let async_input = self.input.clone();
|
||||||
|
let async_messages = self.messages.clone();
|
||||||
let mut async_cx = cx.to_async();
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
cx.foreground_executor()
|
cx.foreground_executor()
|
||||||
@@ -194,33 +201,46 @@ impl RoomPanel {
|
|||||||
let client = get_client();
|
let client = get_client();
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
let send: anyhow::Result<(), anyhow::Error> = async_cx
|
// Send message to all members
|
||||||
|
async_cx
|
||||||
|
.background_executor()
|
||||||
|
.spawn(async move { client.send_private_msg(owner, content, vec![]).await })
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
// Send a copy to yourself
|
||||||
|
async_cx
|
||||||
.background_executor()
|
.background_executor()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
let signer = client.signer().await?;
|
client
|
||||||
let public_key = signer.get_public_key().await?;
|
|
||||||
|
|
||||||
// Send message to [owner]
|
|
||||||
if client
|
|
||||||
.send_private_msg(owner, content, vec![])
|
|
||||||
.await
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
// Send a copy to [yourself]
|
|
||||||
_ = client
|
|
||||||
.send_private_msg(
|
.send_private_msg(
|
||||||
public_key,
|
current_user,
|
||||||
content_clone,
|
content2,
|
||||||
vec![Tag::public_key(owner)],
|
vec![Tag::public_key(owner)],
|
||||||
)
|
)
|
||||||
.await?
|
.await
|
||||||
}
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
Ok(())
|
// Create a new room message
|
||||||
|
let new_message: anyhow::Result<RoomMessage, anyhow::Error> = async_cx
|
||||||
|
.background_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let metadata = client.database().metadata(current_user).await?;
|
||||||
|
let created_at = Timestamp::now();
|
||||||
|
let message =
|
||||||
|
RoomMessage::new(current_user, metadata, content3, created_at);
|
||||||
|
|
||||||
|
Ok(message)
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if send.is_ok() {
|
if let Ok(message) = new_message {
|
||||||
|
_ = async_cx.update_model(&async_messages, |model, cx| {
|
||||||
|
model.items.extend(vec![message]);
|
||||||
|
model.count = model.items.len();
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
|
||||||
_ = async_cx.update_view(&async_input, |input, cx| {
|
_ = async_cx.update_view(&async_input, |input, cx| {
|
||||||
input.set_text("", cx);
|
input.set_text("", cx);
|
||||||
});
|
});
|
||||||
@@ -248,7 +268,26 @@ impl Render for RoomPanel {
|
|||||||
.child(
|
.child(
|
||||||
Button::new("upload")
|
Button::new("upload")
|
||||||
.icon(Icon::new(IconName::Upload))
|
.icon(Icon::new(IconName::Upload))
|
||||||
.ghost(),
|
.ghost()
|
||||||
|
.on_click(|_, cx| {
|
||||||
|
let paths = cx.prompt_for_paths(PathPromptOptions {
|
||||||
|
files: true,
|
||||||
|
directories: false,
|
||||||
|
multiple: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.spawn(move |_async_cx| async move {
|
||||||
|
match Flatten::flatten(paths.await.map_err(|e| e.into())) {
|
||||||
|
Ok(Some(paths)) => {
|
||||||
|
// TODO: upload file to blossom server
|
||||||
|
println!("Paths: {:?}", paths)
|
||||||
|
}
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
|
|||||||
Reference in New Issue
Block a user