Commit e9cdb3371a

e9cdb3371abb60d74e2a3949b6e0adcd073165ce

parent: 0ff8828a1c

Unregistered key

vaw <git@nlih.de> · 2025-07-23 05:19 UTC
committer: <noreply@github.com>

Clear desktop notification when message is read (#427)

Co-authored-by: Ulyssa <git@ulyssa.dev>

Layout: unified · split

src/base.rs +5
@@ -92,6 +92,7 @@ use modalkit::{
92 92
93use crate::config::ImagePreviewProtocolValues; 93use crate::config::ImagePreviewProtocolValues;
94use crate::message::ImageStatus; 94use crate::message::ImageStatus;
95use crate::notifications::NotificationHandle;
95use crate::preview::{source_from_event, spawn_insert_preview}; 96use crate::preview::{source_from_event, spawn_insert_preview};
96use crate::{ 97use crate::{
97 message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages}, 98 message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages},
@@ -1558,6 +1559,9 @@ pub struct ChatStore {
1558 1559
1559 /// Collator for locale-aware text sorting. 1560 /// Collator for locale-aware text sorting.
1560 pub collator: feruca::Collator, 1561 pub collator: feruca::Collator,
1562
1563 /// Notifications that should be dismissed when the user opens the room.
1564 pub open_notifications: HashMap<OwnedRoomId, Vec<NotificationHandle>>,
1561} 1565}
1562 1566
1563impl ChatStore { 1567impl ChatStore {
@@ -1582,6 +1586,7 @@ impl ChatStore {
1582 draw_curr: None, 1586 draw_curr: None,
1583 ring_bell: false, 1587 ring_bell: false,
1584 focused: true, 1588 focused: true,
1589 open_notifications: Default::default(),
1585 } 1590 }
1586 } 1591 }
1587 1592
src/main.rs +3
@@ -561,6 +561,9 @@ impl Application {
561 IambAction::ClearUnreads => { 561 IambAction::ClearUnreads => {
562 let user_id = &store.application.settings.profile.user_id; 562 let user_id = &store.application.settings.profile.user_id;
563 563
564 // Clear any notifications we displayed:
565 store.application.open_notifications.clear();
566
564 for room_id in store.application.sync_info.chats() { 567 for room_id in store.application.sync_info.chats() {
565 if let Some(room) = store.application.rooms.get_mut(room_id) { 568 if let Some(room) = store.application.rooms.get_mut(room_id) {
566 room.fully_read(user_id); 569 room.fully_read(user_id);
src/notifications.rs +47 −7
@@ -8,6 +8,7 @@ use matrix_sdk::{
8 events::{room::message::MessageType, AnyMessageLikeEventContent, AnySyncTimelineEvent}, 8 events::{room::message::MessageType, AnyMessageLikeEventContent, AnySyncTimelineEvent},
9 serde::Raw, 9 serde::Raw,
10 MilliSecondsSinceUnixEpoch, 10 MilliSecondsSinceUnixEpoch,
11 OwnedRoomId,
11 RoomId, 12 RoomId,
12 }, 13 },
13 Client, 14 Client,
@@ -24,6 +25,21 @@ const IAMB_XDG_NAME: &str = match option_env!("IAMB_XDG_NAME") {
24 Some(iamb) => iamb, 25 Some(iamb) => iamb,
25}; 26};
26 27
28/// Handle for an open notification that should be closed when the user views it.
29pub struct NotificationHandle(
30 #[cfg(all(feature = "desktop", unix, not(target_os = "macos")))]
31 Option<notify_rust::NotificationHandle>,
32);
33
34impl Drop for NotificationHandle {
35 fn drop(&mut self) {
36 #[cfg(all(feature = "desktop", unix, not(target_os = "macos")))]
37 if let Some(handle) = self.0.take() {
38 handle.close();
39 }
40 }
41}
42
27pub async fn register_notifications( 43pub async fn register_notifications(
28 client: &Client, 44 client: &Client,
29 settings: &ApplicationSettings, 45 settings: &ApplicationSettings,
@@ -54,6 +70,7 @@ pub async fn register_notifications(
54 return; 70 return;
55 } 71 }
56 72
73 let room_id = room.room_id().to_owned();
57 match notification.event { 74 match notification.event {
58 RawAnySyncOrStrippedTimelineEvent::Sync(e) => { 75 RawAnySyncOrStrippedTimelineEvent::Sync(e) => {
59 match parse_full_notification(e, room, show_message).await { 76 match parse_full_notification(e, room, show_message).await {
@@ -66,8 +83,14 @@ pub async fn register_notifications(
66 return; 83 return;
67 } 84 }
68 85
69 send_notification(&notify_via, &store, &summary, body.as_deref()) 86 send_notification(
70 .await; 87 &notify_via,
88 &summary,
89 body.as_deref(),
90 room_id,
91 &store,
92 )
93 .await;
71 }, 94 },
72 Err(err) => { 95 Err(err) => {
73 tracing::error!("Failed to extract notification data: {err}") 96 tracing::error!("Failed to extract notification data: {err}")
@@ -86,13 +109,14 @@ pub async fn register_notifications(
86 109
87async fn send_notification( 110async fn send_notification(
88 via: &NotifyVia, 111 via: &NotifyVia,
89 store: &AsyncProgramStore,
90 summary: &str, 112 summary: &str,
91 body: Option<&str>, 113 body: Option<&str>,
114 room_id: OwnedRoomId,
115 store: &AsyncProgramStore,
92) { 116) {
93 #[cfg(feature = "desktop")] 117 #[cfg(feature = "desktop")]
94 if via.desktop { 118 if via.desktop {
95 send_notification_desktop(summary, body); 119 send_notification_desktop(summary, body, room_id, store).await;
96 } 120 }
97 #[cfg(not(feature = "desktop"))] 121 #[cfg(not(feature = "desktop"))]
98 { 122 {
@@ -110,7 +134,12 @@ async fn send_notification_bell(store: &AsyncProgramStore) {
110} 134}
111 135
112#[cfg(feature = "desktop")] 136#[cfg(feature = "desktop")]
113fn send_notification_desktop(summary: &str, body: Option<&str>) { 137async fn send_notification_desktop(
138 summary: &str,
139 body: Option<&str>,
140 room_id: OwnedRoomId,
141 _store: &AsyncProgramStore,
142) {
114 let mut desktop_notification = notify_rust::Notification::new(); 143 let mut desktop_notification = notify_rust::Notification::new();
115 desktop_notification 144 desktop_notification
116 .summary(summary) 145 .summary(summary)
@@ -125,8 +154,19 @@ fn send_notification_desktop(summary: &str, body: Option<&str>) {
125 desktop_notification.body(body); 154 desktop_notification.body(body);
126 } 155 }
127 156
128 if let Err(err) = desktop_notification.show() { 157 match desktop_notification.show() {
129 tracing::error!("Failed to send notification: {err}") 158 Err(err) => tracing::error!("Failed to send notification: {err}"),
159 Ok(handle) => {
160 #[cfg(all(unix, not(target_os = "macos")))]
161 _store
162 .lock()
163 .await
164 .application
165 .open_notifications
166 .entry(room_id)
167 .or_default()
168 .push(NotificationHandle(Some(handle)));
169 },
130 } 170 }
131} 171}
132 172
src/worker.rs +7 −3
@@ -502,19 +502,23 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
502 loop { 502 loop {
503 interval.tick().await; 503 interval.tick().await;
504 504
505 let locked = store.lock().await; 505 let mut locked = store.lock().await;
506 let user_id = &locked.application.settings.profile.user_id; 506 let ChatStore { settings, open_notifications, rooms, .. } = &mut locked.application;
507 let user_id = &settings.profile.user_id;
507 508
508 let mut updates = Vec::new(); 509 let mut updates = Vec::new();
509 for room in client.joined_rooms() { 510 for room in client.joined_rooms() {
510 let room_id = room.room_id(); 511 let room_id = room.room_id();
511 let Some(info) = locked.application.rooms.get(&room_id) else { 512 let Some(info) = rooms.get(room_id) else {
512 continue; 513 continue;
513 }; 514 };
514 515
515 let changed = info.receipts(user_id).filter_map(|(thread, new_receipt)| { 516 let changed = info.receipts(user_id).filter_map(|(thread, new_receipt)| {
516 let old_receipt = sent.get(room_id).and_then(|ts| ts.get(thread)); 517 let old_receipt = sent.get(room_id).and_then(|ts| ts.get(thread));
517 let changed = Some(new_receipt) != old_receipt; 518 let changed = Some(new_receipt) != old_receipt;
519 if changed {
520 open_notifications.remove(room_id);
521 }
518 changed.then(|| (room_id.to_owned(), thread.to_owned(), new_receipt.to_owned())) 522 changed.then(|| (room_id.to_owned(), thread.to_owned(), new_receipt.to_owned()))
519 }); 523 });
520 524