zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::message::channel_permission::*;
use crate::{field_access, impl_default, impl_display, TopicsPolicy};

/// Represents the properties of a channel the user is subscribed to (as well as that user's personal per-channel settings).
///
/// **Changes**: Removed `email_address` field from the dictionary in Zulip 8.0 (feature level 226).  Removed `role` field from the dictionary in Zulip 6.0 (feature level 133).
///
/// Modified from `openapi-generator-cli` output:
///
/// ```shell,no_build,no_run
/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
/// ```
///
/// ```text
/// /*
/// * Zulip REST API
/// *
/// * Powerful open source group chat
/// *
/// * The version of the OpenAPI document: 1.0.0
/// *
/// * Generated by: https://openapi-generator.tech
/// */
/// ```
///
/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
///
/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Subscription {
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    rendered_description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    date_created: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    creator_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    invite_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    subscribers: Option<Vec<u64>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    partial_subscribers: Option<Vec<u64>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    desktop_notifications: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    email_notifications: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    wildcard_mentions_notify: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    push_notifications: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    audible_notifications: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pin_to_top: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_muted: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    in_home_view: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_announcement_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_web_public: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    color: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_post_policy: Option<StreamPostPolicy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    message_retention_days: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    history_public_to_subscribers: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    first_message_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    folder_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    topics_policy: Option<TopicsPolicy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_recently_active: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_weekly_traffic: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_add_subscribers_group: Option<CanAddSubscribersGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_remove_subscribers_group: Option<CanRemoveSubscribersGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_administer_channel_group: Option<CanAdministerChannelGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_delete_any_message_group: Option<CanDeleteAnyMessageGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_delete_own_message_group: Option<CanDeleteOwnMessageGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_move_messages_out_of_channel_group: Option<CanMoveMessagesOutOfChannelGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_move_messages_within_channel_group: Option<CanMoveMessagesWithinChannelGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_send_message_group: Option<CanSendMessageGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_subscribe_group: Option<CanSubscribeGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_resolve_topics_group: Option<CanResolveTopicsGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_archived: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    subscriber_count: Option<f64>,
}

impl Subscription {
    /// Creates a new [Subscription].
    pub const fn new() -> Self {
        Self {
            stream_id: None,
            name: None,
            description: None,
            rendered_description: None,
            date_created: None,
            creator_id: None,
            invite_only: None,
            subscribers: None,
            partial_subscribers: None,
            desktop_notifications: None,
            email_notifications: None,
            wildcard_mentions_notify: None,
            push_notifications: None,
            audible_notifications: None,
            pin_to_top: None,
            is_muted: None,
            in_home_view: None,
            is_announcement_only: None,
            is_web_public: None,
            color: None,
            stream_post_policy: None,
            message_retention_days: None,
            history_public_to_subscribers: None,
            first_message_id: None,
            folder_id: None,
            topics_policy: None,
            is_recently_active: None,
            stream_weekly_traffic: None,
            can_add_subscribers_group: None,
            can_remove_subscribers_group: None,
            can_administer_channel_group: None,
            can_delete_any_message_group: None,
            can_delete_own_message_group: None,
            can_move_messages_out_of_channel_group: None,
            can_move_messages_within_channel_group: None,
            can_send_message_group: None,
            can_subscribe_group: None,
            can_resolve_topics_group: None,
            is_archived: None,
            subscriber_count: None,
        }
    }
}

