1//! `/v1/` ([spec])
2//!
3//! [spec]: https://spec.matrix.org/latest/server-server-api/#put_matrixfederationv1inviteroomideventid
45use 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;
1415const METADATA: Metadata = metadata! {
16 method: PUT,
17 rate_limited: false,
18 authentication: ServerSignatures,
19 history: {
201.0 => "/_matrix/federation/v1/invite/:room_id/:event_id",
21 }
22};
2324/// 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)]
29pub room_id: OwnedRoomId,
3031/// The event ID for the invite event, generated by the inviting server.
32#[ruma_api(path)]
33pub event_id: OwnedEventId,
3435/// The matrix ID of the user who sent the original `m.room.third_party_invite`.
36pub sender: OwnedUserId,
3738/// The name of the inviting homeserver.
39pub origin: OwnedServerName,
4041/// A timestamp added by the inviting homeserver.
42pub origin_server_ts: MilliSecondsSinceUnixEpoch,
4344/// The value `m.room.member`.
45#[serde(rename = "type")]
46pub kind: StateEventType,
4748/// The user ID of the invited member.
49pub state_key: OwnedUserId,
5051/// The content of the event.
52pub content: RoomMemberEventContent,
5354/// Information included alongside the event that is not signed.
55#[serde(default, skip_serializing_if = "UnsignedEventContent::is_empty")]
56pub unsigned: UnsignedEventContent,
57}
5859/// 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")]
65pub event: Box<RawJsonValue>,
66}
6768/// 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")]
76pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
77}
7879impl UnsignedEventContent {
80/// Creates an empty `UnsignedEventContent`.
81pub fn new() -> Self {
82 Default::default()
83 }
8485/// Checks whether all of the fields are empty.
86pub fn is_empty(&self) -> bool {
87self.invite_room_state.is_empty()
88 }
89}
9091/// 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.
96pub room_id: OwnedRoomId,
9798/// The event ID for the invite event, generated by the inviting server.
99pub event_id: OwnedEventId,
100101/// The matrix ID of the user who sent the original `m.room.third_party_invite`.
102pub sender: OwnedUserId,
103104/// The name of the inviting homeserver.
105pub origin: OwnedServerName,
106107/// A timestamp added by the inviting homeserver.
108pub origin_server_ts: MilliSecondsSinceUnixEpoch,
109110/// The user ID of the invited member.
111pub state_key: OwnedUserId,
112113/// The content of the event.
114pub content: RoomMemberEventContent,
115116/// Information included alongside the event that is not signed.
117pub unsigned: UnsignedEventContent,
118}
119120impl From<RequestInit> for Request {
121/// Creates a new `Request` from `RequestInit`.
122fn from(init: RequestInit) -> Self {
123Self {
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}
136137impl Response {
138/// Creates a new `Response` with the given invite event.
139pub fn new(event: Box<RawJsonValue>) -> Self {
140Self { event }
141 }
142}