Skip to content

Commit

Permalink
fix(utils): use String::truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Nov 4, 2024
1 parent b3a5e0f commit ca5a985
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use tokio::{net::TcpListener, sync::RwLock};
use tower_http::trace::TraceLayer;
use tracing::info;

use crate::{config::CONFIG, utils::axum::AxumResult};
use crate::{
config::CONFIG,
utils::{self, axum::AxumResult},
};

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ValfiskPresenceData {
Expand Down Expand Up @@ -158,7 +161,7 @@ async fn route_kofi_webhook(
.color(0xffd43b);

if let Some(message) = data.message {
embed = embed.field("Message", message[..1024].to_owned(), false);
embed = embed.field("Message", utils::truncate(&message, 1024), false);
}

channel
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use poise::serenity_prelude::{self as serenity, Mentionable as _};

use eyre::Result;

use crate::{config::CONFIG, storage::log::MessageLog, Data};
use crate::{config::CONFIG, storage::log::MessageLog, utils, Data};

pub async fn handle_message(
ctx: &serenity::Context,
Expand Down Expand Up @@ -74,10 +74,10 @@ pub async fn edit(
"Previous content",
prev_content
.clone()
.map_or_else(|| "*Unknown*".to_owned(), |s| s[..1024].to_owned()),
.map_or_else(|| "*Unknown*".to_owned(), |s| utils::truncate(&s, 1024)),
false,
)
.field("New content", &new_content[..1024], false)
.field("New content", utils::truncate(new_content, 1024), false)
.field("Author", format_user(author.as_ref()), false)
.color(0xffd43b)
.timestamp(timestamp);
Expand Down Expand Up @@ -153,7 +153,7 @@ pub async fn delete(
.field("Channel", channel.mention().to_string(), false)
.field(
"Content",
content.map_or_else(|| "*Unknown*".to_owned(), |s| s[..1024].to_owned()),
content.map_or_else(|| "*Unknown*".to_owned(), |s| utils::truncate(&s, 1024)),
false,
)
.field("Author", format_user(author.as_ref()), false)
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/safe_browsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::LazyLock;
use eyre::Result;

use super::log::format_user;
use crate::{config::CONFIG, Data};
use crate::{config::CONFIG, utils, Data};

static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
Expand Down Expand Up @@ -63,7 +63,7 @@ pub async fn handle(
.title("Safe Browsing")
.field("Channel", message.channel_id.mention().to_string(), false)
.field("Author", format_user(Some(&message.author.id)), false)
.field("Content", &content[..1024], false)
.field("Content", utils::truncate(&content, 1024), false)
.field(
"URLs",
matches
Expand Down
6 changes: 6 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ pub mod serenity;

mod pluralize;
pub use pluralize::Pluralize;

pub fn truncate(s: &str, new_len: usize) -> String {
let mut s = s.to_owned();
s.truncate(new_len);
s
}

0 comments on commit ca5a985

Please sign in to comment.