Skip to main content

ruma_events/room/
image_pack.rs

1//! Types for `m.room.image_pack` event.
2
3use std::collections::{BTreeMap, BTreeSet};
4
5use ruma_common::OwnedMxcUri;
6use ruma_macros::{EventContent, StringEnum};
7use serde::{Deserialize, Serialize};
8
9use crate::{PrivOwnedStr, room::ImageInfo};
10
11/// The content of an `m.room.image_pack` event.
12///
13/// The state key is the unique identifier for the image pack.
14#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.room.image_pack", kind = State, state_key_type = String)]
17pub struct RoomImagePackEventContent {
18    /// A map from a shortcode to an image object.
19    ///
20    /// Each entry defines one image available in this pack.
21    pub images: BTreeMap<String, ImagePackImage>,
22
23    /// Metadata about the image pack as a whole.
24    ///
25    /// This field is not serialized if it is empty, and deserializes to its default value if it is
26    /// missing.
27    #[serde(default, skip_serializing_if = "ImagePackMeta::is_empty")]
28    pub pack: ImagePackMeta,
29}
30
31impl RoomImagePackEventContent {
32    /// Creates a new `RoomImagePackEventContent` with a list of images.
33    pub fn new(images: BTreeMap<String, ImagePackImage>) -> Self {
34        Self { images, pack: ImagePackMeta::default() }
35    }
36}
37
38/// An image object in a image pack.
39#[derive(Clone, Debug, Deserialize, Serialize)]
40#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
41pub struct ImagePackImage {
42    /// The MXC URI to the media file.
43    pub url: OwnedMxcUri,
44
45    /// An optional text body for this image.
46    ///
47    /// Useful for the sticker body text or the emote alt text.
48    ///
49    /// Defaults to the shortcode.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub body: Option<String>,
52
53    /// The [ImageInfo] object used for the `info` block of `m.sticker` events.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub info: Option<ImageInfo>,
56}
57
58impl ImagePackImage {
59    /// Creates a new `ImagePackImage` with the given MXC URI to the media file.
60    pub fn new(url: OwnedMxcUri) -> Self {
61        Self { url, body: None, info: None }
62    }
63}
64
65/// Details about an image pack.
66#[derive(Clone, Debug, Default, Deserialize, Serialize)]
67#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
68pub struct ImagePackMeta {
69    /// A display name for the pack.
70    ///
71    /// If absent and the pack is defined in a room, defaults to the room's name.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub display_name: Option<String>,
74
75    /// The MXC URI of an avatar for the pack.
76    ///
77    /// If absent and the pack is defined in a room, defaults to the room's avatar.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub avatar_url: Option<OwnedMxcUri>,
80
81    /// The intended usage(s) for this pack.
82    ///
83    /// If empty, all usage types are assumed.
84    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
85    pub usage: BTreeSet<PackUsage>,
86
87    /// The attribution of this pack.
88    ///
89    /// For crediting the original author or source, for example.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub attribution: Option<String>,
92}
93
94impl ImagePackMeta {
95    /// Creates a new empty `ImagePackMeta`.
96    pub fn new() -> Self {
97        Self::default()
98    }
99
100    /// Whether this `ImagePackMeta` is empty.
101    fn is_empty(&self) -> bool {
102        let Self { display_name, avatar_url, usage, attribution } = self;
103        display_name.is_none() && avatar_url.is_none() && usage.is_empty() && attribution.is_none()
104    }
105}
106
107/// The intended usages for an image pack.
108#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
109#[derive(Clone, StringEnum)]
110#[ruma_enum(rename_all = "snake_case")]
111#[non_exhaustive]
112pub enum PackUsage {
113    /// The images are intended to be sent inline in messages.
114    Emoticon,
115
116    /// The images are intended to be sent as standalone sticker events.
117    Sticker,
118
119    #[doc(hidden)]
120    _Custom(PrivOwnedStr),
121}