From 2e422c5d81e7a34c3eb773d85d24b5c9bd62e1b6 Mon Sep 17 00:00:00 2001 From: Adam Blanchet Date: Tue, 30 Mar 2021 18:12:11 +0200 Subject: [PATCH] Create ImageInfo struct specific to AvatarEventContent Add a new specific ImageInfo struct, with all the same fields, except for `thumbnail_file`, to disallow encrypted room avatars. Resolves: #397 --- ruma-events/src/room/avatar.rs | 40 +++++++++++++++++++++++++++++++- ruma-events/tests/state_event.rs | 7 ++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/ruma-events/src/room/avatar.rs b/ruma-events/src/room/avatar.rs index 79608e52..b276ae35 100644 --- a/ruma-events/src/room/avatar.rs +++ b/ruma-events/src/room/avatar.rs @@ -1,9 +1,10 @@ //! Types for the *m.room.avatar* event. +use js_int::UInt; use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use super::ImageInfo; +use super::ThumbnailInfo; use crate::StateEvent; /// A picture that is associated with the room. @@ -43,3 +44,40 @@ impl AvatarEventContent { Self::default() } } + +/// Metadata about an image (specific to avatars). +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct ImageInfo { + /// The height of the image in pixels. + #[serde(rename = "h", skip_serializing_if = "Option::is_none")] + pub height: Option, + + /// The width of the image in pixels. + #[serde(rename = "w", skip_serializing_if = "Option::is_none")] + pub width: Option, + + /// The MIME type of the image, e.g. "image/png." + #[serde(skip_serializing_if = "Option::is_none")] + pub mimetype: Option, + + /// The file size of the image in bytes. + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, + + /// Metadata about the image referred to in `thumbnail_url`. + #[serde(skip_serializing_if = "Option::is_none")] + pub thumbnail_info: Option>, + + /// The URL to the thumbnail of the image. + #[serde(skip_serializing_if = "Option::is_none")] + pub thumbnail_url: Option, + + /// The [BlurHash](https://blurha.sh) for this image. + /// + /// This uses the unstable prefix in + /// [MSC2448](https://github.com/matrix-org/matrix-doc/pull/2448). + #[cfg(feature = "unstable-pre-spec")] + #[serde(rename = "xyz.amorgan.blurhash")] + #[serde(skip_serializing_if = "Option::is_none")] + pub blurhash: Option, +} diff --git a/ruma-events/tests/state_event.rs b/ruma-events/tests/state_event.rs index b5f56ce8..462c8f13 100644 --- a/ruma-events/tests/state_event.rs +++ b/ruma-events/tests/state_event.rs @@ -3,7 +3,11 @@ use std::time::{Duration, UNIX_EPOCH}; use js_int::UInt; use matches::assert_matches; use ruma_events::{ - room::{aliases::AliasesEventContent, avatar::AvatarEventContent, ImageInfo, ThumbnailInfo}, + room::{ + aliases::AliasesEventContent, + avatar::{AvatarEventContent, ImageInfo}, + ThumbnailInfo, + }, AnyRoomEvent, AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, RawExt, StateEvent, SyncStateEvent, Unsigned, }; @@ -219,7 +223,6 @@ fn deserialize_avatar_without_prev_content() { size, thumbnail_info: Some(thumbnail_info), thumbnail_url: Some(thumbnail_url), - thumbnail_file: None, #[cfg(feature = "unstable-pre-spec")] blurhash: None, } if *height == UInt::new(423)