ruwuma/src/presence.rs
Ragotzy.devin 800fba7c32
Implement PresenceEvent and EphemeralEvent
* Add derive for PresenceEventContent and create struct AnyPresenceEventContent
since there is only one content type
* Add derive for Ephemeral event and create enum AnyEphemeralEventContent, convert
receipt and typing to use derive(EphemeralEventContent) over ruma_api!
2020-06-07 22:56:43 +02:00

129 lines
4.2 KiB
Rust

//! A presence event is represented by a parameterized struct.
//!
//! There is only one type that will satisfy the bounds of `PresenceEventContent`
//! as this event has only one possible content value according to Matrix spec.
use js_int::UInt;
pub use ruma_common::presence::PresenceState;
use ruma_events_macros::Event;
use ruma_identifiers::UserId;
use serde::{Deserialize, Serialize};
/// Presence event.
#[derive(Clone, Debug, Event)]
pub struct PresenceEvent {
/// Data specific to the event type.
pub content: PresenceEventContent,
/// Contains the fully-qualified ID of the user who sent this event.
pub sender: UserId,
}
/// Informs the room of members presence.
///
/// This is the only event content a `PresenceEvent` can contain as it's
/// `content` field.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PresenceEventContent {
/// The current avatar URL for this user.
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<String>,
/// Whether or not the user is currently active.
#[serde(skip_serializing_if = "Option::is_none")]
pub currently_active: Option<bool>,
/// The current display name for this user.
#[serde(skip_serializing_if = "Option::is_none")]
pub displayname: Option<String>,
/// The last time since this user performed some action, in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub last_active_ago: Option<UInt>,
/// The presence state for this user.
pub presence: PresenceState,
/// An optional description to accompany the presence.
#[serde(skip_serializing_if = "Option::is_none")]
pub status_msg: Option<String>,
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use js_int::UInt;
use matches::assert_matches;
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 crate::EventJson;
#[test]
fn serialization() {
let event = PresenceEvent {
content: PresenceEventContent {
avatar_url: Some("mxc://localhost:wefuiwegh8742w".to_string()),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(UInt::try_from(2_478_593).unwrap()),
presence: PresenceState::Online,
status_msg: Some("Making cupcakes".to_string()),
},
sender: UserId::try_from("@example:localhost").unwrap(),
};
let json = json!({
"content": {
"avatar_url": "mxc://localhost:wefuiwegh8742w",
"currently_active": false,
"last_active_ago": 2_478_593,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
});
assert_eq!(to_json_value(&event).unwrap(), json);
}
#[test]
fn deserialization() {
let json = json!({
"content": {
"avatar_url": "mxc://localhost:wefuiwegh8742w",
"currently_active": false,
"last_active_ago": 2_478_593,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
});
assert_matches!(
from_json_value::<EventJson<PresenceEvent>>(json)
.unwrap()
.deserialize()
.unwrap(),
PresenceEvent {
content: PresenceEventContent {
avatar_url: Some(avatar_url),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(last_active_ago),
presence: PresenceState::Online,
status_msg: Some(status_msg),
},
sender,
} if avatar_url == "mxc://localhost:wefuiwegh8742w"
&& status_msg == "Making cupcakes"
&& sender == "@example:localhost"
&& last_active_ago == UInt::from(2_478_593u32)
);
}
}