1use std::{ops::Deref, sync::Arc};
9
10use futures::{
11 FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt, future::try_join, pin_mut,
12 stream::empty,
13};
14use ruma::{
15 OwnedEventId, UserId,
16 events::{
17 StateEventType,
18 room::{
19 history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
20 member::{MembershipState, RoomMemberEventContent},
21 },
22 },
23};
24use serde::Deserialize;
25use tuwunel_core::{
26 Error, Result, at, err, implement, pair_of,
27 utils::{
28 result::FlatOk,
29 stream::{
30 BroadbandExt, IterStream, ReadyExt, TryBroadbandExt, TryIgnore, TryReadyExt, TryTools,
31 },
32 },
33};
34use tuwunel_matrix::{Event, Pdu, StateKey};
35
36use crate::rooms::{
37 short::{ShortEventId, ShortStateHash, ShortStateKey},
38 state_compressor::{CompressedState, compress_state_event, parse_compressed_state_event},
39};
40
41type Listing = ((Vec<StateKey>, Vec<ShortEventId>), Vec<Error>);
42
43#[implement(super::Service)]
48#[inline]
49pub async fn user_was_joined(&self, shortstatehash: ShortStateHash, user_id: &UserId) -> bool {
50 matches!(
51 self.user_membership(shortstatehash, user_id)
52 .await,
53 MembershipState::Join
54 )
55}
56
57#[implement(super::Service)]
62#[inline]
63pub async fn user_was_invited(&self, shortstatehash: ShortStateHash, user_id: &UserId) -> bool {
64 let membership = self
65 .user_membership(shortstatehash, user_id)
66 .await;
67
68 matches!(membership, MembershipState::Join | MembershipState::Invite)
69}
70
71#[implement(super::Service)]
76pub async fn user_membership(
77 &self,
78 shortstatehash: ShortStateHash,
79 user_id: &UserId,
80) -> MembershipState {
81 self.state_get_content(shortstatehash, &StateEventType::RoomMember, user_id.as_str())
82 .await
83 .map_or(MembershipState::Leave, |c: RoomMemberEventContent| c.membership)
84}
85
86#[implement(super::Service)]
92pub async fn user_membership_at_pdu(&self, user_id: &UserId, pdu: &Pdu) -> MembershipState {
93 if let Some(membership) = pdu.membership_for(user_id) {
94 return membership;
95 }
96
97 let Ok(shortstatehash) = self
98 .services
99 .state
100 .pdu_shortstatehash(pdu.event_id())
101 .await
102 else {
103 return MembershipState::Leave;
104 };
105
106 self.user_membership(shortstatehash, user_id)
107 .await
108}
109
110#[implement(super::Service)]
115pub async fn history_visibility(&self, shortstatehash: ShortStateHash) -> HistoryVisibility {
116 self.state_get_content(shortstatehash, &StateEventType::RoomHistoryVisibility, "")
117 .await
118 .map_or(HistoryVisibility::Shared, |c: RoomHistoryVisibilityEventContent| {
119 c.history_visibility
120 })
121}
122
123#[implement(super::Service)]
128pub async fn state_get_content<T>(
129 &self,
130 shortstatehash: ShortStateHash,
131 event_type: &StateEventType,
132 state_key: &str,
133) -> Result<T>
134where
135 T: for<'de> Deserialize<'de> + Send,
136{
137 self.state_get(shortstatehash, event_type, state_key)
138 .await
139 .and_then(|event| event.get_content())
140}
141
142#[implement(super::Service)]
147pub async fn state_contains(
148 &self,
149 shortstatehash: ShortStateHash,
150 event_type: &StateEventType,
151 state_key: &str,
152) -> bool {
153 let Ok(shortstatekey) = self
154 .services
155 .short
156 .get_shortstatekey(event_type, state_key)
157 .await
158 else {
159 return false;
160 };
161
162 self.state_contains_shortstatekey(shortstatehash, shortstatekey)
163 .await
164}
165
166#[implement(super::Service)]
171pub async fn state_contains_type(
172 &self,
173 shortstatehash: ShortStateHash,
174 event_type: &StateEventType,
175) -> bool {
176 let state_keys = self.state_keys(shortstatehash, event_type);
177
178 pin_mut!(state_keys);
179 state_keys.next().await.is_some()
180}
181
182#[implement(super::Service)]
187pub async fn state_contains_shortstatekey(
188 &self,
189 shortstatehash: ShortStateHash,
190 shortstatekey: ShortStateKey,
191) -> bool {
192 let start = compress_state_event(shortstatekey, 0);
193 let end = compress_state_event(shortstatekey, u64::MAX);
194
195 self.load_full_state(shortstatehash)
196 .map_ok(|full_state| full_state.range(start..=end).next().copied())
197 .await
198 .flat_ok()
199 .is_some()
200}
201
202#[implement(super::Service)]
207pub async fn state_get(
208 &self,
209 shortstatehash: ShortStateHash,
210 event_type: &StateEventType,
211 state_key: &str,
212) -> Result<Pdu> {
213 let event_id: OwnedEventId = self
214 .state_get_id(shortstatehash, event_type, state_key)
215 .await?;
216
217 self.services.timeline.get_pdu(&event_id).await
218}
219
220#[implement(super::Service)]
224pub async fn state_get_id(
225 &self,
226 shortstatehash: ShortStateHash,
227 event_type: &StateEventType,
228 state_key: &str,
229) -> Result<OwnedEventId> {
230 let shorteventid = self
231 .state_get_shortid(shortstatehash, event_type, state_key)
232 .await?;
233
234 self.services
235 .short
236 .get_eventid_from_short(shorteventid)
237 .await
238}
239
240#[implement(super::Service)]
245pub async fn state_get_shortid(
246 &self,
247 shortstatehash: ShortStateHash,
248 event_type: &StateEventType,
249 state_key: &str,
250) -> Result<ShortEventId> {
251 let shortstatekey = self
252 .services
253 .short
254 .get_shortstatekey(event_type, state_key)
255 .await?;
256
257 let start = compress_state_event(shortstatekey, 0);
258 let end = compress_state_event(shortstatekey, u64::MAX);
259 self.load_full_state(shortstatehash)
260 .map_ok(|full_state| {
261 full_state
262 .range(start..=end)
263 .next()
264 .copied()
265 .map(parse_compressed_state_event)
266 .map(at!(1))
267 .ok_or(err!(Request(NotFound("Not found in room state"))))
268 })
269 .await?
270}
271
272#[implement(super::Service)]
277pub fn state_type_pdus<'a>(
278 &'a self,
279 shortstatehash: ShortStateHash,
280 event_type: &'a StateEventType,
281) -> impl Stream<Item = impl Event> + Send + 'a {
282 self.state_keys_with_ids(shortstatehash, event_type)
283 .ready_filter_map(Result::ok)
284 .map(at!(1))
285 .broad_filter_map(async |event_id: OwnedEventId| {
286 self.services
287 .timeline
288 .get_pdu(&event_id)
289 .await
290 .ok()
291 })
292}
293
294#[implement(super::Service)]
301pub fn state_keys_with_ids<'a>(
302 &'a self,
303 shortstatehash: ShortStateHash,
304 event_type: &'a StateEventType,
305) -> impl Stream<Item = Result<(StateKey, OwnedEventId)>> + Send + 'a {
306 self.state_keys_with_shortids(shortstatehash, event_type)
307 .try_unzip_partial()
308 .map(|((state_keys, shorteventids), errors): Listing| {
309 let entries = self
310 .services
311 .short
312 .multi_get_eventid_from_short(shorteventids.into_iter().stream())
313 .zip(state_keys.into_iter().stream())
314 .map(|(event_id, state_key)| event_id.map(|event_id| (state_key, event_id)));
315
316 errors
317 .into_iter()
318 .map(Err)
319 .stream()
320 .chain(entries)
321 })
322 .flatten_stream()
323}
324
325#[implement(super::Service)]
331pub fn state_keys_with_shortids<'a>(
332 &'a self,
333 shortstatehash: ShortStateHash,
334 event_type: &'a StateEventType,
335) -> impl Stream<Item = Result<(StateKey, ShortEventId)>> + Send + 'a {
336 self.state_full_shortids(shortstatehash)
337 .try_unzip()
338 .map_ok(move |(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
339 self.services
340 .short
341 .multi_get_statekey_from_short(shortstatekeys.into_iter().stream())
342 .zip(shorteventids.into_iter().stream())
343 .map(|(key, shorteventid)| key.map(|key| (key, shorteventid)))
344 .ready_try_filter_map(move |((entry_type, state_key), shorteventid)| {
345 Ok(entry_type
346 .eq(event_type)
347 .then_some((state_key, shorteventid)))
348 })
349 })
350 .try_flatten_stream()
351}
352
353#[implement(super::Service)]
358pub fn state_keys<'a>(
359 &'a self,
360 shortstatehash: ShortStateHash,
361 event_type: &'a StateEventType,
362) -> impl Stream<Item = StateKey> + Send + 'a {
363 let short_ids = self
364 .state_full_shortids(shortstatehash)
365 .ignore_err()
366 .map(at!(0));
367
368 self.services
369 .short
370 .multi_get_statekey_from_short(short_ids)
371 .ready_filter_map(Result::ok)
372 .ready_filter_map(move |(event_type_, state_key)| {
373 event_type_.eq(event_type).then_some(state_key)
374 })
375}
376
377#[implement(super::Service)]
382#[inline]
383pub fn state_removed(
384 &self,
385 shortstatehash: pair_of!(ShortStateHash),
386) -> impl Stream<Item = (ShortStateKey, ShortEventId)> + Send + '_ {
387 self.state_added((shortstatehash.1, shortstatehash.0))
388}
389
390#[implement(super::Service)]
395pub fn state_added(
396 &self,
397 shortstatehash: pair_of!(ShortStateHash),
398) -> impl Stream<Item = (ShortStateKey, ShortEventId)> + Send + '_ {
399 if shortstatehash.0 == shortstatehash.1 {
400 return empty().left_stream();
401 }
402
403 let a = self.load_full_state(shortstatehash.0);
404 let b = self.load_full_state(shortstatehash.1);
405 try_join(a, b)
406 .map_ok(|(a, b)| b.difference(&a).copied().collect::<Vec<_>>())
407 .map_ok(IterStream::try_stream)
408 .try_flatten_stream()
409 .ignore_err()
410 .map(parse_compressed_state_event)
411 .right_stream()
412}
413
414#[implement(super::Service)]
419pub fn state_full(
420 &self,
421 shortstatehash: ShortStateHash,
422) -> impl Stream<Item = ((StateEventType, StateKey), impl Event)> + Send + '_ {
423 self.state_full_pdus(shortstatehash)
424 .ready_filter_map(|pdu| {
425 Some(((pdu.kind().to_cow_str().into(), pdu.state_key()?.into()), pdu))
426 })
427}
428
429#[implement(super::Service)]
434pub fn state_full_pdus(
435 &self,
436 shortstatehash: ShortStateHash,
437) -> impl Stream<Item = impl Event> + Send + '_ {
438 let short_ids = self
439 .state_full_shortids(shortstatehash)
440 .ignore_err()
441 .map(at!(1));
442
443 self.services
444 .short
445 .multi_get_eventid_from_short(short_ids)
446 .ready_filter_map(Result::ok)
447 .broad_filter_map(async |event_id: OwnedEventId| {
448 self.services
449 .timeline
450 .get_pdu(&event_id)
451 .await
452 .ok()
453 })
454}
455
456#[implement(super::Service)]
461pub fn state_full_pdus_strict(
462 &self,
463 shortstatehash: ShortStateHash,
464) -> impl Stream<Item = Result<impl Event>> + Send + '_ {
465 self.state_full_ids_strict(shortstatehash)
466 .broad_and_then(async |(_, event_id)| self.services.timeline.get_pdu(&event_id).await)
467}
468
469#[implement(super::Service)]
474pub fn state_full_ids(
475 &self,
476 shortstatehash: ShortStateHash,
477) -> impl Stream<Item = (ShortStateKey, OwnedEventId)> + Send + '_ {
478 self.state_full_shortids(shortstatehash)
479 .ignore_err()
480 .unzip()
481 .map(|(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
482 self.services
483 .short
484 .multi_get_eventid_from_short(shorteventids.into_iter().stream())
485 .zip(shortstatekeys.into_iter().stream())
486 .ready_filter_map(|(eid, ssk)| eid.ok().map(|eid| (ssk, eid)))
487 })
488 .flatten_stream()
489}
490
491#[implement(super::Service)]
496pub fn state_full_ids_strict(
497 &self,
498 shortstatehash: ShortStateHash,
499) -> impl Stream<Item = Result<(ShortStateKey, OwnedEventId)>> + Send + '_ {
500 self.state_full_ids_complete(shortstatehash)
501 .map_ok(Vec::into_iter)
502 .map_ok(IterStream::try_stream)
503 .try_flatten_stream()
504}
505
506#[implement(super::Service)]
512pub async fn state_full_ids_complete(
513 &self,
514 shortstatehash: ShortStateHash,
515) -> Result<Vec<(ShortStateKey, OwnedEventId)>> {
516 self.state_full_shortids(shortstatehash)
517 .try_unzip()
518 .and_then(async move |(shortstatekeys, shorteventids): (Vec<_>, Vec<_>)| {
519 self.services
520 .short
521 .multi_get_eventid_from_short(shorteventids.into_iter().stream())
522 .zip(shortstatekeys.into_iter().stream())
523 .map(|(event_id, shortstatekey)| {
524 event_id.map(|event_id| (shortstatekey, event_id))
525 })
526 .try_collect()
527 .await
528 })
529 .await
530}
531
532#[implement(super::Service)]
537pub fn state_full_shortids(
538 &self,
539 shortstatehash: ShortStateHash,
540) -> impl Stream<Item = Result<(ShortStateKey, ShortEventId)>> + Send + '_ {
541 self.load_full_state(shortstatehash)
542 .map_ok(|full_state| {
543 full_state
544 .deref()
545 .iter()
546 .copied()
547 .map(parse_compressed_state_event)
548 .collect()
549 })
550 .map_ok(Vec::into_iter)
551 .map_ok(IterStream::try_stream)
552 .try_flatten_stream()
553}
554
555#[implement(super::Service)]
556#[tracing::instrument(name = "load", level = "debug", skip(self))]
557async fn load_full_state(&self, shortstatehash: ShortStateHash) -> Result<Arc<CompressedState>> {
558 self.services
559 .state_compressor
560 .load_shortstatehash_info(shortstatehash)
561 .map_err(|e| err!(Database("Missing state IDs: {e}")))
562 .map_ok(|vec| {
563 vec.last()
564 .expect("at least one layer")
565 .full_state
566 .clone()
567 })
568 .await
569}