Commit 745a051cfb

745a051cfbf658526afa4f577c46badb52151cfd

parent: 1166654006

Unregistered key

vaw <git@nlih.de> · 2026-11-21 12:05 UTC
committer: <noreply@github.com>

Support notifications for keyword mentions (#867)

Layout: unified · split

src/notifications.rs +66 −40
@@ -9,7 +9,13 @@ use matrix_sdk::notification_settings::{
99 RoomNotificationMode,
1010};
1111use matrix_sdk::ruma::events::room::message::Relation;
12use matrix_sdk::ruma::events::{AnyMessageLikeEventContent, AnySyncTimelineEvent};
12use matrix_sdk::ruma::events::{
13 AnyMessageLikeEventContent,
14 AnySyncMessageLikeEvent,
15 AnySyncTimelineEvent,
16 Mentions,
17 SyncMessageLikeEvent,
18};
1319use matrix_sdk::ruma::serde::Raw;
1420
1521use crate::config::NotifyVia;
@@ -44,8 +50,6 @@ pub async fn register_notifications(
4450 return;
4551 }
4652 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();
4953 let server_settings = client.notification_settings().await;
5054 let Some(startup_ts) = MilliSecondsSinceUnixEpoch::from_system_time(SystemTime::now()) else {
5155 return;
@@ -56,7 +60,6 @@ pub async fn register_notifications(
5660 .register_notification_handler(move |notification, room: MatrixRoom, client: Client| {
5761 let store = store.clone();
5862 let server_settings = server_settings.clone();
59 let sound_hint = sound_hint.clone();
6063 async move {
6164 let mode = global_or_room_mode(&server_settings, &room).await;
6265 if mode == RoomNotificationMode::Mute {
@@ -70,29 +73,31 @@ pub async fn register_notifications(
7073 let room_id = room.room_id().to_owned();
7174 match notification.event {
7275 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)) => {
7578 if server_ts < startup_ts {
7679 return;
7780 }
7881
7982 let Some(body) = body else {
83 // Don't show notifications for unsupported event types.
8084 return;
8185 };
8286
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 {
8496 return;
8597 }
8698
87 send_notification(
88 &notify_via,
89 &summary,
90 &body,
91 room_id,
92 &store,
93 sound_hint.as_deref(),
94 )
95 .await;
99 send_notification(&notify_via, &summary, &body, room_id, &store)
100 .await;
96101 },
97102 Err(err) => {
98103 tracing::error!("Failed to extract notification data: {err}")
@@ -115,11 +120,10 @@ async fn send_notification(
115120 body: &str,
116121 room_id: OwnedRoomId,
117122 store: &AsyncProgramStore,
118 sound_hint: Option<&str>,
119123) {
120124 #[cfg(feature = "desktop")]
121125 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;
123127 }
124128 #[cfg(not(feature = "desktop"))]
125129 {
@@ -137,14 +141,16 @@ async fn send_notification_bell(store: &AsyncProgramStore) {
137141}
138142
139143#[cfg(feature = "desktop")]
140#[cfg_attr(target_os = "macos", allow(unused_variables))]
144#[cfg_attr(target_os = "macos", allow(unused_variables, unused_mut))]
141145async fn send_notification_desktop(
142146 summary: &str,
143147 body: &str,
144148 room_id: OwnedRoomId,
145 _store: &AsyncProgramStore,
146 sound_hint: Option<&str>,
149 store: &AsyncProgramStore,
147150) {
151 let mut locked = store.lock().await;
152 let settings = &locked.application.settings;
153
148154 let mut desktop_notification = notify_rust::Notification::new();
149155 desktop_notification
150156 .summary(summary)
@@ -152,14 +158,16 @@ async fn send_notification_desktop(
152158 .icon(IAMB_XDG_NAME)
153159 .action("default", "default");
154160
155 if let Some(sound_hint) = sound_hint {
161 if let Some(sound_hint) = &settings.tunables.notifications.sound_hint {
156162 desktop_notification.sound_name(sound_hint);
157163 }
158164
159165 #[cfg(all(unix, not(target_os = "macos")))]
160166 desktop_notification.urgency(notify_rust::Urgency::Normal);
161167
162 desktop_notification.body(body);
168 if settings.tunables.notifications.show_message {
169 desktop_notification.body(body);
170 }
163171
164172 #[cfg(all(unix, not(target_os = "macos")))]
165173 let res = desktop_notification.show_async().await;
@@ -170,9 +178,7 @@ async fn send_notification_desktop(
170178 Err(err) => tracing::error!("Failed to send notification: {err}"),
171179 Ok(handle) => {
172180 #[cfg(all(unix, not(target_os = "macos")))]
173 _store
174 .lock()
175 .await
181 locked
176182 .application
177183 .open_notifications
178184 .entry(room_id)
@@ -203,15 +209,31 @@ async fn global_or_room_mode(
203209 .await
204210}
205211
206fn 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;
212async 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;
213225 }
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
215237}
216238
217239fn is_open(locked: &mut ProgramStore, room_id: &RoomId) -> bool {
@@ -237,9 +259,8 @@ async fn is_visible_room(store: &AsyncProgramStore, room_id: &RoomId) -> bool {
237259pub async fn parse_full_notification(
238260 event: Raw<AnySyncTimelineEvent>,
239261 room: MatrixRoom,
240 show_body: bool,
241262 store: &AsyncProgramStore,
242) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch)> {
263) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch, Mentions)> {
243264 let event = event.deserialize().map_err(IambError::from)?;
244265
245266 let server_ts = event.origin_server_ts();
@@ -268,13 +289,18 @@ pub async fn parse_full_notification(
268289 sender_name.to_string()
269290 };
270291
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()
273299 } else {
274 None
300 Default::default()
275301 };
276302
277 return Ok((summary, body, server_ts));
303 return Ok((summary, body, server_ts, mentions));
278304}
279305
280306pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) -> Option<String> {