matrix-ui-serializable 0.4.0

Opinionated abstraction of the matrix-sdk crate with serializable structs
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use std::borrow::Cow;

use matrix_sdk::ruma::events::{
    StateEventContentChange,
    room::{
        guest_access::GuestAccess,
        history_visibility::HistoryVisibility,
        join_rules::JoinRule,
        message::{MessageFormat, MessageType},
    },
};
use matrix_sdk_ui::timeline::{
    AnyOtherStateEventContentChange, EventTimelineItem, MembershipChange, MsgLikeKind,
    RoomMembershipChange, TimelineItemContent,
};

use crate::utils::{get_or_fetch_event_sender, trim_start_html_whitespace};

/// What should be displayed before the text preview of an event.
pub enum BeforeText {
    /// Nothing should be displayed before the text preview.
    Nothing,
    /// The sender's username with a colon should be displayed before the text preview.
    UsernameWithColon,
    /// The sender's username (without a colon) should be displayed before the text preview.
    UsernameWithoutColon,
}

/// A text preview of a timeline event, plus how a username should be displayed before it.
///
/// Call [`TextPreview::format_with()`] to generate displayable text
/// with the appropriately-formatted preceding username.
pub struct TextPreview {
    text: String,
    before_text: BeforeText,
}
impl From<(String, BeforeText)> for TextPreview {
    fn from((text, before_text): (String, BeforeText)) -> Self {
        Self { text, before_text }
    }
}
impl TextPreview {
    /// Formats the text preview with the appropriate preceding username.
    pub fn format_with(self, username: &str, as_html: bool) -> String {
        let Self { text, before_text } = self;
        match before_text {
            BeforeText::Nothing => text,
            BeforeText::UsernameWithColon => format!(
                "<b>{}</b>: {}",
                if as_html {
                    htmlize::escape_text(username)
                } else {
                    username.into()
                },
                text,
            ),
            BeforeText::UsernameWithoutColon => format!(
                "{} {}",
                if as_html {
                    htmlize::escape_text(username)
                } else {
                    username.into()
                },
                text,
            ),
        }
    }
}

/// Returns a text preview of the given timeline event as an Html-formatted string.
pub fn text_preview_of_timeline_item(
    content: &TimelineItemContent,
    sender_username: &str,
) -> TextPreview {
    match content {
        TimelineItemContent::MsgLike(m) => {
            let message = m.clone();
            match message.kind {
                MsgLikeKind::Message(a) => text_preview_of_message(&a, sender_username),
                MsgLikeKind::Sticker(sticker) => TextPreview::from((
                    format!(
                        "[Sticker]: <i>{}</i>",
                        htmlize::escape_text(&sticker.content().body)
                    ),
                    BeforeText::UsernameWithColon,
                )),
                MsgLikeKind::Poll(poll_state) => TextPreview::from((
                    format!(
                        "[Poll]: {}",
                        htmlize::escape_text(
                            poll_state
                                .fallback_text()
                                .unwrap_or_else(|| poll_state.results().question)
                        ),
                    ),
                    BeforeText::UsernameWithColon,
                )),
                MsgLikeKind::Redacted => TextPreview::from((
                    String::from("[Message was deleted]"),
                    BeforeText::UsernameWithColon,
                )),
                MsgLikeKind::LiveLocation(_) => TextPreview::from((
                    String::from("[Live Location]"),
                    BeforeText::UsernameWithColon,
                )),
                MsgLikeKind::UnableToDecrypt(_encrypted_message) => TextPreview::from((
                    String::from("[Unable to decrypt message]"),
                    BeforeText::UsernameWithColon,
                )),
                MsgLikeKind::Other(kind) => TextPreview::from((
                    format!("[Unsupported event: {} ]", kind.event_type()),
                    BeforeText::UsernameWithColon,
                )),
            }
        }
        TimelineItemContent::MembershipChange(membership_change) => {
            text_preview_of_room_membership_change(membership_change, true).unwrap_or_else(|| {
                TextPreview::from((
                    String::from("<i>underwent a membership change</i>"),
                    BeforeText::UsernameWithoutColon,
                ))
            })
        }
        TimelineItemContent::ProfileChange(profile_change) => {
            text_preview_of_member_profile_change(profile_change, sender_username, true)
        }
        TimelineItemContent::OtherState(other_state) => {
            text_preview_of_other_state(other_state, true).unwrap_or_else(|| {
                TextPreview::from((
                    String::from("<i>initiated another state change</i>"),
                    BeforeText::UsernameWithoutColon,
                ))
            })
        }
        TimelineItemContent::FailedToParseMessageLike { event_type, .. } => TextPreview::from((
            format!("[Failed to parse <i>{}</i> message]", event_type),
            BeforeText::UsernameWithColon,
        )),
        TimelineItemContent::FailedToParseState { event_type, .. } => TextPreview::from((
            format!("[Failed to parse <i>{}</i> state]", event_type),
            BeforeText::UsernameWithColon,
        )),
        TimelineItemContent::CallInvite => TextPreview::from((
            String::from("[Call Invitation]"),
            BeforeText::UsernameWithColon,
        )),
        TimelineItemContent::RtcNotification { .. } => TextPreview::from((
            String::from("[Call Notification]"),
            BeforeText::UsernameWithColon,
        )),
    }
}

