Remove Event, RoomEvent and StateEvent traits

This commit is contained in:
Ragotzy.devin 2020-05-08 12:58:37 -04:00 committed by Jonas Platte
parent c1ee72db0f
commit 3d01bfa96d
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
14 changed files with 93 additions and 664 deletions

View File

@ -1,4 +1,7 @@
//! Details of generating code for the `ruma_event` procedural macro.
#![allow(dead_code)]
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{
@ -75,18 +78,9 @@ impl ToTokens for RumaEvent {
// allowance.
#[allow(clippy::cognitive_complexity)]
fn to_tokens(&self, tokens: &mut TokenStream) {
let attrs = &self.attrs;
// let attrs = &self.attrs;
let content_name = &self.content_name;
let event_fields = &self.fields;
let event_type_variant = {
let event_type = to_camel_case(self.event_type.value());
let variant = Ident::new(&event_type, event_type.span());
quote! {
::ruma_events::EventType::#variant
}
};
// let event_fields = &self.fields;
let name = &self.name;
let content_docstring = format!("The payload for `{}`.", name);
@ -125,61 +119,6 @@ impl ToTokens for RumaEvent {
Content::Typedef(_) => TokenStream::new(),
};
let impl_room_event = match self.kind {
EventKind::RoomEvent | EventKind::StateEvent => {
quote! {
impl ::ruma_events::RoomEvent for #name {
/// The unique identifier for the event.
fn event_id(&self) -> &ruma_identifiers::EventId {
&self.event_id
}
/// Time on originating homeserver when this event was sent.
fn origin_server_ts(&self) -> std::time::SystemTime {
self.origin_server_ts
}
/// The unique identifier for the room associated with this event.
///
/// This can be `None` if the event came from a context where there is
/// no ambiguity which room it belongs to, like a `/sync` response for example.
fn room_id(&self) -> Option<&ruma_identifiers::RoomId> {
self.room_id.as_ref()
}
/// The unique identifier for the user who sent this event.
fn sender(&self) -> &ruma_identifiers::UserId {
&self.sender
}
/// Additional key-value pairs not signed by the homeserver.
fn unsigned(&self) -> &ruma_events::UnsignedData {
&self.unsigned
}
}
}
}
_ => TokenStream::new(),
};
let impl_state_event = if self.kind == EventKind::StateEvent {
quote! {
impl ::ruma_events::StateEvent for #name {
/// The previous content for this state key, if any.
fn prev_content(&self) -> Option<&Self::Content> {
self.prev_content.as_ref()
}
/// A key that determines which piece of room state the event represents.
fn state_key(&self) -> &str {
&self.state_key
}
}
}
} else {
TokenStream::new()
};
let impl_event_result_compatible_for_content =
if let Content::Struct(content_fields) = &self.content {
let mut content_field_values: Vec<TokenStream> =
@ -213,48 +152,16 @@ impl ToTokens for RumaEvent {
TokenStream::new()
};
let event_type_name = self.event_type.value();
// let event_type_name = self.event_type.value();
let output = quote!(
#(#attrs)*
#[derive(Clone, Debug, serde::Serialize, ruma_events_macros::FromRaw)]
#[serde(rename = #event_type_name, tag = "type")]
pub struct #name {
#(#event_fields),*
}
#content
#impl_event_result_compatible_for_content
impl ::ruma_events::Event for #name {
/// The type of this event's `content` field.
type Content = #content_name;
/// The event's content.
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> ::ruma_events::EventType {
#event_type_variant
}
}
#impl_room_event
#impl_state_event
/// "Raw" versions of the event and its content which implement `serde::Deserialize`.
pub(crate) mod raw {
use super::*;
#(#attrs)*
#[derive(Clone, Debug, serde::Deserialize)]
pub struct #name {
#(#event_fields),*
}
#raw_content
}
);

View File

