Skip to main content

tuwunel_service/rooms/state/
fetch_state.rs

1use std::collections::HashMap;
2
3use ruma::{OwnedEventId, events::StateEventType};
4use tuwunel_core::{Result, err};
5use tuwunel_matrix::{PduEvent, ShortStateKey, StateKey, room::state::resolution::FetchState};
6
7use crate::Services;
8
9#[cfg(test)]
10mod tests;
11
12/// Fetches state events named by a held map of short state keys.
13///
14/// An absent key is missing state. An event named by the map but absent from the
15/// timeline is a storage failure.
16#[derive(Clone, Copy)]
17pub(crate) struct IdMapState<'a> {
18	/// Services used to resolve short keys and read event storage.
19	pub(crate) services: &'a Services,
20
21	/// Held state event IDs indexed by their interned type and state key.
22	pub(crate) ids: &'a HashMap<ShortStateKey, OwnedEventId>,
23}
24
25impl FetchState for IdMapState<'_> {
26	type Pdu = PduEvent;
27
28	async fn get(self, event_type: StateEventType, state_key: StateKey) -> Result<Self::Pdu> {
29		let shortstatekey = self
30			.services
31			.short
32			.get_shortstatekey(&event_type, state_key.as_str())
33			.await?;
34
35		let event_id = self
36			.ids
37			.get(&shortstatekey)
38			.ok_or_else(|| err!(Request(NotFound("State key is absent from the held map."))))?;
39
40		self.services
41			.timeline
42			.get_pdu(event_id)
43			.await
44			.map_err(|error| match error {
45				| error if !error.is_not_found() => error,
46				| _ => err!(Database("State map references missing event {event_id}.")),
47			})
48	}
49}