ruma_events/room/member/
change.rs

1use ruma_common::{MxcUri, UserId};
2
3use super::MembershipState;
4
5/// The details of a member event required to calculate a [`MembershipChange`].
6#[derive(Clone, Debug)]
7pub struct MembershipDetails<'a> {
8    pub(crate) avatar_url: Option<&'a MxcUri>,
9    pub(crate) displayname: Option<&'a str>,
10    pub(crate) membership: &'a MembershipState,
11}
12
13/// Translation of the membership change in `m.room.member` event.
14#[derive(Clone, Debug)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16pub enum MembershipChange<'a> {
17    /// No change.
18    None,
19
20    /// Must never happen.
21    Error,
22
23    /// User joined the room.
24    Joined,
25
26    /// User left the room.
27    Left,
28
29    /// User was banned.
30    Banned,
31
32    /// User was unbanned.
33    Unbanned,
34
35    /// User was kicked.
36    Kicked,
37
38    /// User was invited.
39    Invited,
40
41    /// User was kicked and banned.
42    KickedAndBanned,
43
44    /// User accepted the invite.
45    InvitationAccepted,
46
47    /// User rejected the invite.
48    InvitationRejected,
49
50    /// User had their invite revoked.
51    InvitationRevoked,
52
53    /// User knocked.
54    Knocked,
55
56    /// User had their knock accepted.
57    KnockAccepted,
58
59    /// User retracted their knock.
60    KnockRetracted,
61
62    /// User had their knock denied.
63    KnockDenied,
64
65    /// `displayname` or `avatar_url` changed.
66    ProfileChanged {
67        /// The details of the displayname change, if applicable.
68        displayname_change: Option<Change<Option<&'a str>>>,
69
70        /// The details of the avatar url change, if applicable.
71        avatar_url_change: Option<Change<Option<&'a MxcUri>>>,
72    },
73
74    /// Not implemented.
75    NotImplemented,
76}
77
78/// A simple representation of a change, containing old and new data.
79#[derive(Clone, Debug)]
80#[allow(clippy::exhaustive_structs)]
81pub struct Change<T> {
82    /// The old data.
83    pub old: T,
84
85    /// The new data.
86    pub new: T,
87}
88
89impl<T: PartialEq> Change<T> {
90    fn new(old: T, new: T) -> Option<Self> {
91        if old == new {
92            None
93        } else {
94            Some(Self { old, new })
95        }
96    }
97}
98
99/// Internal function so all `RoomMemberEventContent` state event kinds can share the same
100/// implementation.
101///
102/// This must match the table for [`m.room.member`] in the spec.
103///
104/// [`m.room.member`]: https://spec.matrix.org/latest/client-server-api/#mroommember
105pub(super) fn membership_change<'a>(
106    details: MembershipDetails<'a>,
107    prev_details: Option<MembershipDetails<'a>>,
108    sender: &UserId,
109    state_key: &UserId,
110) -> MembershipChange<'a> {
111    use MembershipChange as Ch;
112    use MembershipState as St;
113
114    let prev_details = match prev_details {
115        Some(prev) => prev,
116        None => MembershipDetails { avatar_url: None, displayname: None, membership: &St::Leave },
117    };
118
119    match (&prev_details.membership, &details.membership) {
120        (St::Leave, St::Join) => Ch::Joined,
121        (St::Invite, St::Join) => Ch::InvitationAccepted,
122        (St::Invite, St::Leave) if sender == state_key => Ch::InvitationRejected,
123        (St::Invite, St::Leave) => Ch::InvitationRevoked,
124        (St::Invite, St::Ban) | (St::Leave, St::Ban) | (St::Knock, St::Ban) => Ch::Banned,
125        (St::Join, St::Invite)
126        | (St::Ban, St::Invite)
127        | (St::Ban, St::Join)
128        | (St::Join, St::Knock)
129        | (St::Invite, St::Knock)
130        | (St::Ban, St::Knock)
131        | (St::Knock, St::Join) => Ch::Error,
132        (St::Join, St::Join)
133            if sender == state_key
134                && (prev_details.displayname != details.displayname
135                    || prev_details.avatar_url != details.avatar_url) =>
136        {
137            Ch::ProfileChanged {
138                displayname_change: Change::new(prev_details.displayname, details.displayname),
139                avatar_url_change: Change::new(prev_details.avatar_url, details.avatar_url),
140            }
141        }
142        (St::Join, St::Leave) if sender == state_key => Ch::Left,
143        (St::Join, St::Leave) => Ch::Kicked,
144        (St::Join, St::Ban) => Ch::KickedAndBanned,
145        (St::Leave, St::Invite) => Ch::Invited,
146        (St::Ban, St::Leave) => Ch::Unbanned,
147        (St::Leave, St::Knock) => Ch::Knocked,
148        (St::Knock, St::Invite) => Ch::KnockAccepted,
149        (St::Knock, St::Leave) if sender == state_key => Ch::KnockRetracted,
150        (St::Knock, St::Leave) => Ch::KnockDenied,
151        (a, b) if a == b => Ch::None,
152        _ => Ch::NotImplemented,
153    }
154}