djs-model 0.1.0-alpha.2

Discord API v10 types — users, guilds, channels, messages, interactions and gateway events.
Documentation
//! Content components (Components V2): text, thumbnails, media galleries and files.

use compact_str::CompactString;
use serde::{Deserialize, Serialize};

use crate::{
    id::AttachmentId,
    macros::{int_enum, int_flags},
};

/// A [text display](https://docs.discord.com/developers/components/reference#text-display):
/// markdown text, like a message's content (messages and modals).
///
/// discord.js counterpart: `TextDisplayComponent` / `TextDisplayBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct TextDisplay {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The markdown text.
    pub content: CompactString,
}

impl TextDisplay {
    /// Creates a text display.
    ///
    /// discord.js counterpart: `TextDisplayBuilder` (`@discordjs/builders`).
    #[must_use]
    pub fn new(content: impl Into<CompactString>) -> Self {
        Self {
            id: None,
            content: content.into(),
        }
    }
}

/// A [thumbnail](https://docs.discord.com/developers/components/reference#thumbnail): a small
/// image, used as a [section](super::Section) accessory.
///
/// discord.js counterpart: `ThumbnailComponent` / `ThumbnailBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Thumbnail {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The image (a URL or an `attachment://` reference).
    pub media: Box<UnfurledMediaItem>,
    /// The alt text (max 1024 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<CompactString>,
    /// Whether the image is blurred until clicked.
    #[serde(skip_serializing_if = "core::ops::Not::not")]
    pub spoiler: bool,
}

impl Thumbnail {
    /// Creates a thumbnail.
    ///
    /// discord.js counterpart: `ThumbnailBuilder` (`@discordjs/builders`).
    #[must_use]
    pub fn new(media: UnfurledMediaItem) -> Self {
        Self {
            id: None,
            media: Box::new(media),
            description: None,
            spoiler: false,
        }
    }
}

/// A [media gallery](https://docs.discord.com/developers/components/reference#media-gallery):
/// 1-10 images or videos in a grid.
///
/// discord.js counterpart: `MediaGalleryComponent` / `MediaGalleryBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct MediaGallery {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The items (1-10).
    pub items: Vec<MediaGalleryItem>,
}

impl MediaGallery {
    /// Creates a media gallery.
    ///
    /// discord.js counterpart: `MediaGalleryBuilder` (`@discordjs/builders`).
    #[must_use]
    pub const fn new(items: Vec<MediaGalleryItem>) -> Self {
        Self { id: None, items }
    }
}

/// An [item of a media gallery](https://docs.discord.com/developers/components/reference#media-gallery-media-gallery-item-structure).
///
/// discord.js counterpart: `MediaGalleryItem` / `MediaGalleryItemBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MediaGalleryItem {
    /// The image or video (a URL or an `attachment://` reference).
    pub media: UnfurledMediaItem,
    /// The alt text (max 1024 characters).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<CompactString>,
    /// Whether the item is blurred until clicked.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub spoiler: bool,
}

impl MediaGalleryItem {
    /// Creates an item.
    ///
    /// discord.js counterpart: `MediaGalleryItemBuilder` (`@discordjs/builders`).
    #[must_use]
    pub const fn new(media: UnfurledMediaItem) -> Self {
        Self {
            media,
            description: None,
            spoiler: false,
        }
    }
}

/// A [file](https://docs.discord.com/developers/components/reference#file): an attached file
/// shown in the message.
///
/// discord.js counterpart: `FileComponent` / `FileBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct File {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The file: only `attachment://<filename>` references are supported.
    pub file: Box<UnfurledMediaItem>,
    /// Whether the file is blurred until clicked.
    #[serde(skip_serializing_if = "core::ops::Not::not")]
    pub spoiler: bool,
    /// The file's name (set by Discord in responses; ignored when sending).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<CompactString>,
    /// The file's size in bytes (set by Discord in responses; ignored when sending).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u64>,
}

impl File {
    /// Creates a file component.
    ///
    /// discord.js counterpart: `FileBuilder` (`@discordjs/builders`).
    #[must_use]
    pub fn new(file: UnfurledMediaItem) -> Self {
        Self {
            id: None,
            file: Box::new(file),
            spoiler: false,
            name: None,
            size: None,
        }
    }
}

