tuwunel_service/rooms/state_accessor/
room_state.rs1use 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#[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#[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#[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#[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#[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#[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#[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#[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}