From af3a14e3b4c3bf5c572b469bf968680478f047ce Mon Sep 17 00:00:00 2001 From: Florian Jacob Date: Sat, 1 Sep 2018 01:17:11 +0200 Subject: [PATCH] Apply upcoming spec change to m.presence event representation, making ruma-client more interoperable with synapse. See https://github.com/matrix-org/matrix-doc/pull/1137 for the details of the spec change. Fixes https://github.com/ruma/ruma-client-api/issues/27 --- src/lib.rs | 1 + src/presence.rs | 54 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aed404f6..a1e15bba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,6 +95,7 @@ //! However, the `ruma_events::collections::only::Event` enum does *not* include *m.room.message*, //! because *m.room.message* implements a *more specific* event trait than `Event`. +#![feature(try_from)] #![deny(missing_debug_implementations)] #![deny(missing_docs)] #![deny(warnings)] diff --git a/src/presence.rs b/src/presence.rs index 07ff6360..64a05187 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -1,17 +1,17 @@ //! Types for the *m.presence* event. -use ruma_identifiers::{EventId, UserId}; +use ruma_identifiers::UserId; event! { /// Informs the client of a user's presence state change. pub struct PresenceEvent(PresenceEventContent) { - /// The unique identifier for the event. - pub event_id: EventId + /// The unique identifier for the user associated with this event. + pub sender: UserId } } /// The payload of a `PresenceEvent`. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct PresenceEventContent { /// The current avatar URL for this user. #[serde(skip_serializing_if = "Option::is_none")] @@ -31,9 +31,6 @@ pub struct PresenceEventContent { /// The presence state for this user. pub presence: PresenceState, - - /// The unique identifier for the user associated with this event. - pub user_id: UserId, } /// A description of a user's connectivity and availability for chat. @@ -59,3 +56,46 @@ impl_enum! { Unavailable => "unavailable", } } + +#[cfg(test)] +mod tests { + use serde_json::{from_str, to_string}; + use std::convert::TryFrom; + + use ruma_identifiers::UserId; + use super::{PresenceEvent, PresenceEventContent, PresenceState}; + use super::super::{EventType}; + + /// Test serialization and deserialization of example m.presence event from the spec + /// https://github.com/turt2live/matrix-doc/blob/master/event-schemas/examples/m.presence + #[test] + fn test_example_event() { + let event = PresenceEvent { + content: PresenceEventContent { + avatar_url: Some("mxc://localhost:wefuiwegh8742w".to_string()), + currently_active: Some(false), + displayname: None, + last_active_ago: Some(2478593), + presence: PresenceState::Online, + }, + event_type: EventType::Presence, + sender: UserId::try_from("@example:localhost").unwrap(), + }; + let serialized_event = + r#"{"content":{"avatar_url":"mxc://localhost:wefuiwegh8742w","currently_active":false,"last_active_ago":2478593,"presence":"online"},"type":"m.presence","sender":"@example:localhost"}"#; + + assert_eq!( + to_string(&event).unwrap(), + serialized_event + ); + let deserialized_event = from_str::(serialized_event).unwrap(); + assert_eq!( + deserialized_event.content, + event.content + ); + assert_eq!( + deserialized_event.sender, + event.sender + ); + } +}