/// Returns the plaintext `body` of the given timeline event.
pub fn _plaintext_body_of_timeline_item(event_tl_item: &EventTimelineItem) -> String {
    match event_tl_item.content() {
        TimelineItemContent::MsgLike(m) => {
            let message = m.clone();
            match message.kind {
                MsgLikeKind::Message(msg) => msg.body().into(),
                MsgLikeKind::Redacted => "[Message was deleted]".into(),
                MsgLikeKind::Sticker(sticker) => sticker.content().body.clone(),
                MsgLikeKind::UnableToDecrypt(_encrypted_msg) => "[Unable to Decrypt]".into(),
                MsgLikeKind::Poll(poll_state) => {
                    format!(
                        "[Poll]: {}",
                        poll_state
                            .fallback_text()
                            .unwrap_or_else(|| poll_state.results().question)
                    )
                }
                MsgLikeKind::LiveLocation(_) => String::from("[Live location]"),
                MsgLikeKind::Other(kind) => kind.event_type().to_string(),
            }
        }
        TimelineItemContent::MembershipChange(membership_change) => {
            text_preview_of_room_membership_change(membership_change, false)
                .unwrap_or_else(|| {
                    TextPreview::from((
                        String::from("underwent a membership change."),
                        BeforeText::UsernameWithoutColon,
                    ))
                })
                .format_with(&get_or_fetch_event_sender(event_tl_item, None), false)
        }
        TimelineItemContent::ProfileChange(profile_change) => {
            text_preview_of_member_profile_change(
                profile_change,
                &get_or_fetch_event_sender(event_tl_item, None),
                false,
            )
            .text
        }
        TimelineItemContent::OtherState(other_state) => {
            text_preview_of_other_state(other_state, false)
                .unwrap_or_else(|| {
                    TextPreview::from((
                        String::from("initiated another state change."),
                        BeforeText::UsernameWithoutColon,
                    ))
                })
                .format_with(&get_or_fetch_event_sender(event_tl_item, None), false)
        }
        TimelineItemContent::FailedToParseMessageLike { event_type, error } => {
            format!("Failed to parse {} message. Error: {}", event_type, error)
        }
        TimelineItemContent::FailedToParseState {
            event_type,
            error,
            state_key,
        } => {
            format!(
                "Failed to parse {} state; key: {}. Error: {}",
                event_type, state_key, error
            )
        }
        TimelineItemContent::CallInvite => String::from("[Call Invitation]"),
        TimelineItemContent::RtcNotification { .. } => String::from("[Call Notification]"),
    }
}