/// An [unfurled media item](https://docs.discord.com/developers/components/reference#unfurled-media-item):
/// a URL or an uploaded attachment that Discord resolves into media.
///
/// When sending, only [`url`](Self::url) is used; Discord fills in the other fields in
/// responses.
///
/// discord.js counterpart: `UnfurledMediaItem`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UnfurledMediaItem {
    /// Any URL, or `attachment://<filename>` for an uploaded file.
    pub url: CompactString,
    /// The URL through Discord's media proxy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub proxy_url: Option<CompactString>,
    /// The height of an image or video, in pixels.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,
    /// The width of an image or video, in pixels.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,
    /// A [thumbhash](https://evanw.github.io/thumbhash/) placeholder of an image or video.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub placeholder: Option<CompactString>,
    /// The version of the placeholder.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub placeholder_version: Option<u32>,
    /// The [media type](https://en.wikipedia.org/wiki/Media_type).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_type: Option<CompactString>,
    /// Whether Discord could load the media.
    ///
    /// **Undocumented:** known from discord-api-types, not in the official docs; may change
    /// without notice.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub loading_state: Option<LoadingState>,
    /// The media flags.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<UnfurledMediaItemFlags>,
    /// The results of Discord's content scanning, once it ran. Scanning can finish after the
    /// message was sent; then the results arrive in a later `MESSAGE_UPDATE`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_scan_metadata: Option<ContentScanMetadata>,
    /// The uploaded attachment, for `attachment://` references.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attachment_id: Option<AttachmentId>,
}

impl UnfurledMediaItem {
    /// Creates a media item from a URL or `attachment://<filename>` reference.
    ///
    /// discord.js counterpart: `UnfurledMediaItemData`.
    #[must_use]
    pub fn new(url: impl Into<CompactString>) -> Self {
        Self {
            url: url.into(),
            proxy_url: None,
            height: None,
            width: None,
            placeholder: None,
            placeholder_version: None,
            content_type: None,
            loading_state: None,
            flags: None,
            content_scan_metadata: None,
            attachment_id: None,
        }
    }
}

int_enum! {
    /// Whether Discord could load an [`UnfurledMediaItem`].
    ///
    /// **Undocumented:** known from discord-api-types (`UnfurledMediaItemLoadingState`), not in
    /// the official docs; may change without notice.
    ///
    /// discord.js counterpart: `UnfurledMediaItemLoadingState` (discord-api-types).
    #[doc(alias = "UnfurledMediaItemLoadingState")]
    pub enum LoadingState: u8 {
        /// The state is not known.
        Unspecified = 0,
        /// The media is being loaded.
        Loading = 1,
        /// The media was loaded.
        LoadedSuccess = 2,
        /// The media was not found.
        LoadedNotFound = 3,
    }
}

int_flags! {
    /// [Flags of an unfurled media item](https://docs.discord.com/developers/components/reference#unfurled-media-item-unfurled-media-item-flags).
    ///
    /// discord.js counterpart: `UnfurledMediaItemFlags` (discord-api-types).
    pub struct UnfurledMediaItemFlags: u64 {
        /// The image is animated.
        const IS_ANIMATED = 1 << 0;
    }
}

/// The [results of Discord's content scanning](https://docs.discord.com/developers/components/reference#unfurled-media-item-content-scan-metadata-structure)
/// of an [`UnfurledMediaItem`].
///
/// discord.js counterpart: `UnfurledMediaItemContentScanMetadata` (discord-api-types).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ContentScanMetadata {
    /// What the media was flagged as.
    pub flags: ContentScanMetadataFlags,
}

int_flags! {
    /// [Flags of content scan metadata](https://docs.discord.com/developers/components/reference#unfurled-media-item-content-scan-metadata-flags).
    ///
    /// discord.js counterpart: `UnfurledMediaItemContentScanMetadataFlags` (discord-api-types).
    pub struct ContentScanMetadataFlags: u64 {
        /// The media was flagged as explicit.
        const IS_EXPLICIT_CONTENT = 1 << 0;
        /// The media was flagged as gore.
        const IS_GORE_CONTENT = 1 << 1;
        /// The media was flagged as self-harm.
        const IS_SELF_HARM_CONTENT = 1 << 2;
    }
}