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 d93315a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion 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 @@ -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.to_string()

Check warning on line 220 in src/notifications.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

[clippy] reported by reviewdog 🐶 warning: redundant clone --> src/notifications.rs:220:10 | 220 | s.to_string() | ^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use --> src/notifications.rs:220:9 | 220 | s.to_string() | ^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone = note: `#[warn(clippy::redundant_clone)]` on by default Raw Output: src/notifications.rs:220:10:w:warning: redundant clone --> src/notifications.rs:220:10 | 220 | s.to_string() | ^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use --> src/notifications.rs:220:9 | 220 | s.to_string() | ^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone = note: `#[warn(clippy::redundant_clone)]` on by default __END__
}
}

0 comments on commit d93315a

Please sign in to comment.