ruma_identity_service_api/invitation/
store_invitation.rs1pub mod v2 {
6 use ruma_common::{
11 api::{request, response, Metadata},
12 metadata,
13 room::RoomType,
14 third_party_invite::IdentityServerBase64PublicKey,
15 thirdparty::Medium,
16 OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId, OwnedUserId,
17 };
18 use ruma_events::room::third_party_invite::RoomThirdPartyInviteEventContent;
19 use serde::{ser::SerializeSeq, Deserialize, Serialize};
20
21 const METADATA: Metadata = metadata! {
22 method: POST,
23 rate_limited: false,
24 authentication: AccessToken,
25 history: {
26 1.0 => "/_matrix/identity/v2/store-invite",
27 }
28 };
29
30 #[request]
32 pub struct Request {
33 pub medium: Medium,
37
38 pub address: String,
40
41 pub room_id: OwnedRoomId,
43
44 pub sender: OwnedUserId,
46
47 #[serde(skip_serializing_if = "Option::is_none")]
51 pub room_alias: Option<OwnedRoomAliasId>,
52
53 #[serde(skip_serializing_if = "Option::is_none")]
57 pub room_avatar_url: Option<OwnedMxcUri>,
58
59 #[serde(skip_serializing_if = "Option::is_none")]
63 pub room_join_rules: Option<String>,
64
65 #[serde(skip_serializing_if = "Option::is_none")]
69 pub room_name: Option<String>,
70
71 #[serde(skip_serializing_if = "Option::is_none")]
75 pub room_type: Option<RoomType>,
76
77 #[serde(skip_serializing_if = "Option::is_none")]
79 pub sender_display_name: Option<String>,
80
81 #[serde(skip_serializing_if = "Option::is_none")]
83 pub sender_avatar_url: Option<OwnedMxcUri>,
84 }
85
86 #[response]
88 pub struct Response {
89 pub token: String,
94
95 pub public_keys: PublicKeys,
97
98 pub display_name: String,
102 }
103
104 impl Request {
105 pub fn new(
107 medium: Medium,
108 address: String,
109 room_id: OwnedRoomId,
110 sender: OwnedUserId,
111 ) -> Self {
112 Self {
113 medium,
114 address,
115 room_id,
116 sender,
117 room_alias: None,
118 room_avatar_url: None,
119 room_join_rules: None,
120 room_name: None,
121 room_type: None,
122 sender_display_name: None,
123 sender_avatar_url: None,
124 }
125 }
126
127 pub fn email(address: String, room_id: OwnedRoomId, sender: OwnedUserId) -> Self {
129 Self::new(Medium::Email, address, room_id, sender)
130 }
131 }
132
133 impl Response {
134 pub fn new(token: String, public_keys: PublicKeys, display_name: String) -> Self {
136 Self { token, public_keys, display_name }
137 }
138 }
139
140 #[derive(Debug, Clone)]
142 #[allow(clippy::exhaustive_structs)]
143 pub struct PublicKeys {
144 pub server_key: PublicKey,
146
147 pub ephemeral_key: PublicKey,
149 }
150
151 impl<'de> Deserialize<'de> for PublicKeys {
152 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
153 where
154 D: serde::Deserializer<'de>,
155 {
156 let [server_key, ephemeral_key] = <[PublicKey; 2]>::deserialize(deserializer)?;
157
158 Ok(Self { server_key, ephemeral_key })
159 }
160 }
161
162 impl Serialize for PublicKeys {
163 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
164 where
165 S: serde::Serializer,
166 {
167 let mut seq = serializer.serialize_seq(Some(2))?;
168
169 seq.serialize_element(&self.server_key)?;
170 seq.serialize_element(&self.ephemeral_key)?;
171
172 seq.end()
173 }
174 }
175
176 #[derive(Clone, Debug, Serialize, Deserialize)]
178 #[non_exhaustive]
179 pub struct PublicKey {
180 pub public_key: IdentityServerBase64PublicKey,
182
183 pub key_validity_url: String,
186 }
187
188 impl PublicKey {
189 pub fn new(public_key: IdentityServerBase64PublicKey, key_validity_url: String) -> Self {
191 Self { public_key, key_validity_url }
192 }
193 }
194
195 impl From<PublicKey> for ruma_events::room::third_party_invite::PublicKey {
196 fn from(key: PublicKey) -> Self {
197 let mut new_key = Self::new(key.public_key);
198 new_key.key_validity_url = Some(key.key_validity_url);
199 new_key
200 }
201 }
202
203 impl From<Response> for RoomThirdPartyInviteEventContent {
204 fn from(response: Response) -> Self {
205 let mut content = RoomThirdPartyInviteEventContent::new(
206 response.display_name,
207 response.public_keys.server_key.key_validity_url.clone(),
208 response.public_keys.server_key.public_key.clone(),
209 );
210 content.public_keys = Some(vec![
211 response.public_keys.server_key.into(),
212 response.public_keys.ephemeral_key.into(),
213 ]);
214 content
215 }
216 }
217}