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::{
9292
9393use crate::config::ImagePreviewProtocolValues;
9494use crate::message::ImageStatus;
95use crate::notifications::NotificationHandle;
9596use crate::preview::{source_from_event, spawn_insert_preview};
9697use crate::{
9798 message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages},
@@ -1558,6 +1559,9 @@ pub struct ChatStore {
15581559
15591560 /// Collator for locale-aware text sorting.
15601561 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>>,
15611565}
15621566
15631567impl ChatStore {
@@ -1582,6 +1586,7 @@ impl ChatStore {
15821586 draw_curr: None,
15831587 ring_bell: false,
15841588 focused: true,
1589 open_notifications: Default::default(),
15851590 }
15861591 }
15871592
src/main.rs +3
@@ -561,6 +561,9 @@ impl Application {
561561 IambAction::ClearUnreads => {
562562 let user_id = &store.application.settings.profile.user_id;
563563
564 // Clear any notifications we displayed:
565 store.application.open_notifications.clear();
566
564567 for room_id in store.application.sync_info.chats() {
565568 if let Some(room) = store.application.rooms.get_mut(room_id) {
566569 room.fully_read(user_id);
src/notifications.rs +47 −7
@@ -8,6 +8,7 @@ use matrix_sdk::{
88 events::{room::message::MessageType, AnyMessageLikeEventContent, AnySyncTimelineEvent},
99 serde::Raw,
1010 MilliSecondsSinceUnixEpoch,
11 OwnedRoomId,
1112 RoomId,
1213 },
1314 Client,
@@ -24,6 +25,21 @@ const IAMB_XDG_NAME: &str = match option_env!("IAMB_XDG_NAME") {
2425 Some(iamb) => iamb,
2526};
2627
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
2743pub async fn register_notifications(
2844 client: &Client,
2945 settings: &ApplicationSettings,
@@ -54,6 +70,7 @@ pub async fn register_notifications(
5470 return;
5571 }
5672
73 let room_id = room.room_id().to_owned();
5774 match notification.event {
5875 RawAnySyncOrStrippedTimelineEvent::Sync(e) => {
5976 match parse_full_notification(e, room, show_message).await {
@@ -66,8 +83,14 @@ pub async fn register_notifications(
6683 return;
6784 }
6885
69 send_notification(&notify_via, &store, &summary, body.as_deref())
70 .await;
86 send_notification(
87 &notify_via,
88 &summary,
89 body.as_deref(),
90 room_id,
91 &store,
92 )
93 .await;
7194 },
7295 Err(err) => {
7396 tracing::error!("Failed to extract notification data: {err}")
@@ -86,13 +109,14 @@ pub async fn register_notifications(
86109
87110async fn send_notification(
88111 via: &NotifyVia,
89 store: &AsyncProgramStore,
90112 summary: &str,
91113 body: Option<&str>,
114 room_id: OwnedRoomId,
115 store: &AsyncProgramStore,
92116) {
93117 #[cfg(feature = "desktop")]
94118 if via.desktop {
95 send_notification_desktop(summary, body);
119 send_notification_desktop(summary, body, room_id, store).await;
96120 }
97121 #[cfg(not(feature = "desktop"))]
98122 {
@@ -110,7 +134,12 @@ async fn send_notification_bell(store: &AsyncProgramStore) {
110134}
111135
112136#[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) {
114143 let mut desktop_notification = notify_rust::Notification::new();
115144 desktop_notification
116145 .summary(summary)
@@ -125,8 +154,19 @@ fn send_notification_desktop(summary: &str, body: Option<&str>) {
125154 desktop_notification.body(body);
126155 }
127156
128 if let Err(err) = desktop_notification.show() {
129 tracing::error!("Failed to send notification: {err}")
157 match desktop_notification.show() {
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 },
130170 }
131171}
132172
src/worker.rs +7 −3
@@ -502,19 +502,23 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
502502 loop {
503503 interval.tick().await;
504504
505 let locked = store.lock().await;
506 let user_id = &locked.application.settings.profile.user_id;
505 let mut locked = store.lock().await;
506 let ChatStore { settings, open_notifications, rooms, .. } = &mut locked.application;
507 let user_id = &settings.profile.user_id;
507508
508509 let mut updates = Vec::new();
509510 for room in client.joined_rooms() {
510511 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 {
512513 continue;
513514 };
514515
515516 let changed = info.receipts(user_id).filter_map(|(thread, new_receipt)| {
516517 let old_receipt = sent.get(room_id).and_then(|ts| ts.get(thread));
517518 let changed = Some(new_receipt) != old_receipt;
519 if changed {
520 open_notifications.remove(room_id);
521 }
518522 changed.then(|| (room_id.to_owned(), thread.to_owned(), new_receipt.to_owned()))
519523 });
520524