test/templates: new_room method

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-12-23 22:43:09 +01:00
parent 6fb526a14c
commit a86a85cf19
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -15,6 +15,7 @@
use crate::occupant::Occupant;
use crate::room::Room;
use crate::session::Nick;
use std::str::FromStr;
use std::sync::LazyLock;
use xmpp_parsers::{
@ -101,3 +102,27 @@ pub fn two_participant_room(roomjid: BareJid) -> Room {
);
room
}
// TODO: Fix this. Add yet another layer of struct that doesn't send stanzas so we don't need to
// duplicate Room code in here without all the safeguards?
pub async fn new_room<N: Into<Nick>>(roomjid: BareJid, sessions: Vec<(FullJid, N)>) -> Room {
let mut room = Room::new(roomjid.clone());
for (session, nick) in sessions {
let nick: Nick = nick.into();
let participant = roomjid.clone().with_resource(nick.clone());
let presence = Presence::new(PresenceType::None)
.with_to(participant)
.with_from(session);
match room.occupants.get_mut(&nick) {
Some(occupant) => occupant.add_session(presence).unwrap(),
None => {
let _ = room
.occupants
.insert(nick.clone(), Occupant::new(presence).unwrap());
}
};
}
room
}