Skip to main content

tuwunel_service/rooms/threads/
mod.rs

1use std::{collections::BTreeMap, iter::once, pin::pin, sync::Arc};
2
3use futures::{
4	Stream, StreamExt, TryFutureExt, TryStreamExt,
5	future::{join3, ready},
6};
7use ruma::{
8	CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedUserId, RoomId, UInt,
9	UserId,
10	api::{Direction, client::threads::get_threads::v1::IncludeThreads},
11	events::{TimelineEventType, relation::RelationType, room::encrypted::Relation},
12	uint,
13};
14use serde::Deserialize;
15use tuwunel_core::{
16	Err, Error, Result, err, error, implement,
17	itertools::{
18		Either::{Left, Right},
19		Itertools,
20	},
21	utils::{
22		BoolExt, ReadyExt,
23		result::{LogErr, NotFound},
24		stream::{TryBroadbandExt, TryIgnore, TryReadyExt, WidebandExt},
25	},
26};
27use tuwunel_database::{Deserialized, Map, Txn};
28use tuwunel_matrix::{
29	Event, Pdu,
30	pdu::{PduCount, PduEvent, PduId, RawPduId},
31};
32
33pub(crate) use self::summary::thread_bundle_mut;
34use self::summary::{
35	counts_replies, omit_thread_latest, set_thread_count, set_thread_latest, thread_bundle,
36	thread_count, thread_latest, thread_reply_json, update_thread_bundle,
37};
38use crate::{
39	integrity::undecodable_as,
40	rooms::{
41		pdu_metadata::RelationQuery,
42		timeline::{ExtractRelatesTo, Mapped},
43	},
44};
45
46mod summary;
47#[cfg(test)]
48mod tests;
49
50/// Maximum relation hops walked when resolving thread membership, per
51/// the Matrix v1.4 spec recommendation (also MSC3771/MSC3773).
52const MAX_THREAD_HOPS: usize = 3;
53
54#[derive(Deserialize)]
55struct ExtractThreadRelation {
56	#[serde(rename = "m.relates_to")]
57	relates_to: ThreadRelation,
58}
59
60#[derive(Deserialize)]
61struct ThreadRelation {
62	rel_type: RelationType,
63	event_id: OwnedEventId,
64}
65
66pub struct Service {
67	db: Data,
68	services: Arc<crate::services::OnceServices>,
69}
70
71pub(super) struct Data {
72	threadid_userids: Arc<Map>,
73	threadactivityid_rootid: Arc<Map>,
74	threadrootid_latestcount: Arc<Map>,
75}
76
77/// A thread root rewritten for the redaction of one of its replies.
78///
79/// A root left with no replies also leaves the room's thread list.
80pub(crate) struct RedactedRoot {
81	id: RawPduId,
82	pdu: CanonicalJsonObject,
83
84	/// No reply remains, so the thread's listing and participants go.
85	emptied: bool,
86
87	/// The activity row listing an emptied thread, when its pointer was read.
88	activity: Option<RawPduId>,
89}
90
91/// A thread root's stored summary set against its replies.
92///
93/// Rewriting the root with these values brings its summary back in line.
94pub(crate) struct Recount {
95	/// The count the summary holds.
96	pub(crate) stored: Option<UInt>,
97
98	/// The count the replies give, or the stored one for a backfilled root.
99	pub(crate) counted: Option<UInt>,
100
101	/// Whether the summary's latest reply is gone or redacted.
102	pub(crate) stale_latest: bool,
103
104	/// The newest reply, to replace a stale latest reply, or none to drop the summary.
105	pub(crate) newest: Option<CanonicalJsonValue>,
106
107	/// Relations under the root left uncounted, their index key too short to hold
108	/// a position or their reply's row undecodable.
109	pub(crate) malformed: usize,
110
111	/// Whether the summary's latest reply has a timeline mapping or row that
112	/// does not decode.
113	pub(crate) malformed_latest: bool,
114}
115
116/// What became of a summary's latest reply.
117///
118/// The recount replaces a stale latest reply, and leaves one whose mapping or
119/// row does not decode for its caller to report.
120enum Latest {
121	/// The reply is stored and unredacted, or the summary names none.
122	Current,
123
124	/// The reply is gone or redacted.
125	Stale,
126
127	/// The reply's timeline mapping is not a PDU id, or its row does not decode.
128	Malformed,
129}
130
131impl crate::Service for Service {
132	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
133		Ok(Arc::new(Self {
134			db: Data {
135				threadid_userids: args.db["threadid_userids"].clone(),
136				threadactivityid_rootid: args.db["threadactivityid_rootid"].clone(),
137				threadrootid_latestcount: args.db["threadrootid_latestcount"].clone(),
138			},
139			services: args.services.clone(),
140		}))
141	}
142
143	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
144}
145
146impl Service {
147	/// Resolves the thread root for `event` by walking up `m.relates_to`
148	/// links, bounded at `MAX_THREAD_HOPS`. Returns `None` for events
149	/// that belong to the main timeline. Redaction events carry no
150	/// `m.relates_to` of their own; their thread is resolved from the
151	/// redacted target event per MSC3771/MSC3773.
152	pub async fn get_thread_id<E>(&self, event: &E) -> Option<OwnedEventId>
153	where
154		E: Event,
155	{
156		let initial = match event.get_content::<ExtractThreadRelation>() {
157			| Ok(t) => Some(t.relates_to),
158			| Err(_) => self.relates_to_via_redaction_target(event).await,
159		};
160
161		let mut relates_to = initial?;
162
163		for _ in 0..MAX_THREAD_HOPS {
164			if relates_to.rel_type == RelationType::Thread {
165				return Some(relates_to.event_id);
166			}
167
168			relates_to = self
169				.services
170				.timeline
171				.get_pdu(&relates_to.event_id)
172				.await
173				.ok()?
174				.get_content::<ExtractThreadRelation>()
175				.ok()?
176				.relates_to;
177		}
178
179		None
180	}
181
182	/// Resolve a redaction event's thread by looking through to the
183	/// redacted target. Returns `None` for non-redaction events and for
184	/// redactions whose target is unknown or carries no thread relation.
185	async fn relates_to_via_redaction_target<E>(&self, event: &E) -> Option<ThreadRelation>
186	where
187		E: Event,
188	{
189		if *event.kind() != TimelineEventType::RoomRedaction {
190			return None;
191		}
192
193		let room_rules = self
194			.services
195			.state
196			.get_room_version_rules(event.room_id())
197			.await
198			.ok()?;
199
200		let target_id = event.redacts_id(&room_rules)?;
201
202		self.services
203			.timeline
204			.get_pdu(&target_id)
205			.await
206			.ok()?
207			.get_content::<ExtractThreadRelation>()
208			.ok()
209			.map(|t| t.relates_to)
210	}
211
212	/// `get_thread_id` for an event referenced by id; events missing
213	/// locally resolve to `None` (the main timeline).
214	pub async fn get_thread_id_for_event(&self, event_id: &EventId) -> Option<OwnedEventId> {
215		let pdu = self
216			.services
217			.timeline
218			.get_pdu(event_id)
219			.await
220			.ok()?;
221
222		self.get_thread_id(&pdu).await
223	}
224
225	pub async fn add_to_thread<E>(
226		&self,
227		root_event_id: &EventId,
228		pdu_id: RawPduId,
229		event: &E,
230	) -> Result
231	where
232		E: Event,
233	{
234		let root_id = self
235			.services
236			.timeline
237			.get_pdu_id(root_event_id)
238			.await
239			.map_err(|e| {
240				err!(Request(InvalidParam("Invalid event_id in thread message: {e:?}")))
241			})?;
242
243		let root_pdu = self
244			.services
245			.timeline
246			.get_pdu_from_id(&root_id)
247			.await
248			.map_err(|e| err!(Request(InvalidParam("Thread root not found: {e:?}"))))?;
249
250		if root_pdu.room_id() != event.room_id() {
251			return Ok(());
252		}
253
254		let mut root_pdu_json = self
255			.services
256			.timeline
257			.get_pdu_json_from_id(&root_id)
258			.await
259			.map_err(|e| err!(Request(InvalidParam("Thread root pdu not found: {e:?}"))))?;
260
261		let sender = event.sender();
262		let participants = self.db.threadid_userids.get(&root_id).await;
263		let participated = participants
264			.as_ref()
265			.is_ok_and(|participants| lists_participant(participants, sender));
266
267		let mut txn = self.services.db.txn();
268
269		if !participated {
270			let stored: Result<Vec<OwnedUserId>> = participants.deserialized();
271			let users = stored
272				.as_deref()
273				.map(|users| Left(users.iter().map(AsRef::as_ref)))
274				.unwrap_or_else(|_| Right(once(root_pdu.sender())))
275				.chain(once(sender));
276
277			self.update_participants(&mut txn, &root_id, users);
278		}
279
280		let count = pdu_id.pdu_count();
281
282		if matches!(count, PduCount::Normal(_)) {
283			txn.insert_raw(&self.db.threadactivityid_rootid, pdu_id, root_id);
284			txn.insert_raw(&self.db.threadrootid_latestcount, root_id, count.to_be_bytes());
285		}
286
287		if let CanonicalJsonValue::Object(unsigned) = root_pdu_json
288			.entry("unsigned".into())
289			.or_insert_with(|| CanonicalJsonValue::Object(BTreeMap::default()))
290		{
291			update_thread_bundle(unsigned, event);
292
293			self.services
294				.timeline
295				.stage_replace_pdu(&mut txn, &root_id, &root_pdu_json);
296		}
297
298		txn.execute();
299		Ok(())
300	}
301
302	/// Returns the root row changed by redacting a counted thread reply.
303	///
304	/// The caller holds the room lock while the count decreases and the latest
305	/// reply is replaced. A bounded miss omits the latest event while preserving
306	/// the count; when no reply remains, the whole bundle is removed and the
307	/// result carries what unlisting the thread needs. Missing roots and unchanged
308	/// summaries return `None`; storage errors are logged.
309	pub(crate) async fn redacted_reply_root(
310		&self,
311		root_event_id: &EventId,
312		reply_id: &RawPduId,
313		reply_event_id: &EventId,
314	) -> Option<RedactedRoot> {
315		matches!(reply_id.pdu_count(), PduCount::Normal(_)).into_option()?;
316
317		let root_id = self
318			.services
319			.timeline
320			.get_pdu_id(root_event_id)
321			.await
322			.optional()
323			.log_err()
324			.ok()??;
325
326		root_id
327			.shortroomid()
328			.eq(&reply_id.shortroomid())
329			.into_option()?;
330
331		let root = self.summary_root(&root_id).await?;
332		let count = thread_count(&root).and_then(|count| count.checked_sub(uint!(1)));
333		let replace =
334			thread_latest(&root).is_some_and(|latest| latest == reply_event_id.as_str());
335
336		let latest = replace
337			.then_async(|| self.latest_thread_reply(root_id, reply_event_id))
338			.await;
339
340		let latest = latest.map(|latest| latest.log_err().ok().flatten());
341
342		let changed = count.is_some() || latest.is_some();
343
344		changed.into_option()?;
345
346		let root = set_thread_count(root, count);
347		let pdu = match (count, latest) {
348			| (Some(count), _) if count == uint!(0) => set_thread_latest(root, None),
349			| (_, None) => root,
350			| (_, Some(None)) => omit_thread_latest(root),
351			| (_, Some(Some(latest))) => set_thread_latest(root, Some(latest)),
352		};
353
354		let emptied = !counts_replies(&pdu);
355		let activity = emptied
356			.then_async(|| self.listed_activity(root_id))
357			.await
358			.flatten();
359
360		Some(RedactedRoot { id: root_id, pdu, emptied, activity })
361	}
362
363	pub fn threads_until<'a>(
364		&'a self,
365		user_id: &'a UserId,
366		room_id: &'a RoomId,
367		count: PduCount,
368		include: &'a IncludeThreads,
369	) -> impl Stream<Item = Result<(PduCount, PduEvent)>> + Send {
370		let participated = matches!(include, IncludeThreads::Participated);
371
372		self.services
373			.short
374			.get_shortroomid(room_id)
375			.map_ok(move |shortroomid| PduId {
376				shortroomid,
377				count: count.saturating_sub(1),
378			})
379			.map_ok(Into::into)
380			.map_ok(move |current: RawPduId| {
381				self.db
382					.threadactivityid_rootid
383					.rev_raw_stream_from(&current)
384					.ignore_err()
385					.map(|(key, root_id)| (RawPduId::from(key), RawPduId::from(root_id)))
386					.ready_take_while(move |(activity_id, _)| {
387						activity_id.shortroomid() == current.shortroomid()
388					})
389					.map(move |(activity_id, root_id)| {
390						(activity_id, root_id, user_id, participated)
391					})
392					.wide_filter_map(async |(activity_id, root_id, user_id, participated)| {
393						self.live_thread(user_id, participated, activity_id, root_id)
394							.await
395					})
396					.map(Ok)
397			})
398			.try_flatten_stream()
399	}
400
401	/// Resolve one activity row to its thread root, skipping and reaping rows
402	/// the validity pointer has left behind.
403	async fn live_thread(
404		&self,
405		user_id: &UserId,
406		participated: bool,
407		activity_id: RawPduId,
408		root_id: RawPduId,
409	) -> Option<(PduCount, PduEvent)> {
410		let count = activity_id.pdu_count();
411		let pointer = self.latest_count(&root_id).await.ok()?;
412
413		if count != pointer {
414			// A row ahead of the pointer is a write in flight; only rows behind
415			// the pointer are dead and safe to reap.
416			if count < pointer {
417				self.db
418					.threadactivityid_rootid
419					.remove(&activity_id);
420			}
421
422			return None;
423		}
424
425		if participated && !self.is_participant(&root_id, user_id).await {
426			return None;
427		}
428
429		let pdu = self
430			.services
431			.timeline
432			.get_pdu_from_id(&root_id)
433			.await
434			.ok()?
435			.without_private_unless_sender(Some(user_id));
436
437		Some((count, pdu))
438	}
439
440	async fn is_participant(&self, root_id: &RawPduId, user_id: &UserId) -> bool {
441		self.db
442			.threadid_userids
443			.get(root_id)
444			.await
445			.is_ok_and(|participants| lists_participant(&participants, user_id))
446	}
447
448	/// Stages a thread's participant list, replacing the stored row.
449	///
450	/// Each user is written once, in the order first seen.
451	pub(super) fn update_participants<'a, I>(
452		&self,
453		txn: &mut Txn,
454		root_id: &RawPduId,
455		participants: I,
456	) where
457		I: IntoIterator<Item = &'a UserId>,
458	{
459		let users = participants
460			.into_iter()
461			.unique()
462			.map(UserId::as_bytes)
463			.collect::<Vec<_>>()
464			.join(&[0xFF][..]);
465
466		txn.insert_raw(&self.db.threadid_userids, root_id, &users);
467	}
468
469	pub(super) async fn get_participants(&self, root_id: &RawPduId) -> Result<Vec<OwnedUserId>> {
470		self.db
471			.threadid_userids
472			.get(root_id)
473			.await
474			.deserialized()
475	}
476
477	/// MSC3816: whether `user_id` has participated in the thread rooted at
478	/// `root_event_id`, having sent the root event or a threaded reply to it.
479	pub async fn user_participated(&self, root_event_id: &EventId, user_id: &UserId) -> bool {
480		let Ok(root_id) = self
481			.services
482			.timeline
483			.get_pdu_id(root_event_id)
484			.await
485		else {
486			return false;
487		};
488
489		self.is_participant(&root_id, user_id).await
490	}
491
492	#[tracing::instrument(skip(self), level = "debug")]
493	pub(super) async fn delete_all_rooms_threads(&self, room_id: &RoomId) -> Result {
494		let Ok(shortroomid) = self.services.short.get_shortroomid(room_id).await else {
495			return Ok(());
496		};
497
498		join3(
499			self.db.threadid_userids.del_prefix(&shortroomid),
500			self.db
501				.threadactivityid_rootid
502				.del_prefix(&shortroomid),
503			self.db
504				.threadrootid_latestcount
505				.del_prefix(&shortroomid),
506		)
507		.await;
508
509		Ok(())
510	}
511
512	/// Rebuild the thread activity index from every thread root.
513	///
514	/// Clears first so a partial or stale index is replaced wholesale, and leaves
515	/// out a root whose summary counts no reply. A root or key the storage engine
516	/// could not read fails the rebuild so a marker-gated caller retries; any
517	/// other failed root or malformed key is logged and skipped without failing
518	/// the rebuild.
519	pub async fn rebuild_thread_activity(&self) -> Result {
520		self.db.threadactivityid_rootid.clear().await;
521		self.db.threadrootid_latestcount.clear().await;
522
523		let unreadable = self
524			.db
525			.threadid_userids
526			.raw_keys()
527			.ready_try_filter_map(|key| Ok(root_id(key).log_err().ok()))
528			.broad_and_then(async |root_id| {
529				self.index_thread_activity(root_id)
530					.inspect_err(|error| error!(?root_id, %error, "Thread activity not indexed."))
531					.await
532					.or_else(unreadable_only)
533			})
534			.ready_filter(Result::is_err)
535			.count()
536			.await;
537
538		if unreadable > 0 {
539			return Err!(Database("{unreadable} thread roots could not be read"));
540		}
541
542		Ok(())
543	}
544
545	#[tracing::instrument(skip(self), level = "trace")]
546	async fn index_thread_activity(&self, root_id: RawPduId) -> Result {
547		let Some(pdu) = self.try_summary_root(&root_id).await? else {
548			return Ok(());
549		};
550
551		// A redaction unlists its thread under this lock.
552		let room_id: &RoomId = pdu.get("room_id").try_into()?;
553		let _lock = self.services.state.mutex.lock(room_id).await;
554		let root: PduId = root_id.into();
555		let replies = self
556			.services
557			.pdu_metadata
558			.get_relations(root.shortroomid, root.count, None, Direction::Backward, None)
559			.ready_filter_map(|(count, pdu)| {
560				pdu.get_content()
561					.is_ok_and(|content: ExtractThreadRelation| {
562						content.relates_to.rel_type == RelationType::Thread
563					})
564					.then_some(count)
565			});
566
567		// A backfilled root's replies are not in the relation index, so its summary decides.
568		let latest = match pin!(replies).next().await {
569			| Some(latest) => latest,
570			| None if self.root_counts_replies(&root_id).await? => root.count,
571			| None => return Ok(()),
572		};
573
574		self.activity_txn(root_id, latest).execute();
575
576		Ok(())
577	}
578
579	/// Rebuilds stored thread counts and removes redacted latest replies.
580	///
581	/// Each root is locked and walked once; an unredacted stored latest is kept,
582	/// and a backfilled root keeps its count. Returns the numbers of rewritten
583	/// and failed roots, counting malformed keys as failed and skipping missing bundles.
584	#[tracing::instrument(skip_all, level = "debug")]
585	pub async fn rebuild_thread_summaries(&self) -> (usize, usize) {
586		// get_relations already fans out per root, so a concurrent outer stage nests fan-outs.
587		self.db
588			.threadid_userids
589			.raw_keys()
590			.ignore_err()
591			.map(root_id)
592			.and_then(|root_id| self.rebuild_thread_summary(root_id))
593			.map(LogErr::log_err)
594			.ready_fold((0_usize, 0_usize), |(changed, failed), result| match result {
595				| Ok(rewritten) => (changed.saturating_add(usize::from(rewritten)), failed),
596				| Err(_) => (changed, failed.saturating_add(1)),
597			})
598			.await
599	}
600}
601
602/// Stages the root row a reply's redaction rewrote.
603///
604/// An emptied thread leaves the thread list, and its participants go with it
605/// so a later reply starts the thread afresh. Older activity rows the list
606/// has not reaped yet stay behind unlisted until a later reply or a rebuild.
607#[implement(Service)]
608pub(crate) fn stage_redacted_root(&self, txn: &mut Txn, root: &RedactedRoot) {
609	let RedactedRoot { id, pdu, emptied, activity } = root;
610
611	self.services
612		.timeline
613		.stage_replace_pdu(txn, id, pdu);
614
615	if *emptied {
616		txn.del_raw(&self.db.threadid_userids, id);
617		txn.del_raw(&self.db.threadrootid_latestcount, id);
618	}
619
620	if let Some(activity) = activity {
621		txn.del_raw(&self.db.threadactivityid_rootid, activity);
622	}
623}
624
625/// Reports whether a stored participant row names `user_id`.
626///
627/// The row separates user IDs with `0xFF` and is compared as bytes, so it
628/// needs no decoding.
629fn lists_participant(participants: &[u8], user_id: &UserId) -> bool {
630	participants
631		.split(|&byte| byte == 0xFF)
632		.any(|user| user == user_id.as_bytes())
633}
634
635#[implement(Service)]
636async fn summary_root(&self, root_id: &RawPduId) -> Option<CanonicalJsonObject> {
637	self.try_summary_root(root_id)
638		.await
639		.log_err()
640		.ok()?
641}
642
643#[implement(Service)]
644async fn try_summary_root(&self, root_id: &RawPduId) -> Result<Option<CanonicalJsonObject>> {
645	self.services
646		.timeline
647		.get_pdu_json_from_id(root_id)
648		.await
649		.optional()
650}
651
652#[implement(Service)]
653async fn latest_thread_reply(
654	&self,
655	root_id: RawPduId,
656	excluding: &EventId,
657) -> Result<Option<CanonicalJsonValue>> {
658	let limit = self
659		.services
660		.server
661		.config
662		.rooms
663		.redaction
664		.thread_latest_reply_search_limit;
665
666	let replies = self
667		.thread_replies(root_id, limit)
668		.ready_try_filter(|(_, pdu)| pdu.event_id != excluding);
669
670	pin!(replies)
671		.try_next()
672		.await
673		.map(|reply| reply.and_then(|(_, pdu)| thread_reply_json(&pdu)))
674}
675
676#[implement(Service)]
677fn thread_replies(
678	&self,
679	root_id: RawPduId,
680	limit: usize,
681) -> impl Stream<Item = Result<(PduCount, Pdu)>> + Send + '_ {
682	let PduId { shortroomid, count } = root_id.into();
683
684	self.services
685		.pdu_metadata
686		.try_get_relations_limited(RelationQuery {
687			shortroomid,
688			target: count,
689			from: None,
690			dir: Direction::Backward,
691			user_id: None,
692			limit,
693		})
694		.ready_try_filter(|(_, pdu)| !pdu.is_redacted())
695		.ready_try_filter(|(_, pdu)| is_thread_reply(pdu))
696}
697
698/// The activity row listing a thread, read from its validity pointer.
699///
700/// An unlisted thread has none; a failed read is logged and also yields none.
701#[implement(Service)]
702async fn listed_activity(&self, root_id: RawPduId) -> Option<RawPduId> {
703	self.latest_count(&root_id)
704		.map_ok(|count| activity_key(root_id, count))
705		.await
706		.optional()
707		.log_err()
708		.ok()
709		.flatten()
710}
711
712/// Reads a thread's validity pointer.
713///
714/// It holds the count of the one activity row that lists the thread.
715#[implement(Service)]
716async fn latest_count(&self, root_id: &RawPduId) -> Result<PduCount> {
717	self.db
718		.threadrootid_latestcount
719		.get(root_id)
720		.await
721		.map_de(PduCount::from_unsigned)
722}
723
724/// The key of the activity row listing a thread at `count`.
725///
726/// It shares the root's room, so a room's thread list is one prefix.
727pub(crate) fn activity_key(root_id: RawPduId, count: PduCount) -> RawPduId {
728	let PduId { shortroomid, .. } = root_id.into();
729
730	PduId { shortroomid, count }.into()
731}
732
733/// Whether the root's stored `m.thread` summary still counts a reply.
734///
735/// A missing root has none; a read error is returned.
736#[implement(Service)]
737async fn root_counts_replies(&self, root_id: &RawPduId) -> Result<bool> {
738	self.try_summary_root(root_id)
739		.map_ok(|root| root.as_ref().is_some_and(counts_replies))
740		.await
741}
742
743/// Stages a thread's activity row at `latest` and the validity pointer to it.
744///
745/// The pair is written together so the listing never sees one without the other.
746#[implement(Service)]
747fn activity_txn(&self, root_id: RawPduId, latest: PduCount) -> Txn {
748	let activity_id = activity_key(root_id, latest);
749	let latest = latest.to_be_bytes();
750
751	Txn::insert_each_slice(&[
752		(&self.db.threadactivityid_rootid, activity_id.as_bytes(), root_id.as_bytes()),
753		(&self.db.threadrootid_latestcount, root_id.as_bytes(), latest.as_slice()),
754	])
755}
756
757#[implement(Service)]
758#[tracing::instrument(skip(self), level = "debug")]
759async fn rebuild_thread_summary(&self, root_id: RawPduId) -> Result<bool> {
760	let Some(pdu) = self.try_summary_root(&root_id).await? else {
761		return Ok(false);
762	};
763
764	let Some(_) = thread_bundle(&pdu) else { return Ok(false) };
765	let room_id: &RoomId = pdu.get("room_id").try_into()?;
766	let _lock = self.services.state.mutex.lock(room_id).await;
767	let Some(root) = self.try_summary_root(&root_id).await? else {
768		return Ok(false);
769	};
770
771	let recount = self.recount_summary(root_id, &root).await?;
772
773	if recount
774		.as_ref()
775		.is_some_and(|recount| recount.malformed_latest)
776	{
777		return Err!(Database(
778			"Thread root {root_id:?} names a latest reply whose timeline mapping or row does \
779			 not decode"
780		));
781	}
782
783	if recount
784		.as_ref()
785		.is_some_and(|recount| recount.malformed > 0)
786	{
787		return Err!(Database(
788			"Thread root {root_id:?} has relation keys or reply rows that do not decode"
789		));
790	}
791
792	let Some(Recount { counted, stale_latest, newest, .. }) = recount.filter(Recount::changed)
793	else {
794		return Ok(false);
795	};
796
797	let root = set_thread_count(root, counted);
798	let root = if stale_latest {
799		set_thread_latest(root, newest)
800	} else {
801		root
802	};
803
804	self.services
805		.timeline
806		.replace_pdu(&root_id, &root)
807		.await?;
808
809	Ok(true)
810}
811
812/// Measures a root's stored thread summary against a recount of its replies.
813///
814/// A root with no summary gives `None`. A backfilled root's replies are not in
815/// the relation index, so it keeps its count, and a stored latest reply that is
816/// gone or redacted gives way to the newest reply. Relation keys, the replies'
817/// rows, and the latest reply's mapping and row are read without trusting them,
818/// so damage to any of them is reported rather than panicking or failing the
819/// recount.
820#[implement(Service)]
821#[tracing::instrument(level = "debug", skip(self, root))]
822pub(crate) async fn recount_summary(
823	&self,
824	root_id: RawPduId,
825	root: &CanonicalJsonObject,
826) -> Result<Option<Recount>> {
827	let Some(_) = thread_bundle(root) else {
828		return Ok(None);
829	};
830
831	let PduId { shortroomid, count: root_count } = root_id.into();
832	let (count, newest, malformed) = self
833		.services
834		.pdu_metadata
835		.try_get_relations_checked(shortroomid, root_count, Direction::Backward)
836		.ready_try_filter(|related| {
837			related
838				.as_ref()
839				.is_none_or(|(_, pdu)| !pdu.is_redacted() && is_thread_reply(pdu))
840		})
841		.ready_try_fold((0_usize, None, 0_usize), |(count, newest, malformed), related| {
842			Ok(match related {
843				| None => (count, newest, malformed.saturating_add(1)),
844				| Some((_, pdu)) => (count.saturating_add(1), newest.or(Some(pdu)), malformed),
845			})
846		})
847		.await?;
848
849	let stored = thread_count(root);
850	let counted = matches!(root_id.pdu_count(), PduCount::Normal(_))
851		.and_then(|| UInt::try_from(count).ok())
852		.or(stored);
853
854	let latest = self.latest_reply(root).await?;
855	let replace = matches!(latest, Latest::Stale);
856	let newest = replace
857		.then(|| newest.as_ref().and_then(thread_reply_json))
858		.flatten();
859
860	let recount = Recount {
861		stored,
862		counted,
863		stale_latest: replace,
864		newest,
865		malformed,
866		malformed_latest: matches!(latest, Latest::Malformed),
867	};
868
869	Ok(Some(recount))
870}
871
872/// Reads what became of a root summary's latest reply.
873///
874/// The reply is looked up as the timeline's event reader does, but with its
875/// mapping and row decoded untrusted, so damage to either is reported rather
876/// than panicking or failing.
877#[implement(Service)]
878#[tracing::instrument(level = "debug", skip_all)]
879async fn latest_reply(&self, root: &CanonicalJsonObject) -> Result<Latest> {
880	let Some(latest) = thread_latest(root).and_then(|id| <&EventId>::try_from(id).ok()) else {
881		return Ok(Latest::Current);
882	};
883
884	let reply: Result<Mapped<PduEvent>> = self
885		.services
886		.timeline
887		.get_checked(latest)
888		.or_else(|error| ready(undecodable_as(error, Mapped::Damaged)))
889		.await;
890
891	let latest = match reply.optional()? {
892		| None => Latest::Stale,
893		| Some(Mapped::Damaged) => Latest::Malformed,
894		| Some(Mapped::Event(reply)) if reply.is_redacted() => Latest::Stale,
895		| Some(Mapped::Event(_)) => Latest::Current,
896	};
897
898	Ok(latest)
899}
900
901/// Whether the root's summary needs rewriting.
902///
903/// The count or the latest reply disagrees with the replies the recount read.
904#[implement(Recount)]
905#[inline]
906#[must_use]
907pub(crate) fn changed(&self) -> bool { self.stale_latest || self.stored != self.counted }
908
909fn root_id(key: &[u8]) -> Result<RawPduId> {
910	RawPduId::try_from_bytes(key)
911		.ok_or_else(|| err!(Database("Malformed thread root key: {key:?}")))
912}
913
914/// Keeps a failed root's error only when the storage engine could not read it.
915///
916/// Any other failure skips that root, already logged by the caller.
917fn unreadable_only(error: Error) -> Result {
918	error
919		.is_transient_io()
920		.then_some(error)
921		.map_or(Ok(()), Err)
922}
923
924fn is_thread_reply(pdu: &impl Event) -> bool {
925	pdu.get_content()
926		.is_ok_and(|content: ExtractRelatesTo| matches!(content.relates_to, Relation::Thread(_)))
927}
928
929/// Returns the thread root named by borrowed event content.
930///
931/// Uses the same relation shape as the timeline append path, including legacy replies.
932pub(crate) fn thread_root(content: &CanonicalJsonValue) -> Option<OwnedEventId> {
933	let content: ExtractRelatesTo =
934		serde_json::from_value(serde_json::to_value(content).ok()?).ok()?;
935
936	match content.relates_to {
937		| Relation::Thread(thread) => Some(thread.event_id),
938		| _ => None,
939	}
940}