Skip to main content

tuwunel_service/sending/sender/dispatch/
appservice.rs

1use std::{
2	collections::{BTreeMap, BTreeSet},
3	str::from_utf8,
4};
5
6use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
7use futures::{StreamExt, future::join};
8use ruma::{
9	OneTimeKeyAlgorithm, OwnedDeviceId, OwnedUserId, UInt, UserId,
10	api::appservice::event::push_events::v1::{
11		AnyAppserviceToDeviceEvent, DeviceLists, EphemeralData, Request as PushEventsRequest,
12	},
13	events::AnyTimelineEvent,
14	serde::Raw,
15};
16use serde::Deserialize;
17use tuwunel_core::{
18	debug_warn, err, implement,
19	itertools::Itertools,
20	smallvec::SmallVec,
21	utils::{
22		IterStream, ReadyExt, calculate_hash,
23		stream::{BroadbandExt, WidebandExt},
24	},
25};
26use tuwunel_matrix::Event;
27
28use super::SendingResult;
29use crate::{
30	appservice::RegistrationInfo,
31	sending::{Destination, SendingEvent, Service, TAG_PREFIX_LEN},
32};
33
34/// The appservice-injected recipient fields of a queued to-device event
35/// (MSC4203).
36///
37/// Parsed to scope MSC3202 one-time-key counts to the addressed devices.
38#[derive(Deserialize)]
39struct ToDeviceRecipient {
40	to_user_id: OwnedUserId,
41	to_device_id: OwnedDeviceId,
42}
43
44/// The wire pieces of one appservice transaction, accumulated per event.
45///
46/// `otk_users` and `otk_recipients` are the MSC3202 one-time-key scope: the
47/// appservice sender, the namespace-matched PDU senders, and the to-device
48/// recipients of the transaction.
49#[derive(Default)]
50struct Parts {
51	pdus: Vec<Raw<AnyTimelineEvent>>,
52	edus: Vec<Raw<EphemeralData>>,
53	to_device: Vec<Raw<AnyAppserviceToDeviceEvent>>,
54	changed: Vec<OwnedUserId>,
55	otk_users: BTreeSet<OwnedUserId>,
56	otk_recipients: Devices,
57}
58
59/// One queued event rendered for the appservice transaction.
60///
61/// `None` is an event the appservice does not receive, or one that failed to
62/// load or decode; the transaction skips it.
63enum Part {
64	Pdu(Raw<AnyTimelineEvent>, Option<OwnedUserId>),
65	Edu(Raw<EphemeralData>),
66	ToDevice(Raw<AnyAppserviceToDeviceEvent>, Option<UserDevice>),
67	Changed(OwnedUserId),
68	None,
69}
70
71/// One device of one user.
72///
73/// The MSC3202 key-count maps are keyed by this pair.
74type UserDevice = (OwnedUserId, OwnedDeviceId);
75
76/// MSC3202 `device_one_time_keys_count`: unclaimed one-time-key counts per
77/// algorithm, keyed by user then device.
78///
79/// Matches the ruma request field type.
80type OtkCounts =
81	BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, BTreeMap<OneTimeKeyAlgorithm, UInt>>>;
82
83/// MSC3202 `device_unused_fallback_key_types`: algorithms with an unused
84/// fallback key, keyed by user then device.
85///
86/// Matches the ruma request field type.
87type FallbackTypes = BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Vec<OneTimeKeyAlgorithm>>>;
88
89/// The MSC3202-interesting devices of one transaction: the appservice
90/// sender's plus matched PDU senders' devices and the to-device recipients.
91///
92/// Sorted and deduplicated before the per-device key lookups fan out.
93type Devices = SmallVec<[UserDevice; 1]>;
94
95#[implement(Service)]
96#[tracing::instrument(
97	name = "appservice",
98	level = "debug",
99	skip(self, events),
100	fields(
101		events = %events.len(),
102	),
103)]
104pub(super) async fn send_events_dest_appservice(
105	&self,
106	id: String,
107	events: Vec<SendingEvent>,
108) -> SendingResult {
109	let Some(info) = self
110		.services
111		.appservice
112		.get_registration_info(&id)
113		.await
114	else {
115		//TODO: appservice queue cleanup.
116		return Err((
117			Destination::Appservice(id.clone()),
118			err!(Database(debug_warn!(?id, "Missing appservice registration"))),
119		));
120	};
121
122	let msc3202 = info.registration.msc3202_transaction_extensions;
123	let parts = Parts {
124		otk_users: msc3202
125			.then(|| info.sender.clone())
126			.into_iter()
127			.collect(),
128		..Default::default()
129	};
130
131	let Parts {
132		pdus,
133		edus,
134		to_device,
135		changed,
136		otk_users,
137		otk_recipients,
138	} = events
139		.iter()
140		.stream()
141		.wide_then(async |event| self.txn_part(&info, msc3202, event).await)
142		.ready_fold(parts, Parts::merge)
143		.await;
144
145	let txn_hash = calculate_hash(events.iter().filter_map(|e| match e {
146		| SendingEvent::Edu(b)
147		| SendingEvent::ToDevice(b)
148		| SendingEvent::DeviceListChanged(b) => Some(b.as_ref()),
149		| SendingEvent::Pdu(b) => Some(b.as_ref()),
150		| SendingEvent::BadgeRefresh | SendingEvent::Flush => None,
151	}));
152
153	let (device_lists, device_one_time_keys_count, device_unused_fallback_key_types) = if msc3202
154	{
155		let changed = changed
156			.into_iter()
157			.sorted_unstable()
158			.dedup()
159			.collect();
160
161		let (counts, fallbacks) = self
162			.msc3202_key_counts(otk_users, otk_recipients)
163			.await;
164
165		(DeviceLists { changed, left: Vec::new() }, counts, fallbacks)
166	} else {
167		(DeviceLists::new(), OtkCounts::new(), FallbackTypes::new())
168	};
169
170	if pdus.is_empty()
171		&& edus.is_empty()
172		&& to_device.is_empty()
173		&& device_lists.is_empty()
174		&& device_one_time_keys_count.is_empty()
175		&& device_unused_fallback_key_types.is_empty()
176	{
177		return Ok(Destination::Appservice(id));
178	}
179
180	let request = PushEventsRequest {
181		txn_id: URL_SAFE_NO_PAD.encode(txn_hash).into(),
182		events: pdus,
183		ephemeral: edus,
184		to_device,
185		device_lists,
186		device_one_time_keys_count,
187		device_unused_fallback_key_types,
188	};
189
190	match self
191		.services
192		.appservice
193		.send_request(info.registration, request)
194		.await
195	{
196		| Ok(_) => Ok(Destination::Appservice(id)),
197		| Err(e) => Err((Destination::Appservice(id), e)),
198	}
199}
200
201#[implement(Service)]
202async fn txn_part(&self, info: &RegistrationInfo, msc3202: bool, event: &SendingEvent) -> Part {
203	match event {
204		| SendingEvent::Pdu(pdu_id) => {
205			let Ok(pdu) = self
206				.services
207				.timeline
208				.get_pdu_from_id(pdu_id)
209				.await
210			else {
211				return Part::None;
212			};
213
214			let own_user = info.is_user_match(pdu.sender());
215			let sender = (msc3202 && own_user).then(|| pdu.sender().to_owned());
216
217			// The appservice is the client that sent its own users' events.
218			let pdu = if own_user {
219				pdu
220			} else {
221				pdu.without_private_unless_sender(None)
222			};
223
224			Part::Pdu(pdu.to_format(), sender)
225		},
226		| SendingEvent::Edu(edu) => {
227			if !info.registration.receive_ephemeral {
228				return Part::None;
229			}
230
231			serde_json::from_slice::<EphemeralData>(edu)
232				.and_then(|edu| Raw::new(&edu))
233				.map_or(Part::None, Part::Edu)
234		},
235		| SendingEvent::ToDevice(buf) => {
236			let Some(bytes) = buf.get(TAG_PREFIX_LEN..) else {
237				debug_warn!("skipping malformed queued to-device event");
238				return Part::None;
239			};
240
241			let Ok(raw) = serde_json::from_slice(bytes) else {
242				debug_warn!("skipping malformed queued to-device event");
243				return Part::None;
244			};
245
246			let recipient = msc3202
247				.then(|| serde_json::from_slice(bytes).ok())
248				.flatten()
249				.map(|ToDeviceRecipient { to_user_id, to_device_id }| (to_user_id, to_device_id));
250
251			Part::ToDevice(raw, recipient)
252		},
253		| SendingEvent::DeviceListChanged(buf) => {
254			if msc3202
255				&& let Some(bytes) = buf.get(TAG_PREFIX_LEN..)
256				&& let Ok(user) = from_utf8(bytes)
257				&& let Ok(user_id) = UserId::parse(user)
258			{
259				return Part::Changed(user_id);
260			}
261
262			Part::None
263		},
264		| SendingEvent::BadgeRefresh | SendingEvent::Flush => Part::None,
265	}
266}
267
268impl Parts {
269	fn merge(mut self, part: Part) -> Self {
270		match part {
271			| Part::Pdu(pdu, sender) => {
272				self.pdus.push(pdu);
273				self.otk_users.extend(sender);
274			},
275			| Part::Edu(edu) => self.edus.push(edu),
276			| Part::ToDevice(raw, recipient) => {
277				self.to_device.push(raw);
278				self.otk_recipients.extend(recipient);
279			},
280			| Part::Changed(user_id) => self.changed.push(user_id),
281			| Part::None => {},
282		}
283
284		self
285	}
286}
287
288/// MSC3202 one-time-key counts and unused fallback key types over every
289/// device of `users` plus the specific `recipients`.
290///
291/// Recomputed per build rather than snapshotted, so a retry ships fresh
292/// counts.
293#[implement(Service)]
294#[tracing::instrument(
295	level = "debug",
296	skip_all,
297	fields(
298		users = %users.len(),
299		recipients = %recipients.len(),
300	),
301)]
302async fn msc3202_key_counts(
303	&self,
304	users: BTreeSet<OwnedUserId>,
305	recipients: Devices,
306) -> (OtkCounts, FallbackTypes) {
307	let devices: Devices = users
308		.iter()
309		.stream()
310		.flat_map(|user_id| {
311			self.services
312				.users
313				.all_device_ids(user_id)
314				.map(move |device_id| (user_id.to_owned(), device_id.to_owned()))
315		})
316		.chain(recipients.into_iter().stream())
317		.collect()
318		.await;
319
320	dedup(devices)
321		.into_iter()
322		.stream()
323		.broad_then(async |(user_id, device_id): UserDevice| {
324			let counts = self
325				.services
326				.users
327				.count_one_time_keys(&user_id, &device_id);
328
329			let fallbacks = self
330				.services
331				.users
332				.unused_fallback_key_algorithms(&user_id, &device_id)
333				.collect();
334
335			let (counts, fallbacks) = join(counts, fallbacks).await;
336
337			(user_id, device_id, counts, fallbacks)
338		})
339		.ready_fold(
340			(OtkCounts::new(), FallbackTypes::new()),
341			|(mut counts, mut fallbacks), (user_id, device_id, otk, fallback)| {
342				counts
343					.entry(user_id.clone())
344					.or_default()
345					.insert(device_id.clone(), otk);
346
347				fallbacks
348					.entry(user_id)
349					.or_default()
350					.insert(device_id, fallback);
351
352				(counts, fallbacks)
353			},
354		)
355		.await
356}
357
358fn dedup(mut devices: Devices) -> Devices {
359	devices.sort_unstable();
360	devices.dedup();
361
362	devices
363}