Skip to main content

iris_chat_core/
forwarding.rs

1//! Forwarded content stays readable in older clients and notification previews.
2//! This is a sender-provided heading, not verified original-author attribution.
3
4const FORWARDED_HEADING: &str = "Forwarded:\n\n";
5
6/// Mark a forward without including the source chat, author or message ID.
7/// Keep the heading in the encrypted body so every renderer can display it.
8#[uniffi::export]
9pub fn format_forwarded_message(text: String) -> String {
10    let text = crate::display_message_mentions(text);
11    let text = text.trim();
12    // Attachment extraction trims an attachment-only forward to "Forwarded:";
13    // native forward pickers append its attachment links on the next line.
14    if text.is_empty() || text == "Forwarded:" || text.starts_with("Forwarded:\n") {
15        return text.to_string();
16    }
17    format!("{FORWARDED_HEADING}{text}")
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn forwarding_keeps_content_and_marks_it_once() {
26        for body in [
27            "Hello",
28            "A caption\nhtree://nhash1example/photo.jpg",
29            "😀",
30            "Line one\n\nLine two",
31        ] {
32            let forwarded = format_forwarded_message(body.into());
33            assert_eq!(forwarded, format!("Forwarded:\n\n{body}"));
34            assert_eq!(format_forwarded_message(forwarded.clone()), forwarded);
35        }
36        assert_eq!(format_forwarded_message(" \n ".into()), "");
37        assert_eq!(
38            format_forwarded_message("Forwarded:\nhtree://nhash1example/photo.jpg".into()),
39            "Forwarded:\nhtree://nhash1example/photo.jpg"
40        );
41        assert_eq!(
42            format_forwarded_message("  Hello\n".into()),
43            "Forwarded:\n\nHello"
44        );
45    }
46}