ruma_events/
member_hints.rsuse std::collections::BTreeSet;
use ruma_common::OwnedUserId;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use crate::EmptyStateKey;
#[derive(Clone, Debug, Default, Serialize, Deserialize, EventContent, PartialEq)]
#[ruma_event(type = "io.element.functional_members", kind = State, state_key_type = EmptyStateKey)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct MemberHintsEventContent {
pub service_members: BTreeSet<OwnedUserId>,
}
impl MemberHintsEventContent {
pub fn new(service_members: BTreeSet<OwnedUserId>) -> Self {
Self { service_members }
}
}
#[cfg(test)]
mod test {
use std::collections::BTreeSet;
use assert_matches2::assert_matches;
use ruma_common::user_id;
use serde_json::{from_value as from_json_value, json};
use super::*;
use crate::AnyStateEvent;
#[test]
fn deserialize() {
let user_id = user_id!("@slackbot:matrix.org");
let data = json!({
"type": "io.element.functional_members",
"state_key": "",
"content": {
"service_members": [
user_id,
]
},
"origin_server_ts": 111,
"event_id": "$3qfxjGYSu4sL25FtR0ep6vePOc",
"room_id": "!1234:example.org",
"sender": "@user:example.org"
});
let event = from_json_value::<AnyStateEvent>(data)
.expect("We should be able to deserialize the member hints event");
assert_matches!(event, AnyStateEvent::MemberHints(event));
assert_matches!(event, crate::StateEvent::Original(event));
assert!(event.content.service_members.contains(user_id));
let data = json!({
"type": "m.member_hints",
"state_key": "",
"content": {
"service_members": [
user_id,
]
},
"origin_server_ts": 111,
"event_id": "$3qfxjGYSu4sL25FtR0ep6vePOc",
"room_id": "!1234:example.org",
"sender": "@user:example.org"
});
let event = from_json_value::<AnyStateEvent>(data)
.expect("We should be able to deserialize the member hints event");
assert_matches!(event, AnyStateEvent::MemberHints(event));
assert_matches!(event, crate::StateEvent::Original(event));
assert!(event.content.service_members.contains(user_id));
}
#[test]
fn serialize() {
let user_id = user_id!("@slackbot:matrix.org");
let content = MemberHintsEventContent::new(BTreeSet::from([user_id.to_owned()]));
let serialized = serde_json::to_value(content)
.expect("We should be able to serialize the member hints content");
let expected = json!({
"service_members": [
user_id,
]
});
assert_eq!(
expected, serialized,
"The serialized member hints content should match the expected one"
);
}
}