Commit 99996e275b
Unregistered key
Layout: unified · split
docs/iamb.5.md +3 −2
| @@ -90,9 +90,10 @@ overridden as described in *PROFILES*. | ||
| 90 | 90 | > Defines whether or not the message body is colored like the username. |
| 91 | 91 | |
| 92 | 92 | **notifications** (type: notifications object) |
| 93 | > Configures push-notifications, which are delivered as desktop | |
| 94 | > notifications if available. | |
| 93 | > Configures push-notifications. | |
| 95 | 94 | > *enabled* `true` to enable the feature, defaults to `false`. |
| 95 | > *via* `"desktop"` to use desktop mechanism (default), or `"bell"` to use | |
| 96 | > terminal bell. | |
| 96 | 97 | > *show_message* to show the message in the desktop notification. Defaults |
| 97 | 98 | > to `true`. Messages are truncated beyond a small length. |
| 98 | 99 | > The notification _rules_ are stored server side, loaded once at startup, |
src/base.rs +4
| @@ -1273,6 +1273,9 @@ pub struct ChatStore { | ||
| 1273 | 1273 | |
| 1274 | 1274 | /// Last draw time, used to match with RoomInfo's draw_last. |
| 1275 | 1275 | pub draw_curr: Option<Instant>, |
| 1276 | ||
| 1277 | /// Whether to ring the terminal bell on the next redraw. | |
| 1278 | pub ring_bell: bool, | |
| 1276 | 1279 | } |
| 1277 | 1280 | |
| 1278 | 1281 | impl ChatStore { |
| @@ -1294,6 +1297,7 @@ impl ChatStore { | ||
| 1294 | 1297 | need_load: Default::default(), |
| 1295 | 1298 | sync_info: Default::default(), |
| 1296 | 1299 | draw_curr: None, |
| 1300 | ring_bell: false, | |
| 1297 | 1301 | } |
| 1298 | 1302 | } |
| 1299 | 1303 | |
src/config.rs +19 −1
| @@ -86,6 +86,10 @@ fn is_profile_char(c: char) -> bool { | ||
| 86 | 86 | c.is_ascii_alphanumeric() || c == '.' || c == '-' |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | fn default_true() -> bool { | |
| 90 | true | |
| 91 | } | |
| 92 | ||
| 89 | 93 | fn validate_profile_name(name: &str) -> bool { |
| 90 | 94 | if name.is_empty() { |
| 91 | 95 | return false; |
| @@ -391,10 +395,24 @@ pub enum UserDisplayStyle { | ||
| 391 | 395 | DisplayName, |
| 392 | 396 | } |
| 393 | 397 | |
| 398 | #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)] | |
| 399 | #[serde(rename_all = "lowercase")] | |
| 400 | pub enum NotifyVia { | |
| 401 | /// Deliver notifications via terminal bell. | |
| 402 | Bell, | |
| 403 | /// Deliver notifications via desktop mechanism. | |
| 404 | #[default] | |
| 405 | Desktop, | |
| 406 | } | |
| 407 | ||
| 394 | 408 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| 395 | 409 | pub struct Notifications { |
| 410 | #[serde(default)] | |
| 396 | 411 | pub enabled: bool, |
| 397 | pub show_message: Option<bool>, | |
| 412 | #[serde(default)] | |
| 413 | pub via: NotifyVia, | |
| 414 | #[serde(default = "default_true")] | |
| 415 | pub show_message: bool, | |
| 398 | 416 | } |
| 399 | 417 | |
| 400 | 418 | #[derive(Clone)] |
src/main.rs +5 −1
| @@ -19,7 +19,7 @@ use std::collections::VecDeque; | ||
| 19 | 19 | use std::convert::TryFrom; |
| 20 | 20 | use std::fmt::Display; |
| 21 | 21 | use std::fs::{create_dir_all, File}; |
| 22 | use std::io::{stdout, BufWriter, Stdout}; | |
| 22 | use std::io::{stdout, BufWriter, Stdout, Write}; | |
| 23 | 23 | use std::ops::DerefMut; |
| 24 | 24 | use std::process; |
| 25 | 25 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| @@ -293,6 +293,10 @@ impl Application { | ||
| 293 | 293 | let sstate = &mut self.screen; |
| 294 | 294 | let term = &mut self.terminal; |
| 295 | 295 | |
| 296 | if store.application.ring_bell { | |
| 297 | store.application.ring_bell = term.backend_mut().write_all(&[7]).is_err(); | |
| 298 | } | |
| 299 | ||
| 296 | 300 | if full { |
| 297 | 301 | term.clear()?; |
| 298 | 302 | } |
src/notifications.rs +41 −22
| @@ -15,7 +15,7 @@ use unicode_segmentation::UnicodeSegmentation; | ||
| 15 | 15 | |
| 16 | 16 | use crate::{ |
| 17 | 17 | base::{AsyncProgramStore, IambError, IambResult}, |
| 18 | config::ApplicationSettings, | |
| 18 | config::{ApplicationSettings, NotifyVia}, | |
| 19 | 19 | }; |
| 20 | 20 | |
| 21 | 21 | pub async fn register_notifications( |
| @@ -26,6 +26,7 @@ pub async fn register_notifications( | ||
| 26 | 26 | if !settings.tunables.notifications.enabled { |
| 27 | 27 | return; |
| 28 | 28 | } |
| 29 | let notify_via = settings.tunables.notifications.via; | |
| 29 | 30 | let show_message = settings.tunables.notifications.show_message; |
| 30 | 31 | let server_settings = client.notification_settings().await; |
| 31 | 32 | let Some(startup_ts) = MilliSecondsSinceUnixEpoch::from_system_time(SystemTime::now()) else { |
| @@ -47,29 +48,19 @@ pub async fn register_notifications( | ||
| 47 | 48 | return; |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | match parse_notification(notification, room).await { | |
| 51 | match parse_notification(notification, room, show_message).await { | |
| 51 | 52 | Ok((summary, body, server_ts)) => { |
| 52 | 53 | if server_ts < startup_ts { |
| 53 | 54 | return; |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | let mut desktop_notification = notify_rust::Notification::new(); | |
| 57 | desktop_notification | |
| 58 | .summary(&summary) | |
| 59 | .appname("iamb") | |
| 60 | .timeout(notify_rust::Timeout::Milliseconds(3000)) | |
| 61 | .action("default", "default"); | |
| 62 | ||
| 63 | 57 | if is_missing_mention(&body, mode, &client) { |
| 64 | 58 | return; |
| 65 | 59 | } |
| 66 | if show_message != Some(false) { | |
| 67 | if let Some(body) = body { | |
| 68 | desktop_notification.body(&body); | |
| 69 | } | |
| 70 | } | |
| 71 | if let Err(err) = desktop_notification.show() { | |
| 72 | tracing::error!("Failed to send notification: {err}") | |
| 60 | ||
| 61 | match notify_via { | |
| 62 | NotifyVia::Desktop => send_notification_desktop(summary, body), | |
| 63 | NotifyVia::Bell => send_notification_bell(&store).await, | |
| 73 | 64 | } |
| 74 | 65 | }, |
| 75 | 66 | Err(err) => { |
| @@ -81,6 +72,28 @@ pub async fn register_notifications( | ||
| 81 | 72 | .await; |
| 82 | 73 | } |
| 83 | 74 | |
| 75 | async fn send_notification_bell(store: &AsyncProgramStore) { | |
| 76 | let mut locked = store.lock().await; | |
| 77 | locked.application.ring_bell = true; | |
| 78 | } | |
| 79 | ||
| 80 | fn send_notification_desktop(summary: String, body: Option<String>) { | |
| 81 | let mut desktop_notification = notify_rust::Notification::new(); | |
| 82 | desktop_notification | |
| 83 | .summary(&summary) | |
| 84 | .appname("iamb") | |
| 85 | .timeout(notify_rust::Timeout::Milliseconds(3000)) | |
| 86 | .action("default", "default"); | |
| 87 | ||
| 88 | if let Some(body) = body { | |
| 89 | desktop_notification.body(&body); | |
| 90 | } | |
| 91 | ||
| 92 | if let Err(err) = desktop_notification.show() { | |
| 93 | tracing::error!("Failed to send notification: {err}") | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 84 | 97 | async fn global_or_room_mode( |
| 85 | 98 | settings: &NotificationSettings, |
| 86 | 99 | room: &MatrixRoom, |
| @@ -129,6 +142,7 @@ async fn is_open(store: &AsyncProgramStore, room_id: &RoomId) -> bool { | ||
| 129 | 142 | pub async fn parse_notification( |
| 130 | 143 | notification: Notification, |
| 131 | 144 | room: MatrixRoom, |
| 145 | show_body: bool, | |
| 132 | 146 | ) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch)> { |
| 133 | 147 | let event = notification.event.deserialize().map_err(IambError::from)?; |
| 134 | 148 | |
| @@ -142,12 +156,17 @@ pub async fn parse_notification( | ||
| 142 | 156 | .and_then(|m| m.display_name()) |
| 143 | 157 | .unwrap_or_else(|| sender_id.localpart()); |
| 144 | 158 | |
| 145 | let body = event_notification_body( | |
| 146 | &event, | |
| 147 | sender_name, | |
| 148 | room.is_direct().await.map_err(IambError::from)?, | |
| 149 | ) | |
| 150 | .map(truncate); | |
| 159 | let body = if show_body { | |
| 160 | event_notification_body( | |
| 161 | &event, | |
| 162 | sender_name, | |
| 163 | room.is_direct().await.map_err(IambError::from)?, | |
| 164 | ) | |
| 165 | .map(truncate) | |
| 166 | } else { | |
| 167 | None | |
| 168 | }; | |
| 169 | ||
| 151 | 170 | return Ok((sender_name.to_string(), body, server_ts)); |
| 152 | 171 | } |
| 153 | 172 | |
src/tests.rs +6 −1
| @@ -28,6 +28,7 @@ use crate::{ | ||
| 28 | 28 | ApplicationSettings, |
| 29 | 29 | DirectoryValues, |
| 30 | 30 | Notifications, |
| 31 | NotifyVia, | |
| 31 | 32 | ProfileConfig, |
| 32 | 33 | SortOverrides, |
| 33 | 34 | TunableValues, |
| @@ -187,7 +188,11 @@ pub fn mock_tunables() -> TunableValues { | ||
| 187 | 188 | open_command: None, |
| 188 | 189 | username_display: UserDisplayStyle::Username, |
| 189 | 190 | message_user_color: false, |
| 190 | notifications: Notifications { enabled: false, show_message: None }, | |
| 191 | notifications: Notifications { | |
| 192 | enabled: false, | |
| 193 | via: NotifyVia::Desktop, | |
| 194 | show_message: true, | |
| 195 | }, | |
| 191 | 196 | image_preview: None, |
| 192 | 197 | user_gutter_width: 30, |
| 193 | 198 | } |