xmpp: Agent::send_room_private_message takes RoomPrivateMessageSettings

This commit is contained in:
xmppftw xmppftw 2024-12-17 18:51:19 +01:00
commit d98ed615a5
3 changed files with 45 additions and 20 deletions

View file

@ -4,28 +4,55 @@
// 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/.
use tokio_xmpp::{
jid::{BareJid, Jid},
use crate::{
parsers::{
message::{Body, Message, MessageType},
muc::user::MucUser,
},
tokio_xmpp::jid::{BareJid, Jid},
Agent, RoomNick,
};
use crate::{Agent, RoomNick};
pub async fn send_room_private_message(
agent: &mut Agent,
room: BareJid,
recipient: RoomNick,
lang: &str,
text: &str,
) {
let recipient: Jid = room.with_resource(recipient.as_ref()).into();
let mut message = Message::new(recipient).with_payload(MucUser::new());
message.type_ = MessageType::Chat;
message
.bodies
.insert(String::from(lang), Body(String::from(text)));
let _ = agent.client.send_stanza(message.into()).await;
#[derive(Clone, Debug)]
pub struct RoomPrivateMessageSettings<'a> {
pub room: BareJid,
pub recipient: RoomNick,
pub message: &'a str,
pub lang: Option<&'a str>,
}
impl<'a> RoomPrivateMessageSettings<'a> {
pub fn new(room: BareJid, recipient: RoomNick, message: &'a str) -> Self {
Self {
room,
recipient,
message,
lang: None,
}
}
pub fn with_lang(mut self, lang: &'a str) -> Self {
self.lang = Some(lang);
self
}
}
pub async fn send_room_private_message<'a>(
agent: &mut Agent,
settings: RoomPrivateMessageSettings<'a>,
) {
let RoomPrivateMessageSettings {
room,
recipient,
message,
lang,
} = settings;
let recipient: Jid = room.with_resource(recipient.as_ref()).into();
let mut stanza = Message::new(recipient).with_payload(MucUser::new());
stanza.type_ = MessageType::Chat;
stanza
.bodies
.insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
agent.client.send_stanza(stanza.into()).await.unwrap();
}