/// Returns a text preview of the given message as an Html-formatted string.
pub fn text_preview_of_message(
    message: &matrix_sdk_ui::timeline::Message,
    sender_username: &str,
) -> TextPreview {
    let text = match message.msgtype() {
        MessageType::Audio(audio) => format!(
            "[Audio]: <i>{}</i>",
            if let Some(formatted_body) = audio.formatted.as_ref() {
                Cow::Borrowed(formatted_body.body.as_str())
            } else {
                htmlize::escape_text(audio.body.as_str())
            }
        ),
        MessageType::Emote(emote) => format!(
            "* {} {}",
            sender_username,
            if let Some(formatted_body) = emote.formatted.as_ref() {
                Cow::Borrowed(formatted_body.body.as_str())
            } else {
                htmlize::escape_text(emote.body.as_str())
            }
        ),
        MessageType::File(file) => format!(
            "[File]: <i>{}</i>",
            if let Some(formatted_body) = file.formatted.as_ref() {
                Cow::Borrowed(formatted_body.body.as_str())
            } else {
                htmlize::escape_text(file.body.as_str())
            }
        ),
        MessageType::Image(image) => format!(
            "[Image]: <i>{}</i>",
            if let Some(formatted_body) = image.formatted.as_ref() {
                Cow::Borrowed(formatted_body.body.as_str())
            } else {
                htmlize::escape_text(image.body.as_str())
            }
        ),
        MessageType::Location(location) => format!(
            "[Location]: <i>{}</i>",
            htmlize::escape_text(&location.body),
        ),
        MessageType::Notice(notice) => format!(
            "<i>{}</i>",
            if let Some(formatted_body) = notice.formatted.as_ref() {
                trim_start_html_whitespace(&formatted_body.body).into()
            } else {
                htmlize::escape_text(notice.body.as_str())
            }
        ),
        MessageType::ServerNotice(notice) => format!(
            "[Server Notice]: <i>{} -- {}</i>",
            notice.server_notice_type.as_str(),
            notice.body,
        ),
        MessageType::Text(text) => text
            .formatted
            .as_ref()
            .and_then(|fb| {
                (fb.format == MessageFormat::Html).then(|| {
                    crate::utils::linkify(trim_start_html_whitespace(&fb.body), true).to_string()
                })
            })
            .unwrap_or_else(|| match crate::utils::linkify(&text.body, false) {
                Cow::Borrowed(plaintext) => htmlize::escape_text(plaintext).to_string(),
                Cow::Owned(linkified) => linkified,
            }),
        MessageType::VerificationRequest(verification) => {
            format!("[Verification Request] <i>to user {}</i>", verification.to,)
        }
        MessageType::Video(video) => format!(
            "[Video]: <i>{}</i>",
            if let Some(formatted_body) = video.formatted.as_ref() {
                Cow::Borrowed(formatted_body.body.as_str())
            } else {
                htmlize::escape_text(&video.body)
            }
        ),
        MessageType::_Custom(custom) => format!("[Custom message]: {:?}", custom,),
        other => format!(
            "[Unknown message type]: {}",
            htmlize::escape_text(other.body()),
        ),
    };
    TextPreview::from((text, BeforeText::UsernameWithColon))
}

