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    /// The user's own public read receipt event id, for this particular thread.
58    pub public_read_receipt_event_id: Option<OwnedEventId>,
59
60    /// The user's own private read receipt event id, for this particular
61    /// thread.
62    pub private_read_receipt_event_id: Option<OwnedEventId>,
63}
64
65/// A special kind of [`super::TimelineItemContent`] that groups together
66/// different room message types with their respective reactions and thread
67/// information.
68#[derive(Clone, Debug)]
69pub struct MsgLikeContent {
70    pub kind: MsgLikeKind,
71    pub reactions: ReactionsByKeyBySender,
72    /// The event this message is replying to, if any.
73    pub in_reply_to: Option<InReplyToDetails>,
74    /// Event ID of the thread root, if this is a message in a thread.
75    pub thread_root: Option<OwnedEventId>,
76    /// Information about the thread this message is the root of, if any.
77    pub thread_summary: Option<ThreadSummary>,
78}
79
80impl MsgLikeContent {
81    #[cfg(not(tarpaulin_include))] // debug-logging functionality
82    pub(crate) fn debug_string(&self) -> &'static str {
83        match self.kind {
84            MsgLikeKind::Message(_) => "a message",
85            MsgLikeKind::Sticker(_) => "a sticker",
86            MsgLikeKind::Poll(_) => "a poll",
87            MsgLikeKind::Redacted => "a redacted message",
88            MsgLikeKind::UnableToDecrypt(_) => "an encrypted message we couldn't decrypt",
89            MsgLikeKind::Other(_) => "a custom message-like event",
90        }
91    }
92
93    pub fn redacted() -> Self {
94        Self {
95            kind: MsgLikeKind::Redacted,
96            reactions: Default::default(),
97            thread_root: None,
98            in_reply_to: None,
99            thread_summary: None,
100        }
101    }
102
103    pub fn unable_to_decrypt(encrypted_message: EncryptedMessage) -> Self {
104        Self {
105            kind: MsgLikeKind::UnableToDecrypt(encrypted_message),
106            reactions: Default::default(),
107            thread_root: None,
108            in_reply_to: None,
109            thread_summary: None,
110        }
111    }
112
113    /// Whether this item is part of a thread.
114    pub fn is_threaded(&self) -> bool {
115        self.thread_root.is_some()
116    }
117
118    pub fn with_in_reply_to(&self, in_reply_to: InReplyToDetails) -> Self {
119        Self { in_reply_to: Some(in_reply_to), ..self.clone() }
120    }
121
122    pub fn with_kind(&self, kind: MsgLikeKind) -> Self {
123        Self { kind, ..self.clone() }
124    }
125
126    /// If `kind` is of the [`MsgLikeKind`][MsgLikeKind::Message] variant,
127    /// return the inner [`Message`].
128    pub fn as_message(&self) -> Option<Message> {
129        as_variant!(&self.kind, MsgLikeKind::Message(message) => message.clone())
130    }
131}