ruma_federation_api/membership/create_invite/
v1.rs

1//! `/v1/` ([spec])
2//!
3//! [spec]: https://spec.matrix.org/latest/server-server-api/#put_matrixfederationv1inviteroomideventid
4
5use ruma_common::{
6    api::{request, response, Metadata},
7    metadata,
8    serde::Raw,
9    MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedServerName, OwnedUserId,
10};
11use ruma_events::{room::member::RoomMemberEventContent, AnyStrippedStateEvent, StateEventType};
12use serde::{Deserialize, Serialize};
13use serde_json::value::RawValue as RawJsonValue;
14
15const METADATA: Metadata = metadata! {
16    method: PUT,
17    rate_limited: false,
18    authentication: ServerSignatures,
19    history: {
20        1.0 => "/_matrix/federation/v1/invite/:room_id/:event_id",
21    }
22};
23
24/// Request type for the `create_invite` endpoint.
25#[request]
26pub struct Request {
27    /// The room ID that the user is being invited to.
28    #[ruma_api(path)]
29    pub room_id: OwnedRoomId,
30
31    /// The event ID for the invite event, generated by the inviting server.
32    #[ruma_api(path)]
33    pub event_id: OwnedEventId,
34
35    /// The matrix ID of the user who sent the original `m.room.third_party_invite`.
36    pub sender: OwnedUserId,
37
38    /// The name of the inviting homeserver.
39    pub origin: OwnedServerName,
40
41    /// A timestamp added by the inviting homeserver.
42    pub origin_server_ts: MilliSecondsSinceUnixEpoch,
43
44    /// The value `m.room.member`.
45    #[serde(rename = "type")]
46    pub kind: StateEventType,
47
48    /// The user ID of the invited member.
49    pub state_key: OwnedUserId,
50
51    /// The content of the event.
52    pub content: RoomMemberEventContent,
53
54    /// Information included alongside the event that is not signed.
55    #[serde(default, skip_serializing_if = "UnsignedEventContent::is_empty")]
56    pub unsigned: UnsignedEventContent,
57}
58
59/// Response type for the `create_invite` endpoint.
60#[response]
61pub struct Response {
62    /// The signed invite event.
63    #[ruma_api(body)]
64    #[serde(with = "crate::serde::v1_pdu")]
65    pub event: Box<RawJsonValue>,
66}
67
68/// Information included alongside an event that is not signed.
69#[derive(Clone, Debug, Default, Serialize, Deserialize)]
70#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
71pub struct UnsignedEventContent {
72    /// An optional list of simplified events to help the receiver of the invite identify the room.
73    /// The recommended events to include are the join rules, canonical alias, avatar, and name of
74    /// the room.
75    #[serde(skip_serializing_if = "<[_]>::is_empty")]
76    pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
77}
78
79impl UnsignedEventContent {
80    /// Creates an empty `UnsignedEventContent`.
81    pub fn new() -> Self {
82        Default::default()
83    }
84
85    /// Checks whether all of the fields are empty.
86    pub fn is_empty(&self) -> bool {
87        self.invite_room_state.is_empty()
88    }
89}
90
91/// Initial set of fields of `Request`.
92#[derive(Debug)]
93#[allow(clippy::exhaustive_structs)]
94pub struct RequestInit {
95    /// The room ID that the user is being invited to.
96    pub room_id: OwnedRoomId,
97
98    /// The event ID for the invite event, generated by the inviting server.
99    pub event_id: OwnedEventId,
100
101    /// The matrix ID of the user who sent the original `m.room.third_party_invite`.
102    pub sender: OwnedUserId,
103
104    /// The name of the inviting homeserver.
105    pub origin: OwnedServerName,
106
107    /// A timestamp added by the inviting homeserver.
108    pub origin_server_ts: MilliSecondsSinceUnixEpoch,
109
110    /// The user ID of the invited member.
111    pub state_key: OwnedUserId,
112
113    /// The content of the event.
114    pub content: RoomMemberEventContent,
115
116    /// Information included alongside the event that is not signed.
117    pub unsigned: UnsignedEventContent,
118}
119
120impl From<RequestInit> for Request {
121    /// Creates a new `Request` from `RequestInit`.
122    fn from(init: RequestInit) -> Self {
123        Self {
124            room_id: init.room_id,
125            event_id: init.event_id,
126            sender: init.sender,
127            origin: init.origin,
128            origin_server_ts: init.origin_server_ts,
129            kind: StateEventType::RoomMember,
130            state_key: init.state_key,
131            content: init.content,
132            unsigned: init.unsigned,
133        }
134    }
135}
136
137impl Response {
138    /// Creates a new `Response` with the given invite event.
139    pub fn new(event: Box<RawJsonValue>) -> Self {
140        Self { event }
141    }
142}