| @@ -22,6 +22,7 @@ use matrix_sdk::ruma::api::client::filter::{ |
| 22 | 22 | RoomEventFilter, |
| 23 | 23 | RoomFilter, |
| 24 | 24 | }; |
| 25 | use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType as CreateReceiptType; |
| 25 | 26 | use matrix_sdk::ruma::api::client::room::Visibility; |
| 26 | 27 | use matrix_sdk::ruma::api::client::room::create_room::v3::{ |
| 27 | 28 | CreationContent, |
| @@ -71,6 +72,7 @@ use ratatui_image::picker::Picker; |
| 71 | 72 | use tokio::sync::Semaphore; |
| 72 | 73 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
| 73 | 74 | use tokio::task::JoinHandle; |
| 75 | use tokio_stream::wrappers::UnboundedReceiverStream; |
| 74 | 76 | use tracing::{Instrument as _, error, warn}; |
| 75 | 77 | |
| 76 | 78 | use crate::base::{CreateRoomFlags, CreateRoomType, EchoLocation, MessageNeed, RoomFetchStatus}; |
| @@ -452,68 +454,64 @@ async fn refresh_rooms_forever(client: &Client, store: &AsyncProgramStore) { |
| 452 | 454 | } |
| 453 | 455 | } |
| 454 | 456 | |
| 455 | | async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) { |
| 456 | | use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType; |
| 457 | | |
| 458 | | let mut interval = tokio::time::interval(Duration::from_secs(2)); |
| 459 | | let mut sent: HashMap<OwnedRoomId, HashMap<ReceiptThread, OwnedEventId>> = Default::default(); |
| 460 | | |
| 461 | | loop { |
| 462 | | interval.tick().await; |
| 463 | | |
| 464 | | let mut locked = store.lock().await; |
| 465 | | let ChatStore { settings, open_notifications, rooms, .. } = &mut locked.application; |
| 466 | | let user_id = &settings.profile.user_id; |
| 467 | | |
| 468 | | let mut updates = Vec::new(); |
| 469 | | for room in client.joined_rooms() { |
| 470 | | let room_id = room.room_id(); |
| 471 | | let Some(info) = rooms.get(room_id) else { |
| 472 | | continue; |
| 473 | | }; |
| 474 | | |
| 475 | | let changed = info.receipts(user_id).filter_map(|(thread, new_receipt)| { |
| 476 | | let old_receipt = sent.get(room_id).and_then(|ts| ts.get(thread)); |
| 477 | | let changed = Some(new_receipt) != old_receipt; |
| 478 | | if changed { |
| 479 | | open_notifications.remove(room_id); |
| 480 | | } |
| 481 | | changed.then(|| (room_id.to_owned(), thread.to_owned(), new_receipt.to_owned())) |
| 482 | | }); |
| 483 | | |
| 484 | | updates.extend(changed); |
| 485 | | } |
| 457 | fn convert_receipt_type(value: ReceiptType) -> CreateReceiptType { |
| 458 | match value { |
| 459 | ReceiptType::Read => CreateReceiptType::Read, |
| 460 | ReceiptType::ReadPrivate => CreateReceiptType::ReadPrivate, |
| 461 | _ => CreateReceiptType::from(value.as_str()), |
| 462 | } |
| 463 | } |
| 486 | 464 | |
| 487 | | let receipt_type = if locked.application.settings.tunables.read_receipt_send { |
| 488 | | ReceiptType::Read |
| 489 | | } else { |
| 490 | | ReceiptType::ReadPrivate |
| 491 | | }; |
| 492 | | drop(locked); |
| 465 | async fn send_single_receipt( |
| 466 | client: &Client, |
| 467 | room_id: OwnedRoomId, |
| 468 | thread: ReceiptThread, |
| 469 | receipt_type: ReceiptType, |
| 470 | event_id: OwnedEventId, |
| 471 | ) { |
| 472 | let Some(room) = client.get_room(&room_id) else { |
| 473 | tracing::warn!(?room_id, "trying to send receipt to unknown room"); |
| 474 | return; |
| 475 | }; |
| 493 | 476 | |
| 494 | | for (room_id, thread, new_receipt) in updates { |
| 495 | | let Some(room) = client.get_room(&room_id) else { |
| 496 | | continue; |
| 497 | | }; |
| 477 | if ReceiptThread::Main == thread || ReceiptThread::Unthreaded == thread { |
| 478 | let _ = room |
| 479 | .set_unread_flag(false) |
| 480 | .await |
| 481 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to clear unread flag: {e}")); |
| 482 | } |
| 498 | 483 | |
| 499 | | if ReceiptThread::Main == thread || ReceiptThread::Unthreaded == thread { |
| 500 | | let _ = room |
| 501 | | .set_unread_flag(false) |
| 502 | | .await |
| 503 | | .inspect_err(|e| tracing::warn!(?room_id, "Failed to clear unread flag: {e}")); |
| 504 | | } |
| 484 | let _ = room |
| 485 | .send_single_receipt(convert_receipt_type(receipt_type), thread, event_id) |
| 486 | .await |
| 487 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to send read receipt: {e}")); |
| 488 | } |
| 505 | 489 | |
| 506 | | match room |
| 507 | | .send_single_receipt(receipt_type.clone(), thread.to_owned(), new_receipt.clone()) |
| 508 | | .await |
| 509 | | { |
| 510 | | Ok(()) => { |
| 511 | | sent.entry(room_id).or_default().insert(thread, new_receipt); |
| 512 | | }, |
| 513 | | Err(e) => tracing::warn!(?room_id, "Failed to set read receipt: {e}"), |
| 490 | async fn send_receipts_forever( |
| 491 | client: &Client, |
| 492 | stream: UnboundedReceiver<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, |
| 493 | ) { |
| 494 | let stream = UnboundedReceiverStream::new(stream); |
| 495 | let mut sent: HashMap<(OwnedRoomId, ReceiptThread, ReceiptType), OwnedEventId> = |
| 496 | Default::default(); |
| 497 | |
| 498 | stream |
| 499 | .for_each_concurrent(None, |(room_id, thread, receipt_type, event_id)| { |
| 500 | let key = (room_id.clone(), thread.clone(), receipt_type.clone()); |
| 501 | if sent.get(&key).is_some_and(|receipt| *receipt == event_id) { |
| 502 | futures::future::Either::Left(futures::future::ready(())) |
| 503 | } else { |
| 504 | sent.insert(key, event_id.clone()); |
| 505 | futures::future::Either::Right(send_single_receipt( |
| 506 | client, |
| 507 | room_id, |
| 508 | thread, |
| 509 | receipt_type, |
| 510 | event_id, |
| 511 | )) |
| 514 | 512 | } |
| 515 | | } |
| 516 | | } |
| 513 | }) |
| 514 | .await; |
| 517 | 515 | } |
| 518 | 516 | |
| 519 | 517 | fn insert_local_echo( |
| @@ -930,6 +928,7 @@ async fn join_room( |
| 930 | 928 | pub struct Requester { |
| 931 | 929 | pub client: Client, |
| 932 | 930 | pub tx: UnboundedSender<WorkerTask>, |
| 931 | pub receipts: UnboundedSender<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, |
| 933 | 932 | } |
| 934 | 933 | |
| 935 | 934 | impl Requester { |
| @@ -941,6 +940,22 @@ impl Requester { |
| 941 | 940 | return response.recv(); |
| 942 | 941 | } |
| 943 | 942 | |
| 943 | pub fn send_receipt( |
| 944 | &self, |
| 945 | room: OwnedRoomId, |
| 946 | thread: ReceiptThread, |
| 947 | event: OwnedEventId, |
| 948 | settings: &ApplicationSettings, |
| 949 | ) { |
| 950 | let receipt_type = if settings.tunables.read_receipt_send { |
| 951 | ReceiptType::Read |
| 952 | } else { |
| 953 | ReceiptType::ReadPrivate |
| 954 | }; |
| 955 | |
| 956 | self.receipts.send((room, thread, receipt_type, event)).unwrap(); |
| 957 | } |
| 958 | |
| 944 | 959 | pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> { |
| 945 | 960 | let (reply, response) = oneshot(); |
| 946 | 961 | |
| @@ -1036,6 +1051,10 @@ pub struct ClientWorker { |
| 1036 | 1051 | load_handle: Option<JoinHandle<()>>, |
| 1037 | 1052 | sync_handle: Option<JoinHandle<()>>, |
| 1038 | 1053 | |
| 1054 | /// this will be removed after login |
| 1055 | unspawned_receipt_stream: |
| 1056 | Option<UnboundedReceiver<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>>, |
| 1057 | |
| 1039 | 1058 | /// Take care when locking since worker commands are sent with the lock already held |
| 1040 | 1059 | store: Option<AsyncProgramStore>, |
| 1041 | 1060 | } |
| @@ -1043,6 +1062,7 @@ pub struct ClientWorker { |
| 1043 | 1062 | impl ClientWorker { |
| 1044 | 1063 | pub async fn spawn(client: Client, settings: ApplicationSettings) -> Requester { |
| 1045 | 1064 | let (tx, rx) = unbounded_channel(); |
| 1065 | let (receipt_tx, receipt_rx) = unbounded_channel(); |
| 1046 | 1066 | |
| 1047 | 1067 | let mut worker = ClientWorker { |
| 1048 | 1068 | initialized: false, |
| @@ -1050,6 +1070,7 @@ impl ClientWorker { |
| 1050 | 1070 | client: client.clone(), |
| 1051 | 1071 | load_handle: None, |
| 1052 | 1072 | sync_handle: None, |
| 1073 | unspawned_receipt_stream: Some(receipt_rx), |
| 1053 | 1074 | store: None, |
| 1054 | 1075 | }; |
| 1055 | 1076 | |
| @@ -1057,7 +1078,7 @@ impl ClientWorker { |
| 1057 | 1078 | worker.work(rx).await; |
| 1058 | 1079 | }); |
| 1059 | 1080 | |
| 1060 | | return Requester { client, tx }; |
| 1081 | return Requester { client, tx, receipts: receipt_tx }; |
| 1061 | 1082 | } |
| 1062 | 1083 | |
| 1063 | 1084 | async fn work(&mut self, mut rx: UnboundedReceiver<WorkerTask>) { |
| @@ -1430,6 +1451,10 @@ impl ClientWorker { |
| 1430 | 1451 | |
| 1431 | 1452 | self.store = Some(store.clone()); |
| 1432 | 1453 | |
| 1454 | let unspawned_receipt_stream = self |
| 1455 | .unspawned_receipt_stream |
| 1456 | .take() |
| 1457 | .expect("client was started multiple times"); |
| 1433 | 1458 | self.load_handle = tokio::spawn({ |
| 1434 | 1459 | let client = self.client.clone(); |
| 1435 | 1460 | let settings = self.settings.clone(); |
| @@ -1440,11 +1465,11 @@ impl ClientWorker { |
| 1440 | 1465 | } |
| 1441 | 1466 | |
| 1442 | 1467 | let load = load_older_forever(&client, &store); |
| 1443 | | let rcpt = send_receipts_forever(&client, &store); |
| 1468 | let rcpt = send_receipts_forever(&client, unspawned_receipt_stream); |
| 1444 | 1469 | let room = refresh_rooms_forever(&client, &store); |
| 1445 | 1470 | let notifications = register_notifications(&client, &settings, &store); |
| 1446 | 1471 | let sendqueue = subscribe_sendqueue_forever(&client, &store); |
| 1447 | | let ((), (), (), (), ()) = tokio::join!(load, rcpt, room, notifications, sendqueue); |
| 1472 | let ((), (), (), (), ()) = tokio::join!(load, room, rcpt, notifications, sendqueue); |
| 1448 | 1473 | } |
| 1449 | 1474 | }) |
| 1450 | 1475 | .into(); |