field_access! {
    Subscription {
        /// The unique ID of a channel.
        stream_id: option { u64 },
        /// The UNIX timestamp for when the channel was created, in UTC seconds.
        ///
        /// **Changes**: New in Zulip 4.0 (feature level 30).
        date_created: option { u64 },
        /// The ID of the user who created this channel.
        ///
        /// A `null` value means the channel has no recorded creator,
        /// which is often because the channel is very old,
        /// was created during realm creation or because it was created via a data import tool or [management command](https://zulip.readthedocs.io/en/latest/production/management-commands.html).
        ///
        /// **Changes**: New in Zulip 9.0 (feature level 254).
        creator_id: option { u64 },
        /// Specifies whether the channel is private or not.
        ///
        /// Only people who have been invited can access a private channel.
        invite_only: option { bool },
        /// A boolean specifying whether desktop notifications are enabled for the given channel.
        ///
        /// A `null` value means the value of this setting should be inherited from the user-level default setting, `enable_stream_desktop_notifications`, for this channel.
        desktop_notifications: option { bool },
        /// A boolean specifying whether email notifications are enabled for the given channel.
        ///
        /// A `null` value means the value of this setting should be inherited from the user-level default setting, `enable_stream_email_notifications`, for this channel.
        email_notifications: option { bool },
        /// A boolean specifying whether wildcard mentions trigger notifications as though they were personal mentions in this channel.
        ///
        /// A `null` value means the value of this setting should be inherited from the user-level default setting, wildcard_mentions_notify, for this channel.
        wildcard_mentions_notify: option { bool },
        /// A boolean specifying whether push notifications are enabled for the given channel.
        ///
        /// A `null` value means the value of this setting should be inherited from the user-level default setting, `enable_stream_push_notifications`, for this channel.
        push_notifications: option { bool },
        /// A boolean specifying whether audible notifications are enabled for the given channel.
        ///
        /// A `null` value means the value of this setting should be inherited from the user-level default setting, `enable_stream_audible_notifications`, for this channel.
        audible_notifications: option { bool },
        /// A boolean specifying whether the given channel has been pinned to the top.
        pin_to_top: option { bool },
        /// Whether the user has muted the channel. Muted channels do not count towards your total unread count and do not show up in the `Combined feed` view (previously known as `All messages`).
        ///
        /// **Changes**: Prior to Zulip 2.1.0, this feature was represented by the more confusingly named `in_home_view` (with the opposite value, `in_home_view=!is_muted`).
        is_muted: option { bool },
        /// Legacy property for if the given channel is muted, with inverted meaning.
        ///
        /// **Changes**: Deprecated in Zulip 2.1.0. Clients should use `is_muted` where available.
        in_home_view: option { bool },
        /// Whether only organization administrators can post to the channel.
        ///
        /// **Changes**: Deprecated in Zulip 3.0 (feature level 1). Clients should use `stream_post_policy` instead.
        is_announcement_only: option { bool },
        /// Whether the channel has been configured to allow unauthenticated access to its message history from the web.
        is_web_public: option { bool },
        /// A deprecated representation of a superset of the users who have permission to post messages to the channel available for backwards-compatibility.
        stream_post_policy: option { StreamPostPolicy },
        /// Number of days that messages sent to this channel will be stored before being automatically deleted by the [message retention policy](https://zulip.com/help/message-retention-policy).
        ///
        /// There are two special values:
        ///
        /// - `null`, the default, means the channel will inherit the organization level setting.
        /// - `-1` encodes retaining messages in this channel forever.
        ///
        /// **Changes**: New in Zulip 3.0 (feature level 17).
        message_retention_days: option { i32 },
        /// Whether the history of the channel is public to its subscribers.
        ///
        /// Currently always true for public channels (i.e. `\"invite_only\": false` implies `\"history_public_to_subscribers\": true`),
        /// but clients should not make that assumption, as we may change that behavior in the future.
        history_public_to_subscribers: option { bool },
        /// The ID of the first message in the channel.
        ///
        /// Intended to help clients determine whether they need to display UI like the \"show all topics\" widget that would suggest the channel has older history that can be accessed.
        ///
        /// Is `null` for channels with no message history.
        first_message_id: option { u64 },
        /// The ID of the folder to which the channel belongs.
        ///
        /// Is `null` if channel does not belong to any folder.
        ///
        /// **Changes**: New in Zulip 11.0 (feature level 389).
        folder_id: option { u64 },
        topics_policy: option { TopicsPolicy },
        /// Whether the channel has recent message activity.
        ///
        /// Clients should use this to implement [hiding inactive channels](https://zulip.com/help/manage-inactive-channels).
        ///
        /// **Changes**: New in Zulip 10.0 (feature level 323). Previously, clients implemented the demote_inactive_streams from local message history, resulting in a choppy loading experience.
        is_recently_active: option { bool },
        /// The average number of messages sent to the channel per week, as estimated based on recent weeks, rounded to the nearest integer.
        ///
        /// If `null`, the channel was recently created and there is insufficient data to estimate the average traffic.
        stream_weekly_traffic: option { u64 },
        /// A boolean indicating whether the channel is [archived](https://zulip.com/help/archive-a-channel).
        ///
        /// **Changes**: New in Zulip 10.0 (feature level 315). Previously, subscriptions only included active channels.
        /// Note that some endpoints will never return archived channels unless the client declares explicit support for them via the `archived_channels` client capability.
        is_archived: option { bool },
        /// The total number of non-deactivated users (including bots) who are subscribed to the channel.
        ///
        /// Clients are responsible for updating this value using `peer_add` and `peer_remove` events.
        ///
        /// The server's internals cannot guarantee this value is correctly synced with `peer_add` and `peer_remove` events for the channel.
        /// As a result, if a (rare) race occurs between a change in the channel's subscribers and fetching this value,
        /// it is possible for a client that is correctly following the events protocol to end up with a permanently off-by-one error in the channel's subscriber count.
        ///
        /// Clients are recommended to fetch full subscriber data for a channel in contexts where it is important to avoid this risk.
        /// The official web application, for example, uses this field primarily while waiting to fetch a given channel's full subscriber list from the server.
        ///
        /// **Changes**: New in Zulip 11.0 (feature level 394).
        subscriber_count: option { f64 },
    }
}

