ruma_events/
sticker.rs

1//! Types for the [`m.sticker`] event.
2//!
3//! [`m.sticker`]: https://spec.matrix.org/latest/client-server-api/#msticker
4
5use ruma_common::OwnedMxcUri;
6use ruma_macros::EventContent;
7use serde::{de, Deserialize, Serialize};
8
9#[cfg(feature = "compat-encrypted-stickers")]
10use crate::room::EncryptedFile;
11use crate::room::{message::Relation, ImageInfo, MediaSource};
12
13/// The source of a sticker media file.
14#[derive(Clone, Debug, Serialize)]
15#[non_exhaustive]
16pub enum StickerMediaSource {
17    /// The MXC URI to the unencrypted media file.
18    #[serde(rename = "url")]
19    Plain(OwnedMxcUri),
20
21    /// The encryption info of the encrypted media file.
22    #[cfg(feature = "compat-encrypted-stickers")]
23    #[serde(rename = "file")]
24    Encrypted(Box<EncryptedFile>),
25}
26
27// Custom implementation of `Deserialize`, because serde doesn't guarantee what variant will be
28// deserialized for "externally tagged"¹ enums where multiple "tag" fields exist.
29//
30// ¹ https://serde.rs/enum-representations.html
31impl<'de> Deserialize<'de> for StickerMediaSource {
32    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
33    where
34        D: serde::Deserializer<'de>,
35    {
36        #[derive(Deserialize)]
37        struct StickerMediaSourceJsonRepr {
38            url: Option<OwnedMxcUri>,
39            #[cfg(feature = "compat-encrypted-stickers")]
40            file: Option<Box<EncryptedFile>>,
41        }
42
43        match StickerMediaSourceJsonRepr::deserialize(deserializer)? {
44            StickerMediaSourceJsonRepr {
45                url: None,
46                #[cfg(feature = "compat-encrypted-stickers")]
47                    file: None,
48            } => Err(de::Error::missing_field("url")),
49            // Prefer file if it is set
50            #[cfg(feature = "compat-encrypted-stickers")]
51            StickerMediaSourceJsonRepr { file: Some(file), .. } => {
52                Ok(StickerMediaSource::Encrypted(file))
53            }
54            StickerMediaSourceJsonRepr { url: Some(url), .. } => Ok(StickerMediaSource::Plain(url)),
55        }
56    }
57}
58
59impl From<StickerMediaSource> for MediaSource {
60    fn from(value: StickerMediaSource) -> Self {
61        match value {
62            StickerMediaSource::Plain(url) => MediaSource::Plain(url),
63            #[cfg(feature = "compat-encrypted-stickers")]
64            StickerMediaSource::Encrypted(file) => MediaSource::Encrypted(file),
65        }
66    }
67}
68
69#[cfg(feature = "compat-encrypted-stickers")]
70impl From<MediaSource> for StickerMediaSource {
71    fn from(value: MediaSource) -> Self {
72        match value {
73            MediaSource::Plain(url) => StickerMediaSource::Plain(url),
74            MediaSource::Encrypted(file) => StickerMediaSource::Encrypted(file),
75        }
76    }
77}
78
79/// The content of an `m.sticker` event.
80///
81/// A sticker message.
82#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
83#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
84#[ruma_event(type = "m.sticker", kind = MessageLike, without_relation)]
85pub struct StickerEventContent {
86    /// A textual representation or associated description of the sticker image.
87    ///
88    /// This could be the alt text of the original image, or a message to accompany and further
89    /// describe the sticker.
90    pub body: String,
91
92    /// Metadata about the image referred to in `url` including a thumbnail representation.
93    pub info: ImageInfo,
94
95    /// The media source of the sticker image.
96    #[serde(flatten)]
97    pub source: StickerMediaSource,
98
99    /// Information about related messages.
100    #[serde(
101        flatten,
102        skip_serializing_if = "Option::is_none",
103        deserialize_with = "crate::room::message::relation_serde::deserialize_relation"
104    )]
105    pub relates_to: Option<Relation<StickerEventContentWithoutRelation>>,
106}
107
108impl StickerEventContent {
109    /// Creates a new `StickerEventContent` with the given body, image info and URL.
110    pub fn new(body: String, info: ImageInfo, url: OwnedMxcUri) -> Self {
111        Self { body, info, source: StickerMediaSource::Plain(url.clone()), relates_to: None }
112    }
113
114    /// Creates a new `StickerEventContent` with the given body, image info, URL, and media source.
115    pub fn with_source(body: String, info: ImageInfo, source: StickerMediaSource) -> Self {
116        Self { body, info, source, relates_to: None }
117    }
118}