1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7/// The `http://jabber.org/protocol/pubsub#event` protocol.
8pub mod event;
9
10/// The `http://jabber.org/protocol/pubsub` protocol.
11pub mod pubsub;
12
13pub use self::event::PubSubEvent;
14pub use self::pubsub::PubSub;
15
16use crate::{Jid, Element};
17
18generate_id!(
19 /// The name of a PubSub node, used to identify it on a JID.
20 NodeName
21);
22
23generate_id!(
24 /// The identifier of an item, which is unique per node.
25 ItemId
26);
27
28generate_id!(
29 /// The identifier of a subscription to a PubSub node.
30 SubscriptionId
31);
32
33generate_attribute!(
34 /// The state of a subscription to a node.
35 Subscription, "subscription", {
36 /// The user is not subscribed to this node.
37 None => "none",
38
39 /// The user’s subscription to this node is still pending.
40 Pending => "pending",
41
42 /// The user is subscribed to this node.
43 Subscribed => "subscribed",
44
45 /// The user’s subscription to this node will only be valid once
46 /// configured.
47 Unconfigured => "unconfigured",
48 }, Default = None
49);
50
51/// An item from a PubSub node.
52#[derive(Debug, Clone)]
53pub struct Item {
54 /// The identifier for this item, unique per node.
55 pub id: Option<ItemId>,
56
57 /// The JID of the entity who published this item.
58 pub publisher: Option<Jid>,
59
60 /// The payload of this item, in an arbitrary namespace.
61 pub payload: Option<Element>,
62}
63
64impl Item {
65 /// Create a new item, accepting only payloads implementing `PubSubPayload`.
66 pub fn new<P: PubSubPayload>(id: Option<ItemId>, publisher: Option<Jid>, payload: Option<P>) -> Item {
67 Item {
68 id,
69 publisher,
70 payload: payload.map(Into::into),
71 }
72 }
73}
74
75/// This trait should be implemented on any element which can be included as a PubSub payload.
76pub trait PubSubPayload: ::std::convert::TryFrom<crate::Element> + Into<crate::Element> {}