From 1868afeda311016730233369c2e787ea9bc59112 Mon Sep 17 00:00:00 2001 From: Werner Kroneman Date: Sun, 3 Mar 2024 17:28:29 +0100 Subject: [PATCH] Factored out create_muc_join_presence_stanza function. --- xmpp/src/muc/room.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/xmpp/src/muc/room.rs b/xmpp/src/muc/room.rs index 17d73d18..7363d8c9 100644 --- a/xmpp/src/muc/room.rs +++ b/xmpp/src/muc/room.rs @@ -23,17 +23,48 @@ pub async fn join_room( lang: &str, status: &str, ) { + let nick = nick.unwrap_or_else(|| agent.default_nick.read().unwrap().clone()); + let presence = create_muc_join_presence_stanza(room, password, lang, status, &nick); + let _ = agent.client.send_stanza(presence.into()).await; +} + +/// Create a Presence stanza for joining a MUC room with a specific nickname. +/// +/// # Arguments +/// - `room`: The JID of the room to join. +/// - `password`: The password to use to join the room, if any. +/// - `lang`: The language of the status message. +/// - `status`: The status message to send. +/// - `nick`: The nickname to use in the room. +pub fn create_muc_join_presence_stanza( + room: BareJid, + password: Option, + lang: &str, + status: &str, + nick: &String, +) -> Presence { + // Combine the room JID with the nickname to create the JID of the room occupant. + let room_jid = room.with_resource_str(&nick).unwrap(); + + // Create a presence stanza. + let mut presence = Presence::new(PresenceType::None).with_to(room_jid); + + // MUC presence stanza's must have a Muc payload. let mut muc = Muc::new(); + + // If a password is provided, include it in the Muc payload. if let Some(password) = password { muc = muc.with_password(password); } - let nick = nick.unwrap_or_else(|| agent.default_nick.read().unwrap().clone()); - let room_jid = room.with_resource_str(&nick).unwrap(); - let mut presence = Presence::new(PresenceType::None).with_to(room_jid); + // Add the Muc payload to the presence stanza. presence.add_payload(muc); + + // Set the user-readable status message. presence.set_status(String::from(lang), String::from(status)); - let _ = agent.client.send_stanza(presence.into()).await; + + // Return the presence stanza. + presence } /// Send a "leave room" request to the server (specifically, an "unavailable" presence stanza).