Skip to content

Commit de322f3

Browse files
committed
Add markdown_plain_text and use in notifications
Introduce a text utility (markdown_plain_text) that strips bold markdown markers and collapses whitespace/newlines into single spaces. Export the helper from the support crate and apply it in SupportClient to sanitize message content for push notifications. Includes a unit test for the sanitizer.
1 parent 76a5f4d commit de322f3

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

core/crates/support/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
ChatwootWebhookPayload,
33
constants::{EVENT_CONVERSATION_STATUS_CHANGED, EVENT_CONVERSATION_UPDATED, EVENT_MESSAGE_CREATED},
4+
markdown_plain_text,
45
};
56
use cacher::CacherClient;
67
use localizer::LanguageLocalizer;
@@ -68,7 +69,7 @@ impl SupportClient {
6869
}
6970

7071
let title = LanguageLocalizer::new_with_language(device.locale.as_str()).notification_support_new_message_title();
71-
let message = payload.content.clone().unwrap_or_default();
72+
let message = markdown_plain_text(payload.content.as_deref().unwrap_or_default());
7273
let data = PushNotification {
7374
notification_type: PushNotificationTypes::Support,
7475
data: serde_json::to_value(PushNotificationSupport {}).ok(),

core/crates/support/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ mod chatwoot;
22
mod client;
33
mod constants;
44
mod model;
5+
mod text;
56

67
pub use chatwoot::ChatwootClient;
78
pub use client::SupportClient;
89
pub use model::*;
10+
pub use text::markdown_plain_text;

core/crates/support/src/text.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub fn markdown_plain_text(message: &str) -> String {
2+
let message = message.replace("**", "");
3+
let mut result = String::with_capacity(message.len());
4+
5+
for word in message.split_whitespace() {
6+
if !result.is_empty() {
7+
result.push(' ');
8+
}
9+
result.push_str(word);
10+
}
11+
12+
result
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_markdown_plain_text() {
21+
assert_eq!(
22+
markdown_plain_text("Gem Wallet charges a **0.5% fee** on all swaps.\nFAQ: https://docs.gemwallet.com/faq/swap-fees"),
23+
"Gem Wallet charges a 0.5% fee on all swaps. FAQ: https://docs.gemwallet.com/faq/swap-fees"
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)