Declare a few state event content structs without the ruma_event! macro

This commit is contained in:
Jonas Platte 2020-05-23 21:58:20 +02:00
parent ef6e2e7023
commit eceef3b96d
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
5 changed files with 92 additions and 100 deletions

View File

@ -1,7 +1,8 @@
//! Types for the *m.room.aliases* event.
use ruma_events_macros::ruma_event;
use ruma_events_macros::{FromRaw, StateEventContent};
use ruma_identifiers::RoomAliasId;
use serde::Serialize;
use serde_json::value::RawValue as RawJsonValue;
use crate::{
@ -9,14 +10,10 @@ use crate::{
EventContent, EventJson, RoomEventContent, StateEventContent,
};
ruma_event! {
/// Informs the room about what room aliases it has been given.
AliasesEvent {
kind: StateEvent,
event_type: "m.room.aliases",
content: {
/// A list of room aliases.
pub aliases: Vec<RoomAliasId>,
},
}
/// Informs the room about what room aliases it has been given.
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
#[ruma_event(type = "m.room.aliases")]
pub struct AliasesEventContent {
/// A list of room aliases.
pub aliases: Vec<RoomAliasId>,
}

View File

@ -1,6 +1,7 @@
//! Types for the *m.room.avatar* event.
use ruma_events_macros::ruma_event;
use ruma_events_macros::{FromRaw, StateEventContent};
use serde::Serialize;
use serde_json::value::RawValue as RawJsonValue;
use super::ImageInfo;
@ -9,21 +10,17 @@ use crate::{
EventContent, EventJson, RoomEventContent, StateEventContent,
};
ruma_event! {
/// A picture that is associated with the room.
///
/// This can be displayed alongside the room information.
AvatarEvent {
kind: StateEvent,
event_type: "m.room.avatar",
content: {
/// Information about the avatar image.
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<Box<ImageInfo>>,
/// A picture that is associated with the room.
///
/// This can be displayed alongside the room information.
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
#[ruma_event(type = "m.room.avatar")]
pub struct AvatarEventContent {
/// Information about the avatar image.
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<Box<ImageInfo>>,
/// Information about the avatar thumbnail image.
/// URL of the avatar image.
pub url: String,
},
}
/// Information about the avatar thumbnail image.
/// URL of the avatar image.
pub url: String,
}

View File

@ -1,31 +1,27 @@
//! Types for the *m.room.canonical_alias* event.
use ruma_events_macros::ruma_event;
use ruma_events_macros::{FromRaw, StateEventContent};
use ruma_identifiers::RoomAliasId;
use serde::Serialize;
ruma_event! {
/// Informs the room as to which alias is the canonical one.
CanonicalAliasEvent {
kind: StateEvent,
event_type: "m.room.canonical_alias",
content: {
/// The canonical alias.
///
/// Rooms with `alias: None` should be treated the same as a room
/// with no canonical alias.
#[serde(
default, deserialize_with = "ruma_serde::empty_string_as_none",
skip_serializing_if = "Option::is_none"
)]
pub alias: Option<RoomAliasId>,
/// List of alternative aliases to the room.
#[serde(
default,
skip_serializing_if = "Vec::is_empty"
)]
pub alt_aliases: Vec<RoomAliasId>,
},
}
/// Informs the room as to which alias is the canonical one.
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
#[ruma_event(type = "m.room.canonical_alias")]
pub struct CanonicalAliasEventContent {
/// The canonical alias.
///
/// Rooms with `alias: None` should be treated the same as a room
/// with no canonical alias.
#[serde(
default,
deserialize_with = "ruma_serde::empty_string_as_none",
skip_serializing_if = "Option::is_none"
)]
pub alias: Option<RoomAliasId>,
/// List of alternative aliases to the room.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub alt_aliases: Vec<RoomAliasId>,
}
#[cfg(test)]

View File

