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