Remove xmpp::Event::{JoinRoom,LeaveRoom,LeaveAllRooms}

This commit is contained in:
xmppftw xmppftw 2024-12-16 18:43:05 +01:00
commit 6d7a95b6ca
9 changed files with 131 additions and 49 deletions

View file

@ -13,7 +13,7 @@ use tokio_xmpp::{
},
};
use crate::{Agent, RoomNick};
use crate::Agent;
pub async fn join_room<C: ServerConnector>(
agent: &mut Agent<C>,
@ -23,6 +23,18 @@ pub async fn join_room<C: ServerConnector>(
lang: &str,
status: &str,
) {
if agent.rooms_joining.contains_key(&room) {
// We are already joining
warn!("Requesting to join again room {room} which is already joining...");
return;
}
if !agent.rooms_joined.contains_key(&room) {
// We are already joined, cannot join
warn!("Requesting to join room {room} which is already joined...");
return;
}
let mut muc = Muc::new();
if let Some(password) = password {
muc = muc.with_password(password);
@ -39,6 +51,8 @@ pub async fn join_room<C: ServerConnector>(
presence.add_payload(muc);
presence.set_status(String::from(lang), String::from(status));
let _ = agent.client.send_stanza(presence.into()).await;
agent.rooms_joining.insert(room, nick);
}
/// Send a "leave room" request to the server (specifically, an "unavailable" presence stanza).
@ -57,21 +71,33 @@ pub async fn join_room<C: ServerConnector>(
/// # Arguments
///
/// * `room_jid`: The JID of the room to leave.
/// * `nickname`: The nickname to use in the room.
/// * `lang`: The language of the status message.
/// * `status`: The status message to send.
pub async fn leave_room<C: ServerConnector>(
agent: &mut Agent<C>,
room_jid: BareJid,
nickname: RoomNick,
room: BareJid,
lang: impl Into<String>,
status: impl Into<String>,
) {
if agent.rooms_leaving.contains_key(&room) {
// We are already leaving
warn!("Requesting to leave again room {room} which is already leaving...");
return;
}
if !agent.rooms_joined.contains_key(&room) {
// We are not joined, cannot leave
warn!("Requesting to leave room {room} which is not joined...");
return;
}
// Get currently-used nickname
let nickname = agent.rooms_joined.get(&room).unwrap();
// XEP-0045 specifies that, to leave a room, the client must send a presence stanza
// with type="unavailable".
let mut presence = Presence::new(PresenceType::Unavailable).with_to(
room_jid
.with_resource_str(nickname.as_str())
room.with_resource_str(nickname)
.expect("Invalid room JID after adding resource part."),
);
@ -85,4 +111,6 @@ pub async fn leave_room<C: ServerConnector>(
// Report any errors to the log.
error!("Failed to send leave room presence: {}", e);
}
agent.rooms_leaving.insert(room, nickname.to_string());
}