1use 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#[derive(Clone, Debug, Serialize)]
15#[non_exhaustive]
16pub enum StickerMediaSource {
17 #[serde(rename = "url")]
19 Plain(OwnedMxcUri),
20
21 #[cfg(feature = "compat-encrypted-stickers")]
23 #[serde(rename = "file")]
24 Encrypted(Box<EncryptedFile>),
25}
26
27impl<'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 #[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#[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 pub body: String,
91
92 pub info: ImageInfo,
94
95 #[serde(flatten)]
97 pub source: StickerMediaSource,
98
99 #[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 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 pub fn with_source(body: String, info: ImageInfo, source: StickerMediaSource) -> Self {
116 Self { body, info, source, relates_to: None }
117 }
118}