1use std::{fmt::Debug, sync::Arc};
7
8use futures::{StreamExt, TryStreamExt, future::join};
9use ruma::{
10 CanonicalJsonObject, EventId, RoomId, UserId, api::Direction, events::relation::RelationType,
11 push::Action,
12};
13use serde::Serialize;
14use tuwunel_core::{
15 debug_warn, implement,
16 utils::{
17 BoolExt,
18 stream::{TryReadyExt, automatic_width},
19 },
20};
21use tuwunel_database::{Deserialized, Map};
22use tuwunel_matrix::{
23 event::{Event, RelationTypeEqual},
24 pdu::{Count, Pdu, PduId, RawPduId},
25};
26
27use super::{CountMutex, Notified};
28use crate::rooms::short::ShortRoomId;
29
30type Raises = fn(&Action) -> bool;
35
36#[derive(Clone, Copy)]
40struct Counter<'a> {
41 counts: &'a Arc<Map>,
42 mutex: &'a CountMutex,
43 raises: Raises,
44}
45
46#[derive(Clone, Copy)]
50struct Redacted<'a> {
51 room_id: &'a RoomId,
52 shortroomid: ShortRoomId,
53 count: u64,
54 thread_root: Option<&'a EventId>,
55}
56
57#[implement(super::Service)]
65#[tracing::instrument(name = "redact", level = "debug", skip_all)]
66pub(crate) async fn redact_pdu(
67 &self,
68 pdu_id: &RawPduId,
69 event_id: &EventId,
70 pdu: &CanonicalJsonObject,
71) {
72 let PduId { shortroomid, count: Count::Normal(count) } = (*pdu_id).into() else {
73 return;
74 };
75
76 let Ok(pdu) = Pdu::from_object_and_eventid(event_id, pdu.clone())
77 .inspect_err(|error| debug_warn!(%event_id, %error, "Redacted event kept its counts"))
78 else {
79 return;
80 };
81
82 let thread_root = self.services.threads.get_thread_id(&pdu).await;
83 let redacted = Redacted {
84 room_id: pdu.room_id(),
85 shortroomid,
86 count,
87 thread_root: thread_root.as_deref(),
88 };
89
90 self.services
91 .state_cache
92 .local_users_in_room(pdu.room_id())
93 .map(ToOwned::to_owned)
94 .for_each_concurrent(automatic_width(), async |user| {
95 self.redact_pdu_for_user(&user, redacted).await;
96 })
97 .await;
98}
99
100#[implement(super::Service)]
101async fn redact_pdu_for_user(&self, user: &UserId, redacted: Redacted<'_>) {
102 let key = (user, redacted.count);
103 let Ok(notified) = self
104 .db
105 .useridcount_notification
106 .qry(&key)
107 .await
108 .deserialized::<Notified>()
109 else {
110 return;
111 };
112
113 self.db.useridcount_notification.del(key);
114
115 let notifications = Counter {
116 counts: &self.db.userroomid_notificationcount,
117 mutex: &self.notification_increment_mutex,
118 raises: Action::should_notify,
119 };
120
121 let highlights = Counter {
122 counts: &self.db.userroomid_highlightcount,
123 mutex: &self.highlight_increment_mutex,
124 raises: Action::is_highlight,
125 };
126
127 let actions = ¬ified.actions;
128 join(
129 self.uncount(notifications, actions, user, redacted),
130 self.uncount(highlights, actions, user, redacted),
131 )
132 .await;
133}
134
135#[implement(super::Service)]
143async fn uncount(
144 &self,
145 counter: Counter<'_>,
146 actions: &[Action],
147 user: &UserId,
148 redacted: Redacted<'_>,
149) {
150 if actions.iter().any(counter.raises).is_false() {
151 return;
152 }
153
154 let Redacted { room_id, thread_root, .. } = redacted;
155 let _lock = counter
156 .mutex
157 .lock(&(room_id.to_owned(), user.to_owned()))
158 .await;
159
160 match thread_root {
161 | Some(root) =>
162 self.uncount_at(counter, (user, room_id, root), user, redacted)
163 .await,
164 | None =>
165 self.uncount_at(counter, (user, room_id), user, redacted)
166 .await,
167 }
168}
169
170#[implement(super::Service)]
171async fn uncount_at<K>(&self, counter: Counter<'_>, key: K, user: &UserId, redacted: Redacted<'_>)
172where
173 K: Serialize + Debug + Send + Sync,
174{
175 let value: u64 = counter
176 .counts
177 .qry(&key)
178 .await
179 .deserialized()
180 .unwrap_or(0);
181
182 if let Some(lowered) = value.checked_sub(1)
184 && self
185 .still_counted(counter, user, redacted, value)
186 .await
187 {
188 counter.counts.put(key, lowered);
189 }
190}
191
192#[implement(super::Service)]
201#[tracing::instrument(level = "trace", skip_all)]
202async fn still_counted(
203 &self,
204 counter: Counter<'_>,
205 user: &UserId,
206 redacted: Redacted<'_>,
207 value: u64,
208) -> bool {
209 let Redacted { room_id, count, thread_root, .. } = redacted;
210
211 if let Some(root) = thread_root
212 && let Ok(read) = self
213 .last_thread_notification_read(user, room_id, root)
214 .await
215 {
216 return count > read;
217 }
218
219 let past_room_read = self
220 .last_notification_read(user, room_id)
221 .await
222 .ok()
223 .is_none_or(|read| count > read);
224
225 let Some(root) = thread_root.filter(|_| past_room_read.is_false()) else {
226 return past_room_read;
227 };
228
229 self.counted_by_replies(counter, user, redacted, root, value)
230 .await
231}
232
233#[implement(super::Service)]
244#[tracing::instrument(level = "trace", skip_all)]
245async fn counted_by_replies(
246 &self,
247 counter: Counter<'_>,
248 user: &UserId,
249 Redacted { shortroomid, count, .. }: Redacted<'_>,
250 root: &EventId,
251 value: u64,
252) -> bool {
253 let Ok(root @ Count::Normal(_)) = self.services.timeline.get_pdu_count(root).await else {
254 return false;
255 };
256
257 let limit = usize::try_from(value).unwrap_or(usize::MAX);
258 let budget = self
259 .services
260 .server
261 .config
262 .rooms
263 .redaction
264 .thread_latest_reply_search_limit;
265
266 let walk = self
268 .services
269 .pdu_metadata
270 .relation_counts(root, Some(Count::Normal(count)), Direction::Forward, budget)
271 .inspect_err(|error| debug_warn!(%error, "Relation read failed, count kept"))
272 .map_err(drop)
273 .and_then(async |reply| {
274 let raised = self
275 .raised_by_reply(counter, user, shortroomid, reply)
276 .await;
277
278 Ok(raised)
279 })
280 .ready_try_fold((0_usize, 0_usize), |(seen, later), raised: bool| {
281 let later = later.saturating_add(usize::from(raised));
282
283 later
284 .lt(&limit)
285 .then_some((seen.saturating_add(1), later))
286 .ok_or(())
287 })
288 .await;
289
290 walk.is_ok_and(|(seen, later)| seen < budget && later < limit)
292}
293
294#[implement(super::Service)]
299#[tracing::instrument(level = "trace", skip_all)]
300async fn raised_by_reply(
301 &self,
302 counter: Counter<'_>,
303 user: &UserId,
304 shortroomid: ShortRoomId,
305 count: Count,
306) -> bool {
307 let raised = self
308 .db
309 .useridcount_notification
310 .qry(&(user, count.into_unsigned()))
311 .await
312 .deserialized()
313 .is_ok_and(|notified: Notified| notified.actions.iter().any(counter.raises));
314
315 let pdu_id: RawPduId = PduId { shortroomid, count }.into();
316
317 raised
318 && self
319 .services
320 .timeline
321 .get_pdu_from_id(&pdu_id)
322 .await
323 .is_ok_and(|reply| RelationType::Thread.relation_type_equal(&reply))
324}
325
326#[cfg(test)]
327mod tests {
328 use std::{fmt::Debug, iter::once, sync::Arc};
329
330 use ruma::{
331 EventId, RoomId, UserId, event_id,
332 push::{Action, HighlightTweakValue, Tweak},
333 room_id, user_id,
334 };
335 use serde::Serialize;
336 use serde_json::{Value, json};
337 use tuwunel_core::{Result, config::Figment, utils::result::NotFound};
338 use tuwunel_database::{Deserialized, Json, Map};
339 use tuwunel_matrix::pdu::{Count, PduId, RawPduId};
340
341 use super::Redacted;
342 use crate::{
343 Services,
344 pusher::{Data, Notified},
345 test_utils::fixture,
346 };
347
348 const SHORTROOMID: u64 = 1;
349
350 #[tokio::test]
355 async fn redaction_uncounts_an_unread_thread_reply_once() -> Result {
356 let Some(fixture) = fixture(Figment::new()).await? else {
357 return Ok(());
358 };
359
360 let pusher = &fixture.services.pusher;
361 let Data {
362 useridcount_notification: notified,
363 userroomid_notificationcount: notifications,
364 roomuserid_lastnotificationread: last_reads,
365 ..
366 } = &pusher.db;
367
368 let (user, room_id, root) = ids();
369 let thread = (user, room_id, root);
370
371 notifications.put(thread, 2_u64);
372 last_reads.put((room_id, user, root), 15_u64);
373 notify(notified, user, 10, &[Action::Notify]);
374 notify(notified, user, 20, &[Action::Notify]);
375
376 for (count, expected) in [(20, 1), (20, 1), (10, 1)] {
377 pusher
378 .redact_pdu_for_user(user, redacted(room_id, count, Some(root)))
379 .await;
380
381 assert_eq!(stored(notifications, thread).await?, expected, "after {count}");
382 }
383
384 for count in [10_u64, 20] {
385 assert!(notified.qry(&(user, count)).await.is_err(), "record {count} kept");
386 }
387
388 Ok(())
389 }
390
391 #[tokio::test]
396 async fn redaction_uncounts_room_counts_past_the_room_stamp() -> Result {
397 let Some(fixture) = fixture(Figment::new()).await? else {
398 return Ok(());
399 };
400
401 let pusher = &fixture.services.pusher;
402 let Data {
403 useridcount_notification: notified,
404 userroomid_notificationcount: notifications,
405 userroomid_highlightcount: highlights,
406 roomuserid_lastnotificationread: last_reads,
407 ..
408 } = &pusher.db;
409
410 let (user, room_id, root) = ids();
411 let (room, thread) = ((user, room_id), (user, room_id, root));
412 let highlight = Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes));
413
414 notifications.put(room, 1_u64);
415 highlights.put(room, 1_u64);
416 notifications.put(thread, 1_u64);
417 last_reads.put((room_id, user), 15_u64);
418 notify(notified, user, 20, &[Action::Notify, highlight]);
419 notify(notified, user, 30, &[Action::Notify]);
420
421 for redaction in [redacted(room_id, 20, None), redacted(room_id, 30, Some(root))] {
422 pusher.redact_pdu_for_user(user, redaction).await;
423 }
424
425 assert_eq!(stored(notifications, room).await?, 0);
426 assert_eq!(stored(highlights, room).await?, 0);
427 assert_eq!(stored(notifications, thread).await?, 0);
428
429 Ok(())
430 }
431
432 #[tokio::test]
441 async fn redaction_behind_a_main_receipt_counts_later_replies() -> Result {
442 let Some(fixture) = fixture(Figment::new()).await? else {
443 return Ok(());
444 };
445
446 let services = &fixture.services;
447 let pusher = &services.pusher;
448 let Data {
449 useridcount_notification: notified,
450 userroomid_notificationcount: notifications,
451 roomuserid_lastnotificationread: last_reads,
452 ..
453 } = &pusher.db;
454
455 let (user, room_id, root) = ids();
456 let thread = (user, room_id, root);
457 let root_count = 10;
458 let (notifies, highlights) =
459 ([Action::Notify], [Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes))]);
460
461 let relations = [
462 (20, event_id!("$first:localhost"), "m.thread", ¬ifies),
463 (30, event_id!("$second:localhost"), "m.thread", ¬ifies),
464 (35, event_id!("$third:localhost"), "m.thread", ¬ifies),
465 (36, event_id!("$reaction:localhost"), "m.annotation", ¬ifies),
466 (37, event_id!("$mention:localhost"), "m.thread", &highlights),
467 ];
468
469 let text = json!({ "msgtype": "m.text", "body": "root" });
470 store_pdu(services, room_id, root_count, root, &text);
471
472 for (count, event_id, rel_type, actions) in relations {
473 store_reply(services, (root, root_count), (event_id, count), rel_type, actions);
474 }
475
476 notify(notified, user, 45, &[Action::Notify]);
477 notifications.put(thread, 2_u64);
478 last_reads.put((room_id, user), 40_u64);
479
480 for (count, expected) in [(20, 2), (30, 1), (35, 0)] {
481 pusher
482 .redact_pdu_for_user(user, redacted(room_id, count, Some(root)))
483 .await;
484
485 assert_eq!(stored(notifications, thread).await?, expected, "after {count}");
486 }
487
488 Ok(())
489 }
490
491 #[tokio::test]
498 async fn redaction_keeps_a_count_the_reply_budget_cannot_settle() -> Result {
499 let config =
500 Figment::new().merge(("rooms.redaction.thread_latest_reply_search_limit", 3));
501
502 let Some(fixture) = fixture(config).await? else {
503 return Ok(());
504 };
505
506 let services = &fixture.services;
507 let pusher = &services.pusher;
508 let Data {
509 userroomid_highlightcount: highlights,
510 roomuserid_lastnotificationread: last_reads,
511 ..
512 } = &pusher.db;
513
514 let (user, room_id, long_root) = ids();
515 let exact_root = event_id!("$exact:localhost");
516 let short_root = event_id!("$short:localhost");
517 let notifies = [Action::Notify];
518 let highlight =
519 [Action::Notify, Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes))];
520
521 let text = json!({ "msgtype": "m.text", "body": "root" });
523 let threads = [
524 (long_root, 10, 20, [30, 31, 32, 33].as_slice(), 1),
525 (exact_root, 50, 60, [70, 71, 72].as_slice(), 1),
526 (short_root, 80, 90, [100, 101].as_slice(), 0),
527 ];
528
529 for (root, root_count, highlighted, later, _) in threads {
530 store_pdu(services, room_id, root_count, root, &text);
531 highlights.put((user, room_id, root), 1_u64);
532
533 let later = later
534 .iter()
535 .map(|&count| (count, notifies.as_slice()));
536
537 for (count, actions) in once((highlighted, highlight.as_slice())).chain(later) {
538 let event_id =
539 EventId::parse(format!("$reply{count}:localhost")).expect("reply id");
540
541 store_reply(
542 services,
543 (root, root_count),
544 (&event_id, count),
545 "m.thread",
546 actions,
547 );
548 }
549 }
550
551 last_reads.put((room_id, user), 200_u64);
552
553 for (root, _, highlighted, _, expected) in threads {
554 pusher
555 .redact_pdu_for_user(user, redacted(room_id, highlighted, Some(root)))
556 .await;
557
558 let count = stored(highlights, (user, room_id, root)).await?;
559
560 assert_eq!(count, expected, "after {highlighted}");
561 }
562
563 Ok(())
564 }
565
566 fn ids() -> (&'static UserId, &'static RoomId, &'static EventId) {
567 (
568 user_id!("@alice:localhost"),
569 room_id!("!room:localhost"),
570 event_id!("$root:localhost"),
571 )
572 }
573
574 fn notify(notified: &Arc<Map>, user: &UserId, count: u64, actions: &[Action]) {
575 let record = Notified {
576 ts: 0,
577 sroomid: SHORTROOMID,
578 tag: None,
579 actions: actions.into(),
580 };
581
582 notified.put((user, count), Json(record));
583 }
584
585 fn redacted<'a>(
586 room_id: &'a RoomId,
587 count: u64,
588 thread_root: Option<&'a EventId>,
589 ) -> Redacted<'a> {
590 Redacted {
591 room_id,
592 shortroomid: SHORTROOMID,
593 count,
594 thread_root,
595 }
596 }
597
598 async fn stored<K>(counter: &Arc<Map>, key: K) -> Result<u64>
602 where
603 K: Serialize + Debug,
604 {
605 counter
606 .qry(&key)
607 .await
608 .deserialized()
609 .optional()
610 .map(Option::unwrap_or_default)
611 }
612
613 fn store_reply(
617 services: &Services,
618 (root, root_count): (&EventId, u64),
619 (event_id, count): (&EventId, u64),
620 rel_type: &str,
621 actions: &[Action],
622 ) {
623 let (user, room_id, _) = ids();
624 let content = json!({
625 "msgtype": "m.text", "body": "reply",
626 "m.relates_to": { "rel_type": rel_type, "event_id": root },
627 });
628
629 store_pdu(services, room_id, count, event_id, &content);
630 services
631 .pdu_metadata
632 .add_relation(Count::Normal(count), Count::Normal(root_count));
633
634 notify(&services.pusher.db.useridcount_notification, user, count, actions);
635 }
636
637 fn store_pdu(
642 services: &Services,
643 room_id: &RoomId,
644 count: u64,
645 event_id: &EventId,
646 content: &Value,
647 ) {
648 let pdu_id: RawPduId = PduId {
649 shortroomid: SHORTROOMID,
650 count: Count::Normal(count),
651 }
652 .into();
653
654 let pdu = json!({
655 "type": "m.room.message", "event_id": event_id, "room_id": room_id,
656 "sender": "@bob:localhost", "origin_server_ts": 1, "depth": 1,
657 "hashes": { "sha256": "hash" }, "prev_events": [], "auth_events": [],
658 "content": content,
659 });
660
661 services.db["eventid_pduid"].insert(event_id.as_bytes(), pdu_id.as_bytes());
662 services.db["pduid_pdu"].raw_put(pdu_id, Json(&pdu));
663 }
664}