Skip to main content

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