events: Add unstable support for MSC2545 for image packs

This commit is contained in:
Yorusaka Miyabi 2024-12-27 19:51:51 +08:00 committed by strawberry
parent 0a37df11cd
commit ec42dd491a
6 changed files with 184 additions and 0 deletions

View File

@ -15,6 +15,7 @@
Improvements: Improvements:
- Add unstable support for the `is_animated` flag for images, according to MSC4230. - Add unstable support for the `is_animated` flag for images, according to MSC4230.
- Add unstable support for MSC2545 for image packs.
# 0.30.0 # 0.30.0

View File

@ -20,6 +20,7 @@ markdown = ["dep:pulldown-cmark"]
unstable-exhaustive-types = [] unstable-exhaustive-types = []
unstable-msc1767 = [] unstable-msc1767 = []
unstable-msc2448 = [] unstable-msc2448 = []
unstable-msc2545 = []
unstable-msc2747 = [] unstable-msc2747 = []
unstable-msc2867 = [] unstable-msc2867 = []
unstable-msc3061 = [] unstable-msc3061 = []

View File

@ -17,6 +17,12 @@ event_enum! {
"m.push_rules" => super::push_rules, "m.push_rules" => super::push_rules,
"m.secret_storage.default_key" => super::secret_storage::default_key, "m.secret_storage.default_key" => super::secret_storage::default_key,
"m.secret_storage.key.*" => super::secret_storage::key, "m.secret_storage.key.*" => super::secret_storage::key,
#[cfg(feature = "unstable-msc2545")]
#[ruma_enum(ident = AccountImagePack, alias = "m.image_pack")]
"im.ponies.user_emotes" => super::image_pack,
#[cfg(feature = "unstable-msc2545")]
#[ruma_enum(ident = ImagePackRooms, alias = "m.image_pack.rooms")]
"im.ponies.emote_rooms" => super::image_pack,
} }
/// Any room account data event. /// Any room account data event.
@ -130,6 +136,9 @@ event_enum! {
"m.room.topic" => super::room::topic, "m.room.topic" => super::room::topic,
"m.space.child" => super::space::child, "m.space.child" => super::space::child,
"m.space.parent" => super::space::parent, "m.space.parent" => super::space::parent,
#[cfg(feature = "unstable-msc2545")]
#[ruma_enum(ident = RoomImagePack, alias = "m.image_pack")]
"im.ponies.room_emotes" => super::image_pack,
#[cfg(feature = "unstable-msc3489")] #[cfg(feature = "unstable-msc3489")]
#[ruma_enum(alias = "m.beacon_info")] #[ruma_enum(alias = "m.beacon_info")]
"org.matrix.msc3672.beacon_info" => super::beacon_info, "org.matrix.msc3672.beacon_info" => super::beacon_info,

View File

@ -0,0 +1,169 @@
//! Types for image packs in Matrix ([MSC2545]).
//!
//! [MSC2545]: https://github.com/matrix-org/matrix-spec-proposals/pull/2545
use std::collections::{BTreeMap, BTreeSet};
use ruma_common::{serde::StringEnum, OwnedMxcUri, OwnedRoomId};
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use crate::{room::ImageInfo, PrivOwnedStr};
/// The content of an `im.ponies.room_emotes` event,
/// the unstable version of `m.image_pack` in room state events.
///
/// State key is the identifier for the image pack in [ImagePackRoomsEventContent].
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[ruma_event(type = "im.ponies.room_emotes", kind = State, state_key_type = String)]
pub struct RoomImagePackEventContent {
/// A list of images available in this image pack.
///
/// Keys in the map are shortcodes for the images.
pub images: BTreeMap<String, PackImage>,
/// Image pack info.
#[serde(skip_serializing_if = "Option::is_none")]
pub pack: Option<PackInfo>,
}
impl RoomImagePackEventContent {
/// Creates a new `RoomImagePackEventContent` with a list of images.
pub fn new(images: BTreeMap<String, PackImage>) -> Self {
Self { images, pack: None }
}
}
/// The content of an `im.ponies.user_emotes` event,
/// the unstable version of `m.image_pack` in account data events.
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[ruma_event(type = "im.ponies.user_emotes", kind = GlobalAccountData)]
pub struct AccountImagePackEventContent {
/// A list of images available in this image pack.
///
/// Keys in the map are shortcodes for the images.
pub images: BTreeMap<String, PackImage>,
/// Image pack info.
#[serde(skip_serializing_if = "Option::is_none")]
pub pack: Option<PackInfo>,
}
impl AccountImagePackEventContent {
/// Creates a new `AccountImagePackEventContent` with a list of images.
pub fn new(images: BTreeMap<String, PackImage>) -> Self {
Self { images, pack: None }
}
}
/// An image object in a image pack.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PackImage {
/// 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>,
/// The usages for the image.
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub usage: BTreeSet<PackUsage>,
}
impl PackImage {
/// Creates a new `PackImage` with the given MXC URI to the media file.
pub fn new(url: OwnedMxcUri) -> Self {
Self { url, body: None, info: None, usage: BTreeSet::new() }
}
}
/// A description for the pack.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PackInfo {
/// A display name for the pack.
/// This does not have to be unique from other packs in a room.
///
/// Defaults to the room name, if the image pack event is in the room.
#[serde(skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
/// The MXC URI of an avatar/icon to display for the pack.
///
/// Defaults to the room avatar, if the pack is in the room.
/// Otherwise, the pack does not have an avatar.
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<OwnedMxcUri>,
/// The usages for the pack.
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub usage: BTreeSet<PackUsage>,
/// The attribution of this pack.
#[serde(skip_serializing_if = "Option::is_none")]
pub attribution: Option<String>,
}
impl PackInfo {
/// Creates a new empty `PackInfo`.
pub fn new() -> Self {
Self::default()
}
}
/// Usages for either an image pack or an individual image.
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PackUsage {
/// Pack or image is usable as a emoticon.
Emoticon,
/// Pack or image is usable as a sticker.
Sticker,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
/// The content of an `im.ponies.emote_rooms` event,
/// the unstable version of `m.image_pack.rooms`.
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[ruma_event(type = "im.ponies.emote_rooms", kind = GlobalAccountData)]
pub struct ImagePackRoomsEventContent {
/// A map of enabled image packs in each room.
pub rooms: BTreeMap<OwnedRoomId, BTreeMap<String, ImagePackRoomContent>>,
}
impl ImagePackRoomsEventContent {
/// Creates a new `ImagePackRoomsEventContent`
/// with a map of enabled image packs in each room.
pub fn new(rooms: BTreeMap<OwnedRoomId, BTreeMap<String, ImagePackRoomContent>>) -> Self {
Self { rooms }
}
}
/// Additional metadatas for a enabled room image pack.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct ImagePackRoomContent {}
impl ImagePackRoomContent {
/// Creates a new empty `ImagePackRoomContent`.
pub fn new() -> Self {
Self {}
}
}

View File

@ -158,6 +158,8 @@ pub mod identity_server;
pub mod ignored_user_list; pub mod ignored_user_list;
#[cfg(feature = "unstable-msc3552")] #[cfg(feature = "unstable-msc3552")]
pub mod image; pub mod image;
#[cfg(feature = "unstable-msc2545")]
pub mod image_pack;
pub mod key; pub mod key;
#[cfg(feature = "unstable-msc3488")] #[cfg(feature = "unstable-msc3488")]
pub mod location; pub mod location;

View File

@ -223,6 +223,7 @@ unstable-msc2448 = [
"ruma-events?/unstable-msc2448", "ruma-events?/unstable-msc2448",
"ruma-federation-api?/unstable-msc2448", "ruma-federation-api?/unstable-msc2448",
] ]
unstable-msc2545 = ["ruma-events?/unstable-msc2545"]
unstable-msc2654 = ["ruma-client-api?/unstable-msc2654"] unstable-msc2654 = ["ruma-client-api?/unstable-msc2654"]
unstable-msc2666 = ["ruma-client-api?/unstable-msc2666"] unstable-msc2666 = ["ruma-client-api?/unstable-msc2666"]
unstable-msc2747 = ["ruma-events?/unstable-msc2747"] unstable-msc2747 = ["ruma-events?/unstable-msc2747"]
@ -281,6 +282,7 @@ unstable-unspecified = [
__unstable-mscs = [ __unstable-mscs = [
"unstable-msc1767", "unstable-msc1767",
"unstable-msc2448", "unstable-msc2448",
"unstable-msc2545",
"unstable-msc2654", "unstable-msc2654",
"unstable-msc2666", "unstable-msc2666",
"unstable-msc2747", "unstable-msc2747",