/// Returns a text preview of the given other state event as an Html-formatted string.
pub fn text_preview_of_other_state(
    other_state: &matrix_sdk_ui::timeline::OtherState,
    format_as_html: bool,
) -> Option<TextPreview> {
    let text = match other_state.content() {
        AnyOtherStateEventContentChange::RoomAvatar(_) => {
            Some(String::from("set this room's avatar picture."))
        }
        AnyOtherStateEventContentChange::RoomCanonicalAlias(
            StateEventContentChange::Original { content, .. },
        ) => Some(format!(
            "set the main address of this room to {}.",
            content.alias.as_ref().map(|a| a.as_str()).unwrap_or("none")
        )),
        AnyOtherStateEventContentChange::RoomCreate(StateEventContentChange::Original {
            content,
            ..
        }) => Some(format!(
            "created this room (v{}).",
            content.room_version.as_str()
        )),
        AnyOtherStateEventContentChange::RoomEncryption(_) => {
            Some(String::from("enabled encryption in this room."))
        }
        AnyOtherStateEventContentChange::RoomGuestAccess(StateEventContentChange::Original {
            content,
            ..
        }) => Some(match &content.guest_access {
            GuestAccess::CanJoin => String::from("has allowed guests to join this room."),
            GuestAccess::Forbidden => String::from("has forbidden guests from joining this room."),
            custom => format!(
                "has set custom guest access rules for this room: {}",
                custom.as_str()
            ),
        }),
        AnyOtherStateEventContentChange::RoomHistoryVisibility(
            StateEventContentChange::Original { content, .. },
        ) => Some(format!(
            "set this room's history to be visible by {}",
            match &content.history_visibility {
                HistoryVisibility::Invited => "invited users, since they were invited.",
                HistoryVisibility::Joined => "joined users, since they joined.",
                HistoryVisibility::Shared => "joined users, for all of time.",
                HistoryVisibility::WorldReadable => "anyone for all time.",
                custom => custom.as_str(),
            },
        )),
        AnyOtherStateEventContentChange::RoomJoinRules(StateEventContentChange::Original {
            content,
            ..
        }) => Some(match &content.join_rule {
            JoinRule::Public => String::from("set this room to be joinable by anyone."),
            JoinRule::Knock => {
                String::from("set this room to be joinable by invite only or by request.")
            }
            JoinRule::Private => String::from("set this room to be private."),
            JoinRule::Restricted(_) => {
                String::from("set this room to be joinable by invite only or with restrictions.")
            }
            JoinRule::KnockRestricted(_) => String::from(
                "set this room to be joinable by invite only or requestable with restrictions.",
            ),
            JoinRule::Invite => String::from("set this room to be joinable by invite only."),
            custom => format!("set custom join rules for this room: {}", custom.as_str()),
        }),
        AnyOtherStateEventContentChange::RoomPinnedEvents(StateEventContentChange::Original {
            content,
            ..
        }) => Some(format!(
            "pinned {} events in this room.",
            content.pinned.len()
        )),
        AnyOtherStateEventContentChange::RoomName(StateEventContentChange::Original {
            content,
            ..
        }) => {
            let name = if format_as_html {
                htmlize::escape_text(&content.name)
            } else {
                Cow::Borrowed(content.name.as_str())
            };
            Some(format!("changed this room's name to \"{name}\"."))
        }
        AnyOtherStateEventContentChange::RoomPowerLevels(_) => {
            Some(String::from("set the power levels for this room."))
        }
        AnyOtherStateEventContentChange::RoomServerAcl(_) => Some(String::from(
            "set the server access control list for this room.",
        )),
        AnyOtherStateEventContentChange::RoomTombstone(StateEventContentChange::Original {
            content,
            ..
        }) => Some(format!(
            "closed this room and upgraded it to {}",
            content.replacement_room.matrix_to_uri()
        )),
        AnyOtherStateEventContentChange::RoomTopic(StateEventContentChange::Original {
            content,
            ..
        }) => {
            let topic = if format_as_html {
                htmlize::escape_text(&content.topic)
            } else {
                Cow::Borrowed(content.topic.as_str())
            };
            Some(format!("changed this room's topic to \"{topic}\"."))
        }
        AnyOtherStateEventContentChange::SpaceParent(_) => {
            let state_key = if format_as_html {
                htmlize::escape_text(other_state.state_key())
            } else {
                Cow::Borrowed(other_state.state_key())
            };
            Some(format!("set this room's parent space to \"{state_key}\"."))
        }
        AnyOtherStateEventContentChange::SpaceChild(_) => {
            let state_key = if format_as_html {
                htmlize::escape_text(other_state.state_key())
            } else {
                Cow::Borrowed(other_state.state_key())
            };
            Some(format!("added a new child to this space: \"{state_key}\"."))
        }
        _other => {
            // log!("*** Unhandled: {:?}.", _other);
            None
        }
    };
    text.map(|t| TextPreview::from((t, BeforeText::UsernameWithoutColon)))
}

