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::{
23    ReactionsByKeyBySender, TimelineDetails, event_item::content::other::OtherMessageLike,
24};
25
26#[derive(Clone, Debug)]
27pub enum MsgLikeKind {
28    /// An `m.room.message` event or extensible event, including edits.
29    Message(Message),
30
31    /// An `m.sticker` event.
32    Sticker(Sticker),
33
34    /// An `m.poll.start` event.
35    Poll(PollState),
36
37    /// A redacted message.
38    Redacted,
39
40    /// An `m.room.encrypted` event that could not be decrypted.
41    UnableToDecrypt(EncryptedMessage),
42
43    /// A custom message like event.
44    Other(OtherMessageLike),
45
46    /// A live location sharing session (MSC3489).
47    LiveLocation(LiveLocationState),
48}
49
50#[derive(Clone, Debug)]
51pub struct ThreadSummary {
52    pub latest_event: TimelineDetails<Box<EmbeddedEvent>>,
53
54    /// The number of events in the thread, except for the thread root.
55    ///
56    /// This can be zero if all the events in the thread have been redacted.
57    ///
58    /// Note: this doesn't interact with the timeline filter; so opening a
59    /// thread-focused timeline with the same timeline filter may result in
60    /// *fewer* events than this number.
61    pub num_replies: u32,
62}
63
64/// A special kind of [`super::TimelineItemContent`] that groups together
65/// different room message types with their respective reactions and thread
66/// information.
67#[derive(Clone, Debug)]
68pub struct MsgLikeContent {
69    pub kind: MsgLikeKind,
70    pub reactions: ReactionsByKeyBySender,
71    /// The event this message is replying to, if any.
72    pub in_reply_to: Option<InReplyToDetails>,
73    /// Event ID of the thread root, if this is a message in a thread.
74    pub thread_root: Option<OwnedEventId>,
75    /// Information about the thread this message is the root of, if any.
76    pub thread_summary: Option<ThreadSummary>,
77}
78
79impl MsgLikeContent {
80    #[cfg(not(tarpaulin_include))] // debug-logging functionality
81    pub(crate) fn debug_string(&self) -> &'static str {
82        match self.kind {
83            MsgLikeKind::Message(_) => "a message",
84            MsgLikeKind::Sticker(_) => "a sticker",
85            MsgLikeKind::Poll(_) => "a poll",
86            MsgLikeKind::Redacted => "a redacted message",
87            MsgLikeKind::UnableToDecrypt(_) => "an encrypted message we couldn't decrypt",
88            MsgLikeKind::Other(_) => "a custom message-like event",
89            MsgLikeKind::LiveLocation(_) => "a live location share",
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}