xmpp-parsers: Use xso for pubsub events
Also split the PubSubEvent enum into Event and Payload.
This commit is contained in:
parent
543d9eb590
commit
fa61f421bb
7 changed files with 156 additions and 252 deletions
|
|
@ -62,7 +62,7 @@ impl Conference {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::pubsub::{pubsub::Item as PubSubItem, PubSubEvent};
|
||||
use crate::pubsub::{self, pubsub::Item as PubSubItem};
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
|
|
@ -132,10 +132,16 @@ mod tests {
|
|||
assert_eq!(conference.clone().password.unwrap(), "secret");
|
||||
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='urn:xmpp:bookmarks:1'><item xmlns='http://jabber.org/protocol/pubsub#event' id='test-muc@muc.localhost'><conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></item></items></event>".parse().unwrap();
|
||||
let mut items = match PubSubEvent::try_from(elem) {
|
||||
Ok(PubSubEvent::PublishedItems { node, items }) => {
|
||||
let event = pubsub::Event::try_from(elem).unwrap();
|
||||
let mut items = match event.payload {
|
||||
pubsub::event::Payload::Items {
|
||||
node,
|
||||
published,
|
||||
retracted,
|
||||
} => {
|
||||
assert_eq!(&node.0, ns::BOOKMARKS2);
|
||||
items
|
||||
assert_eq!(retracted.len(), 0);
|
||||
published
|
||||
}
|
||||
_ => panic!(),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -582,7 +582,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_extract_payload() {
|
||||
use super::super::attention::Attention;
|
||||
use super::super::pubsub::event::PubSubEvent;
|
||||
use super::super::pubsub;
|
||||
|
||||
#[cfg(not(feature = "component"))]
|
||||
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
||||
|
|
@ -590,7 +590,7 @@ mod tests {
|
|||
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
||||
let mut message = Message::try_from(elem).unwrap();
|
||||
assert_eq!(message.payloads.len(), 1);
|
||||
match message.extract_payload::<PubSubEvent>() {
|
||||
match message.extract_payload::<pubsub::Event>() {
|
||||
Ok(None) => (),
|
||||
other => panic!("unexpected result: {:?}", other),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ use crate::ns;
|
|||
use crate::pubsub::{ItemId, NodeName, Subscription, SubscriptionId};
|
||||
use jid::Jid;
|
||||
use minidom::Element;
|
||||
use xso::error::{Error, FromElementError};
|
||||
|
||||
/// An event item from a PubSub node.
|
||||
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||
|
|
@ -33,231 +32,103 @@ pub struct Item {
|
|||
}
|
||||
|
||||
/// Represents an event happening to a PubSub node.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum PubSubEvent {
|
||||
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||
#[xml(namespace = ns::PUBSUB_EVENT, name = "event")]
|
||||
pub struct Event {
|
||||
/// The inner child of this event.
|
||||
#[xml(child)]
|
||||
pub payload: Payload,
|
||||
}
|
||||
|
||||
impl MessagePayload for Event {}
|
||||
|
||||
/// Represents an event happening to a PubSub node.
|
||||
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||
#[xml(namespace = ns::PUBSUB_EVENT, exhaustive)]
|
||||
pub enum Payload {
|
||||
/*
|
||||
Collection {
|
||||
},
|
||||
*/
|
||||
/// This node’s configuration changed.
|
||||
#[xml(name = "configuration")]
|
||||
Configuration {
|
||||
/// The node affected.
|
||||
#[xml(attribute)]
|
||||
node: NodeName,
|
||||
|
||||
/// The new configuration of this node.
|
||||
#[xml(child(default))]
|
||||
form: Option<DataForm>,
|
||||
},
|
||||
|
||||
/// This node has been deleted, with an optional redirect to another node.
|
||||
#[xml(name = "delete")]
|
||||
Delete {
|
||||
/// The node affected.
|
||||
#[xml(attribute)]
|
||||
node: NodeName,
|
||||
|
||||
/// The xmpp: URI of another node replacing this one.
|
||||
#[xml(extract(default, fields(attribute(default, name = "uri"))))]
|
||||
redirect: Option<String>,
|
||||
},
|
||||
|
||||
/// Some items have been published on this node.
|
||||
PublishedItems {
|
||||
/// Some items have been published or retracted on this node.
|
||||
#[xml(name = "items")]
|
||||
Items {
|
||||
/// The node affected.
|
||||
#[xml(attribute)]
|
||||
node: NodeName,
|
||||
|
||||
/// The list of published items.
|
||||
items: Vec<Item>,
|
||||
},
|
||||
|
||||
/// Some items have been removed from this node.
|
||||
RetractedItems {
|
||||
/// The node affected.
|
||||
node: NodeName,
|
||||
#[xml(child(n = ..))]
|
||||
published: Vec<Item>,
|
||||
|
||||
/// The list of retracted items.
|
||||
items: Vec<ItemId>,
|
||||
#[xml(extract(n = .., name = "retract", fields(attribute(name = "id", type_ = ItemId))))]
|
||||
retracted: Vec<ItemId>,
|
||||
},
|
||||
|
||||
/// All items of this node just got removed at once.
|
||||
#[xml(name = "purge")]
|
||||
Purge {
|
||||
/// The node affected.
|
||||
#[xml(attribute)]
|
||||
node: NodeName,
|
||||
},
|
||||
|
||||
/// The user’s subscription to this node has changed.
|
||||
#[xml(name = "subscription")]
|
||||
Subscription {
|
||||
/// The node affected.
|
||||
#[xml(attribute)]
|
||||
node: NodeName,
|
||||
|
||||
/// The time at which this subscription will expire.
|
||||
#[xml(attribute(default))]
|
||||
expiry: Option<DateTime>,
|
||||
|
||||
/// The JID of the user affected.
|
||||
#[xml(attribute(default))]
|
||||
jid: Option<Jid>,
|
||||
|
||||
/// An identifier for this subscription.
|
||||
#[xml(attribute(default))]
|
||||
subid: Option<SubscriptionId>,
|
||||
|
||||
/// The state of this subscription.
|
||||
#[xml(attribute(default))]
|
||||
subscription: Option<Subscription>,
|
||||
},
|
||||
}
|
||||
|
||||
fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
|
||||
let mut is_retract = None;
|
||||
let mut items = vec![];
|
||||
let mut retracts = vec![];
|
||||
for child in elem.children() {
|
||||
if child.is("item", ns::PUBSUB_EVENT) {
|
||||
match is_retract {
|
||||
None => is_retract = Some(false),
|
||||
Some(false) => (),
|
||||
Some(true) => {
|
||||
return Err(Error::Other("Mix of item and retract in items element."));
|
||||
}
|
||||
}
|
||||
items.push(Item::try_from(child.clone())?);
|
||||
} else if child.is("retract", ns::PUBSUB_EVENT) {
|
||||
match is_retract {
|
||||
None => is_retract = Some(true),
|
||||
Some(true) => (),
|
||||
Some(false) => {
|
||||
return Err(Error::Other("Mix of item and retract in items element."));
|
||||
}
|
||||
}
|
||||
check_no_children!(child, "retract");
|
||||
check_no_unknown_attributes!(child, "retract", ["id"]);
|
||||
let id = get_attr!(child, "id", Required);
|
||||
retracts.push(id);
|
||||
} else {
|
||||
return Err(Error::Other("Invalid child in items element."));
|
||||
}
|
||||
}
|
||||
Ok(match is_retract {
|
||||
Some(false) => PubSubEvent::PublishedItems { node, items },
|
||||
Some(true) => PubSubEvent::RetractedItems {
|
||||
node,
|
||||
items: retracts,
|
||||
},
|
||||
None => return Err(Error::Other("Missing children in items element.")),
|
||||
})
|
||||
}
|
||||
|
||||
impl TryFrom<Element> for PubSubEvent {
|
||||
type Error = FromElementError;
|
||||
|
||||
fn try_from(elem: Element) -> Result<PubSubEvent, FromElementError> {
|
||||
check_self!(elem, "event", PUBSUB_EVENT);
|
||||
check_no_attributes!(elem, "event");
|
||||
|
||||
let mut payload = None;
|
||||
for child in elem.children() {
|
||||
let node = get_attr!(child, "node", Required);
|
||||
if child.is("configuration", ns::PUBSUB_EVENT) {
|
||||
let mut payloads = child.children().cloned().collect::<Vec<_>>();
|
||||
let item = payloads.pop();
|
||||
if !payloads.is_empty() {
|
||||
return Err(Error::Other(
|
||||
"More than a single payload in configuration element.",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let form = match item {
|
||||
None => None,
|
||||
Some(payload) => Some(DataForm::try_from(payload)?),
|
||||
};
|
||||
payload = Some(PubSubEvent::Configuration { node, form });
|
||||
} else if child.is("delete", ns::PUBSUB_EVENT) {
|
||||
let mut redirect = None;
|
||||
for item in child.children() {
|
||||
if item.is("redirect", ns::PUBSUB_EVENT) {
|
||||
if redirect.is_some() {
|
||||
return Err(
|
||||
Error::Other("More than one redirect in delete element.").into()
|
||||
);
|
||||
}
|
||||
let uri = get_attr!(item, "uri", Required);
|
||||
redirect = Some(uri);
|
||||
} else {
|
||||
return Err(Error::Other("Unknown child in delete element.").into());
|
||||
}
|
||||
}
|
||||
payload = Some(PubSubEvent::Delete { node, redirect });
|
||||
} else if child.is("items", ns::PUBSUB_EVENT) {
|
||||
payload = Some(parse_items(child.clone(), node)?);
|
||||
} else if child.is("purge", ns::PUBSUB_EVENT) {
|
||||
check_no_children!(child, "purge");
|
||||
payload = Some(PubSubEvent::Purge { node });
|
||||
} else if child.is("subscription", ns::PUBSUB_EVENT) {
|
||||
check_no_children!(child, "subscription");
|
||||
payload = Some(PubSubEvent::Subscription {
|
||||
node,
|
||||
expiry: get_attr!(child, "expiry", Option),
|
||||
jid: get_attr!(child, "jid", Option),
|
||||
subid: get_attr!(child, "subid", Option),
|
||||
subscription: get_attr!(child, "subscription", Option),
|
||||
});
|
||||
} else {
|
||||
return Err(Error::Other("Unknown child in event element.").into());
|
||||
}
|
||||
}
|
||||
payload.ok_or(Error::Other("No payload in event element.").into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PubSubEvent> for Element {
|
||||
fn from(event: PubSubEvent) -> Element {
|
||||
let payload = match event {
|
||||
PubSubEvent::Configuration { node, form } => {
|
||||
Element::builder("configuration", ns::PUBSUB_EVENT)
|
||||
.attr("node", node)
|
||||
.append_all(form.map(Element::from))
|
||||
}
|
||||
PubSubEvent::Delete { node, redirect } => Element::builder("purge", ns::PUBSUB_EVENT)
|
||||
.attr("node", node)
|
||||
.append_all(redirect.map(|redirect| {
|
||||
Element::builder("redirect", ns::PUBSUB_EVENT).attr("uri", redirect)
|
||||
})),
|
||||
PubSubEvent::PublishedItems { node, items } => {
|
||||
Element::builder("items", ns::PUBSUB_EVENT)
|
||||
.attr("node", node)
|
||||
.append_all(items)
|
||||
}
|
||||
PubSubEvent::RetractedItems { node, items } => {
|
||||
Element::builder("items", ns::PUBSUB_EVENT)
|
||||
.attr("node", node)
|
||||
.append_all(
|
||||
items
|
||||
.into_iter()
|
||||
.map(|id| Element::builder("retract", ns::PUBSUB_EVENT).attr("id", id)),
|
||||
)
|
||||
}
|
||||
PubSubEvent::Purge { node } => {
|
||||
Element::builder("purge", ns::PUBSUB_EVENT).attr("node", node)
|
||||
}
|
||||
PubSubEvent::Subscription {
|
||||
node,
|
||||
expiry,
|
||||
jid,
|
||||
subid,
|
||||
subscription,
|
||||
} => Element::builder("subscription", ns::PUBSUB_EVENT)
|
||||
.attr("node", node)
|
||||
.attr("expiry", expiry)
|
||||
.attr("jid", jid)
|
||||
.attr("subid", subid)
|
||||
.attr("subscription", subscription),
|
||||
};
|
||||
Element::builder("event", ns::PUBSUB_EVENT)
|
||||
.append(payload)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
impl PubSubEvent {
|
||||
impl Payload {
|
||||
/// Return the name of the node to which this event is related.
|
||||
pub fn node_name(&self) -> &NodeName {
|
||||
match self {
|
||||
Self::Purge { node, .. } => node,
|
||||
Self::PublishedItems { node, .. } => node,
|
||||
Self::RetractedItems { node, .. } => node,
|
||||
Self::Items { node, .. } => node,
|
||||
Self::Subscription { node, .. } => node,
|
||||
Self::Delete { node, .. } => node,
|
||||
Self::Configuration { node, .. } => node,
|
||||
|
|
@ -265,20 +136,21 @@ impl PubSubEvent {
|
|||
}
|
||||
}
|
||||
|
||||
impl MessagePayload for PubSubEvent {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use jid::BareJid;
|
||||
use xso::error::{Error, FromElementError};
|
||||
|
||||
// TODO: Reenable this test once we support asserting that a Vec isn’t empty.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn missing_items() {
|
||||
let elem: Element =
|
||||
"<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='coucou'/></event>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let error = PubSubEvent::try_from(elem).unwrap_err();
|
||||
let error = Event::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
FromElementError::Invalid(Error::Other(string)) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -289,16 +161,21 @@ mod tests {
|
|||
#[test]
|
||||
fn test_simple_items() {
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='coucou'><item id='test' publisher='test@coucou'/></items></event>".parse().unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::PublishedItems { node, items } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Items {
|
||||
node,
|
||||
published,
|
||||
retracted,
|
||||
} => {
|
||||
assert_eq!(node, NodeName(String::from("coucou")));
|
||||
assert_eq!(items[0].id, Some(ItemId(String::from("test"))));
|
||||
assert_eq!(retracted.len(), 0);
|
||||
assert_eq!(published[0].id, Some(ItemId(String::from("test"))));
|
||||
assert_eq!(
|
||||
items[0].publisher.clone().unwrap(),
|
||||
published[0].publisher.clone().unwrap(),
|
||||
BareJid::new("test@coucou").unwrap()
|
||||
);
|
||||
assert_eq!(items[0].payload, None);
|
||||
assert_eq!(published[0].payload, None);
|
||||
}
|
||||
_ => panic!(),
|
||||
}
|
||||
|
|
@ -307,13 +184,18 @@ mod tests {
|
|||
#[test]
|
||||
fn test_simple_pep() {
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='something'><item><foreign xmlns='example:namespace'/></item></items></event>".parse().unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::PublishedItems { node, items } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Items {
|
||||
node,
|
||||
published,
|
||||
retracted,
|
||||
} => {
|
||||
assert_eq!(node, NodeName(String::from("something")));
|
||||
assert_eq!(items[0].id, None);
|
||||
assert_eq!(items[0].publisher, None);
|
||||
match items[0].payload {
|
||||
assert_eq!(retracted.len(), 0);
|
||||
assert_eq!(published[0].id, None);
|
||||
assert_eq!(published[0].publisher, None);
|
||||
match published[0].payload {
|
||||
Some(ref elem) => assert!(elem.is("foreign", "example:namespace")),
|
||||
_ => panic!(),
|
||||
}
|
||||
|
|
@ -325,12 +207,17 @@ mod tests {
|
|||
#[test]
|
||||
fn test_simple_retract() {
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='something'><retract id='coucou'/><retract id='test'/></items></event>".parse().unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::RetractedItems { node, items } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Items {
|
||||
node,
|
||||
published,
|
||||
retracted,
|
||||
} => {
|
||||
assert_eq!(node, NodeName(String::from("something")));
|
||||
assert_eq!(items[0], ItemId(String::from("coucou")));
|
||||
assert_eq!(items[1], ItemId(String::from("test")));
|
||||
assert_eq!(published.len(), 0);
|
||||
assert_eq!(retracted[0], ItemId(String::from("coucou")));
|
||||
assert_eq!(retracted[1], ItemId(String::from("test")));
|
||||
}
|
||||
_ => panic!(),
|
||||
}
|
||||
|
|
@ -339,9 +226,9 @@ mod tests {
|
|||
#[test]
|
||||
fn test_simple_delete() {
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><delete node='coucou'><redirect uri='hello'/></delete></event>".parse().unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::Delete { node, redirect } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Delete { node, redirect } => {
|
||||
assert_eq!(node, NodeName(String::from("coucou")));
|
||||
assert_eq!(redirect, Some(String::from("hello")));
|
||||
}
|
||||
|
|
@ -355,9 +242,9 @@ mod tests {
|
|||
"<event xmlns='http://jabber.org/protocol/pubsub#event'><purge node='coucou'/></event>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::Purge { node } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Purge { node } => {
|
||||
assert_eq!(node, NodeName(String::from("coucou")));
|
||||
}
|
||||
_ => panic!(),
|
||||
|
|
@ -367,9 +254,9 @@ mod tests {
|
|||
#[test]
|
||||
fn test_simple_configure() {
|
||||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><configuration node='coucou'><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/pubsub#node_config</value></field></x></configuration></event>".parse().unwrap();
|
||||
let event = PubSubEvent::try_from(elem).unwrap();
|
||||
match event {
|
||||
PubSubEvent::Configuration { node, form: _ } => {
|
||||
let event = Event::try_from(elem).unwrap();
|
||||
match event.payload {
|
||||
Payload::Configuration { node, form: _ } => {
|
||||
assert_eq!(node, NodeName(String::from("coucou")));
|
||||
//assert_eq!(form.type_, Result_);
|
||||
}
|
||||
|
|
@ -383,12 +270,12 @@ mod tests {
|
|||
"<event xmlns='http://jabber.org/protocol/pubsub#event'><coucou node='test'/></event>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let error = PubSubEvent::try_from(elem).unwrap_err();
|
||||
let error = Event::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
FromElementError::Invalid(Error::Other(string)) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Unknown child in event element.");
|
||||
assert_eq!(message, "This is not a Payload element.");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "disable-validation"))]
|
||||
|
|
@ -397,12 +284,12 @@ mod tests {
|
|||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event' coucou=''/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let error = PubSubEvent::try_from(elem).unwrap_err();
|
||||
let error = Event::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
FromElementError::Invalid(Error::Other(string)) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Unknown attribute in event element.");
|
||||
assert_eq!(message, "Unknown attribute in Event element.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -410,9 +297,9 @@ mod tests {
|
|||
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><subscription expiry='2006-02-28T23:59:59+00:00' jid='francisco@denmark.lit' node='princely_musings' subid='ba49252aaa4f5d320c24d3766f0bdcade78c78d3' subscription='subscribed'/></event>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let event = PubSubEvent::try_from(elem.clone()).unwrap();
|
||||
match event.clone() {
|
||||
PubSubEvent::Subscription {
|
||||
let event = Event::try_from(elem.clone()).unwrap();
|
||||
match event.payload.clone() {
|
||||
Payload::Subscription {
|
||||
node,
|
||||
expiry,
|
||||
jid,
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ pub mod owner;
|
|||
#[allow(clippy::module_inception)]
|
||||
pub mod pubsub;
|
||||
|
||||
pub use self::event::PubSubEvent;
|
||||
pub use self::event::Event;
|
||||
pub use self::owner::PubSubOwner;
|
||||
pub use self::pubsub::PubSub;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue