matrix_sdk_ui/
events.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ruma::{
    events::{
        EventContent, EventContentFromType, MessageLikeEventContent, MessageLikeEventType,
        MessageLikeUnsigned, OriginalSyncMessageLikeEvent, OriginalSyncStateEvent,
        PossiblyRedactedStateEventContent, RedactContent, RedactedMessageLikeEventContent,
        RedactedStateEventContent, RedactedSyncMessageLikeEvent, RedactedSyncStateEvent,
        StateEventContent, StateEventType, StaticStateEventContent, TimelineEventType,
    },
    serde::from_raw_json_value,
    EventId, MilliSecondsSinceUnixEpoch, TransactionId, UserId,
};
use serde::{de, Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;

#[allow(clippy::large_enum_variant)]
pub(crate) enum SyncTimelineEventWithoutContent {
    OriginalMessageLike(OriginalSyncMessageLikeEvent<NoMessageLikeEventContent>),
    RedactedMessageLike(RedactedSyncMessageLikeEvent<NoMessageLikeEventContent>),
    OriginalState(OriginalSyncStateEvent<NoStateEventContent>),
    RedactedState(RedactedSyncStateEvent<NoStateEventContent>),
}

impl SyncTimelineEventWithoutContent {
    pub(crate) fn event_id(&self) -> &EventId {
        match self {
            Self::OriginalMessageLike(ev) => &ev.event_id,
            Self::RedactedMessageLike(ev) => &ev.event_id,
            Self::OriginalState(ev) => &ev.event_id,
            Self::RedactedState(ev) => &ev.event_id,
        }
    }

    pub(crate) fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
        match self {
            SyncTimelineEventWithoutContent::OriginalMessageLike(ev) => ev.origin_server_ts,
            SyncTimelineEventWithoutContent::RedactedMessageLike(ev) => ev.origin_server_ts,
            SyncTimelineEventWithoutContent::OriginalState(ev) => ev.origin_server_ts,
            SyncTimelineEventWithoutContent::RedactedState(ev) => ev.origin_server_ts,
        }
    }

    pub(crate) fn sender(&self) -> &UserId {
        match self {
            Self::OriginalMessageLike(ev) => &ev.sender,
            Self::RedactedMessageLike(ev) => &ev.sender,
            Self::OriginalState(ev) => &ev.sender,
            Self::RedactedState(ev) => &ev.sender,
        }
    }

    pub(crate) fn transaction_id(&self) -> Option<&TransactionId> {
        match self {
            SyncTimelineEventWithoutContent::OriginalMessageLike(ev) => {
                ev.unsigned.transaction_id.as_deref()
            }
            SyncTimelineEventWithoutContent::OriginalState(ev) => {
                ev.unsigned.transaction_id.as_deref()
            }
            SyncTimelineEventWithoutContent::RedactedMessageLike(_)
            | SyncTimelineEventWithoutContent::RedactedState(_) => None,
        }
    }

    pub(crate) fn event_type(&self) -> TimelineEventType {
        match self {
            SyncTimelineEventWithoutContent::OriginalMessageLike(ev) => {
                ev.content.event_type().into()
            }
            SyncTimelineEventWithoutContent::RedactedMessageLike(ev) => {
                ev.content.event_type().into()
            }
            SyncTimelineEventWithoutContent::OriginalState(ev) => ev.content.event_type().into(),
            SyncTimelineEventWithoutContent::RedactedState(ev) => ev.content.event_type().into(),
        }
    }
}

#[derive(Deserialize)]
struct EventDeHelper {
    state_key: Option<de::IgnoredAny>,
    #[serde(default)]
    unsigned: UnsignedDeHelper,
}

#[derive(Deserialize, Default)]
struct UnsignedDeHelper {
    redacted_because: Option<de::IgnoredAny>,
}

impl<'de> Deserialize<'de> for SyncTimelineEventWithoutContent {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
        let EventDeHelper { state_key, unsigned } = from_raw_json_value(&json)?;

        Ok(match (state_key.is_some(), unsigned.redacted_because.is_some()) {
            (false, false) => Self::OriginalMessageLike(from_raw_json_value(&json)?),
            (false, true) => Self::RedactedMessageLike(from_raw_json_value(&json)?),
            (true, false) => Self::OriginalState(from_raw_json_value(&json)?),
            (true, true) => Self::RedactedState(from_raw_json_value(&json)?),
        })
    }
}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct NoMessageLikeEventContent {
    #[serde(skip)]
    pub event_type: MessageLikeEventType,
}

impl EventContent for NoMessageLikeEventContent {
    type EventType = MessageLikeEventType;

    fn event_type(&self) -> Self::EventType {
        self.event_type.clone()
    }
}
impl EventContentFromType for NoMessageLikeEventContent {
    fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result<Self> {
        Ok(Self { event_type: event_type.into() })
    }
}
impl MessageLikeEventContent for NoMessageLikeEventContent {}
impl RedactedMessageLikeEventContent for NoMessageLikeEventContent {}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct NoStateEventContent {
    #[serde(skip)]
    pub event_type: StateEventType,
}

impl EventContent for NoStateEventContent {
    type EventType = StateEventType;

    fn event_type(&self) -> Self::EventType {
        self.event_type.clone()
    }
}
impl EventContentFromType for NoStateEventContent {
    fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result<Self> {
        Ok(Self { event_type: event_type.into() })
    }
}
impl RedactContent for NoStateEventContent {
    type Redacted = Self;

    fn redact(self, _version: &ruma::RoomVersionId) -> Self::Redacted {
        self
    }
}
impl StateEventContent for NoStateEventContent {
    type StateKey = String;
}
impl StaticStateEventContent for NoStateEventContent {
    // We don't care about the `prev_content` since it won't deserialize with useful
    // data. Use this type which is `StateUnsigned` minus the `prev_content`
    // field.
    type Unsigned = MessageLikeUnsigned<NoMessageLikeEventContent>;
    type PossiblyRedacted = Self;
}
impl RedactedStateEventContent for NoStateEventContent {
    type StateKey = String;
}
impl PossiblyRedactedStateEventContent for NoStateEventContent {
    type StateKey = String;
}