Commit 745a051cfb
Unregistered key
Layout: unified · split
src/notifications.rs +66 −40
| @@ -9,7 +9,13 @@ use matrix_sdk::notification_settings::{ | ||
| 9 | 9 | RoomNotificationMode, |
| 10 | 10 | }; |
| 11 | 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 | 19 | use matrix_sdk::ruma::serde::Raw; |
| 14 | 20 | |
| 15 | 21 | use crate::config::NotifyVia; |
| @@ -44,8 +50,6 @@ pub async fn register_notifications( | ||
| 44 | 50 | return; |
| 45 | 51 | } |
| 46 | 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 | 53 | let server_settings = client.notification_settings().await; |
| 50 | 54 | let Some(startup_ts) = MilliSecondsSinceUnixEpoch::from_system_time(SystemTime::now()) else { |
| 51 | 55 | return; |
| @@ -56,7 +60,6 @@ pub async fn register_notifications( | ||
| 56 | 60 | .register_notification_handler(move |notification, room: MatrixRoom, client: Client| { |
| 57 | 61 | let store = store.clone(); |
| 58 | 62 | let server_settings = server_settings.clone(); |
| 59 | let sound_hint = sound_hint.clone(); | |
| 60 | 63 | async move { |
| 61 | 64 | let mode = global_or_room_mode(&server_settings, &room).await; |
| 62 | 65 | if mode == RoomNotificationMode::Mute { |
| @@ -70,29 +73,31 @@ pub async fn register_notifications( | ||
| 70 | 73 | let room_id = room.room_id().to_owned(); |
| 71 | 74 | match notification.event { |
| 72 | 75 | RawAnySyncOrStrippedTimelineEvent::Sync(e) => { |
| 73 | match parse_full_notification(e, room, show_message, &store).await { | |
| 74 | Ok((summary, body, server_ts)) => { | |
| 76 | match parse_full_notification(e, room, &store).await { | |
| 77 | Ok((summary, body, server_ts, mentions)) => { | |
| 75 | 78 | if server_ts < startup_ts { |
| 76 | 79 | return; |
| 77 | 80 | } |
| 78 | 81 | |
| 79 | 82 | let Some(body) = body else { |
| 83 | // Don't show notifications for unsupported event types. | |
| 80 | 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 | 96 | return; |
| 85 | 97 | } |
| 86 | 98 | |
| 87 | send_notification( | |
| 88 | ¬ify_via, | |
| 89 | &summary, | |
| 90 | &body, | |
| 91 | room_id, | |
| 92 | &store, | |
| 93 | sound_hint.as_deref(), | |
| 94 | ) | |
| 95 | .await; | |
| 99 | send_notification(¬ify_via, &summary, &body, room_id, &store) | |
| 100 | .await; | |
| 96 | 101 | }, |
| 97 | 102 | Err(err) => { |
| 98 | 103 | tracing::error!("Failed to extract notification data: {err}") |
| @@ -115,11 +120,10 @@ async fn send_notification( | ||
| 115 | 120 | body: &str, |
| 116 | 121 | room_id: OwnedRoomId, |
| 117 | 122 | store: &AsyncProgramStore, |
| 118 | sound_hint: Option<&str>, | |
| 119 | 123 | ) { |
| 120 | 124 | #[cfg(feature = "desktop")] |
| 121 | 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 | 128 | #[cfg(not(feature = "desktop"))] |
| 125 | 129 | { |
| @@ -137,14 +141,16 @@ async fn send_notification_bell(store: &AsyncProgramStore) { | ||
| 137 | 141 | } |
| 138 | 142 | |
| 139 | 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 | 145 | async fn send_notification_desktop( |
| 142 | 146 | summary: &str, |
| 143 | 147 | body: &str, |
| 144 | 148 | room_id: OwnedRoomId, |
| 145 | _store: &AsyncProgramStore, | |
| 146 | sound_hint: Option<&str>, | |
| 149 | store: &AsyncProgramStore, | |
| 147 | 150 | ) { |
| 151 | let mut locked = store.lock().await; | |
| 152 | let settings = &locked.application.settings; | |
| 153 | ||
| 148 | 154 | let mut desktop_notification = notify_rust::Notification::new(); |
| 149 | 155 | desktop_notification |
| 150 | 156 | .summary(summary) |
| @@ -152,14 +158,16 @@ async fn send_notification_desktop( | ||
| 152 | 158 | .icon(IAMB_XDG_NAME) |
| 153 | 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 | 162 | desktop_notification.sound_name(sound_hint); |
| 157 | 163 | } |
| 158 | 164 | |
| 159 | 165 | #[cfg(all(unix, not(target_os = "macos")))] |
| 160 | 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 | 172 | #[cfg(all(unix, not(target_os = "macos")))] |
| 165 | 173 | let res = desktop_notification.show_async().await; |
| @@ -170,9 +178,7 @@ async fn send_notification_desktop( | ||
| 170 | 178 | Err(err) => tracing::error!("Failed to send notification: {err}"), |
| 171 | 179 | Ok(handle) => { |
| 172 | 180 | #[cfg(all(unix, not(target_os = "macos")))] |
| 173 | _store | |
| 174 | .lock() | |
| 175 | .await | |
| 181 | locked | |
| 176 | 182 | .application |
| 177 | 183 | .open_notifications |
| 178 | 184 | .entry(room_id) |
| @@ -203,15 +209,31 @@ async fn global_or_room_mode( | ||
| 203 | 209 | .await |
| 204 | 210 | } |
| 205 | 211 | |
| 206 | fn is_missing_mention(body: &str, mode: RoomNotificationMode, client: &Client) -> bool { | |
| 207 | if mode == RoomNotificationMode::MentionsAndKeywordsOnly { | |
| 208 | let mentioned = match client.user_id() { | |
| 209 | Some(user_id) => body.contains(user_id.localpart()), | |
| 210 | _ => false, | |
| 211 | }; | |
| 212 | return !mentioned; | |
| 212 | async fn is_missing_mention( | |
| 213 | body: &str, | |
| 214 | mentions: Mentions, | |
| 215 | mode: RoomNotificationMode, | |
| 216 | client: &Client, | |
| 217 | settings: &NotificationSettings, | |
| 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 | 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 | 259 | pub async fn parse_full_notification( |
| 238 | 260 | event: Raw<AnySyncTimelineEvent>, |
| 239 | 261 | room: MatrixRoom, |
| 240 | show_body: bool, | |
| 241 | 262 | store: &AsyncProgramStore, |
| 242 | ) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch)> { | |
| 263 | ) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch, Mentions)> { | |
| 243 | 264 | let event = event.deserialize().map_err(IambError::from)?; |
| 244 | 265 | |
| 245 | 266 | let server_ts = event.origin_server_ts(); |
| @@ -268,13 +289,18 @@ pub async fn parse_full_notification( | ||
| 268 | 289 | sender_name.to_string() |
| 269 | 290 | }; |
| 270 | 291 | |
| 271 | let body = if show_body { | |
| 272 | event_notification_body(&event, &sender_name).map(truncate) | |
| 292 | let body = 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 | 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 | 306 | pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) -> Option<String> { |