logo
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "unstable-msc2676")]
use super::Replacement;
#[cfg(feature = "unstable-msc2676")]
use super::RoomMessageEventContent;
#[cfg(feature = "unstable-msc3440")]
use super::Thread;
use super::{InReplyTo, Relation};
#[cfg(any(feature = "unstable-msc2676", feature = "unstable-msc3440"))]
use crate::OwnedEventId;

impl<'de> Deserialize<'de> for Relation {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?;

        #[cfg(feature = "unstable-msc3440")]
        if let Some(
            RelationJsonRepr::ThreadStable(ThreadStableJsonRepr { event_id, is_falling_back })
            | RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr { event_id, is_falling_back }),
        ) = ev.relates_to.relation
        {
            let in_reply_to = ev
                .relates_to
                .in_reply_to
                .ok_or_else(|| serde::de::Error::missing_field("m.in_reply_to"))?;
            return Ok(Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }));
        }

        let rel = if let Some(in_reply_to) = ev.relates_to.in_reply_to {
            Relation::Reply { in_reply_to }
        } else if let Some(relation) = ev.relates_to.relation {
            match relation {
                #[cfg(feature = "unstable-msc2676")]
                RelationJsonRepr::Replacement(ReplacementJsonRepr { event_id }) => {
                    let new_content = ev
                        .new_content
                        .ok_or_else(|| serde::de::Error::missing_field("m.new_content"))?;
                    Relation::Replacement(Replacement { event_id, new_content })
                }
                // FIXME: Maybe we should log this, though at this point we don't even have
                // access to the rel_type of the unknown relation.
                RelationJsonRepr::Unknown => Relation::_Custom,
                #[cfg(feature = "unstable-msc3440")]
                RelationJsonRepr::ThreadStable(_) | RelationJsonRepr::ThreadUnstable(_) => {
                    unreachable!()
                }
            }
        } else {
            Relation::_Custom
        };

        Ok(rel)
    }
}

impl Serialize for Relation {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[allow(clippy::needless_update)]
        let json_repr = match self {
            Relation::Reply { in_reply_to } => EventWithRelatesToJsonRepr::new(RelatesToJsonRepr {
                in_reply_to: Some(in_reply_to.clone()),
                ..Default::default()
            }),
            #[cfg(feature = "unstable-msc2676")]
            Relation::Replacement(Replacement { event_id, new_content }) => {
                EventWithRelatesToJsonRepr {
                    relates_to: RelatesToJsonRepr {
                        relation: Some(RelationJsonRepr::Replacement(ReplacementJsonRepr {
                            event_id: event_id.clone(),
                        })),
                        ..Default::default()
                    },
                    new_content: Some(new_content.clone()),
                }
            }
            #[cfg(feature = "unstable-msc3440")]
            Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => {
                EventWithRelatesToJsonRepr::new(RelatesToJsonRepr {
                    in_reply_to: Some(in_reply_to.clone()),
                    relation: Some(RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr {
                        event_id: event_id.clone(),
                        is_falling_back: *is_falling_back,
                    })),
                    ..Default::default()
                })
            }
            Relation::_Custom => EventWithRelatesToJsonRepr::default(),
        };

        json_repr.serialize(serializer)
    }
}

#[derive(Default, Deserialize, Serialize)]
struct EventWithRelatesToJsonRepr {
    #[serde(rename = "m.relates_to", default, skip_serializing_if = "RelatesToJsonRepr::is_empty")]
    relates_to: RelatesToJsonRepr,

    #[cfg(feature = "unstable-msc2676")]
    #[serde(rename = "m.new_content", skip_serializing_if = "Option::is_none")]
    new_content: Option<Box<RoomMessageEventContent>>,
}

impl EventWithRelatesToJsonRepr {
    fn new(relates_to: RelatesToJsonRepr) -> Self {
        Self {
            relates_to,
            #[cfg(feature = "unstable-msc2676")]
            new_content: None,
        }
    }
}

/// Struct modeling the different ways relationships can be expressed in a `m.relates_to` field of
/// an event.
#[derive(Default, Deserialize, Serialize)]
struct RelatesToJsonRepr {
    #[serde(rename = "m.in_reply_to", skip_serializing_if = "Option::is_none")]
    in_reply_to: Option<InReplyTo>,

    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    relation: Option<RelationJsonRepr>,
}

impl RelatesToJsonRepr {
    fn is_empty(&self) -> bool {
        self.in_reply_to.is_none() && self.relation.is_none()
    }
}

/// A relation, which associates new information to an existing event.
#[derive(Clone, Deserialize, Serialize)]
#[serde(tag = "rel_type")]
enum RelationJsonRepr {
    /// An event that replaces another event.
    #[cfg(feature = "unstable-msc2676")]
    #[serde(rename = "m.replace")]
    Replacement(ReplacementJsonRepr),

    /// An event that belongs to a thread, with unstable names.
    #[cfg(feature = "unstable-msc3440")]
    #[serde(rename = "m.thread")]
    ThreadStable(ThreadStableJsonRepr),

    /// An event that belongs to a thread, with unstable names.
    #[cfg(feature = "unstable-msc3440")]
    #[serde(rename = "io.element.thread")]
    ThreadUnstable(ThreadUnstableJsonRepr),

    /// An unknown relation type.
    ///
    /// Not available in the public API, but exists here so deserialization
    /// doesn't fail with new / custom `rel_type`s.
    #[serde(other)]
    Unknown,
}

#[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2676")]
struct ReplacementJsonRepr {
    event_id: OwnedEventId,
}

/// A thread relation without the reply fallback, with stable names.
#[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc3440")]
struct ThreadStableJsonRepr {
    /// The ID of the root message in the thread.
    event_id: OwnedEventId,

    /// Whether the `m.in_reply_to` field is a fallback for older clients or a real reply in a
    /// thread.
    #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
    is_falling_back: bool,
}

/// A thread relation without the reply fallback, with unstable names.
#[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc3440")]
struct ThreadUnstableJsonRepr {
    /// The ID of the root message in the thread.
    event_id: OwnedEventId,

    /// Whether the `m.in_reply_to` field is a fallback for older clients or a real reply in a
    /// thread.
    #[serde(
        rename = "io.element.show_reply",
        default,
        skip_serializing_if = "ruma_common::serde::is_default"
    )]
    is_falling_back: bool,
}