1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use adw::{prelude::BinExt, subclass::prelude::*};
use gtk::{glib, glib::clone, pango, prelude::*};
use matrix_sdk::ruma::events::room::message::FormattedBody;
use once_cell::sync::Lazy;
use ruma::{
    events::room::message::MessageFormat,
    html::{Html, ListBehavior, SanitizerConfig},
};

mod inline_html;
#[cfg(test)]
mod tests;
mod widgets;

use self::widgets::{new_message_label, widget_for_html_nodes, HtmlWidgetConfig};
use super::ContentFormat;
use crate::{
    components::{AtRoom, LabelWithWidgets},
    prelude::*,
    session::model::{Member, Room},
    utils::{
        string::{Linkifier, PangoStrMutExt},
        BoundObjectWeakRef, EMOJI_REGEX,
    },
};

mod imp {
    use std::cell::{Cell, RefCell};

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::MessageText)]
    pub struct MessageText {
        /// The original text of the message that is displayed.
        #[property(get)]
        pub original_text: RefCell<String>,
        /// Whether the original text is HTML.
        ///
        /// Only used for emotes.
        #[property(get)]
        pub is_html: Cell<bool>,
        /// The text format.
        #[property(get, builder(ContentFormat::default()))]
        pub format: Cell<ContentFormat>,
        /// Whether the message might contain an `@room` mention.
        pub detect_at_room: Cell<bool>,
        /// The sender of the message, if we need to listen to changes.
        pub sender: BoundObjectWeakRef<Member>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MessageText {
        const NAME: &'static str = "ContentMessageText";
        type Type = super::MessageText;
        type ParentType = adw::Bin;
    }

    #[glib::derived_properties]
    impl ObjectImpl for MessageText {}

    impl WidgetImpl for MessageText {}
    impl BinImpl for MessageText {}
}

