feat(xmpp): list muc rooms in disco

This commit is contained in:
Nikita Vilunov 2023-04-11 18:28:03 +02:00
parent c44101d5d0
commit d0c579863e
2 changed files with 18 additions and 19 deletions

View File

@ -32,6 +32,9 @@ impl RoomId {
pub fn as_bytes(&self) -> &ByteVec { pub fn as_bytes(&self) -> &ByteVec {
&self.0 &self.0
} }
pub fn into_bytes(self) -> ByteVec {
self.0
}
} }
/// Shared datastructure for storing metadata about rooms. /// Shared datastructure for storing metadata about rooms.

View File

@ -179,6 +179,7 @@ async fn handle_socket(
&mut reader_buf, &mut reader_buf,
&authenticated, &authenticated,
&mut connection, &mut connection,
&rooms,
) )
.await?; .await?;
@ -266,6 +267,7 @@ async fn socket_final(
reader_buf: &mut Vec<u8>, reader_buf: &mut Vec<u8>,
authenticated: &Authenticated, authenticated: &Authenticated,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
rooms: &RoomRegistry,
) -> Result<()> { ) -> Result<()> {
read_xml_header(xml_reader, reader_buf).await?; read_xml_header(xml_reader, reader_buf).await?;
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?; let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
@ -309,7 +311,7 @@ async fn socket_final(
Continuation::Final(res) => { Continuation::Final(res) => {
let res = res?; let res = res?;
dbg!(&res); dbg!(&res);
let stop = handle_packet(&mut events, res, authenticated, user_handle).await?; let stop = handle_packet(&mut events, res, authenticated, user_handle, rooms).await?;
for i in &events { for i in &events {
xml_writer.write_event_async(i).await?; xml_writer.write_event_async(i).await?;
} }
@ -375,10 +377,11 @@ async fn handle_packet(
packet: ClientPacket, packet: ClientPacket,
user: &Authenticated, user: &Authenticated,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
rooms: &RoomRegistry,
) -> Result<bool> { ) -> Result<bool> {
Ok(match packet { Ok(match packet {
proto::ClientPacket::Iq(iq) => { proto::ClientPacket::Iq(iq) => {
handle_iq(output, iq); handle_iq(output, iq, rooms).await;
false false
} }
proto::ClientPacket::Message(m) => { proto::ClientPacket::Message(m) => {
@ -471,7 +474,7 @@ async fn handle_packet(
}) })
} }
fn handle_iq(output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) { async fn handle_iq(output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>, rooms: &RoomRegistry) {
match iq.body { match iq.body {
proto::IqClientBody::Bind(b) => { proto::IqClientBody::Bind(b) => {
let req = Iq { let req = Iq {
@ -519,7 +522,7 @@ fn handle_iq(output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
req.serialize(output); req.serialize(output);
} }
proto::IqClientBody::DiscoItem(item) => { proto::IqClientBody::DiscoItem(item) => {
let response = disco_items(iq.to.as_deref(), &item); let response = disco_items(iq.to.as_deref(), &item, rooms).await;
let req = Iq { let req = Iq {
from: iq.to, from: iq.to,
id: iq.id, id: iq.id,
@ -583,7 +586,7 @@ fn disco_info(to: Option<&str>, req: &InfoQuery) -> InfoQuery {
} }
} }
fn disco_items(to: Option<&str>, req: &ItemQuery) -> ItemQuery { async fn disco_items(to: Option<&str>, req: &ItemQuery, rooms: &RoomRegistry) -> ItemQuery {
let item = match to { let item = match to {
Some("localhost") => { Some("localhost") => {
vec![Item { vec![Item {
@ -597,26 +600,19 @@ fn disco_items(to: Option<&str>, req: &ItemQuery) -> ItemQuery {
}] }]
} }
Some("rooms.localhost") => { Some("rooms.localhost") => {
vec![ let room_list = rooms.get_all_rooms().await;
Item { room_list
.into_iter()
.map(|room_info| Item {
jid: Jid { jid: Jid {
name: Some(Name("room1".to_string())), name: Some(Name(String::from_utf8(room_info.id.into_bytes()).unwrap())),
server: Server("rooms.localhost".to_string()), server: Server("rooms.localhost".to_string()),
resource: None, resource: None,
}, },
name: None, name: None,
node: None, node: None,
}, })
Item { .collect()
jid: Jid {
name: Some(Name("room2".to_string())),
server: Server("rooms.localhost".to_string()),
resource: None,
},
name: None,
node: None,
},
]
} }
_ => vec![], _ => vec![],
}; };