Skip to content

Commit

Permalink
truncate message body if over 100 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
benjajaja committed Mar 17, 2024
1 parent 33641cf commit 4d44b8c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use matrix_sdk::{
},
Client,
};
use unicode_segmentation::UnicodeSegmentation;

use crate::{
base::{AsyncProgramStore, IambError, IambResult},
Expand Down Expand Up @@ -52,8 +53,8 @@ pub async fn register_notifications(
return;
}

let mut notification = notify_rust::Notification::new();
notification
let mut desktop_notification = notify_rust::Notification::new();
desktop_notification
.summary(&summary)
.appname("iamb")
.timeout(notify_rust::Timeout::Milliseconds(3000))
Expand All @@ -64,10 +65,10 @@ pub async fn register_notifications(
}
if show_message != Some(false) {
if let Some(body) = body {
notification.body(&body);
desktop_notification.body(&body);
}
}
if let Err(err) = notification.show() {
if let Err(err) = desktop_notification.show() {
tracing::error!("Failed to send notification: {err}")
}
},
Expand Down Expand Up @@ -146,7 +147,8 @@ pub async fn parse_notification(
&event,
sender_name,
room.is_direct().await.map_err(IambError::from)?,
);
)
.map(truncate);
return Ok((sender_name.to_string(), body, server_ts));
}

Expand Down Expand Up @@ -208,3 +210,13 @@ pub fn event_notification_body(
_ => None,
}
}

fn truncate(s: String) -> String {
static MAX_LENGTH: usize = 100;
if s.graphemes(true).count() > MAX_LENGTH {
let truncated: String = s.graphemes(true).take(MAX_LENGTH).collect();
truncated + "..."
} else {
s
}
}

0 comments on commit 4d44b8c

Please sign in to comment.