mod.rs

  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#owner` protocol.
 11pub mod owner;
 12
 13/// The `http://jabber.org/protocol/pubsub` protocol.
 14pub mod pubsub;
 15
 16pub use self::event::PubSubEvent;
 17pub use self::owner::PubSubOwner;
 18pub use self::pubsub::PubSub;
 19
 20use crate::{Element, Jid};
 21
 22generate_id!(
 23    /// The name of a PubSub node, used to identify it on a JID.
 24    NodeName
 25);
 26
 27generate_id!(
 28    /// The identifier of an item, which is unique per node.
 29    ItemId
 30);
 31
 32generate_id!(
 33    /// The identifier of a subscription to a PubSub node.
 34    SubscriptionId
 35);
 36
 37generate_attribute!(
 38    /// The state of a subscription to a node.
 39    Subscription, "subscription", {
 40        /// The user is not subscribed to this node.
 41        None => "none",
 42
 43        /// The user’s subscription to this node is still pending.
 44        Pending => "pending",
 45
 46        /// The user is subscribed to this node.
 47        Subscribed => "subscribed",
 48
 49        /// The user’s subscription to this node will only be valid once
 50        /// configured.
 51        Unconfigured => "unconfigured",
 52    }, Default = None
 53);
 54
 55generate_attribute!(
 56    /// A list of possible affiliations to a node.
 57    AffiliationAttribute, "affiliation", {
 58        /// You are a member of this node, you can subscribe and retrieve items.
 59        Member => "member",
 60
 61        /// You don’t have a specific affiliation with this node, you can only subscribe to it.
 62        None => "none",
 63
 64        /// You are banned from this node.
 65        Outcast => "outcast",
 66
 67        /// You are an owner of this node, and can do anything with it.
 68        Owner => "owner",
 69
 70        /// You are a publisher on this node, you can publish and retract items to it.
 71        Publisher => "publisher",
 72
 73        /// You can publish and retract items on this node, but not subscribe or retrieve items.
 74        PublishOnly => "publish-only",
 75    }
 76);
 77
 78/// An item from a PubSub node.
 79#[derive(Debug, Clone, PartialEq)]
 80pub struct Item {
 81    /// The identifier for this item, unique per node.
 82    pub id: Option<ItemId>,
 83
 84    /// The JID of the entity who published this item.
 85    pub publisher: Option<Jid>,
 86
 87    /// The payload of this item, in an arbitrary namespace.
 88    pub payload: Option<Element>,
 89}
 90
 91impl Item {
 92    /// Create a new item, accepting only payloads implementing `PubSubPayload`.
 93    pub fn new<P: PubSubPayload>(
 94        id: Option<ItemId>,
 95        publisher: Option<Jid>,
 96        payload: Option<P>,
 97    ) -> Item {
 98        Item {
 99            id,
100            publisher,
101            payload: payload.map(Into::into),
102        }
103    }
104}
105
106/// This trait should be implemented on any element which can be included as a PubSub payload.
107pub trait PubSubPayload: ::std::convert::TryFrom<crate::Element> + Into<crate::Element> {}