Commit e2c346bfd3
Unregistered key
Layout: unified · split
src/base.rs +87 −84
| @@ -1237,6 +1237,14 @@ impl RoomInfo { | |||
| 1237 | } | 1237 | } |
| 1238 | } | 1238 | } |
| 1239 | 1239 | ||
| 1240 | pub fn get_receipt_thread(&self, event_id: &EventId) -> Option<ReceiptThread> { | ||
| 1241 | match self.keys.get(event_id)? { | ||
| 1242 | EventLocation::Message(None, _) | EventLocation::State(_) => Some(ReceiptThread::Main), | ||
| 1243 | EventLocation::Message(Some(root), _) => Some(ReceiptThread::Thread(root.clone())), | ||
| 1244 | _ => None, | ||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1240 | /// Get the reactions and their counts for a message. | 1248 | /// Get the reactions and their counts for a message. |
| 1241 | pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize, &Option<MediaSource>)> { | 1249 | pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize, &Option<MediaSource>)> { |
| 1242 | if let Some(reacts) = self.reactions.get(event_id) { | 1250 | if let Some(reacts) = self.reactions.get(event_id) { |
| @@ -1607,14 +1615,14 @@ impl RoomInfo { | |||
| 1607 | } | 1615 | } |
| 1608 | 1616 | ||
| 1609 | fn clear_receipt(&mut self, thread: &ReceiptThread, user_id: &OwnedUserId) -> Option<()> { | 1617 | fn clear_receipt(&mut self, thread: &ReceiptThread, user_id: &OwnedUserId) -> Option<()> { |
| 1610 | let old_event_id = | 1618 | let old_user_receipts = self.user_receipts.get_mut(thread); |
| 1611 | self.user_receipts.get(thread).and_then(|receipts| receipts.get(user_id))?; | 1619 | let old_event_id = old_user_receipts.and_then(|rs| rs.remove(user_id))?; |
| 1612 | let old_thread = self.event_receipts.get_mut(thread)?; | 1620 | let old_thread = self.event_receipts.get_mut(thread)?; |
| 1613 | let old_receipts = old_thread.get_mut(old_event_id)?; | 1621 | let old_receipts = old_thread.get_mut(&old_event_id)?; |
| 1614 | old_receipts.remove(user_id); | 1622 | old_receipts.remove(user_id); |
| 1615 | 1623 | ||
| 1616 | if old_receipts.is_empty() { | 1624 | if old_receipts.is_empty() { |
| 1617 | old_thread.remove(old_event_id); | 1625 | old_thread.remove(&old_event_id); |
| 1618 | } | 1626 | } |
| 1619 | if old_thread.is_empty() { | 1627 | if old_thread.is_empty() { |
| 1620 | self.event_receipts.remove(thread); | 1628 | self.event_receipts.remove(thread); |
| @@ -1639,13 +1647,21 @@ impl RoomInfo { | |||
| 1639 | } | 1647 | } |
| 1640 | 1648 | ||
| 1641 | self.clear_receipt(&thread, &user_id); | 1649 | self.clear_receipt(&thread, &user_id); |
| 1650 | self.event_receipts(&thread, &event_id).insert(user_id.clone()); | ||
| 1651 | self.user_receipts.entry(thread).or_default().insert(user_id, event_id); | ||
| 1652 | } | ||
| 1653 | |||
| 1654 | /// Get the users whose receipts currently point at the given event in the specified `thread`. | ||
| 1655 | fn event_receipts( | ||
| 1656 | &mut self, | ||
| 1657 | thread: &ReceiptThread, | ||
| 1658 | event_id: &EventId, | ||
| 1659 | ) -> &mut HashSet<OwnedUserId> { | ||
| 1642 | self.event_receipts | 1660 | self.event_receipts |
| 1643 | .entry(thread.clone()) | 1661 | .entry(thread.clone()) |
| 1644 | .or_default() | 1662 | .or_default() |
| 1645 | .entry(event_id.clone()) | 1663 | .entry(event_id.to_owned()) |
| 1646 | .or_default() | 1664 | .or_default() |
| 1647 | .insert(user_id.clone()); | ||
| 1648 | self.user_receipts.entry(thread).or_default().insert(user_id, event_id); | ||
| 1649 | } | 1665 | } |
| 1650 | 1666 | ||
| 1651 | fn receipt_key(&self, event_id: &EventId) -> Option<&MessageKey> { | 1667 | fn receipt_key(&self, event_id: &EventId) -> Option<&MessageKey> { |
| @@ -1655,23 +1671,6 @@ impl RoomInfo { | |||
| 1655 | } | 1671 | } |
| 1656 | } | 1672 | } |
| 1657 | 1673 | ||
| 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 | 1674 | /// 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. | 1675 | /// read receipt, even when their client isn't sending them to the room. |
| 1677 | /// | 1676 | /// |
| @@ -1685,37 +1684,48 @@ impl RoomInfo { | |||
| 1685 | /// we show in the timeline that the user has now read up to the point where they sent | 1684 | /// we show in the timeline that the user has now read up to the point where they sent |
| 1686 | /// a message. | 1685 | /// a message. |
| 1687 | fn set_implicit_receipt(&mut self, user_id: OwnedUserId, event_id: OwnedEventId) { | 1686 | fn set_implicit_receipt(&mut self, user_id: OwnedUserId, event_id: OwnedEventId) { |
| 1688 | let thread = match self.keys.get(&event_id) { | 1687 | let Some(thread) = self.get_receipt_thread(&event_id) else { |
| 1689 | Some(EventLocation::Message(None, _)) | Some(EventLocation::State(_)) => { | 1688 | return; |
| 1690 | ReceiptThread::Main | ||
| 1691 | }, | ||
| 1692 | Some(EventLocation::Message(Some(root), _)) => ReceiptThread::Thread(root.clone()), | ||
| 1693 | _ => return, | ||
| 1694 | }; | 1689 | }; |
| 1695 | let resolved_users: Vec<_> = self | 1690 | |
| 1696 | .user_receipts | 1691 | let current = self.user_receipts.get(&thread).and_then(|receipts| receipts.get(&user_id)); |
| 1697 | .get(&thread) | 1692 | let current_key = current.and_then(|event_id| self.receipt_key(event_id)); |
| 1698 | .into_iter() | 1693 | |
| 1699 | .flat_map(HashMap::iter) | 1694 | if current.is_some() && current_key.is_none() { |
| 1700 | .filter(|(_, receipt_event_id)| *receipt_event_id == &event_id) | 1695 | // Do not set an implicit receipt if we have an explicit receipt that points |
| 1701 | .map(|(receipt_user, _)| receipt_user.clone()) | 1696 | // at an event whose information we don't have, since we need it to make sure |
| 1702 | .collect(); | 1697 | // that we don't move the receipt backwards in the timeline. |
| 1703 | for receipt_user in resolved_users { | 1698 | // |
| 1704 | if let Some(latest_event_id) = self.latest_implicit_receipt(&thread, &receipt_user) { | 1699 | // This event could technically be newer than the one the receipt currently |
| 1705 | self.set_receipt(thread.clone(), receipt_user, latest_event_id); | 1700 | // points at, but we can't be sure, so just trust what the user's receipt in |
| 1706 | } | 1701 | // the room says to point at, and we'll update it once they send us a new one. |
| 1707 | } | 1702 | // |
| 1708 | if self | 1703 | // This technically makes it possible for a user who previously sent room |
| 1709 | .user_receipts | 1704 | // receipts and then switched to just private receipts to not have their marker |
| 1710 | .get(&thread) | 1705 | // moved with recent messages, but solving that would require us to force a |
| 1711 | .and_then(|receipts| receipts.get(&user_id)) | 1706 | // load of the event that every user's read marker points at. |
| 1712 | .is_some_and(|old_event_id| self.receipt_key(old_event_id).is_none()) | ||
| 1713 | { | ||
| 1714 | return; | 1707 | return; |
| 1715 | } | 1708 | } |
| 1709 | |||
| 1710 | // If the user's client isn't thread-aware then make sure the implicit receipt | ||
| 1711 | // doesn't result in displaying a second read receipt after where their unthreaded | ||
| 1712 | // receipt is currently pointing at: | ||
| 1713 | self.clear_receipt(&ReceiptThread::Unthreaded, &user_id); | ||
| 1714 | |||
| 1716 | self.set_receipt(thread, user_id, event_id); | 1715 | self.set_receipt(thread, user_id, event_id); |
| 1717 | } | 1716 | } |
| 1718 | 1717 | ||
| 1718 | /// This is called whenever the user has viewed the most recent message sent to a room or | ||
| 1719 | /// thread, so that we can update the read receipt on the homeserver. | ||
| 1720 | /// | ||
| 1721 | /// Note that the Matrix specification says: | ||
| 1722 | /// | ||
| 1723 | /// > Clients should send read receipts when there is some certainty that the event in | ||
| 1724 | /// > question has been displayed to the user. Simply receiving an event does not provide | ||
| 1725 | /// > enough certainty that the user has seen the event. The user SHOULD need to take some | ||
| 1726 | /// > action such as viewing the room that the event was sent to or dismissing a notification | ||
| 1727 | /// > in order for the event to count as “read”. Clients SHOULD NOT send read receipts for | ||
| 1728 | /// > events sent by their own user. | ||
| 1719 | pub fn fully_read( | 1729 | pub fn fully_read( |
| 1720 | &mut self, | 1730 | &mut self, |
| 1721 | room_id: OwnedRoomId, | 1731 | room_id: OwnedRoomId, |
| @@ -1738,7 +1748,11 @@ impl RoomInfo { | |||
| 1738 | 1748 | ||
| 1739 | let event_id = messages | 1749 | let event_id = messages |
| 1740 | .iter() | 1750 | .iter() |
| 1741 | .filter(|(_, msg)| msg.sender != *user_id) | 1751 | .filter(|(_, msg)| { |
| 1752 | // Handle the "do not send read receipts for our own events" part of the | ||
| 1753 | // specification quoted above. | ||
| 1754 | msg.sender != *user_id | ||
| 1755 | }) | ||
| 1742 | .filter(|(_, msg)| { | 1756 | .filter(|(_, msg)| { |
| 1743 | matches!( | 1757 | matches!( |
| 1744 | msg.event, | 1758 | msg.event, |
| @@ -1749,11 +1763,23 @@ impl RoomInfo { | |||
| 1749 | ) | 1763 | ) |
| 1750 | }) | 1764 | }) |
| 1751 | .flat_map(|(_, msg)| msg.event.event_id()) | 1765 | .flat_map(|(_, msg)| msg.event.event_id()) |
| 1752 | .next_back(); | 1766 | .next_back() |
| 1767 | .map(ToOwned::to_owned); | ||
| 1768 | |||
| 1769 | if let Some((_, msg)) = messages.last_key_value() && | ||
| 1770 | let Some(event_id) = msg.event.event_id() | ||
| 1771 | { | ||
| 1772 | // Always update the local receipt info for this room so that we aren't reliant | ||
| 1773 | // on the homeserver echoing it back to us to update this and clear the trackbar: | ||
| 1774 | self.set_receipt(thread.clone(), user_id.clone(), event_id.to_owned()); | ||
| 1775 | } | ||
| 1753 | 1776 | ||
| 1754 | if let Some(event_id) = event_id { | 1777 | if let Some(event_id) = event_id { |
| 1778 | // Clear any desktop notifications for the room: | ||
| 1755 | open_notifications.remove(&room_id); | 1779 | open_notifications.remove(&room_id); |
| 1756 | worker.send_receipt(room_id, thread.clone(), event_id.to_owned(), settings); | 1780 | |
| 1781 | // Update the homeserver with the latest receipt: | ||
| 1782 | worker.send_receipt(room_id, thread, event_id, settings); | ||
| 1757 | } | 1783 | } |
| 1758 | } | 1784 | } |
| 1759 | 1785 | ||
| @@ -1779,13 +1805,16 @@ impl RoomInfo { | |||
| 1779 | self.fully_read(room_id, ReceiptThread::Main, worker, settings, open_notifications); | 1805 | self.fully_read(room_id, ReceiptThread::Main, worker, settings, open_notifications); |
| 1780 | } | 1806 | } |
| 1781 | 1807 | ||
| 1782 | pub fn receipts<'a>( | 1808 | pub fn read_event_users<'a>( |
| 1783 | &'a self, | 1809 | &'a self, |
| 1784 | user_id: &'a UserId, | 1810 | thread: ReceiptThread, |
| 1785 | ) -> impl Iterator<Item = (&'a ReceiptThread, &'a OwnedEventId)> + 'a { | 1811 | event_id: &'a EventId, |
| 1786 | self.user_receipts | 1812 | ) -> impl Iterator<Item = &'a OwnedUserId> + 'a { |
| 1787 | .iter() | 1813 | self.event_receipts |
| 1788 | .filter_map(move |(t, rs)| rs.get(user_id).map(|r| (t, r))) | 1814 | .get(&thread) |
| 1815 | .and_then(|rs| rs.get(event_id)) | ||
| 1816 | .map(|read| read.iter()) | ||
| 1817 | .unwrap_or_default() | ||
| 1789 | } | 1818 | } |
| 1790 | 1819 | ||
| 1791 | fn get_typers(&self) -> &[OwnedUserId] { | 1820 | fn get_typers(&self) -> &[OwnedUserId] { |
| @@ -2690,32 +2719,6 @@ pub mod tests { | |||
| 2690 | ); | 2719 | ); |
| 2691 | } | 2720 | } |
| 2692 | 2721 | ||
| 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 | |||
| 2719 | #[test] | 2722 | #[test] |
| 2720 | fn test_typing_spans() { | 2723 | fn test_typing_spans() { |
| 2721 | let mut info = RoomInfo::default(); | 2724 | let mut info = RoomInfo::default(); |
src/message/mod.rs +21 −14
| @@ -30,7 +30,7 @@ use modalkit::editing::cursor::Cursor; | |||
| 30 | use ratatui::symbols::line::{HORIZONTAL, THICK_VERTICAL}; | 30 | use ratatui::symbols::line::{HORIZONTAL, THICK_VERTICAL}; |
| 31 | use ratatui_image::sliced::SlicedProtocol; | 31 | use ratatui_image::sliced::SlicedProtocol; |
| 32 | 32 | ||
| 33 | use crate::base::{EventLocation, MessageEdits}; | 33 | use crate::base::MessageEdits; |
| 34 | use crate::message::html::{StyleTree, parse_matrix_html}; | 34 | use crate::message::html::{StyleTree, parse_matrix_html}; |
| 35 | use crate::message::state::{body_cow_state, html_state}; | 35 | use crate::message::state::{body_cow_state, html_state}; |
| 36 | use crate::prelude::*; | 36 | use crate::prelude::*; |
| @@ -1065,12 +1065,10 @@ impl Message { | |||
| 1065 | 1065 | ||
| 1066 | /// The read receipt thread that this message was inserted into. | 1066 | /// The read receipt thread that this message was inserted into. |
| 1067 | fn receipt_thread(&self, info: &RoomInfo) -> ReceiptThread { | 1067 | fn receipt_thread(&self, info: &RoomInfo) -> ReceiptThread { |
| 1068 | match self.event.event_id().and_then(|event_id| info.keys.get(event_id)) { | 1068 | self.event |
| 1069 | Some(EventLocation::Message(Some(thread_root), _)) => { | 1069 | .event_id() |
| 1070 | ReceiptThread::Thread(thread_root.clone()) | 1070 | .and_then(|event_id| info.get_receipt_thread(event_id)) |
| 1071 | }, | 1071 | .unwrap_or(ReceiptThread::Main) |
| 1072 | _ => ReceiptThread::Main, | ||
| 1073 | } | ||
| 1074 | } | 1072 | } |
| 1075 | 1073 | ||
| 1076 | pub fn message_column_width( | 1074 | pub fn message_column_width( |
| @@ -1112,13 +1110,21 @@ impl Message { | |||
| 1112 | let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER; | 1110 | let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER; |
| 1113 | let user = self.show_sender(prev, true, info, settings, width); | 1111 | let user = self.show_sender(prev, true, info, settings, width); |
| 1114 | let time = Some(self.timestamp.show_time()); | 1112 | let time = Some(self.timestamp.show_time()); |
| 1115 | let read = info | 1113 | |
| 1116 | .event_receipts | 1114 | let read = self |
| 1117 | .values() | 1115 | .event |
| 1118 | .filter_map(|receipts| self.event.event_id().and_then(|id| receipts.get(id))) | 1116 | .event_id() |
| 1119 | .flat_map(|read| read.iter()) | 1117 | .map(|event_id| { |
| 1120 | .map(|user_id| user_id.to_owned()) | 1118 | // Iterate over both `ReceiptThread::Unthreaded` for clients unaware |
| 1121 | .collect(); | 1119 | // of threads, and over the appropriate `ReceiptThread::Main` or |
| 1120 | // `ReceiptThread::Thread` for ones from thread-aware clients, like | ||
| 1121 | // our own receipts or implicit receipts we've generated. | ||
| 1122 | let receipt_thread = self.receipt_thread(info); | ||
| 1123 | let threaded = info.read_event_users(receipt_thread, event_id); | ||
| 1124 | let unthreaded = info.read_event_users(ReceiptThread::Unthreaded, event_id); | ||
| 1125 | threaded.chain(unthreaded).map(|user_id| user_id.to_owned()).collect() | ||
| 1126 | }) | ||
| 1127 | .unwrap_or_default(); | ||
| 1122 | 1128 | ||
| 1123 | MessageFormatter { | 1129 | MessageFormatter { |
| 1124 | settings, | 1130 | settings, |
| @@ -1552,6 +1558,7 @@ pub mod tests { | |||
| 1552 | VideoMessageEventContent, | 1558 | VideoMessageEventContent, |
| 1553 | }; | 1559 | }; |
| 1554 | 1560 | ||
| 1561 | use crate::base::EventLocation; | ||
| 1555 | use crate::tests::*; | 1562 | use crate::tests::*; |
| 1556 | 1563 | ||
| 1557 | #[test] | 1564 | #[test] |
src/windows/room/scrollback.rs +41
| @@ -1835,4 +1835,45 @@ mod tests { | |||
| 1835 | // The terminal cursor still lands on the selected message's first line. | 1835 | // The terminal cursor still lands on the selected message's first line. |
| 1836 | assert_eq!(scrollback.term_cursor, (0, 4)); | 1836 | assert_eq!(scrollback.term_cursor, (0, 4)); |
| 1837 | } | 1837 | } |
| 1838 | |||
| 1839 | #[tokio::test] | ||
| 1840 | async fn test_draw_marks_room_read() { | ||
| 1841 | let draw_focused_room = |read_receipt_send| { | ||
| 1842 | async move { | ||
| 1843 | let mut store = mock_store().await; | ||
| 1844 | store.application.settings.tunables.read_receipt_send = read_receipt_send; | ||
| 1845 | |||
| 1846 | let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone(), None); | ||
| 1847 | let area = Rect::new(0, 0, 60, 6); | ||
| 1848 | let mut buffer = Buffer::empty(area); | ||
| 1849 | |||
| 1850 | Scrollback::new(&mut store).focus(true).room_focus(true).render( | ||
| 1851 | area, | ||
| 1852 | &mut buffer, | ||
| 1853 | &mut scrollback, | ||
| 1854 | ); | ||
| 1855 | |||
| 1856 | store | ||
| 1857 | } | ||
| 1858 | }; | ||
| 1859 | |||
| 1860 | // Rendering a focused room at the latest message updates the read receipt, | ||
| 1861 | // regardless of the value of `read_receipt_send`, which only controls whether | ||
| 1862 | // or not it is a private or public receipt that we send to the home server. | ||
| 1863 | for read_receipt_send in [true, false] { | ||
| 1864 | let mut store = draw_focused_room(read_receipt_send).await; | ||
| 1865 | let user_id = store.application.settings.profile.user_id.clone(); | ||
| 1866 | let info = store.application.get_room_info(TEST_ROOM1_ID.clone()); | ||
| 1867 | |||
| 1868 | let receipt = info | ||
| 1869 | .user_receipts | ||
| 1870 | .get(&ReceiptThread::Main) | ||
| 1871 | .and_then(|receipts| receipts.get(&user_id)); | ||
| 1872 | let exp = Some(&*MSG1_EVID); | ||
| 1873 | assert_eq!( | ||
| 1874 | receipt, exp, | ||
| 1875 | "receipt not set with read_receipt_send = {read_receipt_send}" | ||
| 1876 | ); | ||
| 1877 | } | ||
| 1878 | } | ||
| 1838 | } | 1879 | } |
src/worker.rs +80 −34
| @@ -92,6 +92,12 @@ const IAMB_DEVICE_NAME: &str = "iamb"; | |||
| 92 | const IAMB_USER_AGENT: &str = "iamb"; | 92 | const IAMB_USER_AGENT: &str = "iamb"; |
| 93 | const MIN_MSG_LOAD: u32 = 50; | 93 | const MIN_MSG_LOAD: u32 = 50; |
| 94 | 94 | ||
| 95 | /// How long to wait before retrying receipts that we failed to send. | ||
| 96 | const RECEIPT_RETRY_INTERVAL: Duration = Duration::from_secs(2); | ||
| 97 | |||
| 98 | type ReceiptKey = (OwnedRoomId, ReceiptThread, ReceiptType); | ||
| 99 | type ReceiptUpdate = (OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId); | ||
| 100 | |||
| 95 | type MessageFetchResult = IambResult<(Option<String>, Vec<(AnyTimelineEvent, Vec<OwnedUserId>)>)>; | 101 | type MessageFetchResult = IambResult<(Option<String>, Vec<(AnyTimelineEvent, Vec<OwnedUserId>)>)>; |
| 96 | 102 | ||
| 97 | fn initial_devname() -> String { | 103 | fn initial_devname() -> String { |
| @@ -462,16 +468,18 @@ fn convert_receipt_type(value: ReceiptType) -> CreateReceiptType { | |||
| 462 | } | 468 | } |
| 463 | } | 469 | } |
| 464 | 470 | ||
| 471 | /// Send a single read receipt, and return `true` if the server accepted it and we don't need | ||
| 472 | /// to retry sending it later on. | ||
| 465 | async fn send_single_receipt( | 473 | async fn send_single_receipt( |
| 466 | client: &Client, | 474 | client: &Client, |
| 467 | room_id: OwnedRoomId, | 475 | room_id: &RoomId, |
| 468 | thread: ReceiptThread, | 476 | thread: ReceiptThread, |
| 469 | receipt_type: ReceiptType, | 477 | receipt_type: ReceiptType, |
| 470 | event_id: OwnedEventId, | 478 | event_id: OwnedEventId, |
| 471 | ) { | 479 | ) -> bool { |
| 472 | let Some(room) = client.get_room(&room_id) else { | 480 | let Some(room) = client.get_room(room_id) else { |
| 473 | tracing::warn!(?room_id, "trying to send receipt to unknown room"); | 481 | tracing::warn!(?room_id, "trying to send receipt to unknown room"); |
| 474 | return; | 482 | return false; |
| 475 | }; | 483 | }; |
| 476 | 484 | ||
| 477 | if ReceiptThread::Main == thread || ReceiptThread::Unthreaded == thread { | 485 | if ReceiptThread::Main == thread || ReceiptThread::Unthreaded == thread { |
| @@ -481,37 +489,73 @@ async fn send_single_receipt( | |||
| 481 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to clear unread flag: {e}")); | 489 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to clear unread flag: {e}")); |
| 482 | } | 490 | } |
| 483 | 491 | ||
| 484 | let _ = room | 492 | room.send_single_receipt(convert_receipt_type(receipt_type), thread, event_id) |
| 485 | .send_single_receipt(convert_receipt_type(receipt_type), thread, event_id) | ||
| 486 | .await | 493 | .await |
| 487 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to send read receipt: {e}")); | 494 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to send read receipt: {e}")) |
| 495 | .is_ok() | ||
| 488 | } | 496 | } |
| 489 | 497 | ||
| 490 | async fn send_receipts_forever( | 498 | /// Listen for receipt updates from the main thread, and try sending them to the homeserver. |
| 491 | client: &Client, | 499 | /// |
| 492 | stream: UnboundedReceiver<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, | 500 | /// Any receipts we fail to send will be queued for resending later on. |
| 493 | ) { | 501 | async fn send_receipts_forever(client: &Client, stream: UnboundedReceiver<ReceiptUpdate>) { |
| 494 | let stream = UnboundedReceiverStream::new(stream); | 502 | let mut stream = UnboundedReceiverStream::new(stream); |
| 495 | let mut sent: HashMap<(OwnedRoomId, ReceiptThread, ReceiptType), OwnedEventId> = | 503 | |
| 496 | Default::default(); | 504 | let mut sent: HashMap<ReceiptKey, OwnedEventId> = Default::default(); |
| 497 | 505 | let mut outstanding: HashMap<ReceiptKey, OwnedEventId> = Default::default(); | |
| 498 | stream | 506 | let mut next_attempt = Instant::now(); |
| 499 | .for_each_concurrent(None, |(room_id, thread, receipt_type, event_id)| { | 507 | |
| 500 | let key = (room_id.clone(), thread.clone(), receipt_type.clone()); | 508 | loop { |
| 501 | if sent.get(&key).is_some_and(|receipt| *receipt == event_id) { | 509 | tokio::select! { |
| 502 | futures::future::Either::Left(futures::future::ready(())) | 510 | update = stream.next() => { |
| 511 | let Some((room_id, thread, receipt_type, event_id)) = update else { | ||
| 512 | // The sender side has gone away, so just exit the loop. | ||
| 513 | return; | ||
| 514 | }; | ||
| 515 | |||
| 516 | let key = (room_id, thread, receipt_type); | ||
| 517 | |||
| 518 | if sent.get(&key).is_some_and(|sent| *sent == event_id) { | ||
| 519 | // Skip sending a duplicate receipt update. | ||
| 520 | continue; | ||
| 521 | } | ||
| 522 | |||
| 523 | outstanding.insert(key, event_id); | ||
| 524 | }, | ||
| 525 | |||
| 526 | // If we have outstanding receipts, then ensure that we attempt a retry before | ||
| 527 | // the next receipt update arrives over the stream: | ||
| 528 | _ = tokio::time::sleep_until(next_attempt.into()), if !outstanding.is_empty() => {}, | ||
| 529 | } | ||
| 530 | |||
| 531 | if Instant::now() < next_attempt { | ||
| 532 | // Still backing off from an earlier failure. | ||
| 533 | continue; | ||
| 534 | } | ||
| 535 | |||
| 536 | // Receipts are sent one at a time: two updates for the same room sent concurrently | ||
| 537 | // can land out of order and leave the server pointing at the older event. | ||
| 538 | let mut failed = false; | ||
| 539 | |||
| 540 | for (key, event_id) in std::mem::take(&mut outstanding) { | ||
| 541 | let (room_id, thread, receipt_type) = key.clone(); | ||
| 542 | let success = | ||
| 543 | send_single_receipt(client, &room_id, thread, receipt_type, event_id.clone()).await; | ||
| 544 | |||
| 545 | if success { | ||
| 546 | sent.insert(key, event_id); | ||
| 503 | } else { | 547 | } else { |
| 504 | sent.insert(key, event_id.clone()); | 548 | // Could not contact homeserver to send the receipt, so queue for a later |
| 505 | futures::future::Either::Right(send_single_receipt( | 549 | // retry and mark that we need to backoff to avoid spamming send failures. |
| 506 | client, | 550 | outstanding.insert(key, event_id); |
| 507 | room_id, | 551 | failed = true; |
| 508 | thread, | ||
| 509 | receipt_type, | ||
| 510 | event_id, | ||
| 511 | )) | ||
| 512 | } | 552 | } |
| 513 | }) | 553 | } |
| 514 | .await; | 554 | |
| 555 | if failed { | ||
| 556 | next_attempt = Instant::now() + RECEIPT_RETRY_INTERVAL; | ||
| 557 | } | ||
| 558 | } | ||
| 515 | } | 559 | } |
| 516 | 560 | ||
| 517 | fn insert_local_echo( | 561 | fn insert_local_echo( |
| @@ -928,7 +972,7 @@ async fn join_room( | |||
| 928 | pub struct Requester { | 972 | pub struct Requester { |
| 929 | pub client: Client, | 973 | pub client: Client, |
| 930 | pub tx: UnboundedSender<WorkerTask>, | 974 | pub tx: UnboundedSender<WorkerTask>, |
| 931 | pub receipts: UnboundedSender<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, | 975 | pub receipts: UnboundedSender<ReceiptUpdate>, |
| 932 | } | 976 | } |
| 933 | 977 | ||
| 934 | impl Requester { | 978 | impl Requester { |
| @@ -953,7 +997,10 @@ impl Requester { | |||
| 953 | ReceiptType::ReadPrivate | 997 | ReceiptType::ReadPrivate |
| 954 | }; | 998 | }; |
| 955 | 999 | ||
| 956 | self.receipts.send((room, thread, receipt_type, event)).unwrap(); | 1000 | let _ = self |
| 1001 | .receipts | ||
| 1002 | .send((room, thread, receipt_type, event)) | ||
| 1003 | .inspect_err(|_| tracing::warn!("Read receipt worker is no longer running")); | ||
| 957 | } | 1004 | } |
| 958 | 1005 | ||
| 959 | pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> { | 1006 | pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> { |
| @@ -1052,8 +1099,7 @@ pub struct ClientWorker { | |||
| 1052 | sync_handle: Option<JoinHandle<()>>, | 1099 | sync_handle: Option<JoinHandle<()>>, |
| 1053 | 1100 | ||
| 1054 | /// this will be removed after login | 1101 | /// this will be removed after login |
| 1055 | unspawned_receipt_stream: | 1102 | unspawned_receipt_stream: Option<UnboundedReceiver<ReceiptUpdate>>, |
| 1056 | Option<UnboundedReceiver<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>>, | ||
| 1057 | 1103 | ||
| 1058 | /// Take care when locking since worker commands are sent with the lock already held | 1104 | /// Take care when locking since worker commands are sent with the lock already held |
| 1059 | store: Option<AsyncProgramStore>, | 1105 | store: Option<AsyncProgramStore>, |