Skip to main content

tuwunel_service/rooms/state_res/events/
third_party_invite.rs

1//! Types to deserialize `m.room.third_party_invite` events.
2
3use std::{collections::BTreeSet, ops::Deref};
4
5use ruma::{serde::from_raw_json_value, third_party_invite::IdentityServerBase64PublicKey};
6use serde::Deserialize;
7use tuwunel_core::{Error, Result, err, matrix::Event};
8
9/// A helper type for an [`Event`] of type `m.room.third_party_invite`.
10///
11/// This is a type that deserializes each field lazily, when requested.
12#[derive(Debug, Clone)]
13pub struct RoomThirdPartyInviteEvent<E: Event>(E);
14
15impl<E: Event> RoomThirdPartyInviteEvent<E> {
16	/// Construct a new `RoomThirdPartyInviteEvent` around the given event.
17	pub fn new(event: E) -> Self { Self(event) }
18
19	/// The public keys of the identity server that might be used to sign the
20	/// third-party invite.
21	pub(crate) fn public_keys(&self) -> Result<BTreeSet<IdentityServerBase64PublicKey>> {
22		#[derive(Deserialize)]
23		struct RoomThirdPartyInviteContentPublicKeys {
24			public_key: Option<IdentityServerBase64PublicKey>,
25			#[serde(default)]
26			public_keys: Vec<PublicKey>,
27		}
28
29		#[derive(Deserialize)]
30		struct PublicKey {
31			public_key: IdentityServerBase64PublicKey,
32		}
33
34		let content: RoomThirdPartyInviteContentPublicKeys = from_raw_json_value(self.content())
35			.map_err(|err: Error| {
36				err!(
37					"invalid `public_key` or `public_keys` in `m.room.third_party_invite` \
38					 event: {err}"
39				)
40			})?;
41
42		let public_keys = content
43			.public_keys
44			.into_iter()
45			.map(|k| k.public_key);
46
47		Ok(content
48			.public_key
49			.into_iter()
50			.chain(public_keys)
51			.collect())
52	}
53}
54
55impl<E: Event> Deref for RoomThirdPartyInviteEvent<E> {
56	type Target = E;
57
58	#[inline]
59	fn deref(&self) -> &Self::Target { &self.0 }
60}