tuwunel_service/rooms/retention/
mod.rs1use 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
24pub 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#[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#[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#[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#[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#[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#[implement(Service)]
172pub fn purge_original(&self, event_id: &EventId) { self.eventid_originalpdu.remove(event_id); }