Skip to main content

ruma_events/room/
member.rs

1//! Types for the [`m.room.member`] event.
2//!
3//! [`m.room.member`]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
4
5use js_int::Int;
6#[cfg(feature = "unstable-msc4293")]
7use ruma_common::canonical_json::RedactionEvent;
8use ruma_common::{
9    OwnedEventId, OwnedMxcUri, OwnedTransactionId, OwnedUserId, ServerSignatures, UserId,
10    room_version_rules::RedactionRules,
11    serde::{CanBeEmpty, Raw, StringEnum},
12};
13use ruma_macros::EventContent;
14use serde::{Deserialize, Serialize};
15
16use crate::{
17    AnyStrippedStateEvent, BundledStateRelations, PossiblyRedactedStateEventContent, PrivOwnedStr,
18    RedactContent, RedactedStateEventContent, StateEventType, StaticEventContent,
19};
20
21mod change;
22
23use self::change::membership_change;
24pub use self::change::{Change, MembershipChange, MembershipDetails};
25
26/// The content of an `m.room.member` event.
27///
28/// The current membership state of a user in the room.
29///
30/// Adjusts the membership state for a user in a room. It is preferable to use the membership
31/// APIs (`/rooms/<room id>/invite` etc) when performing membership actions rather than
32/// adjusting the state directly as there are a restricted set of valid transformations. For
33/// example, user A cannot force user B to join a room, and trying to force this state change
34/// directly will fail.
35///
36/// This event may also include an `invite_room_state` key inside the event's unsigned data, but
37/// Ruma doesn't currently expose this; see [#998](https://github.com/ruma/ruma/issues/998).
38///
39/// The user for which a membership applies is represented by the `state_key`. Under some
40/// conditions, the `sender` and `state_key` may not match - this may be interpreted as the
41/// `sender` affecting the membership state of the `state_key` user.
42///
43/// The membership for a given user can change over time. Previous membership can be retrieved
44/// from the `prev_content` object on an event. If not present, the user's previous membership
45/// must be assumed as leave.
46#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
47#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
48#[ruma_event(
49    type = "m.room.member",
50    kind = State,
51    state_key_type = OwnedUserId,
52    unsigned_type = RoomMemberUnsigned,
53    custom_redacted,
54    custom_possibly_redacted,
55)]
56pub struct RoomMemberEventContent {
57    /// The avatar URL for this user, if any.
58    ///
59    /// This is added by the homeserver. If you activate the `compat-empty-string-null` feature,
60    /// this field being an empty string in JSON will result in `None` here during deserialization.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    #[cfg_attr(
63        feature = "compat-empty-string-null",
64        serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
65    )]
66    pub avatar_url: Option<OwnedMxcUri>,
67
68    /// The display name for this user, if any.
69    ///
70    /// This is added by the homeserver.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub displayname: Option<String>,
73
74    /// Flag indicating whether the room containing this event was created with the intention of
75    /// being a direct chat.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub is_direct: Option<bool>,
78
79    /// The membership state of this user.
80    pub membership: MembershipState,
81
82    /// If this member event is the successor to a third party invitation, this field will
83    /// contain information about that invitation.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub third_party_invite: Option<ThirdPartyInvite>,
86
87    /// The [BlurHash](https://blurha.sh) for the avatar pointed to by `avatar_url`.
88    ///
89    /// This uses the unstable prefix in
90    /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
91    #[cfg(feature = "unstable-msc2448")]
92    #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")]
93    pub blurhash: Option<String>,
94
95    /// User-supplied text for why their membership has changed.
96    ///
97    /// For kicks and bans, this is typically the reason for the kick or ban. For other membership
98    /// changes, this is a way for the user to communicate their intent without having to send a
99    /// message to the room, such as in a case where Bob rejects an invite from Alice about an
100    /// upcoming concert, but can't make it that day.
101    ///
102    /// Clients are not recommended to show this reason to users when receiving an invite due to
103    /// the potential for spam and abuse. Hiding the reason behind a button or other component
104    /// is recommended.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub reason: Option<String>,
107
108    /// Arbitrarily chosen `UserId` (MxID) of a local user who can send an invite.
109    #[serde(rename = "join_authorised_via_users_server")]
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub join_authorized_via_users_server: Option<OwnedUserId>,
112
113    /// Flag indicating all of this user's events should be redacted.
114    #[cfg(feature = "unstable-msc4293")]
115    #[serde(
116        default,
117        rename = "org.matrix.msc4293.redact_events",
118        skip_serializing_if = "ruma_common::serde::is_default"
119    )]
120    pub redact_events: bool,
121}
122
123impl RoomMemberEventContent {
124    /// Creates a new `RoomMemberEventContent` with the given membership state.
125    pub fn new(membership: MembershipState) -> Self {
126        Self {
127            membership,
128            avatar_url: None,
129            displayname: None,
130            is_direct: None,
131            third_party_invite: None,
132            #[cfg(feature = "unstable-msc2448")]
133            blurhash: None,
134            reason: None,
135            join_authorized_via_users_server: None,
136            #[cfg(feature = "unstable-msc4293")]
137            redact_events: false,
138        }
139    }
140
141    /// Obtain the details about this event that are required to calculate a membership change.
142    ///
143    /// This is required when you want to calculate the change a redacted `m.room.member` event
144    /// made.
145    pub fn details(&self) -> MembershipDetails<'_> {
146        MembershipDetails {
147            avatar_url: self.avatar_url.as_deref(),
148            displayname: self.displayname.as_deref(),
149            membership: &self.membership,
150        }
151    }
152
153    /// Helper function for membership change.
154    ///
155    /// This requires data from the full event:
156    ///
157    /// * The previous details computed from `event.unsigned.prev_content`,
158    /// * The sender of the event,
159    /// * The state key of the event.
160    ///
161    /// Check [the specification][spec] for details.
162    ///
163    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
164    pub fn membership_change<'a>(
165        &'a self,
166        prev_details: Option<MembershipDetails<'a>>,
167        sender: &UserId,
168        state_key: &UserId,
169    ) -> MembershipChange<'a> {
170        membership_change(self.details(), prev_details, sender, state_key)
171    }
172}
173
174impl RedactContent for RoomMemberEventContent {
175    type Redacted = RedactedRoomMemberEventContent;
176
177    fn redact(self, rules: &RedactionRules) -> RedactedRoomMemberEventContent {
178        RedactedRoomMemberEventContent {
179            membership: self.membership,
180            third_party_invite: self.third_party_invite.and_then(|i| i.redact(rules)),
181            join_authorized_via_users_server: self
182                .join_authorized_via_users_server
183                .filter(|_| rules.keep_room_member_join_authorised_via_users_server),
184        }
185    }
186}
187
188/// The possibly redacted form of [`RoomMemberEventContent`].
189///
190/// This type is used when it's not obvious whether the content is redacted or not.
191#[derive(Clone, Debug, Deserialize, Serialize)]
192#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
193pub struct PossiblyRedactedRoomMemberEventContent {
194    /// The avatar URL for this user, if any.
195    ///
196    /// This is added by the homeserver. If you activate the `compat-empty-string-null` feature,
197    /// this field being an empty string in JSON will result in `None` here during deserialization.
198    #[serde(skip_serializing_if = "Option::is_none")]
199    #[cfg_attr(
200        feature = "compat-empty-string-null",
201        serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
202    )]
203    pub avatar_url: Option<OwnedMxcUri>,
204
205    /// The display name for this user, if any.
206    ///
207    /// This is added by the homeserver.
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub displayname: Option<String>,
210
211    /// Flag indicating whether the room containing this event was created with the intention of
212    /// being a direct chat.
213    #[serde(skip_serializing_if = "Option::is_none")]
214    pub is_direct: Option<bool>,
215
216    /// The membership state of this user.
217    pub membership: MembershipState,
218
219    /// If this member event is the successor to a third party invitation, this field will
220    /// contain information about that invitation.
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub third_party_invite: Option<PossiblyRedactedThirdPartyInvite>,
223
224    /// The [BlurHash](https://blurha.sh) for the avatar pointed to by `avatar_url`.
225    ///
226    /// This uses the unstable prefix in
227    /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
228    #[cfg(feature = "unstable-msc2448")]
229    #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")]
230    pub blurhash: Option<String>,
231
232    /// User-supplied text for why their membership has changed.
233    ///
234    /// For kicks and bans, this is typically the reason for the kick or ban. For other membership
235    /// changes, this is a way for the user to communicate their intent without having to send a
236    /// message to the room, such as in a case where Bob rejects an invite from Alice about an
237    /// upcoming concert, but can't make it that day.
238    ///
239    /// Clients are not recommended to show this reason to users when receiving an invite due to
240    /// the potential for spam and abuse. Hiding the reason behind a button or other component
241    /// is recommended.
242    #[serde(skip_serializing_if = "Option::is_none")]
243    pub reason: Option<String>,
244
245    /// Arbitrarily chosen `UserId` (MxID) of a local user who can send an invite.
246    #[serde(rename = "join_authorised_via_users_server")]
247    #[serde(skip_serializing_if = "Option::is_none")]
248    pub join_authorized_via_users_server: Option<OwnedUserId>,
249
250    /// Flag indicating all of this user's events should be redacted.
251    ///
252    /// This uses the unstable prefix defined in [MSC4293].
253    ///
254    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
255    #[cfg(feature = "unstable-msc4293")]
256    #[serde(
257        default,
258        rename = "org.matrix.msc4293.redact_events",
259        skip_serializing_if = "ruma_common::serde::is_default"
260    )]
261    pub redact_events: bool,
262}
263
264impl PossiblyRedactedRoomMemberEventContent {
265    /// Creates a new `PossiblyRedactedRoomMemberEventContent` with the given membership state.
266    pub fn new(membership: MembershipState) -> Self {
267        Self {
268            membership,
269            avatar_url: None,
270            displayname: None,
271            is_direct: None,
272            third_party_invite: None,
273            #[cfg(feature = "unstable-msc2448")]
274            blurhash: None,
275            reason: None,
276            join_authorized_via_users_server: None,
277            #[cfg(feature = "unstable-msc4293")]
278            redact_events: false,
279        }
280    }
281
282    /// Obtain the details about this event that are required to calculate a membership change.
283    ///
284    /// This is required when you want to calculate the change a redacted `m.room.member` event
285    /// made.
286    pub fn details(&self) -> MembershipDetails<'_> {
287        MembershipDetails {
288            avatar_url: self.avatar_url.as_deref(),
289            displayname: self.displayname.as_deref(),
290            membership: &self.membership,
291        }
292    }
293
294    /// Helper function for membership change.
295    ///
296    /// This requires data from the full event:
297    ///
298    /// * The previous details computed from `event.unsigned.prev_content`,
299    /// * The sender of the event,
300    /// * The state key of the event.
301    ///
302    /// Check [the specification][spec] for details.
303    ///
304    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
305    pub fn membership_change<'a>(
306        &'a self,
307        prev_details: Option<MembershipDetails<'a>>,
308        sender: &UserId,
309        state_key: &UserId,
310    ) -> MembershipChange<'a> {
311        membership_change(self.details(), prev_details, sender, state_key)
312    }
313}
314
315impl PossiblyRedactedStateEventContent for PossiblyRedactedRoomMemberEventContent {
316    type StateKey = OwnedUserId;
317
318    fn event_type(&self) -> StateEventType {
319        StateEventType::RoomMember
320    }
321}
322
323impl StaticEventContent for PossiblyRedactedRoomMemberEventContent {
324    const TYPE: &'static str = RoomMemberEventContent::TYPE;
325    type IsPrefix = <RoomMemberEventContent as StaticEventContent>::IsPrefix;
326}
327
328impl RedactContent for PossiblyRedactedRoomMemberEventContent {
329    type Redacted = Self;
330
331    fn redact(self, rules: &RedactionRules) -> Self {
332        Self {
333            membership: self.membership,
334            third_party_invite: self.third_party_invite.and_then(|i| i.redact(rules)),
335            join_authorized_via_users_server: self
336                .join_authorized_via_users_server
337                .filter(|_| rules.keep_room_member_join_authorised_via_users_server),
338            avatar_url: None,
339            displayname: None,
340            is_direct: None,
341            #[cfg(feature = "unstable-msc2448")]
342            blurhash: None,
343            reason: None,
344            #[cfg(feature = "unstable-msc4293")]
345            redact_events: false,
346        }
347    }
348}
349
350impl From<RoomMemberEventContent> for PossiblyRedactedRoomMemberEventContent {
351    fn from(value: RoomMemberEventContent) -> Self {
352        let RoomMemberEventContent {
353            avatar_url,
354            displayname,
355            is_direct,
356            membership,
357            third_party_invite,
358            #[cfg(feature = "unstable-msc2448")]
359            blurhash,
360            reason,
361            join_authorized_via_users_server,
362            #[cfg(feature = "unstable-msc4293")]
363            redact_events,
364        } = value;
365
366        Self {
367            avatar_url,
368            displayname,
369            is_direct,
370            membership,
371            third_party_invite: third_party_invite.map(Into::into),
372            #[cfg(feature = "unstable-msc2448")]
373            blurhash,
374            reason,
375            join_authorized_via_users_server,
376            #[cfg(feature = "unstable-msc4293")]
377            redact_events,
378        }
379    }
380}
381
382impl From<RedactedRoomMemberEventContent> for PossiblyRedactedRoomMemberEventContent {
383    fn from(value: RedactedRoomMemberEventContent) -> Self {
384        let RedactedRoomMemberEventContent {
385            membership,
386            third_party_invite,
387            join_authorized_via_users_server,
388        } = value;
389
390        Self {
391            avatar_url: None,
392            displayname: None,
393            is_direct: None,
394            membership,
395            third_party_invite: third_party_invite.map(Into::into),
396            #[cfg(feature = "unstable-msc2448")]
397            blurhash: None,
398            reason: None,
399            join_authorized_via_users_server,
400            #[cfg(feature = "unstable-msc4293")]
401            redact_events: false,
402        }
403    }
404}
405
406/// A member event that has been redacted.
407#[derive(Clone, Debug, Deserialize, Serialize)]
408#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
409pub struct RedactedRoomMemberEventContent {
410    /// The membership state of this user.
411    pub membership: MembershipState,
412
413    /// If this member event is the successor to a third party invitation, this field will
414    /// contain information about that invitation.
415    #[serde(skip_serializing_if = "Option::is_none")]
416    pub third_party_invite: Option<RedactedThirdPartyInvite>,
417
418    /// An arbitrary user who has the power to issue invites.
419    ///
420    /// This is redacted in room versions 8 and below. It is used for validating
421    /// joins when the join rule is restricted.
422    #[serde(rename = "join_authorised_via_users_server", skip_serializing_if = "Option::is_none")]
423    pub join_authorized_via_users_server: Option<OwnedUserId>,
424}
425
426impl RedactedRoomMemberEventContent {
427    /// Create a `RedactedRoomMemberEventContent` with the given membership.
428    pub fn new(membership: MembershipState) -> Self {
429        Self { membership, third_party_invite: None, join_authorized_via_users_server: None }
430    }
431
432    /// Obtain the details about this event that are required to calculate a membership change.
433    ///
434    /// This is required when you want to calculate the change a redacted `m.room.member` event
435    /// made.
436    pub fn details(&self) -> MembershipDetails<'_> {
437        MembershipDetails { avatar_url: None, displayname: None, membership: &self.membership }
438    }
439
440    /// Helper function for membership change.
441    ///
442    /// Since redacted events don't have `unsigned.prev_content`, you have to pass the `.details()`
443    /// of the previous `m.room.member` event manually (if there is a previous `m.room.member`
444    /// event).
445    ///
446    /// This also requires data from the full event:
447    ///
448    /// * The sender of the event,
449    /// * The state key of the event.
450    ///
451    /// Check [the specification][spec] for details.
452    ///
453    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
454    pub fn membership_change<'a>(
455        &'a self,
456        prev_details: Option<MembershipDetails<'a>>,
457        sender: &UserId,
458        state_key: &UserId,
459    ) -> MembershipChange<'a> {
460        membership_change(self.details(), prev_details, sender, state_key)
461    }
462}
463
464impl RedactedStateEventContent for RedactedRoomMemberEventContent {
465    type StateKey = OwnedUserId;
466
467    fn event_type(&self) -> StateEventType {
468        StateEventType::RoomMember
469    }
470}
471
472impl StaticEventContent for RedactedRoomMemberEventContent {
473    const TYPE: &'static str = RoomMemberEventContent::TYPE;
474    type IsPrefix = <RoomMemberEventContent as StaticEventContent>::IsPrefix;
475}
476
477impl RoomMemberEvent {
478    /// Obtain the membership state, regardless of whether this event is redacted.
479    pub fn membership(&self) -> &MembershipState {
480        match self {
481            Self::Original(ev) => &ev.content.membership,
482            Self::Redacted(ev) => &ev.content.membership,
483        }
484    }
485
486    /// Determines whether the user's events should be redacted based on their membership.
487    ///
488    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
489    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
490    /// should be ignored, and `false` is returned.
491    ///
492    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
493    #[cfg(feature = "unstable-msc4293")]
494    pub fn should_redact_events(&self) -> bool {
495        if let Self::Original(ev) = self { ev.should_redact_events() } else { false }
496    }
497}
498
499impl SyncRoomMemberEvent {
500    /// Obtain the membership state, regardless of whether this event is redacted.
501    pub fn membership(&self) -> &MembershipState {
502        match self {
503            Self::Original(ev) => &ev.content.membership,
504            Self::Redacted(ev) => &ev.content.membership,
505        }
506    }
507
508    /// Determines whether the user's events should be redacted based on their membership.
509    ///
510    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
511    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
512    /// should be ignored, and `false` is returned.
513    ///
514    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
515    #[cfg(feature = "unstable-msc4293")]
516    pub fn should_redact_events(&self) -> bool {
517        if let Self::Original(ev) = self { ev.should_redact_events() } else { false }
518    }
519}
520
521/// The membership state of a user.
522#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
523#[derive(Clone, StringEnum)]
524#[ruma_enum(rename_all = "lowercase")]
525#[non_exhaustive]
526pub enum MembershipState {
527    /// The user is banned.
528    Ban,
529
530    /// The user has been invited.
531    Invite,
532
533    /// The user has joined.
534    Join,
535
536    /// The user has requested to join.
537    Knock,
538
539    /// The user has left.
540    Leave,
541
542    #[doc(hidden)]
543    _Custom(PrivOwnedStr),
544}
545
546/// Information about a third party invitation.
547#[derive(Clone, Debug, Deserialize, Serialize)]
548#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
549pub struct ThirdPartyInvite {
550    /// A name which can be displayed to represent the user instead of their third party
551    /// identifier.
552    pub display_name: String,
553
554    /// A block of content which has been signed, which servers can use to verify the event.
555    ///
556    /// Clients should ignore this.
557    pub signed: Raw<SignedContent>,
558}
559
560impl ThirdPartyInvite {
561    /// Creates a new `ThirdPartyInvite` with the given display name and signed content.
562    pub fn new(display_name: String, signed: Raw<SignedContent>) -> Self {
563        Self { display_name, signed }
564    }
565
566    /// Transform `self` into a redacted form (removing most or all fields) according to the spec.
567    ///
568    /// Returns `None` if the field for this object was redacted according to the given
569    /// [`RedactionRules`], otherwise returns the redacted form.
570    fn redact(self, rules: &RedactionRules) -> Option<RedactedThirdPartyInvite> {
571        rules
572            .keep_room_member_third_party_invite_signed
573            .then_some(RedactedThirdPartyInvite { signed: self.signed })
574    }
575}
576
577/// Redacted information about a third party invitation.
578#[derive(Clone, Debug, Deserialize, Serialize)]
579#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
580pub struct RedactedThirdPartyInvite {
581    /// A block of content which has been signed, which servers can use to verify the event.
582    ///
583    /// Clients should ignore this.
584    pub signed: Raw<SignedContent>,
585}
586
587/// Possibly redacted information about a third party invitation.
588#[derive(Clone, Debug, Deserialize, Serialize)]
589#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
590pub struct PossiblyRedactedThirdPartyInvite {
591    /// A name which can be displayed to represent the user instead of their third party
592    /// identifier.
593    #[serde(skip_serializing_if = "Option::is_none")]
594    pub display_name: Option<String>,
595
596    /// A block of content which has been signed, which servers can use to verify the event.
597    ///
598    /// Clients should ignore this.
599    pub signed: Raw<SignedContent>,
600}
601
602impl PossiblyRedactedThirdPartyInvite {
603    /// Creates a new `PossiblyRedactedThirdPartyInvite` with the given display name and signed
604    /// content.
605    pub fn new(display_name: String, signed: Raw<SignedContent>) -> Self {
606        Self { display_name: Some(display_name), signed }
607    }
608
609    /// Transform `self` into a redacted form (removing most or all fields) according to the spec.
610    ///
611    /// Returns `None` if the field for this object was redacted according to the given
612    /// [`RedactionRules`], otherwise returns the redacted form.
613    fn redact(self, rules: &RedactionRules) -> Option<Self> {
614        rules
615            .keep_room_member_third_party_invite_signed
616            .then_some(Self { display_name: None, signed: self.signed })
617    }
618}
619
620impl From<ThirdPartyInvite> for PossiblyRedactedThirdPartyInvite {
621    fn from(value: ThirdPartyInvite) -> Self {
622        let ThirdPartyInvite { display_name, signed } = value;
623        Self { display_name: Some(display_name), signed }
624    }
625}
626
627impl From<RedactedThirdPartyInvite> for PossiblyRedactedThirdPartyInvite {
628    fn from(value: RedactedThirdPartyInvite) -> Self {
629        let RedactedThirdPartyInvite { signed } = value;
630        Self { display_name: None, signed }
631    }
632}
633
634/// A block of content which has been signed, which servers can use to verify a third party
635/// invitation.
636#[derive(Clone, Debug, Deserialize, Serialize)]
637#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
638pub struct SignedContent {
639    /// The invited Matrix user ID.
640    ///
641    /// Must be equal to the user_id property of the event.
642    pub mxid: OwnedUserId,
643
644    /// A single signature from the verifying server, in the format specified by the Signing Events
645    /// section of the server-server API.
646    pub signatures: ServerSignatures,
647
648    /// The token property of the containing `third_party_invite` object.
649    pub token: String,
650}
651
652impl SignedContent {
653    /// Creates a new `SignedContent` with the given mxid, signature and token.
654    pub fn new(signatures: ServerSignatures, mxid: OwnedUserId, token: String) -> Self {
655        Self { mxid, signatures, token }
656    }
657}
658
659impl OriginalRoomMemberEvent {
660    /// Obtain the details about this event that are required to calculate a membership change.
661    ///
662    /// This is required when you want to calculate the change a redacted `m.room.member` event
663    /// made.
664    pub fn details(&self) -> MembershipDetails<'_> {
665        self.content.details()
666    }
667
668    /// Get a reference to the `prev_content` in unsigned, if it exists.
669    ///
670    /// Shorthand for `event.unsigned.prev_content.as_ref()`
671    pub fn prev_content(&self) -> Option<&PossiblyRedactedRoomMemberEventContent> {
672        self.unsigned.prev_content.as_ref()
673    }
674
675    /// Get a reference to the `prev_sender` in unsigned, if it exists.
676    ///
677    /// Shorthand for `event.unsigned.prev_sender.as_deref()`
678    pub fn prev_sender(&self) -> Option<&UserId> {
679        self.unsigned.prev_sender.as_deref()
680    }
681
682    fn prev_details(&self) -> Option<MembershipDetails<'_>> {
683        self.prev_content().map(|c| c.details())
684    }
685
686    /// Helper function for membership change.
687    ///
688    /// Check [the specification][spec] for details.
689    ///
690    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
691    pub fn membership_change(&self) -> MembershipChange<'_> {
692        membership_change(self.details(), self.prev_details(), &self.sender, &self.state_key)
693    }
694
695    /// Determines whether the user's events should be redacted based on their membership.
696    ///
697    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
698    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
699    /// should be ignored, and `false` is returned.
700    ///
701    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
702    #[cfg(feature = "unstable-msc4293")]
703    pub fn should_redact_events(&self) -> bool {
704        self.content.redact_events
705            && self.state_key != self.sender
706            && matches!(self.content.membership, MembershipState::Ban | MembershipState::Leave)
707    }
708}
709
710impl RedactedRoomMemberEvent {
711    /// Obtain the details about this event that are required to calculate a membership change.
712    ///
713    /// This is required when you want to calculate the change a redacted `m.room.member` event
714    /// made.
715    pub fn details(&self) -> MembershipDetails<'_> {
716        self.content.details()
717    }
718
719    /// Helper function for membership change.
720    ///
721    /// Since redacted events don't have `unsigned.prev_content`, you have to pass the `.details()`
722    /// of the previous `m.room.member` event manually (if there is a previous `m.room.member`
723    /// event).
724    ///
725    /// Check [the specification][spec] for details.
726    ///
727    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
728    pub fn membership_change<'a>(
729        &'a self,
730        prev_details: Option<MembershipDetails<'a>>,
731    ) -> MembershipChange<'a> {
732        membership_change(self.details(), prev_details, &self.sender, &self.state_key)
733    }
734
735    /// Determines whether the user's events should be redacted based on their membership.
736    ///
737    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
738    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
739    /// should be ignored, and `false` is returned.
740    ///
741    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
742    #[cfg(feature = "unstable-msc4293")]
743    pub fn should_redact_events(&self) -> bool {
744        // Redacted room member events lack the redact_events flag - see proposal.
745        false
746    }
747}
748
749impl OriginalSyncRoomMemberEvent {
750    /// Obtain the details about this event that are required to calculate a membership change.
751    ///
752    /// This is required when you want to calculate the change a redacted `m.room.member` event
753    /// made.
754    pub fn details(&self) -> MembershipDetails<'_> {
755        self.content.details()
756    }
757
758    /// Get a reference to the `prev_content` in unsigned, if it exists.
759    ///
760    /// Shorthand for `event.unsigned.prev_content.as_ref()`
761    pub fn prev_content(&self) -> Option<&PossiblyRedactedRoomMemberEventContent> {
762        self.unsigned.prev_content.as_ref()
763    }
764
765    /// Get a reference to the `prev_sender` in unsigned, if it exists.
766    ///
767    /// Shorthand for `event.unsigned.prev_sender.as_deref()`
768    pub fn prev_sender(&self) -> Option<&UserId> {
769        self.unsigned.prev_sender.as_deref()
770    }
771
772    fn prev_details(&self) -> Option<MembershipDetails<'_>> {
773        self.prev_content().map(|c| c.details())
774    }
775
776    /// Helper function for membership change.
777    ///
778    /// Check [the specification][spec] for details.
779    ///
780    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
781    pub fn membership_change(&self) -> MembershipChange<'_> {
782        membership_change(self.details(), self.prev_details(), &self.sender, &self.state_key)
783    }
784
785    /// Determines whether the user's events should be redacted based on their membership.
786    ///
787    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
788    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
789    /// should be ignored, and `false` is returned.
790    ///
791    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
792    #[cfg(feature = "unstable-msc4293")]
793    pub fn should_redact_events(&self) -> bool {
794        self.content.redact_events
795            && self.state_key != self.sender
796            && matches!(self.content.membership, MembershipState::Ban | MembershipState::Leave)
797    }
798}
799
800impl RedactedSyncRoomMemberEvent {
801    /// Obtain the details about this event that are required to calculate a membership change.
802    ///
803    /// This is required when you want to calculate the change a redacted `m.room.member` event
804    /// made.
805    pub fn details(&self) -> MembershipDetails<'_> {
806        self.content.details()
807    }
808
809    /// Helper function for membership change.
810    ///
811    /// Since redacted events don't have `unsigned.prev_content`, you have to pass the `.details()`
812    /// of the previous `m.room.member` event manually (if there is a previous `m.room.member`
813    /// event).
814    ///
815    /// Check [the specification][spec] for details.
816    ///
817    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
818    pub fn membership_change<'a>(
819        &'a self,
820        prev_details: Option<MembershipDetails<'a>>,
821    ) -> MembershipChange<'a> {
822        membership_change(self.details(), prev_details, &self.sender, &self.state_key)
823    }
824
825    /// Determines whether the user's events should be redacted based on their membership.
826    ///
827    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
828    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
829    /// should be ignored, and `false` is returned.
830    ///
831    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
832    #[cfg(feature = "unstable-msc4293")]
833    pub fn should_redact_events(&self) -> bool {
834        // Redacted room member events lack the redact_events flag - see proposal.
835        false
836    }
837}
838
839impl StrippedRoomMemberEvent {
840    /// Obtain the details about this event that are required to calculate a membership change.
841    ///
842    /// This is required when you want to calculate the change a redacted `m.room.member` event
843    /// made.
844    pub fn details(&self) -> MembershipDetails<'_> {
845        self.content.details()
846    }
847
848    /// Helper function for membership change.
849    ///
850    /// Since stripped events don't have `unsigned.prev_content`, you have to pass the `.details()`
851    /// of the previous `m.room.member` event manually (if there is a previous `m.room.member`
852    /// event).
853    ///
854    /// Check [the specification][spec] for details.
855    ///
856    /// [spec]: https://spec.matrix.org/v1.19/client-server-api/#mroommember
857    pub fn membership_change<'a>(
858        &'a self,
859        prev_details: Option<MembershipDetails<'a>>,
860    ) -> MembershipChange<'a> {
861        membership_change(self.details(), prev_details, &self.sender, &self.state_key)
862    }
863
864    /// Determines whether the user's events should be redacted based on their membership.
865    ///
866    /// Using [MSC4293], if `redact_events` is `true`, the sender is different to the state key,
867    /// and the membership is `ban` or `leave` (kick), `true` is returned. Otherwise, the flag
868    /// should be ignored, and `false` is returned.
869    ///
870    /// [MSC4293]: https://github.com/matrix-org/matrix-spec-proposals/pull/4293
871    #[cfg(feature = "unstable-msc4293")]
872    pub fn should_redact_events(&self) -> bool {
873        self.content.redact_events
874            && self.state_key != self.sender
875            && matches!(self.content.membership, MembershipState::Ban | MembershipState::Leave)
876    }
877}
878
879/// Extra information about a message event that is not incorporated into the event's hash.
880#[derive(Clone, Debug, Default, Deserialize)]
881#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
882pub struct RoomMemberUnsigned {
883    /// The time in milliseconds that has elapsed since the event was sent.
884    ///
885    /// This field is generated by the local homeserver, and may be incorrect if the local time on
886    /// at least one of the two servers is out of sync, which can cause the age to either be
887    /// negative or greater than it actually is.
888    pub age: Option<Int>,
889
890    /// The client-supplied transaction ID, if the client being given the event is the same one
891    /// which sent it.
892    pub transaction_id: Option<OwnedTransactionId>,
893
894    /// The event ID of the state event replaced by this event.
895    ///
896    /// This field is generated by the local homeserver. Unlike `prev_content`, it is included
897    /// regardless of history visibility.
898    pub replaces_state: Option<OwnedEventId>,
899
900    /// Optional previous content of the event.
901    pub prev_content: Option<PossiblyRedactedRoomMemberEventContent>,
902
903    /// The user ID of the sender of the state event replaced by this event.
904    ///
905    /// This field is generated by the local homeserver. Unlike `prev_content`, it is included
906    /// regardless of history visibility.
907    pub prev_sender: Option<OwnedUserId>,
908
909    /// Stripped state events to assist the receiver in identifying the room when receiving an
910    /// invite.
911    #[serde(default)]
912    pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
913
914    /// Stripped state events to assist the receiver in identifying the room after knocking.
915    #[serde(default)]
916    pub knock_room_state: Vec<Raw<AnyStrippedStateEvent>>,
917
918    /// [Bundled aggregations] of related child events.
919    ///
920    /// [Bundled aggregations]: https://spec.matrix.org/v1.19/client-server-api/#aggregations-of-child-events
921    #[serde(rename = "m.relations", default)]
922    pub relations: BundledStateRelations,
923}
924
925impl RoomMemberUnsigned {
926    /// Create a new `Unsigned` with fields set to `None`.
927    pub fn new() -> Self {
928        Self::default()
929    }
930}
931
932impl CanBeEmpty for RoomMemberUnsigned {
933    /// Whether this unsigned data is empty (all fields are `None`).
934    ///
935    /// This method is used to determine whether to skip serializing the `unsigned` field in room
936    /// events. Do not use it to determine whether an incoming `unsigned` field was present - it
937    /// could still have been present but contained none of the known fields.
938    fn is_empty(&self) -> bool {
939        let Self {
940            age,
941            transaction_id,
942            replaces_state,
943            prev_content,
944            prev_sender,
945            invite_room_state,
946            knock_room_state,
947            relations,
948        } = self;
949
950        age.is_none()
951            && transaction_id.is_none()
952            && replaces_state.is_none()
953            && prev_content.is_none()
954            && prev_sender.is_none()
955            && invite_room_state.is_empty()
956            && knock_room_state.is_empty()
957            && relations.is_empty()
958    }
959}
960
961#[cfg(feature = "unstable-msc4293")]
962impl RedactionEvent for OriginalRoomMemberEvent {}
963
964#[cfg(feature = "unstable-msc4293")]
965impl RedactionEvent for OriginalSyncRoomMemberEvent {}
966
967#[cfg(feature = "unstable-msc4293")]
968impl RedactionEvent for RoomMemberEvent {}
969
970#[cfg(feature = "unstable-msc4293")]
971impl RedactionEvent for SyncRoomMemberEvent {}
972
973#[cfg(test)]
974mod tests {
975    use assert_matches::assert_matches;
976    use js_int::uint;
977    use maplit::btreemap;
978    use ruma_common::{
979        MilliSecondsSinceUnixEpoch, ServerSigningKeyId, SigningKeyAlgorithm, serde::CanBeEmpty,
980        server_signing_key_version,
981    };
982    use serde_json::{from_value as from_json_value, json};
983    use strass::assert_variant_eq;
984
985    use super::{MembershipState, RoomMemberEventContent};
986    use crate::OriginalStateEvent;
987
988    #[test]
989    fn serde_with_no_prev_content() {
990        let json = json!({
991            "type": "m.room.member",
992            "content": {
993                "membership": "join"
994            },
995            "event_id": "$h29iv0s8:example.com",
996            "origin_server_ts": 1,
997            "room_id": "!n8f893n9:example.com",
998            "sender": "@carl:example.com",
999            "state_key": "@carl:example.com"
1000        });
1001
1002        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1003        assert_eq!(ev.event_id, "$h29iv0s8:example.com");
1004        assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1)));
1005        assert_eq!(ev.room_id, "!n8f893n9:example.com");
1006        assert_eq!(ev.sender, "@carl:example.com");
1007        assert_eq!(ev.state_key, "@carl:example.com");
1008        assert!(ev.unsigned.is_empty());
1009
1010        assert_eq!(ev.content.avatar_url, None);
1011        assert_eq!(ev.content.displayname, None);
1012        assert_eq!(ev.content.is_direct, None);
1013        assert_eq!(ev.content.membership, MembershipState::Join);
1014        assert_matches!(ev.content.third_party_invite, None);
1015    }
1016
1017    #[test]
1018    fn serde_with_prev_content() {
1019        let json = json!({
1020            "type": "m.room.member",
1021            "content": {
1022                "membership": "join"
1023            },
1024            "event_id": "$h29iv0s8:example.com",
1025            "origin_server_ts": 1,
1026            "room_id": "!n8f893n9:example.com",
1027            "sender": "@carl:example.com",
1028            "state_key": "@carl:example.com",
1029            "unsigned": {
1030                "prev_content": {
1031                    "membership": "join"
1032                },
1033            },
1034        });
1035
1036        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1037        assert_eq!(ev.event_id, "$h29iv0s8:example.com");
1038        assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1)));
1039        assert_eq!(ev.room_id, "!n8f893n9:example.com");
1040        assert_eq!(ev.sender, "@carl:example.com");
1041        assert_eq!(ev.state_key, "@carl:example.com");
1042
1043        assert_eq!(ev.content.avatar_url, None);
1044        assert_eq!(ev.content.displayname, None);
1045        assert_eq!(ev.content.is_direct, None);
1046        assert_eq!(ev.content.membership, MembershipState::Join);
1047        assert_matches!(ev.content.third_party_invite, None);
1048
1049        let prev_content = ev.unsigned.prev_content.unwrap();
1050        assert_eq!(prev_content.avatar_url, None);
1051        assert_eq!(prev_content.displayname, None);
1052        assert_eq!(prev_content.is_direct, None);
1053        assert_eq!(prev_content.membership, MembershipState::Join);
1054        assert_matches!(prev_content.third_party_invite, None);
1055    }
1056
1057    #[test]
1058    fn serde_with_replaces_state() {
1059        let json = json!({
1060            "type": "m.room.member",
1061            "content": {
1062                "membership": "join"
1063            },
1064            "event_id": "$h29iv0s8:example.com",
1065            "origin_server_ts": 1,
1066            "room_id": "!n8f893n9:example.com",
1067            "sender": "@carl:example.com",
1068            "state_key": "@carl:example.com",
1069            "unsigned": {
1070                "replaces_state": "$replaced:example.com",
1071            },
1072        });
1073
1074        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1075        assert_variant_eq!(ev.unsigned.replaces_state, Some("$replaced:example.com"));
1076        assert!(!ev.unsigned.is_empty());
1077    }
1078
1079    #[test]
1080    fn serde_with_prev_sender() {
1081        let json = json!({
1082            "type": "m.room.member",
1083            "content": {
1084                "membership": "join"
1085            },
1086            "event_id": "$h29iv0s8:example.com",
1087            "origin_server_ts": 1,
1088            "room_id": "!n8f893n9:example.com",
1089            "sender": "@carl:example.com",
1090            "state_key": "@carl:example.com",
1091            "unsigned": {
1092                "prev_sender": "@alice:example.com",
1093            },
1094        });
1095
1096        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1097        assert_variant_eq!(ev.unsigned.prev_sender, Some("@alice:example.com"));
1098        assert_variant_eq!(ev.prev_sender(), Some("@alice:example.com"));
1099        assert!(!ev.unsigned.is_empty());
1100    }
1101
1102    #[test]
1103    fn serde_with_content_full() {
1104        let json = json!({
1105            "type": "m.room.member",
1106            "content": {
1107                "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
1108                "displayname": "Alice Margatroid",
1109                "is_direct": true,
1110                "membership": "invite",
1111                "third_party_invite": {
1112                    "display_name": "alice",
1113                    "signed": {
1114                        "mxid": "@alice:example.org",
1115                        "signatures": {
1116                            "magic.forest": {
1117                                "ed25519:3": "foobar"
1118                            }
1119                        },
1120                        "token": "abc123"
1121                    }
1122                }
1123            },
1124            "event_id": "$143273582443PhrSn:example.org",
1125            "origin_server_ts": 233,
1126            "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
1127            "sender": "@alice:example.org",
1128            "state_key": "@alice:example.org"
1129        });
1130
1131        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1132        assert_eq!(ev.event_id, "$143273582443PhrSn:example.org");
1133        assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(233)));
1134        assert_eq!(ev.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
1135        assert_eq!(ev.sender, "@alice:example.org");
1136        assert_eq!(ev.state_key, "@alice:example.org");
1137        assert!(ev.unsigned.is_empty());
1138
1139        assert_variant_eq!(ev.content.avatar_url, Some("mxc://example.org/SEsfnsuifSDFSSEF"));
1140        assert_eq!(ev.content.displayname.as_deref(), Some("Alice Margatroid"));
1141        assert_eq!(ev.content.is_direct, Some(true));
1142        assert_eq!(ev.content.membership, MembershipState::Invite);
1143
1144        let third_party_invite = ev.content.third_party_invite.unwrap();
1145        assert_eq!(third_party_invite.display_name, "alice");
1146        let signed = third_party_invite.signed.deserialize().unwrap();
1147        assert_eq!(signed.mxid, "@alice:example.org");
1148        assert_eq!(signed.signatures.len(), 1);
1149        let server_signatures = signed.signatures.get("magic.forest").unwrap();
1150        assert_eq!(
1151            *server_signatures,
1152            btreemap! {
1153                ServerSigningKeyId::from_parts(
1154                    SigningKeyAlgorithm::Ed25519,
1155                    server_signing_key_version!("3")
1156                ) => "foobar".to_owned()
1157            }
1158        );
1159        assert_eq!(signed.token, "abc123");
1160    }
1161
1162    #[test]
1163    fn serde_with_prev_content_full() {
1164        let json = json!({
1165            "type": "m.room.member",
1166            "content": {
1167                "membership": "join",
1168            },
1169            "event_id": "$143273582443PhrSn:example.org",
1170            "origin_server_ts": 233,
1171            "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
1172            "sender": "@alice:example.org",
1173            "state_key": "@alice:example.org",
1174            "unsigned": {
1175                "prev_content": {
1176                    "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
1177                    "displayname": "Alice Margatroid",
1178                    "is_direct": true,
1179                    "membership": "invite",
1180                    "third_party_invite": {
1181                        "display_name": "alice",
1182                        "signed": {
1183                            "mxid": "@alice:example.org",
1184                            "signatures": {
1185                                "magic.forest": {
1186                                    "ed25519:3": "foobar",
1187                                },
1188                            },
1189                            "token": "abc123"
1190                        },
1191                    },
1192                },
1193            },
1194        });
1195
1196        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1197        assert_eq!(ev.event_id, "$143273582443PhrSn:example.org");
1198        assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(233)));
1199        assert_eq!(ev.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
1200        assert_eq!(ev.sender, "@alice:example.org");
1201        assert_eq!(ev.state_key, "@alice:example.org");
1202
1203        assert_eq!(ev.content.avatar_url, None);
1204        assert_eq!(ev.content.displayname, None);
1205        assert_eq!(ev.content.is_direct, None);
1206        assert_eq!(ev.content.membership, MembershipState::Join);
1207        assert_matches!(ev.content.third_party_invite, None);
1208
1209        let prev_content = ev.unsigned.prev_content.unwrap();
1210        assert_variant_eq!(prev_content.avatar_url, Some("mxc://example.org/SEsfnsuifSDFSSEF"));
1211        assert_eq!(prev_content.displayname.as_deref(), Some("Alice Margatroid"));
1212        assert_eq!(prev_content.is_direct, Some(true));
1213        assert_eq!(prev_content.membership, MembershipState::Invite);
1214
1215        let third_party_invite = prev_content.third_party_invite.unwrap();
1216        assert_eq!(third_party_invite.display_name.as_deref(), Some("alice"));
1217        let signed = third_party_invite.signed.deserialize().unwrap();
1218        assert_eq!(signed.mxid, "@alice:example.org");
1219        assert_eq!(signed.signatures.len(), 1);
1220        let server_signatures = signed.signatures.get("magic.forest").unwrap();
1221        assert_eq!(
1222            *server_signatures,
1223            btreemap! {
1224                ServerSigningKeyId::from_parts(
1225                    SigningKeyAlgorithm::Ed25519,
1226                    server_signing_key_version!("3")
1227                ) => "foobar".to_owned()
1228            }
1229        );
1230        assert_eq!(signed.token, "abc123");
1231    }
1232
1233    #[test]
1234    fn serde_with_join_authorized() {
1235        let json = json!({
1236            "type": "m.room.member",
1237            "content": {
1238                "membership": "join",
1239                "join_authorised_via_users_server": "@notcarl:example.com"
1240            },
1241            "event_id": "$h29iv0s8:example.com",
1242            "origin_server_ts": 1,
1243            "room_id": "!n8f893n9:example.com",
1244            "sender": "@carl:example.com",
1245            "state_key": "@carl:example.com"
1246        });
1247
1248        let ev = from_json_value::<OriginalStateEvent<RoomMemberEventContent>>(json).unwrap();
1249        assert_eq!(ev.event_id, "$h29iv0s8:example.com");
1250        assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1)));
1251        assert_eq!(ev.room_id, "!n8f893n9:example.com");
1252        assert_eq!(ev.sender, "@carl:example.com");
1253        assert_eq!(ev.state_key, "@carl:example.com");
1254        assert!(ev.unsigned.is_empty());
1255
1256        assert_eq!(ev.content.avatar_url, None);
1257        assert_eq!(ev.content.displayname, None);
1258        assert_eq!(ev.content.is_direct, None);
1259        assert_eq!(ev.content.membership, MembershipState::Join);
1260        assert_matches!(ev.content.third_party_invite, None);
1261        assert_variant_eq!(
1262            ev.content.join_authorized_via_users_server,
1263            Some("@notcarl:example.com")
1264        );
1265    }
1266}