@ -2,37 +2,33 @@
use std::convert::TryFrom;
use ruma_events_macros::ruma_event;
use ruma_events_macros::{FromRaw, StateEventContent};
use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId};
use serde::{Deserialize, Serialize};
ruma_event! {
/// This is the first event in a room and cannot be changed. It acts as the root of all other
/// events.
CreateEvent {
kind: StateEvent,
event_type: "m.room.create",
content: {
/// The `user_id` of the room creator. This is set by the homeserver.
pub creator: UserId,
/// This is the first event in a room and cannot be changed. It acts as the root of all other
/// events.
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
#[ruma_event(type = "m.room.create")]
pub struct CreateEventContent {
/// The `user_id` of the room creator. This is set by the homeserver.
pub creator: UserId,
/// Whether or not this room's data should be transferred to other homeservers.
#[serde(
rename = "m.federate",
default = "ruma_serde::default_true",
skip_serializing_if = "ruma_serde::is_true"
)]
pub federate: bool,
/// Whether or not this room's data should be transferred to other homeservers.
#[serde(
rename = "m.federate",
default = "ruma_serde::default_true",
skip_serializing_if = "ruma_serde::is_true"
)]
pub federate: bool,
/// The version of the room. Defaults to "1" if the key does not exist.
#[serde(default = "default_room_version_id")]
pub room_version: RoomVersionId,
/// The version of the room. Defaults to "1" if the key does not exist.
#[serde(default = "default_room_version_id")]
pub room_version: RoomVersionId,
/// A reference to the room this room replaces, if the previous room was upgraded.
#[serde(skip_serializing_if = "Option::is_none")]
pub predecessor: Option<PreviousRoom>,
},
}
/// A reference to the room this room replaces, if the previous room was upgraded.
#[serde(skip_serializing_if = "Option::is_none")]
pub predecessor: Option<PreviousRoom>,
}
/// A reference to an old room replaced during a room version upgrade.

View File

@ -456,20 +456,7 @@ mod tests {
from_json_value::<StateEvent<AnyStateEventContent>>(json_data).unwrap(),
StateEvent {
content: AnyStateEventContent::RoomAvatar(AvatarEventContent {
info: Some(ImageInfo {
height,
width,
mimetype: Some(mimetype),
size,
thumbnail_info: Some(ThumbnailInfo {
width: thumb_width,
height: thumb_height,
mimetype: thumb_mimetype,
size: thumb_size,
}),
thumbnail_url: Some(thumbnail_url),
thumbnail_file: None,
}),
info: Some(info),
url,
}),
event_id,
@ -483,15 +470,34 @@ mod tests {
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
&& sender == UserId::try_from("@carl:example.com").unwrap()
&& state_key == ""
&& height == UInt::new(423)
&& width == UInt::new(1011)
&& mimetype == "image/png"
&& size == UInt::new(84242)
&& thumb_width == UInt::new(800)
&& thumb_height == UInt::new(334)
&& thumb_mimetype == Some("image/png".to_string())
&& thumb_size == UInt::new(82595)
&& thumbnail_url == "mxc://matrix.org"
&& matches!(
info.as_ref(),
ImageInfo {
height,
width,
mimetype: Some(mimetype),
size,
thumbnail_info: Some(thumbnail_info),
thumbnail_url: Some(thumbnail_url),
thumbnail_file: None,
} if *height == UInt::new(423)
&& *width == UInt::new(1011)
&& *mimetype == "image/png"
&& *size == UInt::new(84242)
&& matches!(
thumbnail_info.as_ref(),
ThumbnailInfo {
width: thumb_width,
height: thumb_height,
mimetype: thumb_mimetype,
size: thumb_size,
} if *thumb_width == UInt::new(800)
&& *thumb_height == UInt::new(334)
&& *thumb_mimetype == Some("image/png".to_string())
&& *thumb_size == UInt::new(82595)
&& *thumbnail_url == "mxc://matrix.org"
)
)
&& url == "http://www.matrix.org"
);
}