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