[fractal/fractal-next] content: Parse the formatted body of emotes



commit 273080e6a3f7a4a6728a88d8b709fa178958536e
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Tue Nov 2 20:25:59 2021 +0100

    content: Parse the formatted body of emotes
    
    Fixes #852

 src/session/content/message_row.rs | 46 +++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 16 deletions(-)
---
diff --git a/src/session/content/message_row.rs b/src/session/content/message_row.rs
index bda10ad4..df164d5e 100644
--- a/src/session/content/message_row.rs
+++ b/src/session/content/message_row.rs
@@ -254,23 +254,34 @@ impl MessageRow {
                 match msgtype {
                     MessageType::Audio(_message) => {}
                     MessageType::Emote(message) => {
-                        let text = if let Some(formatted) = message
+                        // TODO we need to bind the display name to the sender
+                        if let Some(html_blocks) = message
                             .formatted
-                            .filter(|m| m.format == MessageFormat::Html)
+                            .filter(|formatted| is_valid_formatted_body(formatted))
+                            .and_then(|formatted| {
+                                let body = FormattedBody {
+                                    body: format!(
+                                        "<b>{}</b> {}",
+                                        event.sender().display_name(),
+                                        formatted.body
+                                    ),
+                                    format: MessageFormat::Html,
+                                };
+
+                                parse_formatted_body(Some(&body))
+                            })
                         {
-                            formatted.body
+                            self.show_html(html_blocks);
                         } else {
-                            message.body
-                        };
-                        // TODO we need to bind the display name to the sender
-                        self.show_text(
-                            &format!(
-                                "<b>{}</b> {}",
-                                event.sender().display_name(),
-                                linkify(&text)
-                            ),
-                            true,
-                        );
+                            self.show_text(
+                                &format!(
+                                    "<b>{}</b> {}",
+                                    event.sender().display_name(),
+                                    linkify(&message.body)
+                                ),
+                                true,
+                            );
+                        }
                     }
                     MessageType::File(_message) => {}
                     MessageType::Image(_message) => {}
@@ -351,10 +362,13 @@ fn linkify(text: &str) -> String {
     markup_links(&html_escape(text))
 }
 
+fn is_valid_formatted_body(formatted: &FormattedBody) -> bool {
+    formatted.format == MessageFormat::Html && !formatted.body.contains("<!-- raw HTML omitted -->")
+}
+
 fn parse_formatted_body(formatted: Option<&FormattedBody>) -> Option<Vec<HtmlBlock>> {
     formatted
-        .filter(|m| m.format == MessageFormat::Html)
-        .filter(|formatted| !formatted.body.contains("<!-- raw HTML omitted -->"))
+        .filter(|formatted| is_valid_formatted_body(formatted))
         .and_then(|formatted| markup_html(&formatted.body).ok())
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]