xmpp/message/receive/
chat.rsuse tokio_xmpp::{
jid::Jid,
parsers::{message::Message, message_correct::Replace, muc::user::MucUser},
};
use crate::{delay::StanzaTimeInfo, Agent, Event, RoomNick};
pub async fn handle_message_chat(
agent: &mut Agent,
events: &mut Vec<Event>,
from: Jid,
message: &mut Message,
time_info: StanzaTimeInfo,
) {
let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
let Some((_lang, body)) = message.get_best_body_cloned(langs) else {
debug!("Received normal/chat message without body:\n{:#?}", message);
return;
};
let is_muc_pm = message.extract_valid_payload::<MucUser>().is_some();
let correction = message.extract_valid_payload::<Replace>();
if is_muc_pm {
if from.resource().is_none() {
warn!("Received malformed MessageType::Chat in muc#user namespace from a bare JID:\n{:#?}", message);
} else {
let full_from = from.clone().try_into_full().unwrap();
let event = if let Some(correction) = correction {
Event::RoomPrivateMessageCorrection(
correction.id,
full_from.to_bare(),
RoomNick::from_resource_ref(full_from.resource()),
body.clone(),
time_info,
)
} else {
Event::RoomPrivateMessage(
message.id.clone(),
from.to_bare(),
RoomNick::from_resource_ref(full_from.resource()),
body.clone(),
time_info,
)
};
events.push(event);
}
} else {
let event = if let Some(correction) = correction {
Event::ChatMessageCorrection(correction.id, from.to_bare(), body.clone(), time_info)
} else {
Event::ChatMessage(message.id.clone(), from.to_bare(), body, time_info)
};
events.push(event);
}
}