tuwunel_service/rooms/timeline/
pdus.rs1use futures::{Stream, StreamExt, TryFutureExt, TryStreamExt};
8use ruma::{MilliSecondsSinceUnixEpoch, RoomId, UInt, UserId, api::Direction};
9use tuwunel_core::{
10 Result, at, err, implement, trace,
11 utils::stream::{TryIgnore, TryReadyExt, TryWidebandExt},
12 warn,
13};
14use tuwunel_database::{KeyVal, keyval::Val};
15use tuwunel_matrix::pdu::{PduCount, PduEvent};
16
17use super::{PduId, RawPduId};
18
19pub type PdusIterItem = (PduCount, PduEvent);
24
25#[must_use]
30pub fn bias_count(count: [u8; 8]) -> u64 {
31 i64::from_be_bytes(count)
32 .wrapping_sub(i64::MIN)
33 .cast_unsigned()
34}
35
36#[implement(super::Service)]
42pub async fn delete_pdus(&self, room_id: &RoomId) -> Result {
43 let current = self
44 .count_to_id(room_id, PduCount::min(), Direction::Forward)
45 .await?;
46
47 let prefix = current.shortroomid();
48 self.db
49 .pduid_pdu
50 .raw_stream_from(¤t)
51 .ready_try_take_while(move |(key, _)| Ok(key.starts_with(&prefix)))
52 .ready_try_for_each(move |(key, value)| {
53 let pdu = serde_json::from_slice::<PduEvent>(value)?;
54 let ts: u64 = pdu.origin_server_ts.into();
55 let event_id = &pdu.event_id;
56
57 let mut txn = self.db.db.txn();
58
59 txn.del_raw(&self.db.pduid_pdu, key);
60 txn.del_raw(&self.db.eventid_pduid, event_id);
61 txn.del_raw(&self.db.eventid_outlierpdu, event_id);
62
63 let room_id_ts_key = (room_id, ts, bias_count(RawPduId::from(key).count()));
64 txn.del(&self.db.roomid_tscount_pducount, room_id_ts_key);
65
66 txn.execute();
67
68 trace!(?event_id, ?room_id, ?ts, ?key, "Removed");
69
70 Ok(())
71 })
72 .await
73}
74
75#[implement(super::Service)]
81pub fn pdus_near_ts(
82 &self,
83 user_id: Option<&UserId>,
84 room_id: &RoomId,
85 ts: MilliSecondsSinceUnixEpoch,
86 dir: Direction,
87) -> impl Stream<Item = Result<PdusIterItem>> + Send {
88 self.pdu_ids_near_ts(room_id, ts, dir)
89 .map_ok(|(ts, pdu_id)| (ts, pdu_id.into()))
90 .wide_and_then(async |(_, pdu_id): (_, RawPduId)| {
91 self.get_pdu_from_id(&pdu_id)
92 .map_ok(|pdu| (pdu_id, pdu))
93 .await
94 })
95 .map_ok(move |item| Self::each_pdu(item, user_id))
96}
97
98#[implement(super::Service)]
104pub fn pdu_ids_near_ts(
105 &self,
106 room_id: &RoomId,
107 ts: MilliSecondsSinceUnixEpoch,
108 dir: Direction,
109) -> impl Stream<Item = Result<(MilliSecondsSinceUnixEpoch, PduId)>> + Send {
110 use Direction::{Backward, Forward};
111
112 type KeyVal<'a> = ((&'a RoomId, UInt, u64), i64);
113
114 let ts: u64 = ts.get().into();
115
116 self.services
117 .short
118 .get_shortroomid(room_id)
119 .map_err(|e| err!(Request(NotFound("Room not found: {e:?}"))))
120 .map_ok(move |shortroomid| {
121 match dir {
122 | Forward => self
123 .db
124 .roomid_tscount_pducount
125 .stream_from(&(room_id, ts, u64::MIN))
126 .left_stream(),
127
128 | Backward => self
129 .db
130 .roomid_tscount_pducount
131 .rev_stream_from(&(room_id, ts, u64::MAX))
132 .right_stream(),
133 }
134 .ready_try_take_while(
135 move |((room_id_, ..), _): &KeyVal<'_>| Ok(room_id == *room_id_),
136 )
137 .map_ok(move |((_, ts, _), count)| {
138 (MilliSecondsSinceUnixEpoch(ts), PduId { shortroomid, count: count.into() })
139 })
140 })
141 .try_flatten_stream()
142}
143
144#[implement(super::Service)]
150#[inline]
151pub fn all_pdus<'a>(
152 &'a self,
153 user_id: &'a UserId,
154 room_id: &'a RoomId,
155) -> impl Stream<Item = PdusIterItem> + Send + 'a {
156 self.pdus(Some(user_id), room_id, None)
157 .ignore_err()
158}
159
160#[implement(super::Service)]
166#[tracing::instrument(skip(self), level = "debug")]
167pub fn pdus<'a>(
168 &'a self,
169 user_id: Option<&'a UserId>,
170 room_id: &'a RoomId,
171 from: Option<PduCount>,
172) -> impl Stream<Item = Result<PdusIterItem>> + Send + 'a {
173 let from = from.unwrap_or_else(PduCount::min);
174 self.count_to_id(room_id, from, Direction::Forward)
175 .map_ok(move |current| {
176 let prefix = current.shortroomid();
177 self.db
178 .pduid_pdu
179 .raw_stream_from(¤t)
180 .ready_try_take_while(move |(key, _)| Ok(key.starts_with(&prefix)))
181 .ready_and_then(move |item| Self::each_slice(item, user_id))
182 })
183 .try_flatten_stream()
184}
185
186#[implement(super::Service)]
192#[tracing::instrument(skip(self), level = "debug")]
193pub fn pdus_rev<'a>(
194 &'a self,
195 user_id: Option<&'a UserId>,
196 room_id: &'a RoomId,
197 until: Option<PduCount>,
198) -> impl Stream<Item = Result<PdusIterItem>> + Send + 'a {
199 let until = until.unwrap_or_else(PduCount::max);
200 self.count_to_id(room_id, until, Direction::Backward)
201 .map_ok(move |current| {
202 let prefix = current.shortroomid();
203 self.db
204 .pduid_pdu
205 .rev_raw_stream_from(¤t)
206 .ready_try_take_while(move |(key, _)| Ok(key.starts_with(&prefix)))
207 .ready_and_then(move |item| Self::each_slice(item, user_id))
208 })
209 .try_flatten_stream()
210}
211
212#[implement(super::Service)]
217pub fn pdus_raw(&self) -> impl Stream<Item = Result<Val<'_>>> + Send {
218 self.db.pduid_pdu.raw_stream().map_ok(at!(1))
219}
220
221#[implement(super::Service)]
226pub fn outlier_pdus_raw(&self) -> impl Stream<Item = Result<Val<'_>>> + Send {
227 self.db
228 .eventid_outlierpdu
229 .raw_stream()
230 .map_ok(at!(1))
231}
232
233#[implement(super::Service)]
234fn each_slice((pdu_id, pdu): KeyVal<'_>, user_id: Option<&UserId>) -> Result<PdusIterItem> {
235 let pdu_id: RawPduId = pdu_id.into();
236 let pdu = serde_json::from_slice::<PduEvent>(pdu)?;
237
238 Ok(Self::each_pdu((pdu_id, pdu), user_id))
239}
240
241#[implement(super::Service)]
242fn each_pdu((pdu_id, pdu): (RawPduId, PduEvent), user_id: Option<&UserId>) -> PdusIterItem {
243 (pdu_id.pdu_count(), pdu.served_to(user_id))
244}