use jid::Jid;
use crate::core::{FromXml, IntoXml, TryExtend};
use crate::data_forms::DataForm;
use crate::date::DateTime;
use crate::message::MessagePayload;
use crate::ns::PUBSUB_EVENT;
use crate::pubsub::{ItemId, NodeName, PubSubPayload, Subscription, SubscriptionId};
use crate::util::error::Error;
use crate::Element;
#[derive(FromXml, IntoXml, Debug, PartialEq, Clone)]
#[xml(namespace = PUBSUB_EVENT, name = "item")]
pub struct Item {
#[xml(attribute)]
pub id: Option<ItemId>,
#[xml(attribute)]
pub publisher: Option<Jid>,
#[xml(element(default))]
pub payload: Option<Element>,
}
impl Item {
pub fn new<P: PubSubPayload>(
id: Option<ItemId>,
publisher: Option<Jid>,
payload: Option<P>,
) -> Item {
Item {
id,
publisher,
payload: payload.map(Into::into),
}
}
}
impl From<super::pubsub::Item> for Item {
fn from(other: super::pubsub::Item) -> Self {
Self {
id: other.id,
publisher: other.publisher,
payload: other.payload,
}
}
}
fn validate_event(ev: &mut PubSubEvent) -> Result<(), Error> {
match ev {
PubSubEvent::Items { ref items, .. } => {
if items.is_empty() {
return Err(Error::ParseError("Missing children in items element."));
}
}
_ => (),
}
Ok(())
}
#[derive(FromXml, IntoXml, Debug, PartialEq, Clone)]
pub enum PubSubEventItem {
#[xml(transparent)]
Published(Item),
#[xml(namespace = PUBSUB_EVENT, name = "retract")]
Retracted {
#[xml(attribute)]
id: ItemId,
},
}
#[derive(Debug, PartialEq, Clone)]
pub enum PubSubEventItems {
Published(Vec<Item>),
Retracted(Vec<ItemId>),
}
pub enum PubSubEventItemsIntoIter {
Published(std::vec::IntoIter<Item>),
Retracted(std::vec::IntoIter<ItemId>),
}
impl Iterator for PubSubEventItemsIntoIter {
type Item = PubSubEventItem;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Published(iter) => Some(PubSubEventItem::Published(iter.next()?)),
Self::Retracted(iter) => Some(PubSubEventItem::Retracted { id: iter.next()? }),
}
}
}
impl Default for PubSubEventItems {
fn default() -> Self {
Self::Published(Vec::new())
}
}
impl PubSubEventItems {
fn is_empty(&self) -> bool {
match self {
Self::Published(v) => v.len() == 0,
Self::Retracted(v) => v.len() == 0,
}
}
fn append_one(&mut self, item: PubSubEventItem) -> Result<(), Error> {
if self.is_empty() {
match item {
PubSubEventItem::Published(item) => *self = Self::Published(vec![item]),
PubSubEventItem::Retracted { id } => *self = Self::Retracted(vec![id]),
};
return Ok(());
}
match self {
Self::Published(ref mut items) => match item {
PubSubEventItem::Published(item) => {
items.push(item);
Ok(())
}
_ => Err(Error::ParseError(
"PubSubEvent::Items::items cannot mix <item/> and <retract/>",
)),
},
Self::Retracted(ref mut items) => match item {
PubSubEventItem::Retracted { id } => {
items.push(id);
Ok(())
}
_ => Err(Error::ParseError(
"PubSubEvent::Items::items cannot mix <item/> and <retract/>",
)),
},
}
}
}
impl IntoIterator for PubSubEventItems {
type Item = PubSubEventItem;
type IntoIter = PubSubEventItemsIntoIter;
fn into_iter(self) -> Self::IntoIter {
match self {
Self::Published(vec) => Self::IntoIter::Published(vec.into_iter()),
Self::Retracted(vec) => Self::IntoIter::Retracted(vec.into_iter()),
}
}
}
impl TryExtend<PubSubEventItem> for PubSubEventItems {
fn try_extend<T: IntoIterator<Item = PubSubEventItem>>(
&mut self,
iter: T,
) -> Result<(), Error> {
for item in iter {
self.append_one(item)?;
}
Ok(())
}
}
#[derive(FromXml, IntoXml, Debug, PartialEq, Clone)]
#[xml(namespace = PUBSUB_EVENT, validate = validate_event, wrapped_with(namespace = PUBSUB_EVENT, name = "event"))]
pub enum PubSubEvent {
#[xml(name = "configuration")]
Configuration {
#[xml(attribute)]
node: NodeName,
#[xml(child(default))]
form: Option<DataForm>,
},
#[xml(name = "delete")]
Delete {
#[xml(attribute)]
node: NodeName,
#[xml(child(namespace = PUBSUB_EVENT, name = "redirect", extract(attribute = "uri"), default))]
redirect: Option<String>,
},
#[xml(name = "items")]
Items {
#[xml(attribute)]
node: NodeName,
#[xml(children)]
items: PubSubEventItems,
},
#[xml(name = "purge")]
Purge {
#[xml(attribute)]
node: NodeName,
},
#[xml(name = "subscription")]
Subscription {
#[xml(attribute)]
node: NodeName,
#[xml(attribute)]
expiry: Option<DateTime>,
#[xml(attribute)]
jid: Option<Jid>,
#[xml(attribute)]
subid: Option<SubscriptionId>,
#[xml(attribute)]
subscription: Option<Subscription>,
},
}
impl MessagePayload for PubSubEvent {}
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
#[test]
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 message = match error {
Error::ParseError(string) => string,
other => panic!("unexpected result: {:?}", other),
};
assert_eq!(message, "Missing children in items element.");
}
#[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::Items {
node,
items: PubSubEventItems::Published(items),
} => {
assert_eq!(node, NodeName(String::from("coucou")));
assert_eq!(items[0].id, Some(ItemId(String::from("test"))));
assert_eq!(
items[0].publisher.clone().unwrap(),
BareJid::new("test@coucou").unwrap()
);
assert_eq!(items[0].payload, None);
}
other => panic!("unexpected event: {:?}", other),
}
}
#[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::Items {
node,
items: PubSubEventItems::Published(items),
} => {
assert_eq!(node, NodeName(String::from("something")));
assert_eq!(items[0].id, None);
assert_eq!(items[0].publisher, None);
match items[0].payload {
Some(ref elem) => assert!(elem.is("foreign", "example:namespace")),
_ => panic!(),
}
}
other => panic!("unexpected event: {:?}", other),
}
}
#[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::Items {
node,
items: PubSubEventItems::Retracted(items),
} => {
assert_eq!(node, NodeName(String::from("something")));
assert_eq!(items[0], ItemId(String::from("coucou")));
assert_eq!(items[1], ItemId(String::from("test")));
}
other => panic!("unexpected event: {:?}", other),
}
}
#[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 } => {
assert_eq!(node, NodeName(String::from("coucou")));
assert_eq!(redirect, Some(String::from("hello")));
}
other => panic!("unexpected event: {:?}", other),
}
}
#[test]
fn test_simple_purge() {
let elem: Element =
"<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 } => {
assert_eq!(node, NodeName(String::from("coucou")));
}
other => panic!("unexpected event: {:?}", other),
}
}
#[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: _ } => {
assert_eq!(node, NodeName(String::from("coucou")));
}
other => panic!("unexpected event: {:?}", other),
}
}
#[test]
fn test_invalid() {
let elem: Element =
"<event xmlns='http://jabber.org/protocol/pubsub#event'><coucou node='test'/></event>"
.parse()
.unwrap();
let error = PubSubEvent::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
other => panic!("unexpected result: {:?}", other),
};
assert_eq!(
message,
"Unknown child in wrapper element of PubSubEvent element."
);
}
#[cfg(not(feature = "disable-validation"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event' coucou=''/>"
.parse()
.unwrap();
let error = PubSubEvent::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(
message,
"Unknown attribute in wrapper element of PubSubEvent element."
);
}
#[test]
fn test_ex221_subscription() {
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 {
node,
expiry,
jid,
subid,
subscription,
} => {
assert_eq!(node, NodeName(String::from("princely_musings")));
assert_eq!(
subid,
Some(SubscriptionId(String::from(
"ba49252aaa4f5d320c24d3766f0bdcade78c78d3"
)))
);
assert_eq!(subscription, Some(Subscription::Subscribed));
assert_eq!(jid.unwrap(), BareJid::new("francisco@denmark.lit").unwrap());
assert_eq!(expiry, Some("2006-02-28T23:59:59Z".parse().unwrap()));
}
_ => panic!(),
}
let elem2: Element = event.into();
assert_eq!(elem, elem2);
}
}