| @@ -22,6 +22,7 @@ use matrix_sdk::ruma::api::client::filter::{ |
| 22 | RoomEventFilter, |
22 | RoomEventFilter, |
| 23 | RoomFilter, |
23 | RoomFilter, |
| 24 | }; |
24 | }; |
| |
25 | use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType as CreateReceiptType; |
| 25 | use matrix_sdk::ruma::api::client::room::Visibility; |
26 | use matrix_sdk::ruma::api::client::room::Visibility; |
| 26 | use matrix_sdk::ruma::api::client::room::create_room::v3::{ |
27 | use matrix_sdk::ruma::api::client::room::create_room::v3::{ |
| 27 | CreationContent, |
28 | CreationContent, |
| @@ -71,6 +72,7 @@ use ratatui_image::picker::Picker; |
| 71 | use tokio::sync::Semaphore; |
72 | use tokio::sync::Semaphore; |
| 72 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
73 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
| 73 | use tokio::task::JoinHandle; |
74 | use tokio::task::JoinHandle; |
| |
75 | use tokio_stream::wrappers::UnboundedReceiverStream; |
| 74 | use tracing::{Instrument as _, error, warn}; |
76 | use tracing::{Instrument as _, error, warn}; |
| 75 | |
77 | |
| 76 | use crate::base::{CreateRoomFlags, CreateRoomType, EchoLocation, MessageNeed, RoomFetchStatus}; |
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) { |
457 | fn convert_receipt_type(value: ReceiptType) -> CreateReceiptType { |
| 456 | use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType; |
458 | match value { |
| 457 | |
459 | ReceiptType::Read => CreateReceiptType::Read, |
| 458 | let mut interval = tokio::time::interval(Duration::from_secs(2)); |
460 | ReceiptType::ReadPrivate => CreateReceiptType::ReadPrivate, |
| 459 | let mut sent: HashMap<OwnedRoomId, HashMap<ReceiptThread, OwnedEventId>> = Default::default(); |
461 | _ => CreateReceiptType::from(value.as_str()), |
| 460 | |
462 | } |
| 461 | loop { |
463 | } |
| 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 | } |
| |
| 486 | |
464 | |
| 487 | let receipt_type = if locked.application.settings.tunables.read_receipt_send { |
465 | async fn send_single_receipt( |
| 488 | ReceiptType::Read |
466 | client: &Client, |
| 489 | } else { |
467 | room_id: OwnedRoomId, |
| 490 | ReceiptType::ReadPrivate |
468 | thread: ReceiptThread, |
| 491 | }; |
469 | receipt_type: ReceiptType, |
| 492 | drop(locked); |
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 { |
477 | if ReceiptThread::Main == thread || ReceiptThread::Unthreaded == thread { |
| 495 | let Some(room) = client.get_room(&room_id) else { |
478 | let _ = room |
| 496 | continue; |
479 | .set_unread_flag(false) |
| 497 | }; |
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 { |
484 | let _ = room |
| 500 | let _ = room |
485 | .send_single_receipt(convert_receipt_type(receipt_type), thread, event_id) |
| 501 | .set_unread_flag(false) |
486 | .await |
| 502 | .await |
487 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to send read receipt: {e}")); |
| 503 | .inspect_err(|e| tracing::warn!(?room_id, "Failed to clear unread flag: {e}")); |
488 | } |
| 504 | } |
| |
| 505 | |
489 | |
| 506 | match room |
490 | async fn send_receipts_forever( |
| 507 | .send_single_receipt(receipt_type.clone(), thread.to_owned(), new_receipt.clone()) |
491 | client: &Client, |
| 508 | .await |
492 | stream: UnboundedReceiver<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, |
| 509 | { |
493 | ) { |
| 510 | Ok(()) => { |
494 | let stream = UnboundedReceiverStream::new(stream); |
| 511 | sent.entry(room_id).or_default().insert(thread, new_receipt); |
495 | let mut sent: HashMap<(OwnedRoomId, ReceiptThread, ReceiptType), OwnedEventId> = |
| 512 | }, |
496 | Default::default(); |
| 513 | Err(e) => tracing::warn!(?room_id, "Failed to set read receipt: {e}"), |
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 | } |
513 | }) |
| 516 | } |
514 | .await; |
| 517 | } |
515 | } |
| 518 | |
516 | |
| 519 | fn insert_local_echo( |
517 | fn insert_local_echo( |
| @@ -930,6 +928,7 @@ async fn join_room( |
| 930 | pub struct Requester { |
928 | pub struct Requester { |
| 931 | pub client: Client, |
929 | pub client: Client, |
| 932 | pub tx: UnboundedSender<WorkerTask>, |
930 | pub tx: UnboundedSender<WorkerTask>, |
| |
931 | pub receipts: UnboundedSender<(OwnedRoomId, ReceiptThread, ReceiptType, OwnedEventId)>, |
| 933 | } |
932 | } |
| 934 | |
933 | |
| 935 | impl Requester { |
934 | impl Requester { |
| @@ -941,6 +940,22 @@ impl Requester { |
| 941 | return response.recv(); |
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 | pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> { |
959 | pub fn login(&self, style: LoginStyle) -> IambResult<EditInfo> { |
| 945 | let (reply, response) = oneshot(); |
960 | let (reply, response) = oneshot(); |
| 946 | |
961 | |
| @@ -1036,6 +1051,10 @@ pub struct ClientWorker { |
| 1036 | load_handle: Option<JoinHandle<()>>, |
1051 | load_handle: Option<JoinHandle<()>>, |
| 1037 | sync_handle: Option<JoinHandle<()>>, |
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 | /// Take care when locking since worker commands are sent with the lock already held |
1058 | /// Take care when locking since worker commands are sent with the lock already held |
| 1040 | store: Option<AsyncProgramStore>, |
1059 | store: Option<AsyncProgramStore>, |
| 1041 | } |
1060 | } |
| @@ -1043,6 +1062,7 @@ pub struct ClientWorker { |
| 1043 | impl ClientWorker { |
1062 | impl ClientWorker { |
| 1044 | pub async fn spawn(client: Client, settings: ApplicationSettings) -> Requester { |
1063 | pub async fn spawn(client: Client, settings: ApplicationSettings) -> Requester { |
| 1045 | let (tx, rx) = unbounded_channel(); |
1064 | let (tx, rx) = unbounded_channel(); |
| |
1065 | let (receipt_tx, receipt_rx) = unbounded_channel(); |
| 1046 | |
1066 | |
| 1047 | let mut worker = ClientWorker { |
1067 | let mut worker = ClientWorker { |
| 1048 | initialized: false, |
1068 | initialized: false, |
| @@ -1050,6 +1070,7 @@ impl ClientWorker { |
| 1050 | client: client.clone(), |
1070 | client: client.clone(), |
| 1051 | load_handle: None, |
1071 | load_handle: None, |
| 1052 | sync_handle: None, |
1072 | sync_handle: None, |
| |
1073 | unspawned_receipt_stream: Some(receipt_rx), |
| 1053 | store: None, |
1074 | store: None, |
| 1054 | }; |
1075 | }; |
| 1055 | |
1076 | |
| @@ -1057,7 +1078,7 @@ impl ClientWorker { |
| 1057 | worker.work(rx).await; |
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 | async fn work(&mut self, mut rx: UnboundedReceiver<WorkerTask>) { |
1084 | async fn work(&mut self, mut rx: UnboundedReceiver<WorkerTask>) { |
| @@ -1430,6 +1451,10 @@ impl ClientWorker { |
| 1430 | |
1451 | |
| 1431 | self.store = Some(store.clone()); |
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 | self.load_handle = tokio::spawn({ |
1458 | self.load_handle = tokio::spawn({ |
| 1434 | let client = self.client.clone(); |
1459 | let client = self.client.clone(); |
| 1435 | let settings = self.settings.clone(); |
1460 | let settings = self.settings.clone(); |
| @@ -1440,11 +1465,11 @@ impl ClientWorker { |
| 1440 | } |
1465 | } |
| 1441 | |
1466 | |
| 1442 | let load = load_older_forever(&client, &store); |
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 | let room = refresh_rooms_forever(&client, &store); |
1469 | let room = refresh_rooms_forever(&client, &store); |
| 1445 | let notifications = register_notifications(&client, &settings, &store); |
1470 | let notifications = register_notifications(&client, &settings, &store); |
| 1446 | let sendqueue = subscribe_sendqueue_forever(&client, &store); |
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 | .into(); |
1475 | .into(); |