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