/// Returns a text preview of the given member profile change
/// as a plaintext or HTML-formatted string.
pub fn text_preview_of_member_profile_change(
    change: &matrix_sdk_ui::timeline::MemberProfileChange,
    username: &str,
    format_as_html: bool,
) -> TextPreview {
    let name_text = if let Some(name_change) = change.displayname_change() {
        let old = name_change.old.as_deref().unwrap_or(username);
        let old_un = if format_as_html {
            htmlize::escape_text(old)
        } else {
            old.into()
        };
        if let Some(new) = name_change.new.as_ref() {
            let new_un = if format_as_html {
                htmlize::escape_text(new)
            } else {
                new.into()
            };
            format!("{old_un} changed their display name to \"{new_un}\"")
        } else {
            format!("{old_un} removed their display name")
        }
    } else {
        String::new()
    };
    let avatar_text = if let Some(_avatar_change) = change.avatar_url_change() {
        if name_text.is_empty() {
            let un = if format_as_html {
                htmlize::escape_text(username)
            } else {
                username.into()
            };
            format!("{un} changed their profile picture")
        } else {
            String::from(" and changed their profile picture")
        }
    } else {
        String::new()
    };

    TextPreview::from((
        format!("{}{}.", name_text, avatar_text),
        BeforeText::Nothing,
    ))
}

/// Returns a text preview of the given room membership change
/// as a plaintext or HTML-formatted string.
pub fn text_preview_of_room_membership_change(
    change: &RoomMembershipChange,
    format_as_html: bool,
) -> Option<TextPreview> {
    let dn = change.display_name();
    let change_user_id = dn.as_deref().unwrap_or_else(|| change.user_id().as_str());
    let change_user_id = if format_as_html {
        htmlize::escape_text(change_user_id)
    } else {
        change_user_id.into()
    };
    let text = match change.change() {
        None
        | Some(MembershipChange::NotImplemented)
        | Some(MembershipChange::None)
        | Some(MembershipChange::Error) => {
            // Don't actually display anything for nonexistent/unimportant membership changes.
            return None;
        }
        Some(MembershipChange::Joined) => String::from("joined this room."),
        Some(MembershipChange::Left) => String::from("left this room."),
        Some(MembershipChange::Banned) => format!("banned {} from this room.", change_user_id),
        Some(MembershipChange::Unbanned) => format!("unbanned {} from this room.", change_user_id),
        Some(MembershipChange::Kicked) => format!("kicked {} from this room.", change_user_id),
        Some(MembershipChange::Invited) => format!("invited {} to this room.", change_user_id),
        Some(MembershipChange::KickedAndBanned) => {
            format!("kicked and banned {} from this room.", change_user_id)
        }
        Some(MembershipChange::InvitationAccepted) => {
            String::from("accepted an invitation to this room.")
        }
        Some(MembershipChange::InvitationRejected) => {
            String::from("rejected an invitation to this room.")
        }
        Some(MembershipChange::InvitationRevoked) => {
            format!("revoked {}'s invitation to this room.", change_user_id)
        }
        Some(MembershipChange::Knocked) => String::from("requested to join this room."),
        Some(MembershipChange::KnockAccepted) => {
            format!("accepted {}'s request to join this room.", change_user_id)
        }
        Some(MembershipChange::KnockRetracted) => {
            String::from("retracted their request to join this room.")
        }
        Some(MembershipChange::KnockDenied) => {
            format!("denied {}'s request to join this room.", change_user_id)
        }
    };
    Some(TextPreview::from((text, BeforeText::UsernameWithoutColon)))
}