forked from lavina/lavina
1
0
Fork 0

xmpp: add x-user element to muc presence response (#67)

Reviewed-on: lavina/lavina#67
This commit is contained in:
Nikita Vilunov 2024-05-14 14:44:49 +00:00
parent 89918d9de1
commit 1b59250042
3 changed files with 24 additions and 5 deletions

View File

@ -6,6 +6,7 @@ use quick_xml::events::Event;
use lavina_core::room::RoomId; use lavina_core::room::RoomId;
use proto_xmpp::bind::{Jid, Name, Server}; use proto_xmpp::bind::{Jid, Name, Server};
use proto_xmpp::client::Presence; use proto_xmpp::client::Presence;
use proto_xmpp::muc::XUser;
use proto_xmpp::xml::{Ignore, ToXml}; use proto_xmpp::xml::{Ignore, ToXml};
use crate::XmppConnection; use crate::XmppConnection;
@ -60,10 +61,10 @@ impl<'a> XmppConnection<'a> {
} }
// todo: return Presence and serialize on the outside. // todo: return Presence and serialize on the outside.
async fn muc_presence(&mut self, name: &Name) -> Result<(Presence<()>)> { async fn muc_presence(&mut self, name: &Name) -> Result<(Presence<XUser>)> {
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?; let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?;
// TODO handle bans // TODO handle bans
let response = Presence::<()> { let response = Presence {
to: Some(Jid { to: Some(Jid {
name: Some(self.user.xmpp_name.clone()), name: Some(self.user.xmpp_name.clone()),
server: Server(self.hostname.clone()), server: Server(self.hostname.clone()),
@ -74,6 +75,7 @@ impl<'a> XmppConnection<'a> {
server: Server(self.hostname_rooms.clone()), server: Server(self.hostname_rooms.clone()),
resource: Some(self.user.xmpp_muc_name.clone()), resource: Some(self.user.xmpp_muc_name.clone()),
}), }),
custom: vec![XUser],
..Default::default() ..Default::default()
}; };
Ok(response) Ok(response)
@ -91,6 +93,7 @@ mod tests {
use lavina_core::player::PlayerId; use lavina_core::player::PlayerId;
use proto_xmpp::bind::{Jid, Name, Resource, Server}; use proto_xmpp::bind::{Jid, Name, Resource, Server};
use proto_xmpp::client::Presence; use proto_xmpp::client::Presence;
use proto_xmpp::muc::XUser;
#[tokio::test] #[tokio::test]
async fn test_muc_joining() -> Result<()> { async fn test_muc_joining() -> Result<()> {
@ -110,7 +113,7 @@ mod tests {
let mut conn = expect_user_authenticated(&server, &user, &mut player_conn).await.unwrap(); let mut conn = expect_user_authenticated(&server, &user, &mut player_conn).await.unwrap();
let response = conn.muc_presence(&user.xmpp_name).await.unwrap(); let response = conn.muc_presence(&user.xmpp_name).await.unwrap();
let expected = Presence::<()> { let expected = Presence {
to: Some(Jid { to: Some(Jid {
name: Some(conn.user.xmpp_name.clone()), name: Some(conn.user.xmpp_name.clone()),
server: Server(conn.hostname.clone()), server: Server(conn.hostname.clone()),
@ -121,6 +124,7 @@ mod tests {
server: Server(conn.hostname_rooms.clone()), server: Server(conn.hostname_rooms.clone()),
resource: Some(conn.user.xmpp_muc_name.clone()), resource: Some(conn.user.xmpp_muc_name.clone()),
}), }),
custom: vec![XUser],
..Default::default() ..Default::default()
}; };
assert_eq!(expected, response); assert_eq!(expected, response);
@ -149,7 +153,7 @@ mod tests {
let mut conn = expect_user_authenticated(&server, &user, &mut player_conn).await.unwrap(); let mut conn = expect_user_authenticated(&server, &user, &mut player_conn).await.unwrap();
let response = conn.muc_presence(&user.xmpp_name).await.unwrap(); let response = conn.muc_presence(&user.xmpp_name).await.unwrap();
let expected = Presence::<()> { let expected = Presence {
to: Some(Jid { to: Some(Jid {
name: Some(conn.user.xmpp_name.clone()), name: Some(conn.user.xmpp_name.clone()),
server: Server(conn.hostname.clone()), server: Server(conn.hostname.clone()),
@ -160,6 +164,7 @@ mod tests {
server: Server(conn.hostname_rooms.clone()), server: Server(conn.hostname_rooms.clone()),
resource: Some(conn.user.xmpp_muc_name.clone()), resource: Some(conn.user.xmpp_muc_name.clone()),
}), }),
custom: vec![XUser],
..Default::default() ..Default::default()
}; };
assert_eq!(expected, response); assert_eq!(expected, response);

View File

@ -669,6 +669,9 @@ impl<T: ToXml> ToXml for Presence<T> {
Event::End(BytesEnd::new("priority")), Event::End(BytesEnd::new("priority")),
]); ]);
} }
for c in &self.custom {
c.serialize(events);
}
events.push(Event::End(BytesEnd::new("presence"))); events.push(Event::End(BytesEnd::new("presence")));
} }
} }

View File

@ -1,12 +1,13 @@
#![allow(unused_variables)] #![allow(unused_variables)]
use quick_xml::events::Event; use quick_xml::events::{BytesStart, Event};
use quick_xml::name::ResolveResult; use quick_xml::name::ResolveResult;
use crate::xml::*; use crate::xml::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
pub const XMLNS: &'static str = "http://jabber.org/protocol/muc"; pub const XMLNS: &'static str = "http://jabber.org/protocol/muc";
pub const XMLNS_USER: &'static str = "http://jabber.org/protocol/muc#user";
#[derive(PartialEq, Eq, Debug, Default)] #[derive(PartialEq, Eq, Debug, Default)]
pub struct History { pub struct History {
@ -143,6 +144,16 @@ impl FromXml for X {
} }
} }
#[derive(Debug, PartialEq, Eq)]
pub struct XUser;
impl ToXml for XUser {
fn serialize(&self, output: &mut Vec<Event<'static>>) {
let mut tag = BytesStart::new("x");
tag.push_attribute(("xmlns", XMLNS_USER));
output.push(Event::Empty(tag));
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;