glib::wrapper! {
    /// A widget displaying the content of a text message.
    // FIXME: We have to be able to allow text selection and override popover
    // menu. See https://gitlab.gnome.org/GNOME/gtk/-/issues/4606
    pub struct MessageText(ObjectSubclass<imp::MessageText>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

impl MessageText {
    /// Creates a text widget.
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Display the given plain text.
    pub fn with_plain_text(&self, body: String, format: ContentFormat) {
        if !self.original_text_changed(&body) && !self.format_changed(format) {
            return;
        }

        self.reset();
        self.set_format(format);

        let mut escaped_body = body.escape_markup();
        escaped_body.truncate_end_whitespaces();

        self.build_plain_text(escaped_body);
        self.set_original_text(body);
    }

    /// Display the given text with possible markup.
    ///
    /// It will detect if it should display the body or the formatted body.
    pub fn with_markup(
        &self,
        formatted: Option<FormattedBody>,
        body: String,
        room: &Room,
        format: ContentFormat,
        detect_at_room: bool,
    ) {
        self.set_detect_at_room(detect_at_room);

        if let Some(formatted) = formatted.filter(formatted_body_is_html).map(|f| f.body) {
            if !self.original_text_changed(&formatted) && !self.format_changed(format) {
                return;
            }

            self.reset();
            self.set_format(format);

            if self.build_html(&formatted, room, None).is_ok() {
                self.set_original_text(formatted);
                return;
            }
        }

        if !self.original_text_changed(&body) && !self.format_changed(format) {
            return;
        }

        self.reset();
        self.set_format(format);

        self.build_text(&body, room, None);
        self.set_original_text(body);
    }

    /// Display the given emote for `sender`.
    ///
    /// It will detect if it should display the body or the formatted body.
    pub fn with_emote(
        &self,
        formatted: Option<FormattedBody>,
        body: String,
        sender: Member,
        room: &Room,
        format: ContentFormat,
        detect_at_room: bool,
    ) {
        self.set_detect_at_room(detect_at_room);

        if let Some(formatted) = formatted.filter(formatted_body_is_html).map(|f| f.body) {
            if !self.original_text_changed(&body)
                && !self.format_changed(format)
                && !self.sender_changed(&sender)
            {
                return;
            }

            self.reset();
            self.set_format(format);

            let sender_name = sender.disambiguated_name();

            if self
                .build_html(&formatted, room, Some(&sender_name))
                .is_ok()
            {
                self.add_css_class("emote");
                self.set_is_html(true);
                self.set_original_text(formatted);

                let handler = sender.connect_disambiguated_name_notify(
                    clone!(@weak self as obj, @weak room => move |sender| {
                        obj.update_emote(&room, &sender.disambiguated_name());
                    }),
                );
                self.imp().sender.set(&sender, vec![handler]);

                return;
            }
        }

        if !self.original_text_changed(&body)
            && !self.format_changed(format)
            && !self.sender_changed(&sender)
        {
            return;
        }

        self.reset();
        self.set_format(format);
        self.add_css_class("emote");
        self.set_is_html(false);

        let sender_name = sender.disambiguated_name();
        self.build_text(&body, room, Some(&sender_name));
        self.set_original_text(body);

        let handler = sender.connect_disambiguated_name_notify(
            clone!(@weak self as obj, @weak room => move |sender| {
                obj.update_emote(&room, &sender.disambiguated_name());
            }),
        );
        self.imp().sender.set(&sender, vec![handler]);
    }

    fn update_emote(&self, room: &Room, sender_name: &str) {
        let text = self.original_text();

        if self.is_html() && self.build_html(&text, room, Some(sender_name)).is_ok() {
            return;
        }

        self.build_text(&text, room, Some(sender_name));
    }

    /// Build the message for the given plain text.
    ///
    /// The text must have been escaped and the end whitespaces removed before
    /// calling this method.
    fn build_plain_text(&self, mut text: String) {
        let child = if let Some(child) = self.child().and_downcast::<gtk::Label>() {
            child
        } else {
            let child = new_message_label();
            self.set_child(Some(&child));
            child
        };

        if EMOJI_REGEX.is_match(&text) {
            child.add_css_class("emoji");
        } else {
            child.remove_css_class("emoji");
        }

        let ellipsize = self.format() == ContentFormat::Ellipsized;
        if ellipsize {
            text.truncate_newline();
        }

        let ellipsize_mode = if ellipsize {
            pango::EllipsizeMode::End
        } else {
            pango::EllipsizeMode::None
        };
        child.set_ellipsize(ellipsize_mode);

        child.set_label(&text);
    }

    /// Build the message for the given text in the given room.
    ///
    /// We will try to detect URIs in the text.
    ///
    /// If `detect_at_room` is `true`, we will try to detect `@room` in the
    /// text.
    ///
    /// If `sender_name` is provided, it is added as a prefix. This is used for
    /// emotes.
    fn build_text(&self, text: &str, room: &Room, mut sender_name: Option<&str>) {
        let detect_at_room = self.detect_at_room();
        let mut result = String::with_capacity(text.len());

        result.maybe_append_emote_name(&mut sender_name);

        let mut pills = Vec::new();
        Linkifier::new(&mut result)
            .detect_mentions(room, &mut pills, detect_at_room)
            .linkify(text);

        result.truncate_end_whitespaces();

        if pills.is_empty() {
            self.build_plain_text(result);
            return;
        };

        let ellipsize = self.format() == ContentFormat::Ellipsized;
        pills.iter().for_each(|p| {
            if !p.source().is_some_and(|s| s.is::<AtRoom>()) {
                // Show the profile on click.
                p.set_activatable(true);
            }
        });

        let child = if let Some(child) = self.child().and_downcast::<LabelWithWidgets>() {
            child
        } else {
            let child = LabelWithWidgets::new();
            self.set_child(Some(&child));
            child
        };

        child.set_ellipsize(ellipsize);
        child.set_use_markup(true);
        child.set_label(Some(result));
        child.set_widgets(pills);
    }

    /// Build the message for the given HTML in the given room.
    ///
    /// We will try to detect URIs in the text.
    ///
    /// If `detect_at_room` is `true`, we will try to detect `@room` in the
    /// text.
    ///
    /// If `sender_name` is provided, it is added as a prefix. This is used for
    /// emotes.
    ///
    /// Returns an error if the HTML string doesn't contain any HTML.
    fn build_html(&self, html: &str, room: &Room, mut sender_name: Option<&str>) -> Result<(), ()> {
        let detect_at_room = self.detect_at_room();
        let ellipsize = self.format() == ContentFormat::Ellipsized;

        let mut html = Html::parse(html.trim_matches('\n'));
        html.sanitize_with(&HTML_MESSAGE_SANITIZER_CONFIG);

        if !html.has_children() {
            return Err(());
        }

        let Some(child) = widget_for_html_nodes(
            html.children(),
            HtmlWidgetConfig {
                room,
                detect_at_room,
                ellipsize,
            },
            false,
            &mut sender_name,
        ) else {
            return Err(());
        };

        self.set_child(Some(&child));

        Ok(())
    }

    /// Whether the given text is different than the current original text.
    fn original_text_changed(&self, text: &str) -> bool {
        *self.imp().original_text.borrow() != text
    }

    /// Set the original text of the message to display.
    fn set_original_text(&self, text: String) {
        self.imp().original_text.replace(text);
        self.notify_original_text();
    }

    /// Set whether the original text of the message is HTML.
    fn set_is_html(&self, is_html: bool) {
        if self.is_html() == is_html {
            return;
        }

        self.imp().is_html.set(is_html);
        self.notify_is_html();
    }

    /// Whether the given format is different than the current format.
    fn format_changed(&self, format: ContentFormat) -> bool {
        self.format() != format
    }

    /// Set the text format.
    fn set_format(&self, format: ContentFormat) {
        self.imp().format.set(format);
        self.notify_format();
    }

    /// Set whether the message might contain an `@room` mention.
    fn detect_at_room(&self) -> bool {
        self.imp().detect_at_room.get()
    }

    /// Set whether the message might contain an `@room` mention.
    fn set_detect_at_room(&self, detect_at_room: bool) {
        self.imp().detect_at_room.set(detect_at_room);
    }

    /// Whether the sender of the message changed.
    fn sender_changed(&self, sender: &Member) -> bool {
        self.imp().sender.obj().as_ref() == Some(sender)
    }

    /// Reset this `MessageText`.
    fn reset(&self) {
        self.imp().sender.disconnect_signals();
        self.remove_css_class("emote");
    }
}

impl Default for MessageText {
    fn default() -> Self {
        Self::new()
    }
}

/// Whether the given [`FormattedBody`] contains HTML.
fn formatted_body_is_html(formatted: &FormattedBody) -> bool {
    formatted.format == MessageFormat::Html && !formatted.body.contains("<!-- raw HTML omitted -->")
}

/// All supported inline elements from the Matrix spec.
const SUPPORTED_INLINE_ELEMENTS: &[&str] = &[
    "del", "a", "sup", "sub", "b", "i", "u", "strong", "em", "s", "code", "br", "span",
];

/// All supported block elements from the Matrix spec.
const SUPPORTED_BLOCK_ELEMENTS: &[&str] = &[
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "blockquote",
    "p",
    "ul",
    "ol",
    "li",
    "hr",
    "div",
    "pre",
    "details",
    "summary",
];

/// HTML sanitizer config for HTML messages.
static HTML_MESSAGE_SANITIZER_CONFIG: Lazy<SanitizerConfig> = Lazy::new(|| {
    SanitizerConfig::compat()
        .allow_elements(
            SUPPORTED_INLINE_ELEMENTS
                .iter()
                .chain(SUPPORTED_BLOCK_ELEMENTS.iter())
                .copied(),
            ListBehavior::Override,
        )
        .remove_reply_fallback()
});