Skip to main content

tuwunel_service/rooms/state_accessor/
state.rs

1//! Reads events and short IDs from a historical room-state snapshot.
2//!
3//! Strict streams preserve snapshot and reverse-mapping errors, and the listings
4//! of one type's state keys with their IDs yield each failure beside the entries
5//! that resolved. The remaining collection streams are intentionally best effort
6//! and omit unresolved entries.
7
8use std::{ops::Deref, sync::Arc};
9
10use futures::{
11	FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt, future::try_join, pin_mut,
12	stream::empty,
13};
14use ruma::{
15	OwnedEventId, UserId,
16	events::{
17		StateEventType,
18		room::{
19			history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
20			member::{MembershipState, RoomMemberEventContent},
21		},
22	},
23};
24use serde::Deserialize;
25use tuwunel_core::{
26	Error, Result, at, err, implement, pair_of,
27	utils::{
28		result::FlatOk,
29		stream::{
30			BroadbandExt, IterStream, ReadyExt, TryBroadbandExt, TryIgnore, TryReadyExt, TryTools,
31		},
32	},
33};
34use tuwunel_matrix::{Event, Pdu, StateKey};
35
36use crate::rooms::{
37	short::{ShortEventId, ShortStateHash, ShortStateKey},
38	state_compressor::{CompressedState, compress_state_event, parse_compressed_state_event},
39};
40
41type Listing = ((Vec<StateKey>, Vec<ShortEventId>), Vec<Error>);
42
43/// Reports whether a user was joined in a selected state snapshot.
44///
45/// Missing or invalid membership state is treated as `leave`, so lookup errors
46/// return `false`.
47#[implement(super::Service)]
48#[inline]
49pub async fn user_was_joined(&self, shortstatehash: ShortStateHash, user_id: &UserId) -> bool {
50	matches!(
51		self.user_membership(shortstatehash, user_id)
52			.await,
53		MembershipState::Join
54	)
55}
56
57/// Reports whether a user was invited or joined in a selected state snapshot.
58///
59/// Missing or invalid membership state is treated as `leave`, so lookup errors
60/// return `false`.
61#[implement(super::Service)]
62#[inline]
63pub async fn user_was_invited(&self, shortstatehash: ShortStateHash, user_id: &UserId) -> bool {
64	let membership = self
65		.user_membership(shortstatehash, user_id)
66		.await;
67
68	matches!(membership, MembershipState::Join | MembershipState::Invite)
69}
70
71/// Returns a user's membership in a selected state snapshot.
72///
73/// Missing state, unavailable events, and invalid membership content all fall
74/// back to [`MembershipState::Leave`].
75#[implement(super::Service)]
76pub async fn user_membership(
77	&self,
78	shortstatehash: ShortStateHash,
79	user_id: &UserId,
80) -> MembershipState {
81	self.state_get_content(shortstatehash, &StateEventType::RoomMember, user_id.as_str())
82		.await
83		.map_or(MembershipState::Leave, |c: RoomMemberEventContent| c.membership)
84}
85
86/// MSC4115: the user's room membership "just after" the given PDU landed.
87///
88/// `pdu_shortstatehash` returns state-before-the-event, so a member event
89/// targeting `user_id` overrides that lookup with its own content. Missing or
90/// invalid state falls back to [`MembershipState::Leave`].
91#[implement(super::Service)]
92pub async fn user_membership_at_pdu(&self, user_id: &UserId, pdu: &Pdu) -> MembershipState {
93	if let Some(membership) = pdu.membership_for(user_id) {
94		return membership;
95	}
96
97	let Ok(shortstatehash) = self
98		.services
99		.state
100		.pdu_shortstatehash(pdu.event_id())
101		.await
102	else {
103		return MembershipState::Leave;
104	};
105
106	self.user_membership(shortstatehash, user_id)
107		.await
108}
109
110/// Returns the room's history visibility in a selected state snapshot.
111///
112/// Missing, unreadable, or invalid history-visibility state falls back to
113/// [`HistoryVisibility::Shared`], as the spec directs.
114#[implement(super::Service)]
115pub async fn history_visibility(&self, shortstatehash: ShortStateHash) -> HistoryVisibility {
116	self.state_get_content(shortstatehash, &StateEventType::RoomHistoryVisibility, "")
117		.await
118		.map_or(HistoryVisibility::Shared, |c: RoomHistoryVisibilityEventContent| {
119			c.history_visibility
120		})
121}
122
123/// Deserializes one event's content from a selected state snapshot.
124///
125/// The event is selected by `(event_type, state_key)`. State, short-ID,
126/// timeline, and content errors are returned to the caller.
127#[implement(super::Service)]
128pub async fn state_get_content<T>(
129	&self,
130	shortstatehash: ShortStateHash,
131	event_type: &StateEventType,
132	state_key: &str,
133) -> Result<T>
134where
135	T: for<'de> Deserialize<'de> + Send,
136{
137	self.state_get(shortstatehash, event_type, state_key)
138		.await
139		.and_then(|event| event.get_content())
140}
141
142/// Reports whether a state snapshot contains one state tuple.
143///
144/// Failure to resolve the tuple's short state key or load the snapshot is
145/// treated as absence.
146#[implement(super::Service)]
147pub async fn state_contains(
148	&self,
149	shortstatehash: ShortStateHash,
150	event_type: &StateEventType,
151	state_key: &str,
152) -> bool {
153	let Ok(shortstatekey) = self
154		.services
155		.short
156		.get_shortstatekey(event_type, state_key)
157		.await
158	else {
159		return false;
160	};
161
162	self.state_contains_shortstatekey(shortstatehash, shortstatekey)
163		.await
164}
165
166/// Reports whether a state snapshot contains any event of one type.
167///
168/// Snapshot and state-key mapping errors are omitted by the underlying
169/// best-effort stream and can therefore produce `false`.
170#[implement(super::Service)]
171pub async fn state_contains_type(
172	&self,
173	shortstatehash: ShortStateHash,
174	event_type: &StateEventType,
175) -> bool {
176	let state_keys = self.state_keys(shortstatehash, event_type);
177
178	pin_mut!(state_keys);
179	state_keys.next().await.is_some()
180}
181
182/// Reports whether a snapshot contains a short state key.
183///
184/// The compressed snapshot is searched across every short event ID for the
185/// key. Failure to load the snapshot is treated as absence.
186#[implement(super::Service)]
187pub async fn state_contains_shortstatekey(
188	&self,
189	shortstatehash: ShortStateHash,
190	shortstatekey: ShortStateKey,
191) -> bool {
192	let start = compress_state_event(shortstatekey, 0);
193	let end = compress_state_event(shortstatekey, u64::MAX);
194
195	self.load_full_state(shortstatehash)
196		.map_ok(|full_state| full_state.range(start..=end).next().copied())
197		.await
198		.flat_ok()
199		.is_some()
200}
201
202/// Returns one PDU from a selected state snapshot.
203///
204/// The event is selected by `(event_type, state_key)`. Short-ID and timeline
205/// lookup failures are returned to the caller.
206#[implement(super::Service)]
207pub async fn state_get(
208	&self,
209	shortstatehash: ShortStateHash,
210	event_type: &StateEventType,
211	state_key: &str,
212) -> Result<Pdu> {
213	let event_id: OwnedEventId = self
214		.state_get_id(shortstatehash, event_type, state_key)
215		.await?;
216
217	self.services.timeline.get_pdu(&event_id).await
218}
219
220/// Returns one event ID from a selected state snapshot.
221///
222/// Both the state tuple's short key and its short event ID must resolve.
223#[implement(super::Service)]
224pub async fn state_get_id(
225	&self,
226	shortstatehash: ShortStateHash,
227	event_type: &StateEventType,
228	state_key: &str,
229) -> Result<OwnedEventId> {
230	let shorteventid = self
231		.state_get_shortid(shortstatehash, event_type, state_key)
232		.await?;
233
234	self.services
235		.short
236		.get_eventid_from_short(shorteventid)
237		.await
238}
239
240/// Returns one short event ID from a selected state snapshot.
241///
242/// The method resolves `(event_type, state_key)` to a short state key and
243/// searches the compressed snapshot. An absent tuple is returned as not found.
244#[implement(super::Service)]
245pub async fn state_get_shortid(
246	&self,
247	shortstatehash: ShortStateHash,
248	event_type: &StateEventType,
249	state_key: &str,
250) -> Result<ShortEventId> {
251	let shortstatekey = self
252		.services
253		.short
254		.get_shortstatekey(event_type, state_key)
255		.await?;
256
257	let start = compress_state_event(shortstatekey, 0);
258	let end = compress_state_event(shortstatekey, u64::MAX);
259	self.load_full_state(shortstatehash)
260		.map_ok(|full_state| {
261			full_state
262				.range(start..=end)
263				.next()
264				.copied()
265				.map(parse_compressed_state_event)
266				.map(at!(1))
267				.ok_or(err!(Request(NotFound("Not found in room state"))))
268		})
269		.await?
270}
271
272/// Streams resolvable events of one type from a state snapshot.
273///
274/// Snapshot, short-ID, and timeline lookup failures are skipped, so this is a
275/// best-effort view rather than a completeness guarantee.
276#[implement(super::Service)]
277pub fn state_type_pdus<'a>(
278	&'a self,
279	shortstatehash: ShortStateHash,
280	event_type: &'a StateEventType,
281) -> impl Stream<Item = impl Event> + Send + 'a {
282	self.state_keys_with_ids(shortstatehash, event_type)
283		.ready_filter_map(Result::ok)
284		.map(at!(1))
285		.broad_filter_map(async |event_id: OwnedEventId| {
286			self.services
287				.timeline
288				.get_pdu(&event_id)
289				.await
290				.ok()
291		})
292}
293
294/// Streams state keys and event IDs for one type in a snapshot.
295///
296/// A failure to load the snapshot or to resolve an entry's short IDs is yielded
297/// as an error beside the entries that resolved. The stream buffers the selected
298/// short IDs before resolving event IDs in a batch, and yields the failures
299/// known before that batch first.
300#[implement(super::Service)]
301pub fn state_keys_with_ids<'a>(
302	&'a self,
303	shortstatehash: ShortStateHash,
304	event_type: &'a StateEventType,
305) -> impl Stream<Item = Result<(StateKey, OwnedEventId)>> + Send + 'a {
306	self.state_keys_with_shortids(shortstatehash, event_type)
307		.try_unzip_partial()
308		.map(|((state_keys, shorteventids), errors): Listing| {
309			let entries = self
310				.services
311				.short
312				.multi_get_eventid_from_short(shorteventids.into_iter().stream())
313				.zip(state_keys.into_iter().stream())
314				.map(|(event_id, state_key)| event_id.map(|event_id| (state_key, event_id)));
315
316			errors
317				.into_iter()
318				.map(Err)
319				.stream()
320				.chain(entries)
321		})
322		.flatten_stream()
323}
324
325/// Streams state keys and short event IDs for one type in a snapshot.
326///
327/// A failure to load the snapshot is yielded as an error, as is each entry
328/// whose state key cannot be resolved, since its type is then unknown. The full
329/// compressed snapshot is buffered before filtering by event type.
330#[implement(super::Service)]
331pub fn state_keys_with_shortids<'a>(
332	&'a self,
333	shortstatehash: ShortStateHash,
334	event_type: &'a StateEventType,
335) -> impl Stream<Item = Result<(StateKey, ShortEventId)>> + Send + 'a {
336	self.state_full_shortids(shortstatehash)
337		.try_unzip()
338		.map_ok(move |(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
339			self.services
340				.short
341				.multi_get_statekey_from_short(shortstatekeys.into_iter().stream())
342				.zip(shorteventids.into_iter().stream())
343				.map(|(key, shorteventid)| key.map(|key| (key, shorteventid)))
344				.ready_try_filter_map(move |((entry_type, state_key), shorteventid)| {
345					Ok(entry_type
346						.eq(event_type)
347						.then_some((state_key, shorteventid)))
348				})
349		})
350		.try_flatten_stream()
351}
352
353/// Streams state keys for one event type in a snapshot.
354///
355/// Snapshot and short-state-key mapping failures are skipped, so the stream is
356/// best effort.
357#[implement(super::Service)]
358pub fn state_keys<'a>(
359	&'a self,
360	shortstatehash: ShortStateHash,
361	event_type: &'a StateEventType,
362) -> impl Stream<Item = StateKey> + Send + 'a {
363	let short_ids = self
364		.state_full_shortids(shortstatehash)
365		.ignore_err()
366		.map(at!(0));
367
368	self.services
369		.short
370		.multi_get_statekey_from_short(short_ids)
371		.ready_filter_map(Result::ok)
372		.ready_filter_map(move |(event_type_, state_key)| {
373			event_type_.eq(event_type).then_some(state_key)
374		})
375}
376
377/// Streams state entries removed between two snapshots.
378///
379/// Entries present in the first hash and absent from the second are returned.
380/// Failure to load either snapshot produces an empty stream.
381#[implement(super::Service)]
382#[inline]
383pub fn state_removed(
384	&self,
385	shortstatehash: pair_of!(ShortStateHash),
386) -> impl Stream<Item = (ShortStateKey, ShortEventId)> + Send + '_ {
387	self.state_added((shortstatehash.1, shortstatehash.0))
388}
389
390/// Streams state entries added between two snapshots.
391///
392/// Entries absent from the first hash and present in the second are returned.
393/// Failure to load either snapshot produces an empty stream.
394#[implement(super::Service)]
395pub fn state_added(
396	&self,
397	shortstatehash: pair_of!(ShortStateHash),
398) -> impl Stream<Item = (ShortStateKey, ShortEventId)> + Send + '_ {
399	if shortstatehash.0 == shortstatehash.1 {
400		return empty().left_stream();
401	}
402
403	let a = self.load_full_state(shortstatehash.0);
404	let b = self.load_full_state(shortstatehash.1);
405	try_join(a, b)
406		.map_ok(|(a, b)| b.difference(&a).copied().collect::<Vec<_>>())
407		.map_ok(IterStream::try_stream)
408		.try_flatten_stream()
409		.ignore_err()
410		.map(parse_compressed_state_event)
411		.right_stream()
412}
413
414/// Streams resolvable keyed events from a state snapshot.
415///
416/// Events without a state key and entries with failed short-ID or timeline
417/// lookups are omitted by the underlying best-effort PDU stream.
418#[implement(super::Service)]
419pub fn state_full(
420	&self,
421	shortstatehash: ShortStateHash,
422) -> impl Stream<Item = ((StateEventType, StateKey), impl Event)> + Send + '_ {
423	self.state_full_pdus(shortstatehash)
424		.ready_filter_map(|pdu| {
425			Some(((pdu.kind().to_cow_str().into(), pdu.state_key()?.into()), pdu))
426		})
427}
428
429/// Streams every resolvable PDU from a state snapshot.
430///
431/// Snapshot, reverse-mapping, and timeline failures are silently skipped. Use
432/// [`Self::state_full_pdus_strict`] when completeness is required.
433#[implement(super::Service)]
434pub fn state_full_pdus(
435	&self,
436	shortstatehash: ShortStateHash,
437) -> impl Stream<Item = impl Event> + Send + '_ {
438	let short_ids = self
439		.state_full_shortids(shortstatehash)
440		.ignore_err()
441		.map(at!(1));
442
443	self.services
444		.short
445		.multi_get_eventid_from_short(short_ids)
446		.ready_filter_map(Result::ok)
447		.broad_filter_map(async |event_id: OwnedEventId| {
448			self.services
449				.timeline
450				.get_pdu(&event_id)
451				.await
452				.ok()
453		})
454}
455
456/// Streams every PDU in a state snapshot while preserving errors.
457///
458/// Snapshot and reverse-mapping failures are emitted before any partial ID map.
459/// Timeline lookup failures are yielded for their individual entries.
460#[implement(super::Service)]
461pub fn state_full_pdus_strict(
462	&self,
463	shortstatehash: ShortStateHash,
464) -> impl Stream<Item = Result<impl Event>> + Send + '_ {
465	self.state_full_ids_strict(shortstatehash)
466		.broad_and_then(async |(_, event_id)| self.services.timeline.get_pdu(&event_id).await)
467}
468
469/// Streams short state keys and resolvable event IDs from a snapshot.
470///
471/// Snapshot and reverse-mapping failures are skipped, so this best-effort
472/// stream can be partial. Use [`Self::state_full_ids_strict`] for completeness.
473#[implement(super::Service)]
474pub fn state_full_ids(
475	&self,
476	shortstatehash: ShortStateHash,
477) -> impl Stream<Item = (ShortStateKey, OwnedEventId)> + Send + '_ {
478	self.state_full_shortids(shortstatehash)
479		.ignore_err()
480		.unzip()
481		.map(|(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
482			self.services
483				.short
484				.multi_get_eventid_from_short(shorteventids.into_iter().stream())
485				.zip(shortstatekeys.into_iter().stream())
486				.ready_filter_map(|(eid, ssk)| eid.ok().map(|eid| (ssk, eid)))
487		})
488		.flatten_stream()
489}
490
491/// Streams a complete short-state-key to event-ID map for a snapshot.
492///
493/// Snapshot and reverse-mapping failures are returned without yielding a
494/// partial map.
495#[implement(super::Service)]
496pub fn state_full_ids_strict(
497	&self,
498	shortstatehash: ShortStateHash,
499) -> impl Stream<Item = Result<(ShortStateKey, OwnedEventId)>> + Send + '_ {
500	self.state_full_ids_complete(shortstatehash)
501		.map_ok(Vec::into_iter)
502		.map_ok(IterStream::try_stream)
503		.try_flatten_stream()
504}
505
506/// Loads a complete short-state-key to event-ID map for a snapshot.
507///
508/// Snapshot and reverse-mapping failures are returned instead of a partial
509/// map. Pairs keep the snapshot's order; [`Self::state_full_ids_strict`]
510/// streams the same pairs.
511#[implement(super::Service)]
512pub async fn state_full_ids_complete(
513	&self,
514	shortstatehash: ShortStateHash,
515) -> Result<Vec<(ShortStateKey, OwnedEventId)>> {
516	self.state_full_shortids(shortstatehash)
517		.try_unzip()
518		.and_then(async move |(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
519			self.services
520				.short
521				.multi_get_eventid_from_short(shorteventids.into_iter().stream())
522				.zip(shortstatekeys.into_iter().stream())
523				.map(|(event_id, shortstatekey)| {
524					event_id.map(|event_id| (shortstatekey, event_id))
525				})
526				.try_collect()
527				.await
528		})
529		.await
530}
531
532/// Streams every compressed `(short state key, short event ID)` pair.
533///
534/// A snapshot-load failure is yielded as an error. Once loaded, the immutable
535/// compressed state is copied into the stream without further lookups.
536#[implement(super::Service)]
537pub fn state_full_shortids(
538	&self,
539	shortstatehash: ShortStateHash,
540) -> impl Stream<Item = Result<(ShortStateKey, ShortEventId)>> + Send + '_ {
541	self.load_full_state(shortstatehash)
542		.map_ok(|full_state| {
543			full_state
544				.deref()
545				.iter()
546				.copied()
547				.map(parse_compressed_state_event)
548				.collect()
549		})
550		.map_ok(Vec::into_iter)
551		.map_ok(IterStream::try_stream)
552		.try_flatten_stream()
553}
554
555#[implement(super::Service)]
556#[tracing::instrument(name = "load", level = "debug", skip(self))]
557async fn load_full_state(&self, shortstatehash: ShortStateHash) -> Result<Arc<CompressedState>> {
558	self.services
559		.state_compressor
560		.load_shortstatehash_info(shortstatehash)
561		.map_err(|e| err!(Database("Missing state IDs: {e}")))
562		.map_ok(|vec| {
563			vec.last()
564				.expect("at least one layer")
565				.full_state
566				.clone()
567		})
568		.await
569}