| @@ -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. |
| |
29 | pub struct NotificationHandle( |
| |
30 | #[cfg(all(feature = "desktop", unix, not(target_os = "macos")))] |
| |
31 | Option<notify_rust::NotificationHandle>, |
| |
32 | ); |
| |
33 | |
| |
34 | impl 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 | |
| 27 | pub async fn register_notifications( |
43 | pub 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(¬ify_via, &store, &summary, body.as_deref()) |
86 | send_notification( |
| 70 | .await; |
87 | ¬ify_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 | |
| 87 | async fn send_notification( |
110 | async 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")] |
| 113 | fn send_notification_desktop(summary: &str, body: Option<&str>) { |
137 | async 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 | |