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::{
19 Error,
20 send_queue::{AbstractProgress, SendHandle},
21};
22use ruma::{EventId, OwnedEventId, OwnedTransactionId};
23
24use super::TimelineEventItemId;
25
26/// An item for an event that was created locally and not yet echoed back by the
27/// homeserver.
28#[derive(Debug, Clone)]
29pub(in crate::timeline) struct LocalEventTimelineItem {
30 /// The send state of this local event.
31 pub send_state: EventSendState,
32 /// The transaction ID.
33 pub transaction_id: OwnedTransactionId,
34 /// A handle to manipulate this event before it is sent, if possible.
35 pub send_handle: Option<SendHandle>,
36}
37
38impl LocalEventTimelineItem {
39 /// Get the unique identifier of this item.
40 ///
41 /// Returns the transaction ID for a local echo item that has not been sent
42 /// and the event ID for a local echo item that has been sent.
43 pub(crate) fn identifier(&self) -> TimelineEventItemId {
44 if let Some(event_id) =
45 as_variant!(&self.send_state, EventSendState::Sent { event_id } => event_id)
46 {
47 TimelineEventItemId::EventId(event_id.clone())
48 } else {
49 TimelineEventItemId::TransactionId(self.transaction_id.clone())
50 }
51 }
52
53 /// Get the event ID of this item.
54 ///
55 /// Will be `Some` if and only if `send_state` is `EventSendState::Sent`.
56 pub fn event_id(&self) -> Option<&EventId> {
57 as_variant!(&self.send_state, EventSendState::Sent { event_id } => event_id)
58 }
59
60 /// Clone the current event item, and update its `send_state`.
61 pub fn with_send_state(&self, send_state: EventSendState) -> Self {
62 Self { send_state, ..self.clone() }
63 }
64}
65
66/// This type represents the "send state" of a local event timeline item.
67#[derive(Clone, Debug)]
68pub enum EventSendState {
69 /// The local event has not been sent yet.
70 NotSentYet {
71 /// The progress of the sending operation, if the event involves a media
72 /// upload.
73 progress: Option<MediaUploadProgress>,
74 },
75 /// The local event has been sent to the server, but unsuccessfully: The
76 /// sending has failed.
77 SendingFailed {
78 /// Details about how sending the event failed.
79 error: Arc<Error>,
80 /// Whether the error is considered recoverable or not.
81 ///
82 /// An error that's recoverable will disable the room's send queue,
83 /// while an unrecoverable error will be parked, until it's retried or
84 /// aborted.
85 is_recoverable: bool,
86 },
87 /// The local event has been sent successfully to the server.
88 Sent {
89 /// The event ID assigned by the server.
90 event_id: OwnedEventId,
91 },
92}
93
94/// This type represents the progress of a media (consisting of a file and
95/// possibly a thumbnail) being uploaded.
96#[derive(Clone, Debug)]
97pub struct MediaUploadProgress {
98 /// The index of the media within the transaction. A file and its thumbnail
99 /// share the same index. Will always be 0 for non-gallery media uploads.
100 pub index: u64,
101
102 /// The combined upload progress across the file and, if existing, its
103 /// thumbnail. For gallery uploads, the progress is reported per indexed
104 /// gallery item.
105 pub progress: AbstractProgress,
106}