diff --git a/parsers/ChangeLog b/parsers/ChangeLog index 9908c7234ce40c99f46e76b69725733689bcc316..91712bdd67eb029eae4da61f35c434518b414783 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -36,6 +36,9 @@ XXXX-YY-ZZ RELEASER fast::Tls0Rtt, legacy_omemo::IsPreKey, mam::Complete, sm::ResumeAttr (!476) - bookmarks::Conference and bookmarks2::Conference use ResourcePart to store the optional nickname instead of a String (!485) + - message::Message id field is now Option instead of + Option (!504) + - message_correction::Replace id field is now Id instead of String (!504) * New parsers/serialisers: - Stream Features (RFC 6120) (!400) - Spam Reporting (XEP-0377) (!506) @@ -50,10 +53,10 @@ XXXX-YY-ZZ RELEASER - Keep unsupported vCard elements as `minidom::Element`, so that they get serialized back instead of being dropped. We now also test for the size of these elements (!472). - - Add Message::extract_valid_payload method to warn in case of failure - and return simply Option instead of Result> (!497) - - Add Message::get_best_body_cloned and Message::get_best_subject_cloned - to clone automatically when performance is not an issue (!497) + - Add Message::extract_valid_payload method to warn in case of failure + and return simply Option instead of Result> (!497) + - Add Message::get_best_body_cloned and Message::get_best_subject_cloned + to clone automatically when performance is not an issue (!497) Version 0.21.0: 2024-07-25 Emmanuel Gil Peyrot diff --git a/parsers/src/message.rs b/parsers/src/message.rs index 32448adf2b2439cf2c3daecfe5b4e541ed61365b..cb377f30b4f6fd38a77e7b9906d97527fff810d9 100644 --- a/parsers/src/message.rs +++ b/parsers/src/message.rs @@ -36,6 +36,14 @@ generate_attribute!( type Lang = String; +generate_id!( + /// Id field in a [`Message`], if any. + /// + /// This field is not mandatory on incoming messages, but may be useful for moderation/correction, + /// especially for outgoing messages. + Id +); + generate_elem_id!( /// Represents one `` element, that is the free form text content of /// a message. @@ -70,7 +78,7 @@ pub struct Message { /// The @id attribute of this stanza, which is required in order to match a /// request with its response. - pub id: Option, + pub id: Option, /// The type of this message. pub type_: MessageType, diff --git a/parsers/src/message_correct.rs b/parsers/src/message_correct.rs index 200a00a850c83cafd530f7b15eb056c5a41f916e..f954579f14da546d821e868c9db5dbf1d2980e04 100644 --- a/parsers/src/message_correct.rs +++ b/parsers/src/message_correct.rs @@ -6,7 +6,7 @@ use xso::{AsXml, FromXml}; -use crate::message::MessagePayload; +use crate::message::{Id, MessagePayload}; use crate::ns; /// Defines that the message containing this payload should replace a @@ -16,7 +16,7 @@ use crate::ns; pub struct Replace { /// The 'id' attribute of the message getting corrected. #[xml(attribute)] - pub id: String, + pub id: Id, } impl MessagePayload for Replace {} @@ -98,7 +98,7 @@ mod tests { .parse() .unwrap(); let replace = Replace { - id: String::from("coucou"), + id: Id(String::from("coucou")), }; let elem2 = replace.into(); assert_eq!(elem, elem2); diff --git a/tokio-xmpp/src/event.rs b/tokio-xmpp/src/event.rs index 8206f47b193e1d1a127aff81e3c4ba457b7b137a..e789c26e18b93a3d76999981777b474e9b9f99eb 100644 --- a/tokio-xmpp/src/event.rs +++ b/tokio-xmpp/src/event.rs @@ -5,7 +5,12 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use rand::{thread_rng, Rng}; -use xmpp_parsers::{iq::Iq, jid::Jid, message::Message, presence::Presence}; +use xmpp_parsers::{ + iq::Iq, + jid::Jid, + message::{Id, Message}, + presence::Presence, +}; use xso::{AsXml, FromXml}; use crate::xmlstream::XmppStreamElement; @@ -43,7 +48,7 @@ impl Stanza { } &iq.id } - Self::Message(message) => message.id.get_or_insert_with(make_id), + Self::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(), Self::Presence(presence) => presence.id.get_or_insert_with(make_id), } } diff --git a/xmpp/ChangeLog b/xmpp/ChangeLog index 3205fffd4b63a6b5ab45a119d0d1c589deb91978..322f7808ad4499fad131a72e0f089450ea9e9b12 100644 --- a/xmpp/ChangeLog +++ b/xmpp/ChangeLog @@ -13,6 +13,8 @@ XXXX-YY-ZZ [ RELEASER ] 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) + - Event now exposes Option for incoming messages, and MessageId + for incoming message corrections; type alias Id has been removed (!504) * Added: - Agent::send_room_message takes RoomMessageSettings argument (!483) - Agent::send_raw_message takes RawMessageSettings for any message type (!487) diff --git a/xmpp/src/event.rs b/xmpp/src/event.rs index 76fdf77a37fe2f3fa4cd6c0c7d1d2ec03382130e..f6dc97a6dc9c199dd84b543051547e445b796822 100644 --- a/xmpp/src/event.rs +++ b/xmpp/src/event.rs @@ -9,7 +9,7 @@ use tokio_xmpp::jid::BareJid; use tokio_xmpp::jid::Jid; use tokio_xmpp::parsers::{message::Body, roster::Item as RosterItem}; -use crate::{delay::StanzaTimeInfo, Error, Id, RoomNick}; +use crate::{delay::StanzaTimeInfo, Error, MessageId, RoomNick}; #[derive(Debug)] pub enum Event { @@ -21,27 +21,27 @@ pub enum Event { #[cfg(feature = "avatars")] AvatarRetrieved(Jid, String), /// A chat message was received. It may have been delayed on the network. - /// - The [`Id`] is a unique identifier for this message. + /// - The [`MessageId`] is a unique identifier for this message. /// - The [`BareJid`] is the sender's JID. /// - The [`Body`] is the message body. /// - The [`StanzaTimeInfo`] about when message was received, and when the message was claimed sent. - ChatMessage(Id, BareJid, Body, StanzaTimeInfo), + ChatMessage(Option, BareJid, Body, StanzaTimeInfo), /// A message in a one-to-one chat was corrected/edited. - /// - The [`Id`] is the ID of the message that was corrected (always Some) + /// - The [`MessageId`] is the ID of the message that was corrected. /// - The [`BareJid`] is the JID of the other participant in the chat. /// - The [`Body`] is the new body of the message, to replace the old one. /// - The [`StanzaTimeInfo`] is the time the message correction was sent/received - ChatMessageCorrection(Id, BareJid, Body, StanzaTimeInfo), + ChatMessageCorrection(MessageId, BareJid, Body, StanzaTimeInfo), RoomJoined(BareJid), RoomLeft(BareJid), - RoomMessage(Id, BareJid, RoomNick, Body, StanzaTimeInfo), + RoomMessage(Option, BareJid, RoomNick, Body, StanzaTimeInfo), /// A message in a MUC was corrected/edited. - /// - The [`Id`] is the ID of the message that was corrected (always Some) + /// - The [`MessageId`] is the ID of the message that was corrected. /// - The [`BareJid`] is the JID of the room where the message was sent. /// - The [`RoomNick`] is the nickname of the sender of the message. /// - The [`Body`] is the new body of the message, to replace the old one. /// - The [`StanzaTimeInfo`] is the time the message correction was sent/received - RoomMessageCorrection(Id, BareJid, RoomNick, Body, StanzaTimeInfo), + RoomMessageCorrection(MessageId, BareJid, RoomNick, Body, StanzaTimeInfo), /// The subject of a room was received. /// - The BareJid is the room's address. /// - The RoomNick is the nickname of the room member who set the subject. @@ -49,14 +49,14 @@ pub enum Event { RoomSubject(BareJid, Option, String, StanzaTimeInfo), /// A private message received from a room, containing the message ID, the room's BareJid, /// the sender's nickname, and the message body. - RoomPrivateMessage(Id, BareJid, RoomNick, Body, StanzaTimeInfo), + RoomPrivateMessage(Option, BareJid, RoomNick, Body, StanzaTimeInfo), /// A private message in a MUC was corrected/edited. - /// - The [`Id`] is the ID of the message that was corrected (always Some) + /// - The [`MessageId`] is the ID of the message that was corrected. /// - The [`BareJid`] is the JID of the room where the message was sent. /// - The [`RoomNick`] is the nickname of the sender of the message. /// - The [`Body`] is the new body of the message, to replace the old one. /// - The [`StanzaTimeInfo`] is the time the message correction was sent/received - RoomPrivateMessageCorrection(Id, BareJid, RoomNick, Body, StanzaTimeInfo), - ServiceMessage(Id, BareJid, Body, StanzaTimeInfo), + RoomPrivateMessageCorrection(MessageId, BareJid, RoomNick, Body, StanzaTimeInfo), + ServiceMessage(Option, BareJid, Body, StanzaTimeInfo), HttpUploadedFile(String), } diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index d3d76f1c265765d805e7670132a79ebe1f892eb2..358c83e1f569e33c3928d7a4a486cb97b0d1eb03 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -19,6 +19,7 @@ extern crate log; use core::fmt; use jid::{ResourcePart, ResourceRef}; +use parsers::message::Id as MessageId; pub mod agent; pub mod builder; @@ -40,7 +41,6 @@ pub use event::Event; pub use feature::ClientFeature; pub type Error = tokio_xmpp::Error; -pub type Id = Option; /// Nickname for a person in a chatroom. /// diff --git a/xmpp/src/message/receive/chat.rs b/xmpp/src/message/receive/chat.rs index 5baed2932745260acb42c6ba41c23557643aba9b..5614d62e0bd9c634e2b320af50d961c9cd56f9d1 100644 --- a/xmpp/src/message/receive/chat.rs +++ b/xmpp/src/message/receive/chat.rs @@ -36,7 +36,7 @@ pub async fn handle_message_chat( let event = if let Some(correction) = correction { Event::RoomPrivateMessageCorrection( - Some(correction.id), + correction.id, full_from.to_bare(), RoomNick::from_resource_ref(full_from.resource()), body.clone(), @@ -56,14 +56,9 @@ pub async fn handle_message_chat( } else { let event = if let Some(correction) = correction { // TODO: Check that correction is valid (only for last N minutes or last N messages) - Event::ChatMessageCorrection( - Some(correction.id), - from.to_bare(), - body.clone(), - time_info, - ) + Event::ChatMessageCorrection(correction.id, from.to_bare(), body.clone(), time_info) } else { - Event::ChatMessage(message.id.clone(), from.to_bare(), body.clone(), time_info) + Event::ChatMessage(message.id.clone(), from.to_bare(), body, time_info) }; events.push(event); } diff --git a/xmpp/src/message/receive/group_chat.rs b/xmpp/src/message/receive/group_chat.rs index 3246668f458d60801bc8e316cd5bd0977c7816f2..8ea22a32e7ae13299339b82feb70ffc76dac94c1 100644 --- a/xmpp/src/message/receive/group_chat.rs +++ b/xmpp/src/message/receive/group_chat.rs @@ -59,7 +59,7 @@ pub async fn handle_message_group_chat( let event = if let Some(correction) = correction { Event::RoomMessageCorrection( - Some(correction.id), + correction.id, from.to_bare(), RoomNick::from_resource_ref(resource), body.clone(),