Skip to main content

tuwunel_service/migrations/
redactions.rs

1use futures::TryFutureExt;
2use tuwunel_core::{
3	Err, Result, err, info,
4	utils::{TryReadyExt, stream::TryBroadbandExt},
5	warn,
6};
7use tuwunel_matrix::{event::Event, pdu::PduEvent, room_version::rules as room_rules};
8
9use super::rebuild_thread_summaries;
10use crate::{
11	Services,
12	integrity::redactions::{Redaction, Unparsable, records_by_reference, redactions},
13};
14
15/// Stamped once the timeline has been checked for redactions applied without
16/// being recorded.
17pub(super) const MARKER: &str = "restore_foreign_redactions";
18
19/// Counts of what one walk of the timeline did.
20///
21/// A redaction the storage engine could not read transiently is counted apart
22/// from one that failed for good, because only the first withholds the marker:
23/// the walk is idempotent, so the next start retries it whole, while a corrupt
24/// row would refuse every start.
25#[derive(Default)]
26struct Tally {
27	restored: usize,
28	skipped: usize,
29	failed: usize,
30	unreadable: usize,
31}
32
33/// Records each applied redaction that a foreign database left unrecorded on
34/// the event it redacted.
35///
36/// Some origin releases keep only a reference to the redaction on the redacted
37/// event, both when redacting and in a rewrite of every stored row, so on this
38/// server the event reads as unredacted while its content is already gone.
39/// Every stored redaction event is matched to its target, which is rewritten in
40/// the form a redaction on this server leaves; thread summaries are then
41/// rebuilt, also after a start that restored everything before being stopped,
42/// so a redacted reply leaves its root's summary.
43#[tracing::instrument(level = "debug", skip_all)]
44pub(super) async fn restore_redactions(services: &Services) -> Result {
45	if !records_by_reference(services).await? {
46		return Ok(());
47	}
48
49	let cork = services.db.cork_and_sync();
50
51	// Redactions of one target can race, but each restore takes the room's lock
52	// and decides again under it.
53	let Tally { restored, skipped, failed, unreadable } = redactions(services)
54		.broad_and_then(async |redaction| Ok(restore(services, redaction).await))
55		.ready_try_fold(Tally::default(), |tally, outcome| Ok(tally.record(&outcome)))
56		.await?;
57
58	drop(cork);
59
60	info!(
61		%restored,
62		%skipped,
63		%failed,
64		%unreadable,
65		"Restored redactions a foreign database left unrecorded"
66	);
67
68	if unreadable > 0 {
69		return Err!(Database("{unreadable} redactions could not be read"));
70	}
71
72	rebuild_thread_summaries(services).await;
73
74	Ok(())
75}
76
77impl Tally {
78	fn record(mut self, outcome: &Result<bool>) -> Self {
79		match outcome {
80			| Ok(true) => self.restored = self.restored.saturating_add(1),
81			| Ok(false) => self.skipped = self.skipped.saturating_add(1),
82			| Err(error) if error.is_transient_io() =>
83				self.unreadable = self.unreadable.saturating_add(1),
84			| Err(_) => self.failed = self.failed.saturating_add(1),
85		}
86
87		self
88	}
89}
90
91/// Records one redaction on its target, reporting whether the target was
92/// rewritten.
93///
94/// A failure, including a redaction that does not parse, is logged and returned
95/// for the walk to count.
96async fn restore(services: &Services, redaction: Redaction) -> Result<bool> {
97	let redaction = match redaction {
98		| Ok(redaction) => redaction,
99		| Err(Unparsable { error, .. }) => {
100			let error = err!(Database("unparsable redaction event: {error}"));
101
102			warn!(%error, "A stored redaction event could not be read");
103			return Err(error);
104		},
105	};
106
107	restore_target(services, &redaction)
108		.inspect_err(|error| {
109			warn!(event_id = %redaction.event_id(), %error, "A redaction could not be restored");
110		})
111		.await
112}
113
114async fn restore_target(services: &Services, redaction: &PduEvent) -> Result<bool> {
115	let room_id = redaction.room_id();
116	let room_version = services.state.get_room_version(room_id).await?;
117	let rules = room_rules(&room_version)?;
118	let Some(target) = redaction.redacts_id(&rules) else {
119		return Ok(false);
120	};
121
122	let state_lock = services.state.mutex.lock(room_id).await;
123
124	services
125		.timeline
126		.restore_redaction(&target, redaction, &rules.redaction, &state_lock)
127		.await
128}