Skip to main content

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::{
19    EmbeddedEvent, EncryptedMessage, InReplyToDetails, LiveLocationState, Message, PollState,
20    Sticker,
21};
22use crate::timeline::{TimelineDetails, event_item::content::other::OtherMessageLike};
23
24#[derive(Clone, Debug)]
25pub enum MsgLikeKind {
26    /// An `m.room.message` event or extensible event, including edits.
27    Message(Message),
28
29    /// An `m.sticker` event.
30    Sticker(Sticker),
31
32    /// An `m.poll.start` event.
33    Poll(PollState),
34
35    /// A redacted message.
36    Redacted,
37
38    /// An `m.room.encrypted` event that could not be decrypted.
39    UnableToDecrypt(EncryptedMessage),
40
41    /// A custom message like event.
42    Other(OtherMessageLike),
43
44    /// A live location sharing session (MSC3489).
45    LiveLocation(LiveLocationState),
46}
47
48#[derive(Clone, Debug)]
49pub struct ThreadSummary {
50    pub latest_event: TimelineDetails<Box<EmbeddedEvent>>,
51
52    /// The number of events in the thread, except for the thread root.
53    ///
54    /// This can be zero if all the events in the thread have been redacted.
55    ///
56    /// Note: this doesn't interact with the timeline filter; so opening a
57    /// thread-focused timeline with the same timeline filter may result in
58    /// _fewer_ events than this number.
59    pub num_replies: u32,
60}
61
62/// A special kind of [`super::TimelineItemContent`] that groups together
63/// different room message types with their thread information.
64#[derive(Clone, Debug)]
65pub struct MsgLikeContent {
66    pub kind: MsgLikeKind,
67    /// The event this message is replying to, if any.
68    pub in_reply_to: Option<InReplyToDetails>,
69    /// Event ID of the thread root, if this is a message in a thread.
70    pub thread_root: Option<OwnedEventId>,
71    /// Information about the thread this message is the root of, if any.
72    pub thread_summary: Option<ThreadSummary>,
73}
74
75impl MsgLikeContent {
76    #[cfg(not(tarpaulin_include))] // debug-logging functionality
77    pub(crate) fn debug_string(&self) -> &'static str {
78        match self.kind {
79            MsgLikeKind::Message(_) => "a message",
80            MsgLikeKind::Sticker(_) => "a sticker",
81            MsgLikeKind::Poll(_) => "a poll",
82            MsgLikeKind::Redacted => "a redacted message",
83            MsgLikeKind::UnableToDecrypt(_) => "an encrypted message we couldn't decrypt",
84            MsgLikeKind::Other(_) => "a custom message-like event",
85            MsgLikeKind::LiveLocation(_) => "a live location share",
86        }
87    }
88
89    pub fn redacted() -> Self {
90        Self {
91            kind: MsgLikeKind::Redacted,
92            thread_root: None,
93            in_reply_to: None,
94            thread_summary: None,
95        }
96    }
97
98    pub fn unable_to_decrypt(encrypted_message: EncryptedMessage) -> Self {
99        Self {
100            kind: MsgLikeKind::UnableToDecrypt(encrypted_message),
101            thread_root: None,
102            in_reply_to: None,
103            thread_summary: None,
104        }
105    }
106
107    /// Whether this item is part of a thread.
108    pub fn is_threaded(&self) -> bool {
109        self.thread_root.is_some()
110    }
111
112    pub fn with_in_reply_to(&self, in_reply_to: InReplyToDetails) -> Self {
113        Self { in_reply_to: Some(in_reply_to), ..self.clone() }
114    }
115
116    pub fn with_kind(&self, kind: MsgLikeKind) -> Self {
117        Self { kind, ..self.clone() }
118    }
119
120    /// If `kind` is of the [`MsgLikeKind`][MsgLikeKind::Message] variant,
121    /// return the inner [`Message`].
122    pub fn as_message(&self) -> Option<Message> {
123        as_variant!(&self.kind, MsgLikeKind::Message(message) => message.clone())
124    }
125}