Commit 745a051cfb
Unregistered key
Layout: unified · split
src/notifications.rs +66 −40
| @@ -9,7 +9,13 @@ use matrix_sdk::notification_settings::{ | |||
| 9 | RoomNotificationMode, | 9 | RoomNotificationMode, |
| 10 | }; | 10 | }; |
| 11 | use matrix_sdk::ruma::events::room::message::Relation; | 11 | use matrix_sdk::ruma::events::room::message::Relation; |
| 12 | use matrix_sdk::ruma::events::{AnyMessageLikeEventContent, AnySyncTimelineEvent}; | 12 | use matrix_sdk::ruma::events::{ |
| 13 | AnyMessageLikeEventContent, | ||
| 14 | AnySyncMessageLikeEvent, | ||
| 15 | AnySyncTimelineEvent, | ||
| 16 | Mentions, | ||
| 17 | SyncMessageLikeEvent, | ||
| 18 | }; | ||
| 13 | use matrix_sdk::ruma::serde::Raw; | 19 | use matrix_sdk::ruma::serde::Raw; |
| 14 | 20 | ||
| 15 | use crate::config::NotifyVia; | 21 | use crate::config::NotifyVia; |
| @@ -44,8 +50,6 @@ pub async fn register_notifications( | |||
| 44 | return; | 50 | return; |
| 45 | } | 51 | } |
| 46 | let notify_via = settings.tunables.notifications.via; | 52 | let notify_via = settings.tunables.notifications.via; |
| 47 | let show_message = settings.tunables.notifications.show_message; | ||
| 48 | let sound_hint = settings.tunables.notifications.sound_hint.clone(); | ||
| 49 | let server_settings = client.notification_settings().await; | 53 | let server_settings = client.notification_settings().await; |
| 50 | let Some(startup_ts) = MilliSecondsSinceUnixEpoch::from_system_time(SystemTime::now()) else { | 54 | let Some(startup_ts) = MilliSecondsSinceUnixEpoch::from_system_time(SystemTime::now()) else { |
| 51 | return; | 55 | return; |
| @@ -56,7 +60,6 @@ pub async fn register_notifications( | |||
| 56 | .register_notification_handler(move |notification, room: MatrixRoom, client: Client| { | 60 | .register_notification_handler(move |notification, room: MatrixRoom, client: Client| { |
| 57 | let store = store.clone(); | 61 | let store = store.clone(); |
| 58 | let server_settings = server_settings.clone(); | 62 | let server_settings = server_settings.clone(); |
| 59 | let sound_hint = sound_hint.clone(); | ||
| 60 | async move { | 63 | async move { |
| 61 | let mode = global_or_room_mode(&server_settings, &room).await; | 64 | let mode = global_or_room_mode(&server_settings, &room).await; |
| 62 | if mode == RoomNotificationMode::Mute { | 65 | if mode == RoomNotificationMode::Mute { |
| @@ -70,29 +73,31 @@ pub async fn register_notifications( | |||
| 70 | let room_id = room.room_id().to_owned(); | 73 | let room_id = room.room_id().to_owned(); |
| 71 | match notification.event { | 74 | match notification.event { |
| 72 | RawAnySyncOrStrippedTimelineEvent::Sync(e) => { | 75 | RawAnySyncOrStrippedTimelineEvent::Sync(e) => { |
| 73 | match parse_full_notification(e, room, show_message, &store).await { | 76 | match parse_full_notification(e, room, &store).await { |
| 74 | Ok((summary, body, server_ts)) => { | 77 | Ok((summary, body, server_ts, mentions)) => { |
| 75 | if server_ts < startup_ts { | 78 | if server_ts < startup_ts { |
| 76 | return; | 79 | return; |
| 77 | } | 80 | } |
| 78 | 81 | ||
| 79 | let Some(body) = body else { | 82 | let Some(body) = body else { |
| 83 | // Don't show notifications for unsupported event types. | ||
| 80 | return; | 84 | return; |
| 81 | }; | 85 | }; |
| 82 | 86 | ||
| 83 | if is_missing_mention(&body, mode, &client) { | 87 | if is_missing_mention( |
| 88 | &body, | ||
| 89 | mentions, | ||
| 90 | mode, | ||
| 91 | &client, | ||
| 92 | &server_settings, | ||
| 93 | ) | ||
| 94 | .await | ||
| 95 | { | ||
| 84 | return; | 96 | return; |
| 85 | } | 97 | } |
| 86 | 98 | ||
| 87 | send_notification( | 99 | send_notification(¬ify_via, &summary, &body, room_id, &store) |
| 88 | ¬ify_via, | 100 | .await; |
| 89 | &summary, | ||
| 90 | &body, | ||
| 91 | room_id, | ||
| 92 | &store, | ||
| 93 | sound_hint.as_deref(), | ||
| 94 | ) | ||
| 95 | .await; | ||
| 96 | }, | 101 | }, |
| 97 | Err(err) => { | 102 | Err(err) => { |
| 98 | tracing::error!("Failed to extract notification data: {err}") | 103 | tracing::error!("Failed to extract notification data: {err}") |
| @@ -115,11 +120,10 @@ async fn send_notification( | |||
| 115 | body: &str, | 120 | body: &str, |
| 116 | room_id: OwnedRoomId, | 121 | room_id: OwnedRoomId, |
| 117 | store: &AsyncProgramStore, | 122 | store: &AsyncProgramStore, |
| 118 | sound_hint: Option<&str>, | ||
| 119 | ) { | 123 | ) { |
| 120 | #[cfg(feature = "desktop")] | 124 | #[cfg(feature = "desktop")] |
| 121 | if via.desktop { | 125 | if via.desktop { |
| 122 | send_notification_desktop(summary, body, room_id, store, sound_hint).await; | 126 | send_notification_desktop(summary, body, room_id, store).await; |
| 123 | } | 127 | } |
| 124 | #[cfg(not(feature = "desktop"))] | 128 | #[cfg(not(feature = "desktop"))] |
| 125 | { | 129 | { |
| @@ -137,14 +141,16 @@ async fn send_notification_bell(store: &AsyncProgramStore) { | |||
| 137 | } | 141 | } |
| 138 | 142 | ||
| 139 | #[cfg(feature = "desktop")] | 143 | #[cfg(feature = "desktop")] |
| 140 | #[cfg_attr(target_os = "macos", allow(unused_variables))] | 144 | #[cfg_attr(target_os = "macos", allow(unused_variables, unused_mut))] |
| 141 | async fn send_notification_desktop( | 145 | async fn send_notification_desktop( |
| 142 | summary: &str, | 146 | summary: &str, |
| 143 | body: &str, | 147 | body: &str, |
| 144 | room_id: OwnedRoomId, | 148 | room_id: OwnedRoomId, |
| 145 | _store: &AsyncProgramStore, | 149 | store: &AsyncProgramStore, |
| 146 | sound_hint: Option<&str>, | ||
| 147 | ) { | 150 | ) { |
| 151 | let mut locked = store.lock().await; | ||
| 152 | let settings = &locked.application.settings; | ||
| 153 | |||
| 148 | let mut desktop_notification = notify_rust::Notification::new(); | 154 | let mut desktop_notification = notify_rust::Notification::new(); |
| 149 | desktop_notification | 155 | desktop_notification |
| 150 | .summary(summary) | 156 | .summary(summary) |
| @@ -152,14 +158,16 @@ async fn send_notification_desktop( | |||
| 152 | .icon(IAMB_XDG_NAME) | 158 | .icon(IAMB_XDG_NAME) |
| 153 | .action("default", "default"); | 159 | .action("default", "default"); |
| 154 | 160 | ||
| 155 | if let Some(sound_hint) = sound_hint { | 161 | if let Some(sound_hint) = &settings.tunables.notifications.sound_hint { |
| 156 | desktop_notification.sound_name(sound_hint); | 162 | desktop_notification.sound_name(sound_hint); |
| 157 | } | 163 | } |
| 158 | 164 | ||
| 159 | #[cfg(all(unix, not(target_os = "macos")))] | 165 | #[cfg(all(unix, not(target_os = "macos")))] |
| 160 | desktop_notification.urgency(notify_rust::Urgency::Normal); | 166 | desktop_notification.urgency(notify_rust::Urgency::Normal); |
| 161 | 167 | ||
| 162 | desktop_notification.body(body); | 168 | if settings.tunables.notifications.show_message { |
| 169 | desktop_notification.body(body); | ||
| 170 | } | ||
| 163 | 171 | ||
| 164 | #[cfg(all(unix, not(target_os = "macos")))] | 172 | #[cfg(all(unix, not(target_os = "macos")))] |
| 165 | let res = desktop_notification.show_async().await; | 173 | let res = desktop_notification.show_async().await; |
| @@ -170,9 +178,7 @@ async fn send_notification_desktop( | |||
| 170 | Err(err) => tracing::error!("Failed to send notification: {err}"), | 178 | Err(err) => tracing::error!("Failed to send notification: {err}"), |
| 171 | Ok(handle) => { | 179 | Ok(handle) => { |
| 172 | #[cfg(all(unix, not(target_os = "macos")))] | 180 | #[cfg(all(unix, not(target_os = "macos")))] |
| 173 | _store | 181 | locked |
| 174 | .lock() | ||
| 175 | .await | ||
| 176 | .application | 182 | .application |
| 177 | .open_notifications | 183 | .open_notifications |
| 178 | .entry(room_id) | 184 | .entry(room_id) |
| @@ -203,15 +209,31 @@ async fn global_or_room_mode( | |||
| 203 | .await | 209 | .await |
| 204 | } | 210 | } |
| 205 | 211 | ||
| 206 | fn is_missing_mention(body: &str, mode: RoomNotificationMode, client: &Client) -> bool { | 212 | async fn is_missing_mention( |
| 207 | if mode == RoomNotificationMode::MentionsAndKeywordsOnly { | 213 | body: &str, |
| 208 | let mentioned = match client.user_id() { | 214 | mentions: Mentions, |
| 209 | Some(user_id) => body.contains(user_id.localpart()), | 215 | mode: RoomNotificationMode, |
| 210 | _ => false, | 216 | client: &Client, |
| 211 | }; | 217 | settings: &NotificationSettings, |
| 212 | return !mentioned; | 218 | ) -> bool { |
| 219 | if mode != RoomNotificationMode::MentionsAndKeywordsOnly { | ||
| 220 | return false; | ||
| 221 | } | ||
| 222 | |||
| 223 | if mentions.room { | ||
| 224 | return false; | ||
| 213 | } | 225 | } |
| 214 | false | 226 | if client.user_id().is_some_and(|user_id| mentions.user_ids.contains(user_id)) { |
| 227 | return false; | ||
| 228 | } | ||
| 229 | |||
| 230 | for keyword in settings.enabled_keywords().await { | ||
| 231 | if body.contains(&keyword) { | ||
| 232 | return false; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | true | ||
| 215 | } | 237 | } |
| 216 | 238 | ||
| 217 | fn is_open(locked: &mut ProgramStore, room_id: &RoomId) -> bool { | 239 | fn is_open(locked: &mut ProgramStore, room_id: &RoomId) -> bool { |
| @@ -237,9 +259,8 @@ async fn is_visible_room(store: &AsyncProgramStore, room_id: &RoomId) -> bool { | |||
| 237 | pub async fn parse_full_notification( | 259 | pub async fn parse_full_notification( |
| 238 | event: Raw<AnySyncTimelineEvent>, | 260 | event: Raw<AnySyncTimelineEvent>, |
| 239 | room: MatrixRoom, | 261 | room: MatrixRoom, |
| 240 | show_body: bool, | ||
| 241 | store: &AsyncProgramStore, | 262 | store: &AsyncProgramStore, |
| 242 | ) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch)> { | 263 | ) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch, Mentions)> { |
| 243 | let event = event.deserialize().map_err(IambError::from)?; | 264 | let event = event.deserialize().map_err(IambError::from)?; |
| 244 | 265 | ||
| 245 | let server_ts = event.origin_server_ts(); | 266 | let server_ts = event.origin_server_ts(); |
| @@ -268,13 +289,18 @@ pub async fn parse_full_notification( | |||
| 268 | sender_name.to_string() | 289 | sender_name.to_string() |
| 269 | }; | 290 | }; |
| 270 | 291 | ||
| 271 | let body = if show_body { | 292 | let body = event_notification_body(&event, &sender_name).map(truncate); |
| 272 | event_notification_body(&event, &sender_name).map(truncate) | 293 | |
| 294 | let mentions = if let AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage( | ||
| 295 | SyncMessageLikeEvent::Original(ev), | ||
| 296 | )) = event | ||
| 297 | { | ||
| 298 | ev.content.mentions.unwrap_or_default() | ||
| 273 | } else { | 299 | } else { |
| 274 | None | 300 | Default::default() |
| 275 | }; | 301 | }; |
| 276 | 302 | ||
| 277 | return Ok((summary, body, server_ts)); | 303 | return Ok((summary, body, server_ts, mentions)); |
| 278 | } | 304 | } |
| 279 | 305 | ||
| 280 | pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) -> Option<String> { | 306 | pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) -> Option<String> { |