matrix_sdk_ui/timeline/event_item/
local.rs

1// Copyright 2023 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 std::sync::Arc;
16
17use as_variant::as_variant;
18use matrix_sdk::{send_queue::SendHandle, Error};
19use ruma::{EventId, OwnedEventId, OwnedTransactionId};
20
21use super::TimelineEventItemId;
22
23/// An item for an event that was created locally and not yet echoed back by
24/// the homeserver.
25#[derive(Debug, Clone)]
26pub(in crate::timeline) struct LocalEventTimelineItem {
27    /// The send state of this local event.
28    pub send_state: EventSendState,
29    /// The transaction ID.
30    pub transaction_id: OwnedTransactionId,
31    /// A handle to manipulate this event before it is sent, if possible.
32    pub send_handle: Option<SendHandle>,
33}
34
35impl LocalEventTimelineItem {
36    /// Get the unique identifier of this item.
37    ///
38    /// Returns the transaction ID for a local echo item that has not been sent
39    /// and the event ID for a local echo item that has been sent.
40    pub(crate) fn identifier(&self) -> TimelineEventItemId {
41        if let Some(event_id) =
42            as_variant!(&self.send_state, EventSendState::Sent { event_id } => event_id)
43        {
44            TimelineEventItemId::EventId(event_id.clone())
45        } else {
46            TimelineEventItemId::TransactionId(self.transaction_id.clone())
47        }
48    }
49
50    /// Get the event ID of this item.
51    ///
52    /// Will be `Some` if and only if `send_state` is
53    /// `EventSendState::Sent`.
54    pub fn event_id(&self) -> Option<&EventId> {
55        as_variant!(&self.send_state, EventSendState::Sent { event_id } => event_id)
56    }
57
58    /// Clone the current event item, and update its `send_state`.
59    pub fn with_send_state(&self, send_state: EventSendState) -> Self {
60        Self { send_state, ..self.clone() }
61    }
62}
63
64/// This type represents the "send state" of a local event timeline item.
65#[derive(Clone, Debug)]
66pub enum EventSendState {
67    /// The local event has not been sent yet.
68    NotSentYet,
69    /// The local event has been sent to the server, but unsuccessfully: The
70    /// sending has failed.
71    SendingFailed {
72        /// Details about how sending the event failed.
73        error: Arc<Error>,
74        /// Whether the error is considered recoverable or not.
75        ///
76        /// An error that's recoverable will disable the room's send queue,
77        /// while an unrecoverable error will be parked, until the user
78        /// decides to cancel sending it.
79        is_recoverable: bool,
80    },
81    /// The local event has been sent successfully to the server.
82    Sent {
83        /// The event ID assigned by the server.
84        event_id: OwnedEventId,
85    },
86}