| @@ -32,6 +32,7 @@ use matrix_sdk::ruma::events::{MessageLikeEvent, OriginalMessageLikeEvent}; |
| 32 | 32 | use matrix_sdk::ruma::presence::PresenceState; |
| 33 | 33 | use matrix_sdk::ruma::room::{AllowRule, Restricted}; |
| 34 | 34 | use matrix_sdk::ruma::{OwnedMxcUri, OwnedTransactionId, RoomVersionId}; |
| 35 | use matrix_sdk_base::read_receipts::ReadReceipts; |
| 35 | 36 | use modalkit::editing::application::{ |
| 36 | 37 | ApplicationAction, |
| 37 | 38 | ApplicationContentId, |
| @@ -52,6 +53,7 @@ use serde::de::Error as SerdeError; |
| 52 | 53 | use serde::de::Visitor; |
| 53 | 54 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 54 | 55 | use tokio::sync::{Mutex as AsyncMutex, Notify}; |
| 56 | use tokio::task::JoinHandle; |
| 55 | 57 | |
| 56 | 58 | use crate::message::poll::{ |
| 57 | 59 | Poll, |
| @@ -64,6 +66,7 @@ use crate::message::poll::{ |
| 64 | 66 | }; |
| 65 | 67 | use crate::notifications::NotificationHandle; |
| 66 | 68 | use crate::prelude::*; |
| 69 | use crate::worker::subscribe_thread_updates; |
| 67 | 70 | |
| 68 | 71 | /// The set of characters used in different Matrix IDs. |
| 69 | 72 | pub const MATRIX_ID_WORD: WordStyle = WordStyle::CharSet(is_mxid_char); |
| @@ -1399,14 +1402,48 @@ pub struct Thread { |
| 1399 | 1402 | root: Option<Message>, |
| 1400 | 1403 | // TODO: check access |
| 1401 | 1404 | pub(crate) summary: Option<ThreadSummary>, |
| 1405 | |
| 1406 | pub(crate) unreads: ReadReceipts, |
| 1407 | update_handle: Option<JoinHandle<()>>, |
| 1408 | |
| 1409 | /// A map of read markers to display on different events. |
| 1410 | event_receipts: HashMap<OwnedEventId, HashSet<OwnedUserId>>, |
| 1411 | /// A map of the most recent read marker for each user. |
| 1412 | /// |
| 1413 | /// Every receipt in this map should also have an entry in [`event_receipts`](`Self::event_receipts`), |
| 1414 | /// however not every user has an entry. If a user's most recent receipt is |
| 1415 | /// older than the oldest loaded event, that user will not be included. |
| 1416 | user_receipts: HashMap<OwnedUserId, OwnedEventId>, |
| 1402 | 1417 | } |
| 1403 | 1418 | |
| 1404 | 1419 | impl Thread { |
| 1405 | 1420 | fn new(root: OwnedEventId) -> Self { |
| 1406 | 1421 | Self { |
| 1407 | 1422 | messages: Messages::new(ReceiptThread::Thread(root)), |
| 1408 | | root: None, |
| 1409 | | summary: None, |
| 1423 | root: Default::default(), |
| 1424 | summary: Default::default(), |
| 1425 | update_handle: Default::default(), |
| 1426 | unreads: Default::default(), |
| 1427 | event_receipts: Default::default(), |
| 1428 | user_receipts: Default::default(), |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | pub(crate) async fn subscribe_updates( |
| 1433 | &mut self, |
| 1434 | room: &MatrixRoom, |
| 1435 | root: &EventId, |
| 1436 | store: &AsyncProgramStore, |
| 1437 | ) { |
| 1438 | if self.update_handle.is_some() { |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | match subscribe_thread_updates(room.to_owned(), root.to_owned(), Arc::clone(store)).await { |
| 1443 | Ok(handle) => self.update_handle = Some(handle), |
| 1444 | Err(err) => { |
| 1445 | tracing::warn!(room_id = ?room.room_id(), ?root, "unable to subscribe to thread updates: {err}"); |
| 1446 | }, |
| 1410 | 1447 | } |
| 1411 | 1448 | } |
| 1412 | 1449 | } |
| @@ -1443,13 +1480,18 @@ pub struct RoomInfo { |
| 1443 | 1480 | messages: Messages, |
| 1444 | 1481 | |
| 1445 | 1482 | /// A map of read markers to display on different events. |
| 1446 | | pub event_receipts: HashMap<ReceiptThread, HashMap<OwnedEventId, HashSet<OwnedUserId>>>, |
| 1483 | pub event_receipts_main: HashMap<OwnedEventId, HashSet<OwnedUserId>>, |
| 1447 | 1484 | /// A map of the most recent read marker for each user. |
| 1448 | 1485 | /// |
| 1449 | 1486 | /// Every receipt in this map should also have an entry in [`event_receipts`](`Self::event_receipts`), |
| 1450 | 1487 | /// however not every user has an entry. If a user's most recent receipt is |
| 1451 | 1488 | /// older than the oldest loaded event, that user will not be included. |
| 1452 | | pub user_receipts: HashMap<ReceiptThread, HashMap<OwnedUserId, OwnedEventId>>, |
| 1489 | pub user_receipts_main: HashMap<OwnedUserId, OwnedEventId>, |
| 1490 | /// Untreaded mirror of [`event_receipts_main`](Self::event_receipts_main) |
| 1491 | pub event_receipts_unthreaded: HashMap<OwnedEventId, HashSet<OwnedUserId>>, |
| 1492 | /// Untreaded mirror of [`event_receipts_main`](Self::user_receipts_main) |
| 1493 | pub user_receipts_unthreaded: HashMap<OwnedUserId, OwnedEventId>, |
| 1494 | |
| 1453 | 1495 | /// A map of message identifiers to a map of reaction events. |
| 1454 | 1496 | pub reactions: HashMap<OwnedEventId, MessageReactions>, |
| 1455 | 1497 | /// A map of message identifiers to a list of edit events for message that are not yet cached. |
| @@ -1505,8 +1547,10 @@ impl Default for RoomInfo { |
| 1505 | 1547 | keys: Default::default(), |
| 1506 | 1548 | flags: Default::default(), |
| 1507 | 1549 | echo_keys: Default::default(), |
| 1508 | | event_receipts: Default::default(), |
| 1509 | | user_receipts: Default::default(), |
| 1550 | event_receipts_main: Default::default(), |
| 1551 | user_receipts_main: Default::default(), |
| 1552 | event_receipts_unthreaded: Default::default(), |
| 1553 | user_receipts_unthreaded: Default::default(), |
| 1510 | 1554 | reactions: Default::default(), |
| 1511 | 1555 | threads: Default::default(), |
| 1512 | 1556 | fetching: Default::default(), |
| @@ -1545,10 +1589,116 @@ impl RoomInfo { |
| 1545 | 1589 | } |
| 1546 | 1590 | } |
| 1547 | 1591 | |
| 1548 | | pub fn get_thread(&self, root: &EventId) -> Option<&Thread> { |
| 1592 | pub(crate) fn get_thread(&self, root: &EventId) -> Option<&Thread> { |
| 1549 | 1593 | self.threads.get(root) |
| 1550 | 1594 | } |
| 1551 | 1595 | |
| 1596 | pub(crate) fn get_thread_mut(&mut self, root: OwnedEventId) -> &mut Thread { |
| 1597 | self.threads.entry(root.clone()).or_insert_with(|| Thread::new(root)) |
| 1598 | } |
| 1599 | |
| 1600 | pub(crate) fn event_receipts( |
| 1601 | &self, |
| 1602 | thread: &ReceiptThread, |
| 1603 | ) -> Option<&HashMap<OwnedEventId, HashSet<OwnedUserId>>> { |
| 1604 | match thread { |
| 1605 | ReceiptThread::Thread(root) => Some(&self.get_thread(root)?.event_receipts), |
| 1606 | ReceiptThread::Main => Some(&self.event_receipts_main), |
| 1607 | ReceiptThread::Unthreaded => Some(&self.event_receipts_unthreaded), |
| 1608 | _ => { |
| 1609 | tracing::debug!("encountered unknown receipt thread: {thread:?}"); |
| 1610 | None |
| 1611 | }, |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | pub(crate) fn event_receipts_mut( |
| 1616 | &mut self, |
| 1617 | thread: &ReceiptThread, |
| 1618 | ) -> Option<&mut HashMap<OwnedEventId, HashSet<OwnedUserId>>> { |
| 1619 | match thread { |
| 1620 | ReceiptThread::Thread(root) => Some(&mut self.threads.get_mut(root)?.event_receipts), |
| 1621 | ReceiptThread::Main => Some(&mut self.event_receipts_main), |
| 1622 | ReceiptThread::Unthreaded => Some(&mut self.event_receipts_unthreaded), |
| 1623 | _ => { |
| 1624 | tracing::debug!("encountered unknown receipt thread: {thread:?}"); |
| 1625 | None |
| 1626 | }, |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | /// # Panics |
| 1631 | /// |
| 1632 | /// Panics if called with [`ReceiptThread::_Custom`]. |
| 1633 | pub(crate) fn event_receipts_or_default( |
| 1634 | &mut self, |
| 1635 | thread: ReceiptThread, |
| 1636 | ) -> &mut HashMap<OwnedEventId, HashSet<OwnedUserId>> { |
| 1637 | match thread { |
| 1638 | ReceiptThread::Thread(root) => &mut self.get_thread_mut(root).event_receipts, |
| 1639 | ReceiptThread::Main => &mut self.event_receipts_main, |
| 1640 | ReceiptThread::Unthreaded => &mut self.event_receipts_unthreaded, |
| 1641 | _ => { |
| 1642 | panic!("encountered unknown receipt thread: {thread:?}") |
| 1643 | }, |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | pub(crate) fn user_receipts( |
| 1648 | &self, |
| 1649 | thread: &ReceiptThread, |
| 1650 | ) -> Option<&HashMap<OwnedUserId, OwnedEventId>> { |
| 1651 | match thread { |
| 1652 | ReceiptThread::Thread(root) => Some(&self.get_thread(root)?.user_receipts), |
| 1653 | ReceiptThread::Main => Some(&self.user_receipts_main), |
| 1654 | ReceiptThread::Unthreaded => Some(&self.user_receipts_unthreaded), |
| 1655 | _ => { |
| 1656 | tracing::debug!("encountered unknown receipt thread: {thread:?}"); |
| 1657 | None |
| 1658 | }, |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | pub(crate) fn user_receipts_mut( |
| 1663 | &mut self, |
| 1664 | thread: &ReceiptThread, |
| 1665 | ) -> Option<&mut HashMap<OwnedUserId, OwnedEventId>> { |
| 1666 | match thread { |
| 1667 | ReceiptThread::Thread(root) => Some(&mut self.threads.get_mut(root)?.user_receipts), |
| 1668 | ReceiptThread::Main => Some(&mut self.user_receipts_main), |
| 1669 | ReceiptThread::Unthreaded => Some(&mut self.user_receipts_unthreaded), |
| 1670 | _ => { |
| 1671 | tracing::debug!("encountered unknown receipt thread: {thread:?}"); |
| 1672 | None |
| 1673 | }, |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /// Panics if called with [`ReceiptThread::_Custom`]. |
| 1678 | pub(crate) fn user_receipts_or_default( |
| 1679 | &mut self, |
| 1680 | thread: ReceiptThread, |
| 1681 | ) -> &mut HashMap<OwnedUserId, OwnedEventId> { |
| 1682 | match thread { |
| 1683 | ReceiptThread::Thread(root) => &mut self.get_thread_mut(root).user_receipts, |
| 1684 | ReceiptThread::Main => &mut self.user_receipts_main, |
| 1685 | ReceiptThread::Unthreaded => &mut self.user_receipts_unthreaded, |
| 1686 | _ => { |
| 1687 | panic!("encountered unknown receipt thread: {thread:?}") |
| 1688 | }, |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | pub(crate) async fn subscribe_thread_updates( |
| 1693 | &mut self, |
| 1694 | room: &MatrixRoom, |
| 1695 | store: &AsyncProgramStore, |
| 1696 | ) { |
| 1697 | for (root, thread) in self.threads.iter_mut() { |
| 1698 | thread.subscribe_updates(room, root, store).await |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1552 | 1702 | /// Get the event for the last message in a thread (or the thread root if there are no |
| 1553 | 1703 | /// in-thread replies yet). |
| 1554 | 1704 | /// |
| @@ -2219,18 +2369,15 @@ impl RoomInfo { |
| 2219 | 2369 | } |
| 2220 | 2370 | |
| 2221 | 2371 | fn clear_receipt(&mut self, thread: &ReceiptThread, user_id: &OwnedUserId) -> Option<()> { |
| 2222 | | let old_user_receipts = self.user_receipts.get_mut(thread); |
| 2372 | let old_user_receipts = self.user_receipts_mut(thread); |
| 2223 | 2373 | let old_event_id = old_user_receipts.and_then(|rs| rs.remove(user_id))?; |
| 2224 | | let old_thread = self.event_receipts.get_mut(thread)?; |
| 2374 | let old_thread = self.event_receipts_mut(thread)?; |
| 2225 | 2375 | let old_receipts = old_thread.get_mut(&old_event_id)?; |
| 2226 | 2376 | old_receipts.remove(user_id); |
| 2227 | 2377 | |
| 2228 | 2378 | if old_receipts.is_empty() { |
| 2229 | 2379 | old_thread.remove(&old_event_id); |
| 2230 | 2380 | } |
| 2231 | | if old_thread.is_empty() { |
| 2232 | | self.event_receipts.remove(thread); |
| 2233 | | } |
| 2234 | 2381 | |
| 2235 | 2382 | None |
| 2236 | 2383 | } |
| @@ -2242,7 +2389,7 @@ impl RoomInfo { |
| 2242 | 2389 | event_id: OwnedEventId, |
| 2243 | 2390 | ) { |
| 2244 | 2391 | if let Some(old_event_id) = |
| 2245 | | self.user_receipts.get(&thread).and_then(|receipts| receipts.get(&user_id)) && |
| 2392 | self.user_receipts(&thread).and_then(|receipts| receipts.get(&user_id)) && |
| 2246 | 2393 | let (Some(old_key), Some(new_key)) = |
| 2247 | 2394 | (self.receipt_key(old_event_id), self.receipt_key(&event_id)) && |
| 2248 | 2395 | new_key <= old_key |
| @@ -2251,21 +2398,11 @@ impl RoomInfo { |
| 2251 | 2398 | } |
| 2252 | 2399 | |
| 2253 | 2400 | self.clear_receipt(&thread, &user_id); |
| 2254 | | self.event_receipts(&thread, &event_id).insert(user_id.clone()); |
| 2255 | | self.user_receipts.entry(thread).or_default().insert(user_id, event_id); |
| 2256 | | } |
| 2257 | | |
| 2258 | | /// Get the users whose receipts currently point at the given event in the specified `thread`. |
| 2259 | | fn event_receipts( |
| 2260 | | &mut self, |
| 2261 | | thread: &ReceiptThread, |
| 2262 | | event_id: &EventId, |
| 2263 | | ) -> &mut HashSet<OwnedUserId> { |
| 2264 | | self.event_receipts |
| 2265 | | .entry(thread.clone()) |
| 2266 | | .or_default() |
| 2401 | self.event_receipts_or_default(thread.clone()) |
| 2267 | 2402 | .entry(event_id.to_owned()) |
| 2268 | 2403 | .or_default() |
| 2404 | .insert(user_id.clone()); |
| 2405 | self.user_receipts_or_default(thread).insert(user_id, event_id); |
| 2269 | 2406 | } |
| 2270 | 2407 | |
| 2271 | 2408 | fn receipt_key(&self, event_id: &EventId) -> Option<&MessageKey> { |
| @@ -2292,7 +2429,7 @@ impl RoomInfo { |
| 2292 | 2429 | return; |
| 2293 | 2430 | }; |
| 2294 | 2431 | |
| 2295 | | let current = self.user_receipts.get(&thread).and_then(|receipts| receipts.get(&user_id)); |
| 2432 | let current = self.user_receipts(&thread).and_then(|receipts| receipts.get(&user_id)); |
| 2296 | 2433 | let current_key = current.and_then(|event_id| self.receipt_key(event_id)); |
| 2297 | 2434 | |
| 2298 | 2435 | if current.is_some() && current_key.is_none() { |
| @@ -2405,8 +2542,7 @@ impl RoomInfo { |
| 2405 | 2542 | thread: ReceiptThread, |
| 2406 | 2543 | event_id: &'a EventId, |
| 2407 | 2544 | ) -> impl Iterator<Item = &'a OwnedUserId> + 'a { |
| 2408 | | self.event_receipts |
| 2409 | | .get(&thread) |
| 2545 | self.event_receipts(&thread) |
| 2410 | 2546 | .and_then(|rs| rs.get(event_id)) |
| 2411 | 2547 | .map(|read| read.iter()) |
| 2412 | 2548 | .unwrap_or_default() |
| @@ -3318,14 +3454,12 @@ pub mod tests { |
| 3318 | 3454 | info.insert_message(event, ThreadSummaryStatus::Unknown, &settings, &mut previews); |
| 3319 | 3455 | |
| 3320 | 3456 | assert_eq!( |
| 3321 | | info.user_receipts |
| 3322 | | .get(&ReceiptThread::Main) |
| 3457 | info.user_receipts(&ReceiptThread::Main) |
| 3323 | 3458 | .and_then(|receipts| receipts.get(&*TEST_USER2)), |
| 3324 | 3459 | Some(&*MSG5_EVID), |
| 3325 | 3460 | ); |
| 3326 | 3461 | assert!( |
| 3327 | | info.event_receipts |
| 3328 | | .get(&ReceiptThread::Main) |
| 3462 | info.event_receipts(&ReceiptThread::Main) |
| 3329 | 3463 | .and_then(|receipts| receipts.get(&*MSG5_EVID)) |
| 3330 | 3464 | .is_some_and(|users| users.contains(&*TEST_USER2)) |
| 3331 | 3465 | ); |
| @@ -3351,8 +3485,7 @@ pub mod tests { |
| 3351 | 3485 | info.insert_message(older, ThreadSummaryStatus::Unknown, &settings, &mut previews); |
| 3352 | 3486 | |
| 3353 | 3487 | assert_eq!( |
| 3354 | | info.user_receipts |
| 3355 | | .get(&ReceiptThread::Main) |
| 3488 | info.user_receipts(&ReceiptThread::Main) |
| 3356 | 3489 | .and_then(|receipts| receipts.get(&*TEST_USER2)), |
| 3357 | 3490 | Some(&*MSG5_EVID), |
| 3358 | 3491 | ); |
| @@ -3370,9 +3503,7 @@ pub mod tests { |
| 3370 | 3503 | info.set_implicit_receipt(TEST_USER1.clone(), reply.clone()); |
| 3371 | 3504 | |
| 3372 | 3505 | assert_eq!( |
| 3373 | | info.user_receipts |
| 3374 | | .get(&thread) |
| 3375 | | .and_then(|receipts| receipts.get(&*TEST_USER1)), |
| 3506 | info.user_receipts(&thread).and_then(|receipts| receipts.get(&*TEST_USER1)), |
| 3376 | 3507 | Some(&reply), |
| 3377 | 3508 | ); |
| 3378 | 3509 | } |
| @@ -3386,8 +3517,7 @@ pub mod tests { |
| 3386 | 3517 | info.set_implicit_receipt(TEST_USER2.clone(), MSG5_EVID.clone()); |
| 3387 | 3518 | |
| 3388 | 3519 | assert_eq!( |
| 3389 | | info.user_receipts |
| 3390 | | .get(&ReceiptThread::Main) |
| 3520 | info.user_receipts(&ReceiptThread::Main) |
| 3391 | 3521 | .and_then(|receipts| receipts.get(&*TEST_USER2)), |
| 3392 | 3522 | Some(&*MSG5_EVID), |
| 3393 | 3523 | ); |
| @@ -3414,8 +3544,7 @@ pub mod tests { |
| 3414 | 3544 | info.set_receipt(ReceiptThread::Main, TEST_USER2.clone(), MSG2_EVID.clone()); |
| 3415 | 3545 | |
| 3416 | 3546 | assert_eq!( |
| 3417 | | info.user_receipts |
| 3418 | | .get(&ReceiptThread::Main) |
| 3547 | info.user_receipts(&ReceiptThread::Main) |
| 3419 | 3548 | .and_then(|receipts| receipts.get(&*TEST_USER2)), |
| 3420 | 3549 | Some(&*MSG5_EVID), |
| 3421 | 3550 | ); |
| @@ -3436,8 +3565,7 @@ pub mod tests { |
| 3436 | 3565 | info.insert_message(older, ThreadSummaryStatus::Unknown, &settings, &mut previews); |
| 3437 | 3566 | |
| 3438 | 3567 | assert_eq!( |
| 3439 | | info.user_receipts |
| 3440 | | .get(&ReceiptThread::Main) |
| 3568 | info.user_receipts(&ReceiptThread::Main) |
| 3441 | 3569 | .and_then(|receipts| receipts.get(&*TEST_USER2)), |
| 3442 | 3570 | Some(&*MSG5_EVID), |
| 3443 | 3571 | ); |