use futures::StreamExt;
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{disco::DiscoInfoQuery, iq::Iq, roster::Roster},
Event as TokioXmppEvent, Stanza,
};
use crate::{iq, message, presence, Agent, Event};
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Vec<Event> {
if let Some(event) = agent.client.next().await {
let mut events = Vec::new();
match event {
TokioXmppEvent::Online { resumed: false, .. } => {
let presence =
presence::send::make_initial_presence(&agent.disco, &agent.node).into();
let _ = agent.client.send_stanza(presence).await;
events.push(Event::Online);
let iq = Iq::from_get(
"roster",
Roster {
ver: None,
items: vec![],
},
)
.into();
let _ = agent.client.send_stanza(iq).await;
let iq = Iq::from_get("disco-account", DiscoInfoQuery { node: None }).into();
let _ = agent.client.send_stanza(iq).await;
agent.awaiting_disco_bookmarks_type = true;
}
TokioXmppEvent::Online { resumed: true, .. } => {}
TokioXmppEvent::Disconnected(e) => {
events.push(Event::Disconnected(e));
}
TokioXmppEvent::Stanza(Stanza::Iq(iq)) => {
let new_events = iq::handle_iq(agent, iq).await;
events.extend(new_events);
}
TokioXmppEvent::Stanza(Stanza::Message(message)) => {
let new_events = message::receive::handle_message(agent, message).await;
events.extend(new_events);
}
TokioXmppEvent::Stanza(Stanza::Presence(presence)) => {
let new_events = presence::receive::handle_presence(agent, presence).await;
events.extend(new_events);
}
}
events
} else {
panic!("xmpp::Agent should never receive None event (stream closed, no reconnect)");
}
}