matrix_sdk_ui/timeline/event_item/remote.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::fmt;
16
17use indexmap::IndexMap;
18use matrix_sdk::deserialized_responses::EncryptionInfo;
19use ruma::{
20 events::{receipt::Receipt, AnySyncTimelineEvent},
21 serde::Raw,
22 OwnedEventId, OwnedTransactionId, OwnedUserId,
23};
24
25/// An item for an event that was received from the homeserver.
26#[derive(Clone)]
27pub(in crate::timeline) struct RemoteEventTimelineItem {
28 /// The event ID.
29 pub event_id: OwnedEventId,
30
31 /// If available, the transaction id we've used to send this event.
32 pub transaction_id: Option<OwnedTransactionId>,
33
34 /// All read receipts for the event.
35 ///
36 /// The key is the ID of a room member and the value are details about the
37 /// read receipt.
38 ///
39 /// Note that currently this ignores threads.
40 pub read_receipts: IndexMap<OwnedUserId, Receipt>,
41
42 /// Whether the event has been sent by the logged-in user themselves.
43 pub is_own: bool,
44
45 /// Whether the item should be highlighted in the timeline.
46 pub is_highlighted: bool,
47
48 /// Encryption information.
49 pub encryption_info: Option<EncryptionInfo>,
50
51 /// JSON of the original event.
52 ///
53 /// If the event is edited, this *won't* change, instead `latest_edit_json`
54 /// will be updated.
55 ///
56 /// This field always starts out as `Some(_)`, but is set to `None` when the
57 /// event is redacted. The redacted form of the event could be computed
58 /// locally instead (at least when the redaction came from the server and
59 /// thus the whole event is available), but it's not clear whether there is
60 /// a clear need for that.
61 pub original_json: Option<Raw<AnySyncTimelineEvent>>,
62
63 /// JSON of the latest edit to this item.
64 pub latest_edit_json: Option<Raw<AnySyncTimelineEvent>>,
65
66 /// Where we got this event from: A sync response or pagination.
67 pub origin: RemoteEventOrigin,
68}
69
70impl RemoteEventTimelineItem {
71 /// Clone the current event item, and redacts its fields.
72 pub fn redact(&self) -> Self {
73 Self { original_json: None, latest_edit_json: None, ..self.clone() }
74 }
75}
76
77/// Where we got an event from.
78#[derive(Clone, Copy, Debug)]
79pub(in crate::timeline) enum RemoteEventOrigin {
80 /// The event came from a cache.
81 Cache,
82 /// The event came from a sync response.
83 Sync,
84 /// The event came from pagination.
85 Pagination,
86 /// We don't know.
87 Unknown,
88}
89
90#[cfg(not(tarpaulin_include))]
91impl fmt::Debug for RemoteEventTimelineItem {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 // skip raw JSON, too noisy
94 let Self {
95 event_id,
96 transaction_id,
97 read_receipts,
98 is_own,
99 encryption_info,
100 original_json: _,
101 latest_edit_json: _,
102 is_highlighted,
103 origin,
104 } = self;
105
106 f.debug_struct("RemoteEventTimelineItem")
107 .field("event_id", event_id)
108 .field("transaction_id", transaction_id)
109 .field("read_receipts", read_receipts)
110 .field("is_own", is_own)
111 .field("is_highlighted", is_highlighted)
112 .field("encryption_info", encryption_info)
113 .field("origin", origin)
114 .finish_non_exhaustive()
115 }
116}