events: Add support for MSC4171

This commit is contained in:
Damir Jelić 2024-11-26 16:08:32 +01:00 committed by strawberry
parent e31b9dd3a4
commit d82e2a02d5
6 changed files with 125 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# [unreleased] # [unreleased]
Improvements:
- Add unstable support for MSC4171 for the m.member_hints state event.
Breaking changes: Breaking changes:
- Take newly introduced `DirectUserIdentifier(str)` as a key for `DirectEventContent`. - Take newly introduced `DirectUserIdentifier(str)` as a key for `DirectEventContent`.

View File

@ -43,6 +43,7 @@ unstable-msc3955 = ["unstable-msc1767"]
unstable-msc3956 = ["unstable-msc1767"] unstable-msc3956 = ["unstable-msc1767"]
unstable-msc4075 = ["unstable-msc3401"] unstable-msc4075 = ["unstable-msc3401"]
unstable-msc4095 = [] unstable-msc4095 = []
unstable-msc4171 = []
unstable-pdu = [] unstable-pdu = []
# Allow some mandatory fields to be missing, defaulting them to an empty string # Allow some mandatory fields to be missing, defaulting them to an empty string

View File

@ -138,6 +138,9 @@ event_enum! {
"org.matrix.msc3401.call.member" => super::call::member, "org.matrix.msc3401.call.member" => super::call::member,
#[ruma_enum(alias = "m.room.preview_urls")] #[ruma_enum(alias = "m.room.preview_urls")]
"org.matrix.room.preview_urls" => super::room::preview_url, "org.matrix.room.preview_urls" => super::room::preview_url,
#[cfg(feature = "unstable-msc4171")]
#[ruma_enum(alias = "m.member_hints")]
"io.element.functional_members" => super::member_hints,
} }
/// Any to-device event. /// Any to-device event.

View File

@ -162,6 +162,8 @@ pub mod key;
#[cfg(feature = "unstable-msc3488")] #[cfg(feature = "unstable-msc3488")]
pub mod location; pub mod location;
pub mod marked_unread; pub mod marked_unread;
#[cfg(feature = "unstable-msc4171")]
pub mod member_hints;
#[cfg(feature = "unstable-msc1767")] #[cfg(feature = "unstable-msc1767")]
pub mod message; pub mod message;
#[cfg(feature = "unstable-pdu")] #[cfg(feature = "unstable-pdu")]

View File

@ -0,0 +1,113 @@
//! Types for Matrix member hint state events ([MSC4171]).
//!
//! This implements `m.member_hints` state event described in [MSC4171].
//!
//! [MSC4171]: https://github.com/matrix-org/matrix-spec-proposals/pull/4171
use std::collections::BTreeSet;
use ruma_common::OwnedUserId;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use crate::EmptyStateKey;
/// The content for an `m.member_hints` state event.
///
/// Any users (service members) listed in the content should not be considered when computing the
/// room name or avatar based on the member list.
#[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 {
/// The list of user IDs that should be considered a service member of the room.
pub service_members: BTreeSet<OwnedUserId>,
}
impl MemberHintsEventContent {
/// Create a new [`MemberHintsEventContent`] with the given set of service members.
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"
);
}
}

View File

@ -270,6 +270,7 @@ unstable-msc4108 = ["ruma-client-api?/unstable-msc4108"]
unstable-msc4121 = ["ruma-client-api?/unstable-msc4121"] unstable-msc4121 = ["ruma-client-api?/unstable-msc4121"]
unstable-msc4125 = ["ruma-federation-api?/unstable-msc4125"] unstable-msc4125 = ["ruma-federation-api?/unstable-msc4125"]
unstable-msc4140 = ["ruma-client-api?/unstable-msc4140"] unstable-msc4140 = ["ruma-client-api?/unstable-msc4140"]
unstable-msc4171 = ["ruma-events?/unstable-msc4171"]
unstable-msc4186 = ["ruma-client-api?/unstable-msc4186"] unstable-msc4186 = ["ruma-client-api?/unstable-msc4186"]
unstable-msc4210 = ["ruma-common/unstable-msc4210"] unstable-msc4210 = ["ruma-common/unstable-msc4210"]
unstable-pdu = ["ruma-events?/unstable-pdu"] unstable-pdu = ["ruma-events?/unstable-pdu"]
@ -324,6 +325,7 @@ __unstable-mscs = [
"unstable-msc4121", "unstable-msc4121",
"unstable-msc4125", "unstable-msc4125",
"unstable-msc4140", "unstable-msc4140",
"unstable-msc4171",
"unstable-msc4186", "unstable-msc4186",
] ]
__ci = [ __ci = [