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
27/// the 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
56 /// `EventSendState::Sent`.
57 pub fn event_id(&self) -> Option<&EventId> {
58 as_variant!(&self.send_state, EventSendState::Sent { event_id } => event_id)
59 }
60
61 /// Clone the current event item, and update its `send_state`.
62 pub fn with_send_state(&self, send_state: EventSendState) -> Self {
63 Self { send_state, ..self.clone() }
64 }
65}
66
67/// This type represents the "send state" of a local event timeline item.
68#[derive(Clone, Debug)]
69pub enum EventSendState {
70 /// The local event has not been sent yet.
71 NotSentYet {
72 /// The progress of the sending operation, if the event involves a media
73 /// upload.
74 progress: Option<MediaUploadProgress>,
75 },
76 /// The local event has been sent to the server, but unsuccessfully: The
77 /// sending has failed.
78 SendingFailed {
79 /// Details about how sending the event failed.
80 error: Arc<Error>,
81 /// Whether the error is considered recoverable or not.
82 ///
83 /// An error that's recoverable will disable the room's send queue,
84 /// while an unrecoverable error will be parked, until the user
85 /// decides to cancel sending it.
86 is_recoverable: bool,
87 },
88 /// The local event has been sent successfully to the server.
89 Sent {
90 /// The event ID assigned by the server.
91 event_id: OwnedEventId,
92 },
93}
94
95/// This type represents the progress of a media (consisting of a file and
96/// possibly a thumbnail) being uploaded.
97#[derive(Clone, Debug)]
98pub struct MediaUploadProgress {
99 /// The index of the media within the transaction. A file and its
100 /// thumbnail share the same index. Will always be 0 for non-gallery
101 /// media uploads.
102 pub index: u64,
103
104 /// The combined upload progress across the file and, if existing, its
105 /// thumbnail. For gallery uploads, the progress is reported per indexed
106 /// gallery item.
107 pub progress: AbstractProgress,
108}