@ -2,7 +2,7 @@
use std::time::SystemTime;
use crate::{Event, EventType, RoomEvent, StateEvent, UnsignedData};
use crate::{EventType, UnsignedData};
use ruma_events_macros::FromRaw;
use ruma_identifiers::{EventId, RoomId, UserId};
@ -22,21 +22,6 @@ pub struct CustomEvent {
/// The payload for `CustomEvent`.
pub type CustomEventContent = JsonValue;
impl Event for CustomEvent {
/// The type of this event's `content` field.
type Content = CustomEventContent;
/// The event's content.
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> EventType {
EventType::Custom(self.event_type.clone())
}
}
/// A custom room event not covered by the Matrix specification.
#[derive(Clone, Debug, FromRaw, Serialize)]
pub struct CustomRoomEvent {
@ -62,51 +47,6 @@ pub struct CustomRoomEvent {
/// The payload for `CustomRoomEvent`.
pub type CustomRoomEventContent = JsonValue;
impl Event for CustomRoomEvent {
/// The type of this event's `content` field.
type Content = CustomRoomEventContent;
/// The event's content.
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> EventType {
EventType::Custom(self.event_type.clone())
}
}
impl RoomEvent for CustomRoomEvent {
/// The unique identifier for the event.
fn event_id(&self) -> &EventId {
&self.event_id
}
/// Time on originating homeserver when this event was sent.
fn origin_server_ts(&self) -> SystemTime {
self.origin_server_ts
}
/// The unique identifier for the room associated with this event.
///
/// This can be `None` if the event came from a context where there is
/// no ambiguity which room it belongs to, like a `/sync` response for example.
fn room_id(&self) -> Option<&RoomId> {
self.room_id.as_ref()
}
/// The unique identifier for the user who sent this event.
fn sender(&self) -> &UserId {
&self.sender
}
/// Additional key-value pairs not signed by the homeserver.
fn unsigned(&self) -> &UnsignedData {
&self.unsigned
}
}
/// A custom state event not covered by the Matrix specification.
#[derive(Clone, Debug, FromRaw, Serialize)]
pub struct CustomStateEvent {
@ -136,63 +76,6 @@ pub struct CustomStateEvent {
/// The payload for `CustomStateEvent`.
pub type CustomStateEventContent = JsonValue;
impl Event for CustomStateEvent {
/// The type of this event's `content` field.
type Content = CustomStateEventContent;
/// The event's content.
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> EventType {
EventType::Custom(self.event_type.clone())
}
}
impl RoomEvent for CustomStateEvent {
/// The unique identifier for the event.
fn event_id(&self) -> &EventId {
&self.event_id
}
/// Time on originating homeserver when this event was sent.
fn origin_server_ts(&self) -> SystemTime {
self.origin_server_ts
}
/// The unique identifier for the room associated with this event.
///
/// This can be `None` if the event came from a context where there is
/// no ambiguity which room it belongs to, like a `/sync` response for example.
fn room_id(&self) -> Option<&RoomId> {
self.room_id.as_ref()
}
/// The unique identifier for the user who sent this event.
fn sender(&self) -> &UserId {
&self.sender
}
/// Additional key-value pairs not signed by the homeserver.
fn unsigned(&self) -> &UnsignedData {
&self.unsigned
}
}
impl StateEvent for CustomStateEvent {
/// The previous content for this state key, if any.
fn prev_content(&self) -> Option<&Self::Content> {
self.prev_content.as_ref()
}
/// A key that determines which piece of room state the event represents.
fn state_key(&self) -> &str {
&self.state_key
}
}
pub(crate) mod raw {
use std::time::SystemTime;

View File

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use super::{
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
};
use crate::{EventType, InvalidInput, TryFromRaw};
use crate::{InvalidInput, TryFromRaw};
/// Begins an SAS key verification process.
///
@ -36,12 +36,6 @@ impl TryFromRaw for StartEvent {
}
}
impl_event!(
StartEvent,
StartEventContent,
EventType::KeyVerificationStart
);
impl TryFromRaw for StartEventContent {
type Raw = raw::StartEventContent;
type Err = &'static str;

View File

@ -113,23 +113,22 @@
#![warn(missing_debug_implementations, missing_docs, rust_2018_idioms)]
// Since we support Rust 1.36.0, we can't apply this suggestion yet
#![allow(clippy::use_self)]
#![allow(dead_code)]
#![allow(unused_imports)]
use std::{fmt::Debug, time::SystemTime};
use std::fmt::Debug;
use js_int::Int;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{Deserialize, Serialize};
use self::room::redaction::RedactionEvent;
// use self::room::redaction::RedactionEvent;
pub use self::custom::{CustomEvent, CustomRoomEvent, CustomStateEvent};
#[deprecated = "Use ruma_serde::empty::Empty directly instead."]
pub use ruma_serde::empty::Empty;
#[macro_use]
mod macros;
mod algorithm;
mod error;
mod event_type;
@ -145,31 +144,31 @@ extern crate self as ruma_events;
pub mod call;
pub mod custom;
/// Enums for heterogeneous collections of events.
pub mod collections {
pub mod all;
pub mod only;
// pub mod collections {
// pub mod all;
// pub mod only;
mod raw {
pub mod all;
pub mod only;
}
}
pub mod direct;
pub mod dummy;
// mod raw {
// pub mod all;
// pub mod only;
// }
// }
// pub mod direct;
// pub mod dummy;
pub mod forwarded_room_key;
pub mod fully_read;
pub mod ignored_user_list;
// pub mod ignored_user_list;
pub mod key;
pub mod presence;
pub mod push_rules;
// pub mod presence;
// pub mod push_rules;
pub mod receipt;
pub mod room;
pub mod room_key;
// pub mod room_key;
pub mod room_key_request;
pub mod sticker;
pub mod stripped;
// pub mod stripped;
pub mod tag;
pub mod to_device;
// pub mod to_device;
pub mod typing;
pub use self::{
@ -180,48 +179,6 @@ pub use self::{
json::EventJson,
};
/// A basic event.
pub trait Event: Debug + Serialize + Sized + TryFromRaw {
/// The type of this event's `content` field.
type Content: Debug + Serialize;
/// The event's content.
fn content(&self) -> &Self::Content;
/// The type of the event.
fn event_type(&self) -> EventType;
}
/// An event within the context of a room.
pub trait RoomEvent: Event {
/// The unique identifier for the event.
fn event_id(&self) -> &EventId;
/// Time on originating homeserver when this event was sent.
fn origin_server_ts(&self) -> SystemTime;
/// The unique identifier for the room associated with this event.
///
/// This can be `None` if the event came from a context where there is
/// no ambiguity which room it belongs to, like a `/sync` response for example.
fn room_id(&self) -> Option<&RoomId>;
/// The unique identifier for the user who sent this event.
fn sender(&self) -> &UserId;
/// Additional key-value pairs not signed by the homeserver.
fn unsigned(&self) -> &UnsignedData;
}
/// An event that describes persistent state about a room.
pub trait StateEvent: RoomEvent {
/// The previous content for this state key, if any.
fn prev_content(&self) -> Option<&Self::Content>;
/// A key that determines which piece of room state the event represents.
fn state_key(&self) -> &str;
}
/// Extra information about an event that is not incorporated into the event's
/// hash.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
@ -233,10 +190,9 @@ pub struct UnsignedData {
#[serde(skip_serializing_if = "Option::is_none")]
pub age: Option<Int>,
/// The event that redacted this event, if any.
#[serde(skip_serializing_if = "Option::is_none")]
pub redacted_because: Option<EventJson<RedactionEvent>>,
// /// The event that redacted this event, if any.
// #[serde(skip_serializing_if = "Option::is_none")]
// pub redacted_because: Option<EventJson<RedactionEvent>>,
/// The client-supplied transaction ID, if the client being given the event
/// is the same one which sent it.
#[serde(skip_serializing_if = "Option::is_none")]
@ -251,6 +207,7 @@ impl UnsignedData {
/// an incoming `unsigned` field was present - it could still have been
/// present but contained none of the known fields.
pub fn is_empty(&self) -> bool {
self.age.is_none() && self.redacted_because.is_none() && self.transaction_id.is_none()
self.age.is_none() && self.transaction_id.is_none()
// && self.redacted_because.is_none()
}
}

View File

@ -1,82 +0,0 @@
macro_rules! impl_event {
($name:ident, $content_name:ident, $event_type:path) => {
impl crate::Event for $name {
/// The type of this event's `content` field.
type Content = $content_name;
/// The event's content.
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> crate::EventType {
$event_type
}
}
};
}
macro_rules! impl_room_event {
($name:ident, $content_name:ident, $event_type:path) => {
impl_event!($name, $content_name, $event_type);
impl crate::RoomEvent for $name {
/// The unique identifier for the event.
fn event_id(&self) -> &EventId {
&self.event_id
}
/// Time on originating homeserver when this event was sent.
fn origin_server_ts(&self) -> ::std::time::SystemTime {
self.origin_server_ts
}
/// The unique identifier for the room associated with this event.
///
/// This can be `None` if the event came from a context where there is
/// no ambiguity which room it belongs to, like a `/sync` response for example.
fn room_id(&self) -> Option<&::ruma_identifiers::RoomId> {
self.room_id.as_ref()
}
/// The unique identifier for the user who sent this event.
fn sender(&self) -> &::ruma_identifiers::UserId {
&self.sender
}
/// Additional key-value pairs not signed by the homeserver.
fn unsigned(&self) -> &::ruma_events::UnsignedData {
&self.unsigned
}
}
};
}
macro_rules! impl_state_event {
($name:ident, $content_name:ident, $event_type:path) => {
impl_room_event!($name, $content_name, $event_type);
impl crate::StateEvent for $name {
/// The previous content for this state key, if any.
fn prev_content(&self) -> Option<&Self::Content> {
self.prev_content.as_ref()
}
/// A key that determines which piece of room state the event represents.
fn state_key(&self) -> &str {
&self.state_key
}
}
};
}
macro_rules! impl_from_for_enum {
($self_ty:ident, $inner_ty:ty, $variant:ident) => {
impl ::std::convert::From<$inner_ty> for $self_ty {
fn from(event: $inner_ty) -> Self {
$self_ty::$variant(event)
}
}
};
}

View File

@ -51,7 +51,7 @@ mod tests {
use ruma_identifiers::UserId;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{PresenceEvent, PresenceEventContent, PresenceState};
use super::{PresenceEventContent, PresenceState};
use crate::EventJson;
#[test]

View File

@ -153,7 +153,7 @@ mod tests {
use matches::assert_matches;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{PushCondition, PushRulesEvent};
use super::PushCondition;
use crate::EventJson;
#[test]

View File

@ -9,20 +9,20 @@ use serde::{Deserialize, Serialize};
pub mod aliases;
pub mod avatar;
pub mod canonical_alias;
// pub mod canonical_alias;
pub mod create;
pub mod encrypted;
pub mod encryption;
pub mod guest_access;
pub mod history_visibility;
pub mod join_rules;
pub mod member;
pub mod message;
pub mod name;
pub mod pinned_events;
pub mod power_levels;
// pub mod member;
// pub mod message;
// pub mod name;
// pub mod pinned_events;
// pub mod power_levels;
pub mod redaction;
pub mod server_acl;
// pub mod server_acl;
pub mod third_party_invite;
pub mod tombstone;
pub mod topic;

View File

@ -6,36 +6,7 @@ use js_int::UInt;
use ruma_identifiers::{DeviceId, EventId, RoomId, UserId};
use serde::{Deserialize, Serialize};
use crate::{EventType, FromRaw, UnsignedData};
/// This event type is used when sending encrypted events.
///
/// This type is to be used within a room. For a to-device event, use `EncryptedEventContent`
/// directly.
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "type", rename = "m.room.encrypted")]
pub struct EncryptedEvent {
/// The event's content.
pub content: EncryptedEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The unique identifier for the room associated with this event.
#[serde(skip_serializing_if = "Option::is_none")]
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// Additional key-value pairs not signed by the homeserver.
#[serde(skip_serializing_if = "UnsignedData::is_empty")]
pub unsigned: UnsignedData,
}
use crate::{FromRaw, UnsignedData};
/// The payload for `EncryptedEvent`.
#[derive(Clone, Debug, Serialize)]
@ -51,21 +22,6 @@ pub enum EncryptedEventContent {
MegolmV1AesSha2(MegolmV1AesSha2Content),
}
impl FromRaw for EncryptedEvent {
type Raw = raw::EncryptedEvent;
fn from_raw(raw: raw::EncryptedEvent) -> Self {
Self {
content: FromRaw::from_raw(raw.content),
event_id: raw.event_id,
origin_server_ts: raw.origin_server_ts,
room_id: raw.room_id,
sender: raw.sender,
unsigned: raw.unsigned,
}
}
}
impl FromRaw for EncryptedEventContent {
type Raw = raw::EncryptedEventContent;
@ -81,12 +37,6 @@ impl FromRaw for EncryptedEventContent {
}
}
impl_room_event!(
EncryptedEvent,
EncryptedEventContent,
EventType::RoomEncrypted
);
pub(crate) mod raw {
use std::time::SystemTime;
@ -96,33 +46,6 @@ pub(crate) mod raw {
use super::{MegolmV1AesSha2Content, OlmV1Curve25519AesSha2Content};
use crate::UnsignedData;
/// This event type is used when sending encrypted events.
///
/// This type is to be used within a room. For a to-device event, use `EncryptedEventContent`
/// directly.
#[derive(Clone, Debug, Deserialize)]
pub struct EncryptedEvent {
/// The event's content.
pub content: EncryptedEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The unique identifier for the room associated with this event.
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// Additional key-value pairs not signed by the homeserver.
#[serde(default)]
pub unsigned: UnsignedData,
}
/// The payload for `EncryptedEvent`.
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "algorithm")]

View File

@ -154,44 +154,44 @@ pub enum MembershipChange {
NotImplemented,
}
impl MemberEvent {
/// Helper function for membership change. Check [the specification][spec] for details.
///
/// [spec]: https://matrix.org/docs/spec/client_server/latest#m-room-member
pub fn membership_change(&self) -> MembershipChange {
use MembershipState::*;
let prev_membership = if let Some(prev_content) = &self.prev_content {
prev_content.membership
} else {
Leave
};
match (prev_membership, &self.content.membership) {
(Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None,
(Invite, Join) | (Leave, Join) => MembershipChange::Joined,
(Invite, Leave) => {
if self.sender == self.state_key {
MembershipChange::InvitationRevoked
} else {
MembershipChange::InvitationRejected
}
}
(Invite, Ban) | (Leave, Ban) => MembershipChange::Banned,
(Join, Invite) | (Ban, Invite) | (Ban, Join) => MembershipChange::Error,
(Join, Join) => MembershipChange::ProfileChanged,
(Join, Leave) => {
if self.sender == self.state_key {
MembershipChange::Left
} else {
MembershipChange::Kicked
}
}
(Join, Ban) => MembershipChange::KickedAndBanned,
(Leave, Invite) => MembershipChange::Invited,
(Ban, Leave) => MembershipChange::Unbanned,
(Knock, _) | (_, Knock) => MembershipChange::NotImplemented,
}
}
}
// impl MemberEvent {
// /// Helper function for membership change. Check [the specification][spec] for details.
// ///
// /// [spec]: https://matrix.org/docs/spec/client_server/latest#m-room-member
// pub fn membership_change(&self) -> MembershipChange {
// use MembershipState::*;
// let prev_membership = if let Some(prev_content) = &self.prev_content {
// prev_content.membership
// } else {
// Leave
// };
// match (prev_membership, &self.content.membership) {
// (Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None,
// (Invite, Join) | (Leave, Join) => MembershipChange::Joined,
// (Invite, Leave) => {
// if self.sender == self.state_key {
// MembershipChange::InvitationRevoked
// } else {
// MembershipChange::InvitationRejected
// }
// }
// (Invite, Ban) | (Leave, Ban) => MembershipChange::Banned,
// (Join, Invite) | (Ban, Invite) | (Ban, Join) => MembershipChange::Error,
// (Join, Join) => MembershipChange::ProfileChanged,
// (Join, Leave) => {
// if self.sender == self.state_key {
// MembershipChange::Left
// } else {
// MembershipChange::Kicked
// }
// }
// (Join, Ban) => MembershipChange::KickedAndBanned,
// (Leave, Invite) => MembershipChange::Invited,
// (Ban, Leave) => MembershipChange::Unbanned,
// (Knock, _) | (_, Knock) => MembershipChange::NotImplemented,
// }
// }
// }
#[cfg(test)]
mod tests {

View File

@ -7,36 +7,10 @@ use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{Deserialize, Serialize};
use super::{EncryptedFile, ImageInfo, ThumbnailInfo};
use crate::{EventType, FromRaw, UnsignedData};
use crate::{FromRaw, UnsignedData};
pub mod feedback;
/// A message sent to a room.
#[derive(Clone, Debug, Serialize)]
#[serde(rename = "m.room.message", tag = "type")]
pub struct MessageEvent {
/// The event's content.
pub content: MessageEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The unique identifier for the room associated with this event.
#[serde(skip_serializing_if = "Option::is_none")]
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// Additional key-value pairs not signed by the homeserver.
#[serde(skip_serializing_if = "UnsignedData::is_empty")]
pub unsigned: UnsignedData,
}
/// The payload for `MessageEvent`.
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "msgtype")]
@ -78,21 +52,6 @@ pub enum MessageEventContent {
Video(VideoMessageEventContent),
}
impl FromRaw for MessageEvent {
type Raw = raw::MessageEvent;
fn from_raw(raw: raw::MessageEvent) -> Self {
Self {
content: FromRaw::from_raw(raw.content),
event_id: raw.event_id,
origin_server_ts: raw.origin_server_ts,
room_id: raw.room_id,
sender: raw.sender,
unsigned: raw.unsigned,
}
}
}
impl FromRaw for MessageEventContent {
type Raw = raw::MessageEventContent;
@ -113,8 +72,6 @@ impl FromRaw for MessageEventContent {
}
}
impl_room_event!(MessageEvent, MessageEventContent, EventType::RoomMessage);
pub(crate) mod raw {
use std::time::SystemTime;
@ -128,30 +85,6 @@ pub(crate) mod raw {
};
use crate::UnsignedData;
/// A message sent to a room.
#[derive(Clone, Debug, Deserialize)]
pub struct MessageEvent {
/// The event's content.
pub content: MessageEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The unique identifier for the room associated with this event.
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// Additional key-value pairs not signed by the homeserver.
#[serde(default)]
pub unsigned: UnsignedData,
}
/// The payload for `MessageEvent`.
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize)]

View File

@ -5,40 +5,7 @@ use std::time::SystemTime;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{Deserialize, Serialize};
use crate::{EventType, InvalidInput, TryFromRaw, UnsignedData};
/// A human-friendly room name designed to be displayed to the end-user.
#[derive(Clone, Debug, Serialize)]
#[serde(rename = "m.room.name", tag = "type")]
pub struct NameEvent {
/// The event's content.
pub content: NameEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The previous content for this state key, if any.
#[serde(skip_serializing_if = "Option::is_none")]
pub prev_content: Option<NameEventContent>,
/// The unique identifier for the room associated with this event.
#[serde(skip_serializing_if = "Option::is_none")]
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// A key that determines which piece of room state the event represents.
pub state_key: String,
/// Additional key-value pairs not signed by the homeserver.
#[serde(skip_serializing_if = "UnsignedData::is_empty")]
pub unsigned: UnsignedData,
}
use crate::{InvalidInput, TryFromRaw, UnsignedData};
/// The payload for `NameEvent`.
#[derive(Clone, Debug, Serialize)]
@ -47,27 +14,6 @@ pub struct NameEventContent {
pub(crate) name: Option<String>,
}
impl TryFromRaw for NameEvent {
type Raw = raw::NameEvent;
type Err = InvalidInput;
fn try_from_raw(raw: Self::Raw) -> Result<Self, Self::Err> {
let content = TryFromRaw::try_from_raw(raw.content)?;
let prev_content = raw.prev_content.map(TryFromRaw::try_from_raw).transpose()?;
Ok(NameEvent {
content,
event_id: raw.event_id,
origin_server_ts: raw.origin_server_ts,
prev_content,
room_id: raw.room_id,
sender: raw.sender,
state_key: raw.state_key,
unsigned: raw.unsigned,
})
}
}
impl TryFromRaw for NameEventContent {
type Raw = raw::NameEventContent;
@ -81,8 +27,6 @@ impl TryFromRaw for NameEventContent {
}
}
impl_state_event!(NameEvent, NameEventContent, EventType::RoomName);
impl NameEventContent {
/// Create a new `NameEventContent` with the given name.
///
@ -108,36 +52,6 @@ impl NameEventContent {
pub(crate) mod raw {
use super::*;
/// A human-friendly room name designed to be displayed to the end-user.
#[derive(Clone, Debug, Deserialize)]
pub struct NameEvent {
/// The event's content.
pub content: NameEventContent,
/// The unique identifier for the event.
pub event_id: EventId,
/// Time on originating homeserver when this event was sent.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The previous content for this state key, if any.
pub prev_content: Option<NameEventContent>,
/// The unique identifier for the room associated with this event.
pub room_id: Option<RoomId>,
/// The unique identifier for the user who sent this event.
pub sender: UserId,
/// A key that determines which piece of room state the event represents.
pub state_key: String,
/// Additional key-value pairs not signed by the homeserver.
#[serde(default)]
pub unsigned: UnsignedData,
}
/// The payload of a `NameEvent`.
#[derive(Clone, Debug, Deserialize)]
pub struct NameEventContent {
@ -164,7 +78,7 @@ mod tests {
use crate::{EventJson, UnsignedData};
use super::{NameEvent, NameEventContent};
use super::NameEventContent;
#[test]
fn serialization_with_optional_fields_as_none() {

View File

@ -24,7 +24,7 @@ mod tests {
use crate::{
room::pinned_events::{PinnedEventsEvent, PinnedEventsEventContent},
Event, EventJson, RoomEvent, StateEvent, UnsignedData,
EventJson, UnsignedData,
};
#[test]
@ -52,14 +52,14 @@ mod tests {
.deserialize()
.unwrap();
assert_eq!(parsed_event.event_id(), event.event_id());
assert_eq!(parsed_event.room_id(), event.room_id());
assert_eq!(parsed_event.sender(), event.sender());
assert_eq!(parsed_event.state_key(), event.state_key());
assert_eq!(parsed_event.origin_server_ts(), event.origin_server_ts());
assert_eq!(parsed_event.event_id, event.event_id);
assert_eq!(parsed_event.room_id, event.room_id);
assert_eq!(parsed_event.sender, event.sender);
assert_eq!(parsed_event.state_key, event.state_key);
assert_eq!(parsed_event.origin_server_ts, event.origin_server_ts);
assert_eq!(parsed_event.content().pinned, event.content.pinned);
assert_eq!(parsed_event.content().pinned[0], content.pinned[0]);
assert_eq!(parsed_event.content().pinned[1], content.pinned[1]);
assert_eq!(parsed_event.content.pinned, event.content.pinned);
assert_eq!(parsed_event.content.pinned[0], content.pinned[0]);
assert_eq!(parsed_event.content.pinned[1], content.pinned[1]);
}
}