Skip to main content

tuwunel_service/rooms/retention/
mod.rs

1//! Retains original event JSON when accepted timeline events are redacted.
2//!
3//! Originals are indexed by redaction time for periodic expiry. Saving and
4//! scheduled cleanup are controlled independently so operators can retain
5//! originals indefinitely when expiry is disabled.
6
7use std::{sync::Arc, time::Duration};
8
9use async_trait::async_trait;
10use futures::{Stream, TryStreamExt};
11use ruma::{CanonicalJsonObject, EventId};
12use tuwunel_core::{
13	Result, debug_info, expected, implement,
14	utils::{BoolExt as _, TryReadyExt, time::now},
15};
16use tuwunel_database::{Deserialized, Json, Map};
17use tuwunel_matrix::pdu::PduEvent;
18
19use crate::rooms::timeline::RoomMutexGuard;
20
21#[cfg(test)]
22mod tests;
23
24/// Stores and expires the unredacted originals of redacted events.
25///
26/// Each retained event has a primary row and a time-ordered expiry index row.
27/// The background worker removes entries once their configured retention
28/// interval has elapsed.
29pub struct Service {
30	services: Arc<crate::services::OnceServices>,
31	eventid_originalpdu: Arc<Map>,
32	timeredacted_eventid: Arc<Map>,
33}
34
35#[async_trait]
36impl crate::Service for Service {
37	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
38		Ok(Arc::new(Self {
39			services: args.services.clone(),
40			eventid_originalpdu: args.db["eventid_originalpdu"].clone(),
41			timeredacted_eventid: args.db["timeredacted_eventid"].clone(),
42		}))
43	}
44
45	async fn worker(self: Arc<Self>) -> Result {
46		loop {
47			let retention_seconds = self
48				.services
49				.config
50				.rooms
51				.redaction
52				.retention_seconds;
53
54			if retention_seconds != 0 {
55				debug_info!("Cleaning up retained events");
56
57				let now = now().as_secs();
58				let count = self
59					.timeredacted_eventid
60					.keys::<(u64, &EventId)>()
61					.ready_try_take_while(|(time_redacted, _)| {
62						let time_redacted = *time_redacted;
63						Ok(expected!(time_redacted + retention_seconds) < now)
64					})
65					.ready_try_fold_default(|count: usize, (time_redacted, event_id)| {
66						self.eventid_originalpdu.remove(event_id);
67						self.timeredacted_eventid
68							.del((time_redacted, event_id));
69						Ok(count.saturating_add(1))
70					})
71					.await?;
72
73				debug_info!(?count, "Finished cleaning up retained events");
74			}
75
76			tokio::select! {
77				() = tokio::time::sleep(Duration::from_hours(1)) => {},
78				() = self.services.server.until_shutdown() => return Ok(())
79			};
80		}
81	}
82
83	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
84}
85
86/// Returns a retained original decoded as a PDU.
87///
88/// Missing rows and malformed stored JSON are reported to the caller.
89#[implement(Service)]
90pub async fn get_original_pdu(&self, event_id: &EventId) -> Result<PduEvent> {
91	self.eventid_originalpdu
92		.get(event_id)
93		.await?
94		.deserialized()
95}
96
97/// Returns a retained original as canonical event JSON.
98///
99/// Missing rows and malformed stored JSON are reported to the caller.
100#[implement(Service)]
101pub async fn get_original_pdu_json(&self, event_id: &EventId) -> Result<CanonicalJsonObject> {
102	self.eventid_originalpdu
103		.get(event_id)
104		.await?
105		.deserialized()
106}
107
108/// Retains an event's original JSON before it is redacted.
109///
110/// Saving is skipped when saving originals is disabled or an original already
111/// exists.
112/// The primary row and its redaction-time expiry index are committed in one
113/// transaction while the caller retains the room timeline guard.
114#[implement(Service)]
115pub async fn save_original_pdu(
116	&self,
117	event_id: &EventId,
118	pdu: &CanonicalJsonObject,
119	_state_lock: &RoomMutexGuard,
120) {
121	if self
122		.services
123		.config
124		.rooms
125		.redaction
126		.save_unredacted_events
127		.is_false()
128	{
129		return;
130	}
131
132	if self
133		.eventid_originalpdu
134		.exists(event_id)
135		.await
136		.is_ok()
137	{
138		return;
139	}
140
141	self.insert_original(event_id, pdu, now().as_secs());
142}
143
144/// Writes a retained original and its expiry index atomically.
145///
146/// The supplied timestamp is the redaction time used by the cleanup worker.
147#[implement(Service)]
148fn insert_original(&self, event_id: &EventId, pdu: &CanonicalJsonObject, time: u64) {
149	let mut txn = self.services.db.txn();
150
151	txn.raw_put(&self.eventid_originalpdu, event_id, Json(pdu));
152	txn.put_raw(&self.timeredacted_eventid, (time, event_id), []);
153	txn.execute();
154}
155
156/// Streams the raw JSON values of all retained originals.
157///
158/// Values borrow the database cursor and must be owned before they are retained
159/// across another poll. Storage errors remain in the stream.
160#[implement(Service)]
161pub fn retained_pdus_raw(&self) -> impl Stream<Item = Result<&[u8]>> + Send {
162	self.eventid_originalpdu
163		.raw_stream()
164		.map_ok(|x| x.1)
165}
166
167/// Drops the retained original of a purged event.
168///
169/// The paired expiry-index row is deliberately left for the retention worker
170/// to reap at its scheduled time.
171#[implement(Service)]
172pub fn purge_original(&self, event_id: &EventId) { self.eventid_originalpdu.remove(event_id); }