tuwunel_service/rooms/state/
fetch_state.rs1use 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#[derive(Clone, Copy)]
17pub(crate) struct IdMapState<'a> {
18 pub(crate) services: &'a Services,
20
21 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}