Commit b9c187b893
Unregistered key
Layout: unified · split
src/notifications.rs +22 −1
| @@ -8,6 +8,7 @@ use matrix_sdk::notification_settings::{ | ||
| 8 | 8 | NotificationSettings, |
| 9 | 9 | RoomNotificationMode, |
| 10 | 10 | }; |
| 11 | use matrix_sdk::ruma::events::room::message::Relation; | |
| 11 | 12 | use matrix_sdk::ruma::events::{AnyMessageLikeEventContent, AnySyncTimelineEvent}; |
| 12 | 13 | use matrix_sdk::ruma::serde::Raw; |
| 13 | 14 | |
| @@ -277,6 +278,7 @@ pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) | ||
| 277 | 278 | |
| 278 | 279 | match event.original_content()? { |
| 279 | 280 | AnyMessageLikeEventContent::RoomMessage(message) => { |
| 281 | let is_thread = matches!(message.relates_to, Some(Relation::Thread(_))); | |
| 280 | 282 | let body = match message.msgtype { |
| 281 | 283 | MessageType::Audio(_) => { |
| 282 | 284 | format!("{sender_name} sent an audio file.") |
| @@ -304,7 +306,7 @@ pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) | ||
| 304 | 306 | format!("[Unknown message type: {:?}]", &message.msgtype) |
| 305 | 307 | }, |
| 306 | 308 | }; |
| 307 | Some(body) | |
| 309 | Some(thread_notification_body(body, is_thread)) | |
| 308 | 310 | }, |
| 309 | 311 | AnyMessageLikeEventContent::Sticker(_) => Some(format!("{sender_name} sent a sticker.")), |
| 310 | 312 | AnyMessageLikeEventContent::PollStart(_) | |
| @@ -318,6 +320,25 @@ pub fn event_notification_body(event: &AnySyncTimelineEvent, sender_name: &str) | ||
| 318 | 320 | } |
| 319 | 321 | } |
| 320 | 322 | |
| 323 | fn thread_notification_body(body: String, is_thread: bool) -> String { | |
| 324 | if is_thread { | |
| 325 | format!("Thread: {body}") | |
| 326 | } else { | |
| 327 | body | |
| 328 | } | |
| 329 | } | |
| 330 | ||
| 331 | #[cfg(test)] | |
| 332 | mod tests { | |
| 333 | use super::thread_notification_body; | |
| 334 | ||
| 335 | #[test] | |
| 336 | fn thread_messages_are_identified_in_notifications() { | |
| 337 | assert_eq!(thread_notification_body("hello".into(), true), "Thread: hello"); | |
| 338 | assert_eq!(thread_notification_body("hello".into(), false), "hello"); | |
| 339 | } | |
| 340 | } | |
| 341 | ||
| 321 | 342 | fn truncate(s: String) -> String { |
| 322 | 343 | static MAX_LENGTH: usize = 5000; |
| 323 | 344 | if s.graphemes(true).count() > MAX_LENGTH { |