mirror of
https://gitea.mizah.xyz/mizah/dergchat
synced 2024-11-21 13:55:00 -05:00
Minor refactor, removing the rooms state with just the messages state.
This commit is contained in:
parent
fd56718960
commit
ceda732e27
40
src/main.rs
40
src/main.rs
@ -174,11 +174,9 @@ fn SendMessage<'a>(cx: Scope<'a, SendMessageProps>) -> Element<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async fn handle_event(event: xmpp::Event,
|
||||
agent: &mut xmpp::Agent,
|
||||
rooms: &mut UseState<Vec<BareJid>>,
|
||||
messages: &mut UseState<HashMap<BareJid, Vec<Message>>>) {
|
||||
messages: &mut UseRef<HashMap<BareJid, Vec<Message>>>) {
|
||||
match event {
|
||||
xmpp::Event::JoinRoom(jid, conference) => {
|
||||
println!("Will auto-join room: {}", &jid);
|
||||
@ -186,34 +184,34 @@ async fn handle_event(event: xmpp::Event,
|
||||
}
|
||||
xmpp::Event::RoomJoined(room_jid) => {
|
||||
println!("Joined room: {}", &room_jid);
|
||||
messages.make_mut().entry(room_jid.clone()).or_insert(vec![]);
|
||||
rooms.make_mut().push(room_jid);
|
||||
messages.with_mut(move |m| { m.entry(room_jid.clone()).or_insert(vec![]); });
|
||||
}
|
||||
xmpp::Event::RoomLeft(room_jid) => {
|
||||
println!("Left room: {}", &room_jid);
|
||||
messages.make_mut().remove(&room_jid);
|
||||
rooms.make_mut().retain(|x| x != &room_jid);
|
||||
messages.with_mut(move |m| { m.remove(&room_jid); });
|
||||
}
|
||||
xmpp::Event::ChatMessage(id, sender, body) => {
|
||||
println!("Message from {}: {}", &sender, &body.0);
|
||||
messages.make_mut().entry(sender.clone()).or_insert(vec![]).push(Message {
|
||||
sender: sender.to_string(),
|
||||
body: body.0,
|
||||
messages.with_mut(move |m| {
|
||||
m.entry(sender.clone()).or_insert(vec![]).push(Message {
|
||||
sender: sender.to_string(),
|
||||
body: body.0,
|
||||
});
|
||||
});
|
||||
}
|
||||
xmpp::Event::RoomMessage(id, room_jid, sender_nick, body) => {
|
||||
println!("Message in {} from {}: {}", &room_jid, &sender_nick, &body.0);
|
||||
messages.make_mut().entry(room_jid).or_insert(vec![]).push(Message {
|
||||
messages.with_mut(move |m| m.entry(room_jid.clone()).or_insert(vec![]).push(Message {
|
||||
sender: sender_nick,
|
||||
body: body.0,
|
||||
});
|
||||
}));
|
||||
}
|
||||
xmpp::Event::RoomPrivateMessage(id, room_jid, sender_nick, body) => {
|
||||
println!("Private message in {} from {}: {}", &room_jid, &sender_nick, &body.0);
|
||||
messages.make_mut().entry(room_jid).or_insert(vec![]).push(Message {
|
||||
messages.with_mut(move |m| m.entry(room_jid.clone()).or_insert(vec![]).push(Message {
|
||||
sender: sender_nick,
|
||||
body: body.0,
|
||||
});
|
||||
}));
|
||||
}
|
||||
_ => {
|
||||
log::debug!("Received unsupported event {:?}", event);
|
||||
@ -259,12 +257,11 @@ fn RoomJoinWidget<'a>(cx: Scope<'a, RoomJoinProps>) -> Element<'a> {
|
||||
// define a component that renders a div with the text "Hello, world!"
|
||||
fn App(cx: Scope) -> Element {
|
||||
|
||||
let rooms = use_state(cx, || vec![]);
|
||||
let messages = use_state(cx, || HashMap::new());
|
||||
let messages = use_ref(cx, || HashMap::new());
|
||||
let current_room = use_state(cx, || None::<BareJid>);
|
||||
|
||||
let cr = use_coroutine(cx,
|
||||
|rx: UnboundedReceiver<NetworkCommand>| run_xmpp(rooms.clone(), messages.clone(), rx),
|
||||
|rx: UnboundedReceiver<NetworkCommand>| run_xmpp(messages.clone(), rx),
|
||||
);
|
||||
|
||||
render! {
|
||||
@ -285,7 +282,7 @@ fn App(cx: Scope) -> Element {
|
||||
"Mizah"
|
||||
}
|
||||
RoomList {
|
||||
rooms: rooms.to_vec(),
|
||||
rooms: messages.read().keys().cloned().collect(),
|
||||
on_room_picked: move |x:BareJid| {
|
||||
println!("Room selected: {:?}", x);
|
||||
current_room.set(Some(x));
|
||||
@ -304,7 +301,7 @@ fn App(cx: Scope) -> Element {
|
||||
}
|
||||
|
||||
if let Some(room) = current_room.get() {
|
||||
let messages = messages.get().get(&room).expect("Selected non-existant room").to_vec();
|
||||
let messages = messages.read().get(&room).expect("Selected non-existant room").to_vec();
|
||||
|
||||
rsx! {
|
||||
RoomView {
|
||||
@ -334,8 +331,7 @@ fn App(cx: Scope) -> Element {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_xmpp(mut rooms: UseState<Vec<BareJid>>,
|
||||
mut messages: UseState<HashMap<BareJid, Vec<Message>>>,
|
||||
fn run_xmpp(mut room_data: UseRef<HashMap<BareJid, Vec<Message>>>,
|
||||
mut commands: UnboundedReceiver<NetworkCommand>) -> impl Future<Output=()> + Sized {
|
||||
|
||||
async move {
|
||||
@ -354,7 +350,7 @@ fn run_xmpp(mut rooms: UseState<Vec<BareJid>>,
|
||||
events = agent.wait_for_events() => {
|
||||
if let Some(events) = events {
|
||||
for event in events {
|
||||
handle_event(event, &mut agent, &mut rooms, &mut messages).await;
|
||||
handle_event(event, &mut agent, &mut room_data).await;
|
||||
}
|
||||
} else {
|
||||
info!("Disconnected");
|
||||
|
Loading…
Reference in New Issue
Block a user