events: Add RoomMemberUnsigned with invite_room_state field

This commit is contained in:
Jonas Platte 2022-08-17 12:55:10 +02:00 committed by Jonas Platte
parent aa8e48d1c3
commit e468a45426
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 86 additions and 7 deletions

View File

@ -28,6 +28,9 @@ Breaking changes:
* Move `receipt::ReceiptType` to `events::receipt` * Move `receipt::ReceiptType` to `events::receipt`
* Make `Clone` as supertrait of `api::OutgoingRequest` * Make `Clone` as supertrait of `api::OutgoingRequest`
* Rename `Any[Sync]RoomEvent` to `Any[Sync]TimelineEvent` * Rename `Any[Sync]RoomEvent` to `Any[Sync]TimelineEvent`
* `RoomMemberEvent` and related types now have a custom unsigned type including the
`invite_room_state` field, instead of the `StateUnsigned` type used by other state
events
[spec]: https://github.com/matrix-org/matrix-spec-proposals/pull/3669 [spec]: https://github.com/matrix-org/matrix-spec-proposals/pull/3669

View File

@ -4,18 +4,20 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use js_int::Int;
use ruma_macros::EventContent; use ruma_macros::EventContent;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue; use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue};
use crate::{ use crate::{
events::{ events::{
EventContent, HasDeserializeFields, RedactContent, RedactedEventContent, AnyStrippedStateEvent, EventContent, HasDeserializeFields, RedactContent,
RedactedStateEventContent, StateEventContent, StateEventType, StateUnsigned, RedactedEventContent, RedactedStateEventContent, Relations, StateEventContent,
StateEventType, StateUnsigned, StateUnsignedFromParts, StaticEventContent,
}, },
serde::StringEnum, serde::{CanBeEmpty, Raw, StringEnum},
OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedUserId, PrivOwnedStr, OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId,
RoomVersionId, PrivOwnedStr, RoomVersionId,
}; };
mod change; mod change;
@ -45,7 +47,13 @@ pub use self::change::{Change, MembershipChange, MembershipDetails};
/// must be assumed as leave. /// must be assumed as leave.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.member", kind = State, state_key_type = OwnedUserId, custom_redacted)] #[ruma_event(
type = "m.room.member",
kind = State,
state_key_type = OwnedUserId,
unsigned_type = RoomMemberUnsigned,
custom_redacted,
)]
pub struct RoomMemberEventContent { pub struct RoomMemberEventContent {
/// The avatar URL for this user, if any. /// The avatar URL for this user, if any.
/// ///
@ -453,6 +461,74 @@ impl StrippedRoomMemberEvent {
} }
} }
/// Extra information about a message event that is not incorporated into the event's hash.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct RoomMemberUnsigned {
/// The time in milliseconds that has elapsed since the event was sent.
///
/// This field is generated by the local homeserver, and may be incorrect if the local time on
/// at least one of the two servers is out of sync, which can cause the age to either be
/// negative or greater than it actually is.
#[serde(skip_serializing_if = "Option::is_none")]
pub age: Option<Int>,
/// 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")]
pub transaction_id: Option<OwnedTransactionId>,
/// Optional previous content of the event.
#[serde(skip_serializing_if = "Option::is_none")]
pub prev_content: Option<RoomMemberEventContent>,
/// State events to assist the receiver in identifying the room.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
/// [Bundled aggregations] of related child events.
///
/// [Bundled aggregations]: https://spec.matrix.org/v1.3/client-server-api/#aggregations
#[serde(rename = "m.relations", skip_serializing_if = "Option::is_none")]
pub relations: Option<Relations>,
}
impl RoomMemberUnsigned {
/// Create a new `Unsigned` with fields set to `None`.
pub fn new() -> Self {
Self::default()
}
}
impl CanBeEmpty for RoomMemberUnsigned {
/// Whether this unsigned data is empty (all fields are `None`).
///
/// This method is used to determine whether to skip serializing the `unsigned` field in room
/// events. Do not use it to determine whether an incoming `unsigned` field was present - it
/// could still have been present but contained none of the known fields.
fn is_empty(&self) -> bool {
self.age.is_none()
&& self.transaction_id.is_none()
&& self.prev_content.is_none()
&& self.invite_room_state.is_empty()
&& self.relations.is_none()
}
}
impl StateUnsignedFromParts for RoomMemberUnsigned {
fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result<Self> {
const EVENT_TYPE: &str = <RoomMemberEventContent as StaticEventContent>::TYPE;
if event_type != EVENT_TYPE {
return Err(serde::de::Error::custom(format!(
"expected event type of `{EVENT_TYPE}`, found `{event_type}`",
)));
}
from_json_str(object.get())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;