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