xmpp: Agent::send_room_private_message takes RoomPrivateMessageSettings
This commit is contained in:
parent
897c2abe0b
commit
d98ed615a5
3 changed files with 45 additions and 20 deletions
|
|
@ -11,6 +11,7 @@ XXXX-YY-ZZ [ RELEASER <admin@localhost> ]
|
|||
- Event::RoomMessage, Event::RoomPrivateMessage, and Event::RoomSubject now
|
||||
use RoomNick as sender nickname; previously RoomNick was an alias for String
|
||||
now it's a newtype wrapper around ResourcePart (!485)
|
||||
- Agent::send_room_private_message now takes RoomPrivateMessageSettings (!487)
|
||||
* Added:
|
||||
- Agent::send_room_message takes RoomMessageSettings argument (!483)
|
||||
* Fixes:
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ pub use tokio_xmpp::{
|
|||
Client as TokioXmppClient,
|
||||
};
|
||||
|
||||
use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
|
||||
use crate::{event_loop, message, muc, upload, Error, Event};
|
||||
|
||||
pub struct Agent {
|
||||
pub(crate) client: TokioXmppClient,
|
||||
|
|
@ -63,14 +63,11 @@ impl Agent {
|
|||
muc::room::send_room_message(self, settings).await
|
||||
}
|
||||
|
||||
pub async fn send_room_private_message(
|
||||
pub async fn send_room_private_message<'a>(
|
||||
&mut self,
|
||||
room: BareJid,
|
||||
recipient: RoomNick,
|
||||
lang: &str,
|
||||
text: &str,
|
||||
settings: muc::private_message::RoomPrivateMessageSettings<'a>,
|
||||
) {
|
||||
muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
|
||||
muc::private_message::send_room_private_message(self, settings).await
|
||||
}
|
||||
|
||||
/// Wait for new events, or Error::Disconnected when connection is closed and will not reconnect.
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue