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,
|
async fn handle_event(event: xmpp::Event,
|
||||||
agent: &mut xmpp::Agent,
|
agent: &mut xmpp::Agent,
|
||||||
rooms: &mut UseState<Vec<BareJid>>,
|
messages: &mut UseRef<HashMap<BareJid, Vec<Message>>>) {
|
||||||
messages: &mut UseState<HashMap<BareJid, Vec<Message>>>) {
|
|
||||||
match event {
|
match event {
|
||||||
xmpp::Event::JoinRoom(jid, conference) => {
|
xmpp::Event::JoinRoom(jid, conference) => {
|
||||||
println!("Will auto-join room: {}", &jid);
|
println!("Will auto-join room: {}", &jid);
|
||||||
@ -186,34 +184,34 @@ async fn handle_event(event: xmpp::Event,
|
|||||||
}
|
}
|
||||||
xmpp::Event::RoomJoined(room_jid) => {
|
xmpp::Event::RoomJoined(room_jid) => {
|
||||||
println!("Joined room: {}", &room_jid);
|
println!("Joined room: {}", &room_jid);
|
||||||
messages.make_mut().entry(room_jid.clone()).or_insert(vec![]);
|
messages.with_mut(move |m| { m.entry(room_jid.clone()).or_insert(vec![]); });
|
||||||
rooms.make_mut().push(room_jid);
|
|
||||||
}
|
}
|
||||||
xmpp::Event::RoomLeft(room_jid) => {
|
xmpp::Event::RoomLeft(room_jid) => {
|
||||||
println!("Left room: {}", &room_jid);
|
println!("Left room: {}", &room_jid);
|
||||||
messages.make_mut().remove(&room_jid);
|
messages.with_mut(move |m| { m.remove(&room_jid); });
|
||||||
rooms.make_mut().retain(|x| x != &room_jid);
|
|
||||||
}
|
}
|
||||||
xmpp::Event::ChatMessage(id, sender, body) => {
|
xmpp::Event::ChatMessage(id, sender, body) => {
|
||||||
println!("Message from {}: {}", &sender, &body.0);
|
println!("Message from {}: {}", &sender, &body.0);
|
||||||
messages.make_mut().entry(sender.clone()).or_insert(vec![]).push(Message {
|
messages.with_mut(move |m| {
|
||||||
sender: sender.to_string(),
|
m.entry(sender.clone()).or_insert(vec![]).push(Message {
|
||||||
body: body.0,
|
sender: sender.to_string(),
|
||||||
|
body: body.0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
xmpp::Event::RoomMessage(id, room_jid, sender_nick, body) => {
|
xmpp::Event::RoomMessage(id, room_jid, sender_nick, body) => {
|
||||||
println!("Message in {} from {}: {}", &room_jid, &sender_nick, &body.0);
|
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,
|
sender: sender_nick,
|
||||||
body: body.0,
|
body: body.0,
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
xmpp::Event::RoomPrivateMessage(id, room_jid, sender_nick, body) => {
|
xmpp::Event::RoomPrivateMessage(id, room_jid, sender_nick, body) => {
|
||||||
println!("Private message in {} from {}: {}", &room_jid, &sender_nick, &body.0);
|
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,
|
sender: sender_nick,
|
||||||
body: body.0,
|
body: body.0,
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
log::debug!("Received unsupported event {:?}", event);
|
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!"
|
// define a component that renders a div with the text "Hello, world!"
|
||||||
fn App(cx: Scope) -> Element {
|
fn App(cx: Scope) -> Element {
|
||||||
|
|
||||||
let rooms = use_state(cx, || vec![]);
|
let messages = use_ref(cx, || HashMap::new());
|
||||||
let messages = use_state(cx, || HashMap::new());
|
|
||||||
let current_room = use_state(cx, || None::<BareJid>);
|
let current_room = use_state(cx, || None::<BareJid>);
|
||||||
|
|
||||||
let cr = use_coroutine(cx,
|
let cr = use_coroutine(cx,
|
||||||
|rx: UnboundedReceiver<NetworkCommand>| run_xmpp(rooms.clone(), messages.clone(), rx),
|
|rx: UnboundedReceiver<NetworkCommand>| run_xmpp(messages.clone(), rx),
|
||||||
);
|
);
|
||||||
|
|
||||||
render! {
|
render! {
|
||||||
@ -285,7 +282,7 @@ fn App(cx: Scope) -> Element {
|
|||||||
"Mizah"
|
"Mizah"
|
||||||
}
|
}
|
||||||
RoomList {
|
RoomList {
|
||||||
rooms: rooms.to_vec(),
|
rooms: messages.read().keys().cloned().collect(),
|
||||||
on_room_picked: move |x:BareJid| {
|
on_room_picked: move |x:BareJid| {
|
||||||
println!("Room selected: {:?}", x);
|
println!("Room selected: {:?}", x);
|
||||||
current_room.set(Some(x));
|
current_room.set(Some(x));
|
||||||
@ -304,7 +301,7 @@ fn App(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(room) = current_room.get() {
|
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! {
|
rsx! {
|
||||||
RoomView {
|
RoomView {
|
||||||
@ -334,8 +331,7 @@ fn App(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_xmpp(mut rooms: UseState<Vec<BareJid>>,
|
fn run_xmpp(mut room_data: UseRef<HashMap<BareJid, Vec<Message>>>,
|
||||||
mut messages: UseState<HashMap<BareJid, Vec<Message>>>,
|
|
||||||
mut commands: UnboundedReceiver<NetworkCommand>) -> impl Future<Output=()> + Sized {
|
mut commands: UnboundedReceiver<NetworkCommand>) -> impl Future<Output=()> + Sized {
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
@ -354,7 +350,7 @@ fn run_xmpp(mut rooms: UseState<Vec<BareJid>>,
|
|||||||
events = agent.wait_for_events() => {
|
events = agent.wait_for_events() => {
|
||||||
if let Some(events) = events {
|
if let Some(events) = events {
|
||||||
for event in 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 {
|
} else {
|
||||||
info!("Disconnected");
|
info!("Disconnected");
|
||||||
|
Loading…
Reference in New Issue
Block a user