field_access! {
    Subscription {
        /// The name of a channel.
        name: option_cow_deref { str, String },
        /// The [description](https://zulip.com/help/change-the-channel-description) of the channel in [Zulip-flavored Markdown](https://zulip.com/help/format-your-message-using-markdown) format,
        /// intended to be used to prepopulate UI for editing a channel's description.
        ///
        /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
        ///
        /// See also `rendered_description`.
        description: option_cow_deref { str, String },
        /// The [description](https://zulip.com/help/change-the-channel-description) of the channel rendered as HTML,
        /// intended to be used when displaying the channel description in a UI.
        ///
        /// One should use the standard Zulip rendered_markdown CSS when displaying this content so that emoji, LaTeX,
        /// and other syntax work correctly.
        ///
        /// And any client-side security logic for user-generated message content should be applied when displaying this HTML as though it were the body of a Zulip message.
        ///
        /// See also `description`.
        rendered_description: option_cow_deref { str, String },
        /// A list of user IDs of users who are also subscribed to a given channel. Included only if `include_subscribers` is `true`.
        subscribers: option_cow_deref { [u64], Vec<u64> },
        /// If [`include_subscribers=\"partial\"`](https://zulip.com/api/get-subscriptions#parameter-include_subscribers) was requested, the server may,
        /// at its discretion, send a `partial_subscribers` list rather than a `subscribers` list for channels with a large number of subscribers.
        ///
        /// The `partial_subscribers` list contains an arbitrary subset of the channel's subscribers that is guaranteed to include all bot user
        /// subscribers as well as all users who have been active in the last 14 days, but otherwise can be chosen arbitrarily by the server.
        ///
        /// **Changes**: New in Zulip 11.0 (feature level 412).
        partial_subscribers: option_cow_deref { [u64], Vec<u64> },
        /// The user's personal color for the channel.
        color: option_cow_deref { str, String },
    }
}

field_access! {
    Subscription {
        can_add_subscribers_group: option_ref { CanAddSubscribersGroup },
        can_remove_subscribers_group: option_ref { CanRemoveSubscribersGroup },
        can_administer_channel_group: option_ref { CanAdministerChannelGroup },
        can_delete_any_message_group: option_ref { CanDeleteAnyMessageGroup },
        can_delete_own_message_group: option_ref { CanDeleteOwnMessageGroup },
        can_move_messages_out_of_channel_group: option_ref { CanMoveMessagesOutOfChannelGroup },
        can_move_messages_within_channel_group: option_ref { CanMoveMessagesWithinChannelGroup },
        can_send_message_group: option_ref { CanSendMessageGroup },
        can_subscribe_group: option_ref { CanSubscribeGroup },
        can_resolve_topics_group: option_ref { CanResolveTopicsGroup },
    }
}

impl_default!(Subscription);
impl_display!(Subscription);

/// Represents A deprecated representation of a superset of the users who
/// have permission to post messages to the channel available
/// for backwards-compatibility.
///
/// Clients should use `can_send_message_group` instead.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize_repr, Serialize_repr)]
pub enum StreamPostPolicy {
    /// Any user can post.
    AnyUser = 1,
    /// Only administrators can post.
    OnlyAdministrator = 2,
    /// Only full members can post.
    OnlyFullMember = 3,
    /// Only moderators can post.
    OnlyModerator = 4,
}

impl StreamPostPolicy {
    /// Creates a new [StreamPostPolicy].
    pub const fn new() -> Self {
        Self::AnyUser
    }
}

impl_default!(StreamPostPolicy);
impl_display!(StreamPostPolicy);