use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::parsers::{
muc::user::{MucUser, Status},
presence::{Presence, Type as PresenceType},
};
use crate::{Agent, Event};
pub async fn handle_presence<C: ServerConnector>(
_agent: &mut Agent<C>,
presence: Presence,
) -> Vec<Event> {
let mut events = vec![];
let from = presence.from.unwrap().to_bare();
if let Some(muc) = presence
.payloads
.iter()
.filter_map(|p| MucUser::try_from(p.clone()).ok())
.next()
{
if muc.status.iter().any(|s| *s == Status::SelfPresence) {
match presence.type_ {
PresenceType::None => {
events.push(Event::RoomJoined(from.clone()));
}
PresenceType::Unavailable => {
events.push(Event::RoomLeft(from.clone()));
}
_ => unimplemented!("Presence type {:?}", presence.type_), }
}
}
events
}