Skip to main content

tuwunel_service/rooms/state/
fetch_state.rs

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