Commit b5596c4189

b5596c41897164297a07068e33493ea2dedc3207

parent: 9fe600c632

Unregistered key

tayfuryldzz <yildiztayfur668@gmail.com> · 2026-09-19 01:14 UTC
committer: <noreply@github.com>

Support implicit read receipts (#691)

Signed-off-by: tayfuryldzz <yildiztayfur668@gmail.com>
Co-authored-by: TayfurYldz <238304586+TayfurYldz@users.noreply.github.com>
Co-authored-by: Ulyssa <git@ulyssa.dev>

Layout: unified · split

src/base.rs +267 −6
@@ -1378,6 +1378,7 @@ impl RoomInfo {
1378 previews: &mut PreviewManager, 1378 previews: &mut PreviewManager,
1379 ) { 1379 ) {
1380 let event_id = sticker.event_id().to_owned(); 1380 let event_id = sticker.event_id().to_owned();
1381 let sender = sticker.sender().to_owned();
1381 let key = MessageKey { 1382 let key = MessageKey {
1382 ts: sticker.origin_server_ts().into(), 1383 ts: sticker.origin_server_ts().into(),
1383 id: event_id.clone().into(), 1384 id: event_id.clone().into(),
@@ -1406,10 +1407,11 @@ impl RoomInfo {
1406 } 1407 }
1407 1408
1408 let loc = EventLocation::Message(thread_root.clone(), key.clone()); 1409 let loc = EventLocation::Message(thread_root.clone(), key.clone());
1409 self.keys.insert(event_id, loc); 1410 self.keys.insert(event_id.clone(), loc);
1410 1411
1411 let thread = self.get_thread_mut(thread_root); 1412 let thread = self.get_thread_mut(thread_root);
1412 thread.insert_message(key, sticker); 1413 thread.insert_message(key, sticker);
1414 self.set_implicit_receipt(sender, event_id);
1413 } 1415 }
1414 1416
1415 /// Insert a reaction to a message. 1417 /// Insert a reaction to a message.
@@ -1471,14 +1473,16 @@ impl RoomInfo {
1471 1473
1472 pub fn insert_any_state(&mut self, msg: AnySyncStateEvent) { 1474 pub fn insert_any_state(&mut self, msg: AnySyncStateEvent) {
1473 let event_id = msg.event_id().to_owned(); 1475 let event_id = msg.event_id().to_owned();
1476 let sender = msg.sender().to_owned();
1474 let key = MessageKey { 1477 let key = MessageKey {
1475 ts: msg.origin_server_ts().into(), 1478 ts: msg.origin_server_ts().into(),
1476 id: event_id.clone().into(), 1479 id: event_id.clone().into(),
1477 }; 1480 };
1478 1481
1479 let loc = EventLocation::State(key.clone()); 1482 let loc = EventLocation::State(key.clone());
1480 self.keys.insert(event_id, loc); 1483 self.keys.insert(event_id.clone(), loc);
1481 self.messages.insert_message(key, msg); 1484 self.messages.insert_message(key, msg);
1485 self.set_implicit_receipt(sender, event_id);
1482 } 1486 }
1483 1487
1484 /// Indicates whether this room has unread messages. 1488 /// Indicates whether this room has unread messages.
@@ -1501,18 +1505,22 @@ impl RoomInfo {
1501 /// Inserts events that couldn't be decrypted into the scrollback. 1505 /// Inserts events that couldn't be decrypted into the scrollback.
1502 pub fn insert_encrypted(&mut self, msg: RoomEncryptedEvent) { 1506 pub fn insert_encrypted(&mut self, msg: RoomEncryptedEvent) {
1503 let event_id = msg.event_id().to_owned(); 1507 let event_id = msg.event_id().to_owned();
1508 let sender = msg.sender().to_owned();
1504 let key = MessageKey { 1509 let key = MessageKey {
1505 ts: msg.origin_server_ts().into(), 1510 ts: msg.origin_server_ts().into(),
1506 id: event_id.clone().into(), 1511 id: event_id.clone().into(),
1507 }; 1512 };
1508 1513
1509 self.keys.insert(event_id, EventLocation::Message(None, key.clone())); 1514 self.keys
1515 .insert(event_id.clone(), EventLocation::Message(None, key.clone()));
1510 self.messages.insert(key, msg.into()); 1516 self.messages.insert(key, msg.into());
1517 self.set_implicit_receipt(sender, event_id);
1511 } 1518 }
1512 1519
1513 /// Insert a new message. 1520 /// Insert a new message.
1514 pub fn insert_message(&mut self, msg: RoomMessageEvent) { 1521 pub fn insert_message(&mut self, msg: RoomMessageEvent) {
1515 let event_id = msg.event_id().to_owned(); 1522 let event_id = msg.event_id().to_owned();
1523 let sender = msg.sender().to_owned();
1516 let key = MessageKey { 1524 let key = MessageKey {
1517 ts: msg.origin_server_ts().into(), 1525 ts: msg.origin_server_ts().into(),
1518 id: event_id.clone().into(), 1526 id: event_id.clone().into(),
@@ -1523,12 +1531,14 @@ impl RoomInfo {
1523 if let Some(edits) = self.unloaded_edits.remove(&event_id) { 1531 if let Some(edits) = self.unloaded_edits.remove(&event_id) {
1524 message.set_edits(edits); 1532 message.set_edits(edits);
1525 } 1533 }
1526 self.keys.insert(event_id, loc); 1534 self.keys.insert(event_id.clone(), loc);
1527 self.messages.insert_message(key, message); 1535 self.messages.insert_message(key, message);
1536 self.set_implicit_receipt(sender, event_id);
1528 } 1537 }
1529 1538
1530 fn insert_thread(&mut self, msg: RoomMessageEvent, thread_root: OwnedEventId) { 1539 fn insert_thread(&mut self, msg: RoomMessageEvent, thread_root: OwnedEventId) {
1531 let event_id = msg.event_id().to_owned(); 1540 let event_id = msg.event_id().to_owned();
1541 let sender = msg.sender().to_owned();
1532 let key = MessageKey { 1542 let key = MessageKey {
1533 ts: msg.origin_server_ts().into(), 1543 ts: msg.origin_server_ts().into(),
1534 id: event_id.clone().into(), 1544 id: event_id.clone().into(),
@@ -1543,8 +1553,9 @@ impl RoomInfo {
1543 if let Some(edits) = self.unloaded_edits.remove(&event_id) { 1553 if let Some(edits) = self.unloaded_edits.remove(&event_id) {
1544 message.set_edits(edits); 1554 message.set_edits(edits);
1545 } 1555 }
1546 self.keys.insert(event_id, loc); 1556 self.keys.insert(event_id.clone(), loc);
1547 replies.insert_message(key, message); 1557 replies.insert_message(key, message);
1558 self.set_implicit_receipt(sender, event_id);
1548 } 1559 }
1549 1560
1550 /// Insert a new message event. 1561 /// Insert a new message event.
@@ -1618,6 +1629,15 @@ impl RoomInfo {
1618 user_id: OwnedUserId, 1629 user_id: OwnedUserId,
1619 event_id: OwnedEventId, 1630 event_id: OwnedEventId,
1620 ) { 1631 ) {
1632 if let Some(old_event_id) =
1633 self.user_receipts.get(&thread).and_then(|receipts| receipts.get(&user_id)) &&
1634 let (Some(old_key), Some(new_key)) =
1635 (self.receipt_key(old_event_id), self.receipt_key(&event_id)) &&
1636 new_key <= old_key
1637 {
1638 return;
1639 }
1640
1621 self.clear_receipt(&thread, &user_id); 1641 self.clear_receipt(&thread, &user_id);
1622 self.event_receipts 1642 self.event_receipts
1623 .entry(thread.clone()) 1643 .entry(thread.clone())
@@ -1628,6 +1648,74 @@ impl RoomInfo {
1628 self.user_receipts.entry(thread).or_default().insert(user_id, event_id); 1648 self.user_receipts.entry(thread).or_default().insert(user_id, event_id);
1629 } 1649 }
1630 1650
1651 fn receipt_key(&self, event_id: &EventId) -> Option<&MessageKey> {
1652 match self.keys.get(event_id)? {
1653 EventLocation::Message(_, key) | EventLocation::State(key) => Some(key),
1654 _ => None,
1655 }
1656 }
1657
1658 fn latest_implicit_receipt(
1659 &self,
1660 thread: &ReceiptThread,
1661 user_id: &UserId,
1662 ) -> Option<OwnedEventId> {
1663 let messages = match thread {
1664 ReceiptThread::Main => &self.messages,
1665 ReceiptThread::Thread(root) => self.threads.get(root)?,
1666 _ => return None,
1667 };
1668 messages.iter().rev().find_map(|(_, message)| {
1669 (message.sender == user_id)
1670 .then(|| message.event.event_id().map(ToOwned::to_owned))
1671 .flatten()
1672 })
1673 }
1674
1675 /// Whenever a user sends a message, we can consider that an effective update to their
1676 /// read receipt, even when their client isn't sending them to the room.
1677 ///
1678 /// While the Matrix specification doesn't explicitly mention "implicit receipts", this
1679 /// is effectively what is described by "Marking notifications as read",
1680 ///
1681 /// > When the user updates their read receipt (either by using the API or by sending an
1682 /// > event), notifications prior to and including that event MUST be marked as read.
1683 ///
1684 /// This method is called every time we insert a new message into the [RoomInfo], so that
1685 /// we show in the timeline that the user has now read up to the point where they sent
1686 /// a message.
1687 fn set_implicit_receipt(&mut self, user_id: OwnedUserId, event_id: OwnedEventId) {
1688 let thread = match self.keys.get(&event_id) {
1689 Some(EventLocation::Message(None, _)) | Some(EventLocation::State(_)) => {
1690 ReceiptThread::Main
1691 },
1692 Some(EventLocation::Message(Some(root), _)) => ReceiptThread::Thread(root.clone()),
1693 _ => return,
1694 };
1695 let resolved_users: Vec<_> = self
1696 .user_receipts
1697 .get(&thread)
1698 .into_iter()
1699 .flat_map(HashMap::iter)
1700 .filter(|(_, receipt_event_id)| *receipt_event_id == &event_id)
1701 .map(|(receipt_user, _)| receipt_user.clone())
1702 .collect();
1703 for receipt_user in resolved_users {
1704 if let Some(latest_event_id) = self.latest_implicit_receipt(&thread, &receipt_user) {
1705 self.set_receipt(thread.clone(), receipt_user, latest_event_id);
1706 }
1707 }
1708 if self
1709 .user_receipts
1710 .get(&thread)
1711 .and_then(|receipts| receipts.get(&user_id))
1712 .is_some_and(|old_event_id| self.receipt_key(old_event_id).is_none())
1713 {
1714 return;
1715 }
1716 self.set_receipt(thread, user_id, event_id);
1717 }
1718
1631 pub fn fully_read( 1719 pub fn fully_read(
1632 &mut self, 1720 &mut self,
1633 room_id: OwnedRoomId, 1721 room_id: OwnedRoomId,
@@ -2396,6 +2484,19 @@ pub mod tests {
2396 use crate::config::user_style_from_color; 2484 use crate::config::user_style_from_color;
2397 use crate::tests::*; 2485 use crate::tests::*;
2398 2486
2487 fn mock_room_message_event(
2488 content: RoomMessageEventContent,
2489 sender: OwnedUserId,
2490 key: MessageKey,
2491 ) -> RoomMessageEvent {
2492 let message = mock_room1_message(content, sender, key);
2493 let MessageEvent::Original(event, _) = message.event else {
2494 unreachable!("mock_room1_message always returns an original room message")
2495 };
2496
2497 MessageLikeEvent::Original(*event)
2498 }
2499
2399 fn create_reaction_event( 2500 fn create_reaction_event(
2400 content: &ReactionEventContent, 2501 content: &ReactionEventContent,
2401 event_id: &str, 2502 event_id: &str,
@@ -2416,7 +2517,7 @@ pub mod tests {
2416 } 2517 }
2417 2518
2418 #[test] 2519 #[test]
2419 fn multiple_identical_reactions() { 2520 fn test_multiple_identical_reactions() {
2420 let mut info = RoomInfo::default(); 2521 let mut info = RoomInfo::default();
2421 2522
2422 let content = ReactionEventContent::new(Annotation::new( 2523 let content = ReactionEventContent::new(Annotation::new(
@@ -2455,6 +2556,166 @@ pub mod tests {
2455 assert_eq!(reacts, vec![("🏠", 1), ("🙂", 2)]); 2556 assert_eq!(reacts, vec![("🏠", 1), ("🙂", 2)]);
2456 } 2557 }
2457 2558
2559 #[test]
2560 fn test_implicit_receipt_tracks_message_sender_for_display() {
2561 let mut info = RoomInfo::default();
2562 let settings = mock_settings();
2563 let mut previews = PreviewManager::new(&settings);
2564 let event = mock_room_message_event(
2565 RoomMessageEventContent::text_plain("sent by another user"),
2566 TEST_USER2.clone(),
2567 MSG5_KEY.clone(),
2568 );
2569
2570 info.insert_with_preview(event, &settings, &mut previews);
2571
2572 assert_eq!(
2573 info.user_receipts
2574 .get(&ReceiptThread::Main)
2575 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2576 Some(&*MSG5_EVID),
2577 );
2578 assert!(
2579 info.event_receipts
2580 .get(&ReceiptThread::Main)
2581 .and_then(|receipts| receipts.get(&*MSG5_EVID))
2582 .is_some_and(|users| users.contains(&*TEST_USER2))
2583 );
2584 }
2585
2586 #[test]
2587 fn test_implicit_receipts_do_not_move_backwards_during_backpagination() {
2588 let mut info = RoomInfo::default();
2589 let settings = mock_settings();
2590 let mut previews = PreviewManager::new(&settings);
2591 let newer = mock_room_message_event(
2592 RoomMessageEventContent::text_plain("newer"),
2593 TEST_USER2.clone(),
2594 MSG5_KEY.clone(),
2595 );
2596 let older = mock_room_message_event(
2597 RoomMessageEventContent::text_plain("older"),
2598 TEST_USER2.clone(),
2599 MSG2_KEY.clone(),
2600 );
2601
2602 info.insert_with_preview(newer, &settings, &mut previews);
2603 info.insert_with_preview(older, &settings, &mut previews);
2604
2605 assert_eq!(
2606 info.user_receipts
2607 .get(&ReceiptThread::Main)
2608 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2609 Some(&*MSG5_EVID),
2610 );
2611 }
2612
2613 #[test]
2614 fn test_implicit_receipt_preserves_thread_context() {
2615 let mut info = RoomInfo::default();
2616 let root = owned_event_id!("$thread_root");
2617 let reply = owned_event_id!("$thread_reply");
2618 let thread = ReceiptThread::Thread(root.clone());
2619
2620 info.keys
2621 .insert(reply.clone(), EventLocation::Message(Some(root), MSG5_KEY.clone()));
2622 info.set_implicit_receipt(TEST_USER1.clone(), reply.clone());
2623
2624 assert_eq!(
2625 info.user_receipts
2626 .get(&thread)
2627 .and_then(|receipts| receipts.get(&*TEST_USER1)),
2628 Some(&reply),
2629 );
2630 }
2631
2632 #[test]
2633 fn test_implicit_receipt_supports_state_events() {
2634 let mut info = RoomInfo::default();
2635 info.keys.insert(MSG5_EVID.clone(), EventLocation::State(MSG5_KEY.clone()));
2636
2637 info.set_implicit_receipt(TEST_USER2.clone(), MSG5_EVID.clone());
2638
2639 assert_eq!(
2640 info.user_receipts
2641 .get(&ReceiptThread::Main)
2642 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2643 Some(&*MSG5_EVID),
2644 );
2645 }
2646
2647 #[test]
2648 fn test_explicit_receipt_does_not_move_implicit_receipt_backwards() {
2649 let mut info = RoomInfo::default();
2650 let older = mock_room_message_event(
2651 RoomMessageEventContent::text_plain("older"),
2652 TEST_USER2.clone(),
2653 MSG2_KEY.clone(),
2654 );
2655 let newer = mock_room_message_event(
2656 RoomMessageEventContent::text_plain("newer"),
2657 TEST_USER2.clone(),
2658 MSG5_KEY.clone(),
2659 );
2660
2661 info.insert_message(older);
2662 info.insert_message(newer);
2663 info.set_receipt(ReceiptThread::Main, TEST_USER2.clone(), MSG2_EVID.clone());
2664
2665 assert_eq!(
2666 info.user_receipts
2667 .get(&ReceiptThread::Main)
2668 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2669 Some(&*MSG5_EVID),
2670 );
2671 }
2672
2673 #[test]
2674 fn test_implicit_receipt_waits_for_unloaded_explicit_receipt() {
2675 let mut info = RoomInfo::default();
2676 info.set_receipt(ReceiptThread::Main, TEST_USER2.clone(), MSG5_EVID.clone());
2677 let older = mock_room_message_event(
2678 RoomMessageEventContent::text_plain("older"),
2679 TEST_USER2.clone(),
2680 MSG2_KEY.clone(),
2681 );
2682
2683 info.insert_message(older);
2684
2685 assert_eq!(
2686 info.user_receipts
2687 .get(&ReceiptThread::Main)
2688 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2689 Some(&*MSG5_EVID),
2690 );
2691 }
2692
2693 #[test]
2694 fn test_implicit_receipt_advances_after_explicit_event_loads() {
2695 let mut info = RoomInfo::default();
2696 info.set_receipt(ReceiptThread::Main, TEST_USER2.clone(), MSG2_EVID.clone());
2697 let newer = mock_room_message_event(
2698 RoomMessageEventContent::text_plain("newer"),
2699 TEST_USER2.clone(),
2700 MSG5_KEY.clone(),
2701 );
2702 let older = mock_room_message_event(
2703 RoomMessageEventContent::text_plain("older"),
2704 TEST_USER2.clone(),
2705 MSG2_KEY.clone(),
2706 );
2707
2708 info.insert_message(newer);
2709 info.insert_message(older);
2710
2711 assert_eq!(
2712 info.user_receipts
2713 .get(&ReceiptThread::Main)
2714 .and_then(|receipts| receipts.get(&*TEST_USER2)),
2715 Some(&*MSG5_EVID),
2716 );
2717 }
2718
2458 #[test] 2719 #[test]
2459 fn test_typing_spans() { 2720 fn test_typing_spans() {
2460 let mut info = RoomInfo::default(); 2721 let mut info = RoomInfo::default();
src/message/mod.rs +3 −1
@@ -1895,7 +1895,9 @@ pub mod tests {
1895 assert!(has_trackbar(&text, 60)); 1895 assert!(has_trackbar(&text, 60));
1896 assert_eq!(text.lines[0].spans[0].content.as_ref(), HORIZONTAL.repeat(60)); 1896 assert_eq!(text.lines[0].spans[0].content.as_ref(), HORIZONTAL.repeat(60));
1897 1897
1898 // A marker on the message itself means the trackbar belongs to the next one. 1898 // Use a fresh room because receipt updates are monotonic: this case tests a marker
1899 // already positioned on the rendered message, not moving an existing marker backwards.
1900 let mut info = mock_room();
1899 info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG2_EVID.clone()); 1901 info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG2_EVID.clone());
1900 let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews); 1902 let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews);
1901 assert!(!has_trackbar(&text, 60)); 1903 assert!(!has_trackbar(&text, 60));
src/tests.rs +1 −1
@@ -108,7 +108,7 @@ pub fn mock_message5() -> Message {
108pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> { 108pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
109 let mut keys = HashMap::new(); 109 let mut keys = HashMap::new();
110 110
111 keys.insert(MSG1_EVID.clone(), EventLocation::Message(None, MSG2_KEY.clone())); 111 keys.insert(MSG1_EVID.clone(), EventLocation::Message(None, MSG1_KEY.clone()));
112 keys.insert(MSG2_EVID.clone(), EventLocation::Message(None, MSG2_KEY.clone())); 112 keys.insert(MSG2_EVID.clone(), EventLocation::Message(None, MSG2_KEY.clone()));
113 keys.insert(MSG3_EVID.clone(), EventLocation::Message(None, MSG3_KEY.clone())); 113 keys.insert(MSG3_EVID.clone(), EventLocation::Message(None, MSG3_KEY.clone()));
114 keys.insert(MSG4_EVID.clone(), EventLocation::Message(None, MSG4_KEY.clone())); 114 keys.insert(MSG4_EVID.clone(), EventLocation::Message(None, MSG4_KEY.clone()));