Skip to main content

tuwunel_service/rooms/timeline/
pdus.rs

1//! Streams, indexes, and deletes accepted room timeline rows.
2//!
3//! Directional scans use encoded room-local counts, while a secondary index
4//! supports timestamp lookup. Decoded streams apply requester-specific event
5//! presentation without changing which rows are selected.
6
7use 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
19/// Standard item yielded by decoded room timeline streams.
20///
21/// The count is the event's room-local pagination token. The producer
22/// determines whether the decoded PDU receives presentation adjustments.
23pub type PdusIterItem = (PduCount, PduEvent);
24
25/// Converts a signed PDU-count encoding into ordered offset-binary form.
26///
27/// The transformation preserves signed numeric ordering in unsigned database
28/// keys, placing negative backfilled counts before positive normal counts.
29#[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/// Deletes every accepted timeline row belonging to a room.
37///
38/// Each event's accepted row, ID mapping, outlier copy, and timestamp index are
39/// removed in one transaction. An error stops the scan after any prior events
40/// were already removed, and metadata owned by other services is not purged.
41#[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(&current)
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/// Streams decoded room events from the timestamp index in one direction.
76///
77/// The timestamp boundary is inclusive. `user_id` controls sender-only
78/// unsigned fields, while event age is always updated. It does not filter
79/// which events are yielded.
80#[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/// Streams timestamp and PDU-ID pairs from the room timestamp index.
99///
100/// Forward scans begin at the first row at or after the timestamp, while
101/// backward scans begin at the first row at or before it. Only rows for the
102/// requested room are yielded.
103#[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/// Streams all accepted PDUs in a room in forward order.
145///
146/// The requesting user receives sender-only unsigned fields where
147/// applicable. Unknown rooms and all per-item storage or decoding errors are
148/// suppressed, producing only successfully presented events.
149#[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/// Streams accepted room events after an optional count in forward order.
161///
162/// The count boundary is exclusive and defaults to the minimum count. The
163/// optional user controls presentation only; stream, storage, and decoding
164/// errors remain visible to the caller.
165#[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(&current)
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/// Streams accepted room events before an optional count in reverse order.
187///
188/// The count boundary is exclusive and defaults to the maximum count. The
189/// optional user controls presentation only; stream, storage, and decoding
190/// errors remain visible to the caller.
191#[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(&current)
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/// Streams raw JSON values from all accepted timeline rows.
213///
214/// Values borrow the database cursor and must be owned before they are retained
215/// across another poll. Storage errors remain in the stream.
216#[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/// Streams raw JSON values from all outlier rows.
222///
223/// Values borrow the database cursor and must be owned before they are retained
224/// across another poll. Storage errors remain in the stream.
225#[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}