Skip to main content

tuwunel_service/rooms/state_accessor/
room_state.rs

1//! Resolves the current state snapshot for a room.
2//!
3//! These adapters obtain a room's current short-state hash and delegate to the
4//! historical snapshot readers. Stream variants yield a missing room snapshot as
5//! an error and otherwise pass the delegated reader's items through unchanged.
6
7use futures::{Stream, StreamExt, TryFutureExt};
8use ruma::{OwnedEventId, RoomId, events::StateEventType};
9use serde::Deserialize;
10use tuwunel_core::{Result, err, implement};
11use tuwunel_matrix::{Event, Pdu, StateKey};
12
13/// Deserializes one current state event's content.
14///
15/// The event is selected by `(event_type, state_key)`. Snapshot lookup,
16/// timeline lookup, and content errors are returned to the caller.
17#[implement(super::Service)]
18pub async fn room_state_get_content<T>(
19	&self,
20	room_id: &RoomId,
21	event_type: &StateEventType,
22	state_key: &str,
23) -> Result<T>
24where
25	T: for<'de> Deserialize<'de> + Send,
26{
27	self.room_state_get(room_id, event_type, state_key)
28		.await
29		.and_then(|event| event.get_content())
30}
31
32/// Streams current state events of one type.
33///
34/// Failure to resolve the room's current snapshot is yielded as an error.
35/// Missing reverse mappings and unavailable PDUs are skipped by the delegated
36/// best-effort stream.
37#[implement(super::Service)]
38#[tracing::instrument(skip(self), level = "debug")]
39pub fn room_state_type_pdus<'a>(
40	&'a self,
41	room_id: &'a RoomId,
42	event_type: &'a StateEventType,
43) -> impl Stream<Item = Result<impl Event>> + Send + 'a {
44	self.services
45		.state
46		.get_room_shortstatehash(room_id)
47		.map_ok(|shortstatehash| {
48			self.state_type_pdus(shortstatehash, event_type)
49				.map(Ok)
50		})
51		.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
52		.try_flatten_stream()
53}
54
55/// Streams the room's full current state with type and state keys.
56///
57/// Failure to resolve the current snapshot is yielded as an error. Entries
58/// whose IDs, PDUs, or state keys cannot be resolved are skipped.
59#[implement(super::Service)]
60#[tracing::instrument(skip(self), level = "debug")]
61pub fn room_state_full<'a>(
62	&'a self,
63	room_id: &'a RoomId,
64) -> impl Stream<Item = Result<((StateEventType, StateKey), impl Event)>> + Send + 'a {
65	self.services
66		.state
67		.get_room_shortstatehash(room_id)
68		.map_ok(|shortstatehash| self.state_full(shortstatehash).map(Ok))
69		.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
70		.try_flatten_stream()
71}
72
73/// Streams every resolvable PDU in the room's current state.
74///
75/// Failure to resolve the current snapshot is yielded as an error. Individual
76/// state entries with missing reverse mappings or PDUs are skipped.
77#[implement(super::Service)]
78#[tracing::instrument(skip(self), level = "debug")]
79pub fn room_state_full_pdus<'a>(
80	&'a self,
81	room_id: &'a RoomId,
82) -> impl Stream<Item = Result<impl Event>> + Send + 'a {
83	self.services
84		.state
85		.get_room_shortstatehash(room_id)
86		.map_ok(|shortstatehash| self.state_full_pdus(shortstatehash).map(Ok))
87		.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
88		.try_flatten_stream()
89}
90
91/// Returns the event ID for one current state tuple.
92///
93/// The room's current snapshot must exist, and both short-ID mappings must
94/// resolve for `(event_type, state_key)`.
95#[implement(super::Service)]
96#[tracing::instrument(skip(self), level = "debug")]
97pub async fn room_state_get_id(
98	&self,
99	room_id: &RoomId,
100	event_type: &StateEventType,
101	state_key: &str,
102) -> Result<OwnedEventId> {
103	self.services
104		.state
105		.get_room_shortstatehash(room_id)
106		.and_then(|shortstatehash| self.state_get_id(shortstatehash, event_type, state_key))
107		.await
108}
109
110/// Streams state keys and event IDs for one current state event type.
111///
112/// Failure to resolve the current snapshot, to load it, or to resolve an
113/// entry's short IDs is yielded as an error beside the entries that resolved.
114#[implement(super::Service)]
115#[tracing::instrument(skip(self), level = "debug")]
116pub fn room_state_keys_with_ids<'a>(
117	&'a self,
118	room_id: &'a RoomId,
119	event_type: &'a StateEventType,
120) -> impl Stream<Item = Result<(StateKey, OwnedEventId)>> + Send + 'a {
121	self.services
122		.state
123		.get_room_shortstatehash(room_id)
124		.map_ok(|shortstatehash| self.state_keys_with_ids(shortstatehash, event_type))
125		.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
126		.try_flatten_stream()
127}
128
129/// Streams state keys for one current state event type.
130///
131/// Failure to resolve the current snapshot is yielded as an error. Individual
132/// state-key mapping failures are omitted by the best-effort inner stream.
133#[implement(super::Service)]
134#[tracing::instrument(skip(self), level = "debug")]
135pub fn room_state_keys<'a>(
136	&'a self,
137	room_id: &'a RoomId,
138	event_type: &'a StateEventType,
139) -> impl Stream<Item = Result<StateKey>> + Send + 'a {
140	self.services
141		.state
142		.get_room_shortstatehash(room_id)
143		.map_ok(|shortstatehash| {
144			self.state_keys(shortstatehash, event_type)
145				.map(Ok)
146		})
147		.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
148		.try_flatten_stream()
149}
150
151/// Returns one current state PDU.
152///
153/// The event is selected by `(event_type, state_key)`. Snapshot, short-ID, and
154/// timeline lookup failures are returned to the caller.
155#[implement(super::Service)]
156#[tracing::instrument(skip(self), level = "debug")]
157pub async fn room_state_get(
158	&self,
159	room_id: &RoomId,
160	event_type: &StateEventType,
161	state_key: &str,
162) -> Result<Pdu> {
163	self.services
164		.state
165		.get_room_shortstatehash(room_id)
166		.and_then(|shortstatehash| self.state_get(shortstatehash, event_type, state_key))
167		.await
168}