matrix_sdk_ui/timeline/event_item/content/
msg_like.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use as_variant::as_variant;
16use ruma::OwnedEventId;
17
18use super::{EmbeddedEvent, EncryptedMessage, InReplyToDetails, Message, PollState, Sticker};
19use crate::timeline::{
20    ReactionsByKeyBySender, TimelineDetails, event_item::content::other::OtherMessageLike,
21};
22
23#[derive(Clone, Debug)]
24pub enum MsgLikeKind {
25    /// An `m.room.message` event or extensible event, including edits.
26    Message(Message),
27
28    /// An `m.sticker` event.
29    Sticker(Sticker),
30
31    /// An `m.poll.start` event.
32    Poll(PollState),
33
34    /// A redacted message.
35    Redacted,
36
37    /// An `m.room.encrypted` event that could not be decrypted.
38    UnableToDecrypt(EncryptedMessage),
39
40    /// A custom message like event.
41    Other(OtherMessageLike),
42}
43
44#[derive(Clone, Debug)]
45pub struct ThreadSummary {
46    pub latest_event: TimelineDetails<Box<EmbeddedEvent>>,
47
48    /// The number of events in the thread, except for the thread root.
49    ///
50    /// This can be zero if all the events in the thread have been redacted.
51    ///
52    /// Note: this doesn't interact with the timeline filter; so opening a
53    /// thread-focused timeline with the same timeline filter may result in
54    /// *fewer* events than this number.
55    pub num_replies: u32,
56}
57
58/// A special kind of [`super::TimelineItemContent`] that groups together
59/// different room message types with their respective reactions and thread
60/// information.
61#[derive(Clone, Debug)]
62pub struct MsgLikeContent {
63    pub kind: MsgLikeKind,
64    pub reactions: ReactionsByKeyBySender,
65    /// The event this message is replying to, if any.
66    pub in_reply_to: Option<InReplyToDetails>,
67    /// Event ID of the thread root, if this is a message in a thread.
68    pub thread_root: Option<OwnedEventId>,
69    /// Information about the thread this message is the root of, if any.
70    pub thread_summary: Option<ThreadSummary>,
71}
72
73impl MsgLikeContent {
74    #[cfg(not(tarpaulin_include))] // debug-logging functionality
75    pub(crate) fn debug_string(&self) -> &'static str {
76        match self.kind {
77            MsgLikeKind::Message(_) => "a message",
78            MsgLikeKind::Sticker(_) => "a sticker",
79            MsgLikeKind::Poll(_) => "a poll",
80            MsgLikeKind::Redacted => "a redacted message",
81            MsgLikeKind::UnableToDecrypt(_) => "an encrypted message we couldn't decrypt",
82            MsgLikeKind::Other(_) => "a custom message-like event",
83        }
84    }
85
86    pub fn redacted() -> Self {
87        Self {
88            kind: MsgLikeKind::Redacted,
89            reactions: Default::default(),
90            thread_root: None,
91            in_reply_to: None,
92            thread_summary: None,
93        }
94    }
95
96    pub fn unable_to_decrypt(encrypted_message: EncryptedMessage) -> Self {
97        Self {
98            kind: MsgLikeKind::UnableToDecrypt(encrypted_message),
99            reactions: Default::default(),
100            thread_root: None,
101            in_reply_to: None,
102            thread_summary: None,
103        }
104    }
105
106    /// Whether this item is part of a thread.
107    pub fn is_threaded(&self) -> bool {
108        self.thread_root.is_some()
109    }
110
111    pub fn with_in_reply_to(&self, in_reply_to: InReplyToDetails) -> Self {
112        Self { in_reply_to: Some(in_reply_to), ..self.clone() }
113    }
114
115    pub fn with_kind(&self, kind: MsgLikeKind) -> Self {
116        Self { kind, ..self.clone() }
117    }
118
119    /// If `kind` is of the [`MsgLikeKind`][MsgLikeKind::Message] variant,
120    /// return the inner [`Message`].
121    pub fn as_message(&self) -> Option<Message> {
122        as_variant!(&self.kind, MsgLikeKind::Message(message) => message.clone())
123    }
124}