2023-12-30 13:09:01 +01:00
|
|
|
// Copyright (c) 2023 xmpp-rs contributors.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2024-12-19 17:20:11 +01:00
|
|
|
use crate::{
|
|
|
|
|
delay::StanzaTimeInfo,
|
|
|
|
|
jid::Jid,
|
|
|
|
|
parsers::{message::Message, message_correct::Replace},
|
|
|
|
|
Agent, Event, RoomNick,
|
|
|
|
|
};
|
2023-12-30 13:09:01 +01:00
|
|
|
|
2024-08-20 16:54:48 +02:00
|
|
|
pub async fn handle_message_group_chat(
|
|
|
|
|
agent: &mut Agent,
|
2023-12-30 13:09:01 +01:00
|
|
|
events: &mut Vec<Event>,
|
|
|
|
|
from: Jid,
|
2024-12-19 17:20:11 +01:00
|
|
|
message: &mut Message,
|
2023-12-31 21:06:15 +01:00
|
|
|
time_info: StanzaTimeInfo,
|
2023-12-30 13:09:01 +01:00
|
|
|
) {
|
|
|
|
|
let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
|
2024-12-19 17:20:11 +01:00
|
|
|
let mut found_subject = false;
|
2023-12-31 19:41:34 +01:00
|
|
|
|
|
|
|
|
if let Some((_lang, subject)) = message.get_best_subject(langs.clone()) {
|
2023-12-31 19:59:05 +01:00
|
|
|
events.push(Event::RoomSubject(
|
|
|
|
|
from.to_bare(),
|
2024-12-18 12:44:59 +01:00
|
|
|
from.resource().map(RoomNick::from_resource_ref),
|
2025-04-18 12:26:27 +02:00
|
|
|
subject.clone(),
|
2023-12-31 21:06:15 +01:00
|
|
|
time_info.clone(),
|
2023-12-31 19:59:05 +01:00
|
|
|
));
|
2024-12-19 17:20:11 +01:00
|
|
|
found_subject = true;
|
2023-12-31 19:41:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-19 17:20:11 +01:00
|
|
|
let Some((_lang, body)) = message.get_best_body_cloned(langs) else {
|
|
|
|
|
if !found_subject {
|
|
|
|
|
debug!(
|
|
|
|
|
"Received groupchat message without body/subject:\n{:#?}",
|
|
|
|
|
message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let correction = message.extract_payload::<Replace>().unwrap_or_else(|e| {
|
|
|
|
|
warn!("Failed to parse <replace> payload: {e}");
|
|
|
|
|
None
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Now we have a groupchat message... which can be:
|
|
|
|
|
//
|
|
|
|
|
// - a normal MUC message from a user in a room
|
|
|
|
|
// - a MUC message correction from a user in a room
|
|
|
|
|
// - a service message from a MUC channel (barejid)
|
|
|
|
|
//
|
|
|
|
|
// In theory we can have service message correction but nope nope nope
|
|
|
|
|
|
|
|
|
|
if let Some(resource) = from.resource() {
|
|
|
|
|
// User message/correction
|
|
|
|
|
|
|
|
|
|
let event = if let Some(correction) = correction {
|
|
|
|
|
Event::RoomMessageCorrection(
|
2024-12-21 00:10:13 +01:00
|
|
|
correction.id,
|
2024-12-19 17:20:11 +01:00
|
|
|
from.to_bare(),
|
|
|
|
|
RoomNick::from_resource_ref(resource),
|
|
|
|
|
body.clone(),
|
|
|
|
|
time_info,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
Event::RoomMessage(
|
2023-12-30 13:09:01 +01:00
|
|
|
message.id.clone(),
|
|
|
|
|
from.to_bare(),
|
2024-12-19 17:20:11 +01:00
|
|
|
RoomNick::from_resource_ref(resource),
|
2023-12-30 13:09:01 +01:00
|
|
|
body.clone(),
|
2023-12-31 21:06:15 +01:00
|
|
|
time_info,
|
2024-12-19 17:20:11 +01:00
|
|
|
)
|
2023-12-30 13:09:01 +01:00
|
|
|
};
|
2024-12-19 17:20:11 +01:00
|
|
|
events.push(event);
|
|
|
|
|
} else {
|
|
|
|
|
// Service message
|
|
|
|
|
if correction.is_some() {
|
|
|
|
|
warn!("Found correction in service message:\n{:#?}", message);
|
|
|
|
|
} else {
|
|
|
|
|
let event = Event::ServiceMessage(message.id.clone(), from.to_bare(), body, time_info);
|
|
|
|
|
events.push(event);
|
|
|
|
|
}
|
2023-12-30 13:09:01 +01:00
|
|
|
}
|
|
|
|
|
}
|