forked from lavina/lavina
1
0
Fork 0

feat(xmpp): send muc messages to clients

This commit is contained in:
Nikita Vilunov 2023-04-11 16:42:09 +02:00
parent 58582f4e51
commit c44101d5d0
1 changed files with 70 additions and 23 deletions

View File

@ -291,17 +291,20 @@ async fn socket_final(
xml_writer.get_mut().flush().await?; xml_writer.get_mut().flush().await?;
let mut parser = proto::ClientPacket::parse(); let mut parser = proto::ClientPacket::parse();
loop { let mut events = vec![];
reader_buf.clear(); reader_buf.clear();
let (ns, event) = xml_reader let mut next_xml_event = Box::pin(xml_reader.read_resolved_event_into_async(reader_buf));
.read_resolved_event_into_async(reader_buf)
.await?; 'outer: loop {
let should_recreate_xml_future = select! {
biased;
res = &mut next_xml_event => 's: {
let (ns, event) = res?;
if let Event::Text(ref e) = event { if let Event::Text(ref e) = event {
if e.iter().all(|x| *x == 0xA) { if e.iter().all(|x| *x == 0xA) {
continue; break 's true;
} }
} }
let mut events = vec![];
match parser.consume(ns, &event) { match parser.consume(ns, &event) {
Continuation::Final(res) => { Continuation::Final(res) => {
let res = res?; let res = res?;
@ -313,12 +316,56 @@ async fn socket_final(
events.clear(); events.clear();
xml_writer.get_mut().flush().await?; xml_writer.get_mut().flush().await?;
if stop { if stop {
break; break 'outer;
} }
parser = proto::ClientPacket::parse(); parser = proto::ClientPacket::parse();
} }
Continuation::Continue(p) => parser = p, Continuation::Continue(p) => parser = p,
} }
true
},
update = user_handle.receiver.recv() => {
if let Some(update) = update {
match update {
crate::core::player::Updates::NewMessage { room_id, author_id, body } => {
Message {
to: Some(Jid {
name: Some(authenticated.xmpp_name.clone()),
server: Server("localhost".into()),
resource: Some(authenticated.xmpp_resource.clone()),
}),
from: Some(Jid {
name: Some(Name(std::str::from_utf8(room_id.as_bytes())?.to_owned())),
server: Server("rooms.localhost".into()),
resource: Some(Resource(std::str::from_utf8(author_id.as_bytes())?.to_owned())),
}),
id: None,
r#type: xmpp::client::MessageType::Groupchat,
lang: None,
subject: None,
body: body,
}
.serialize(&mut events);
}
_ => {},
}
for i in &events {
xml_writer.write_event_async(i).await?;
}
events.clear();
xml_writer.get_mut().flush().await?;
} else {
log::warn!("Player is terminated, must terminate the connection");
break;
}
false
}
};
if should_recreate_xml_future {
drop(next_xml_event);
next_xml_event = Box::pin(xml_reader.read_resolved_event_into_async(reader_buf));
}
} }
Ok(()) Ok(())
} }