ruma_events/room/redaction/
event_serde.rs

1use ruma_common::{
2    serde::from_raw_json_value, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId,
3};
4use serde::{de, Deserialize, Deserializer};
5use serde_json::value::RawValue as RawJsonValue;
6
7use super::{
8    OriginalRoomRedactionEvent, OriginalSyncRoomRedactionEvent, RoomRedactionEvent,
9    RoomRedactionEventContent, RoomRedactionUnsigned, SyncRoomRedactionEvent,
10};
11use crate::RedactionDeHelper;
12
13impl<'de> Deserialize<'de> for RoomRedactionEvent {
14    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
15    where
16        D: Deserializer<'de>,
17    {
18        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
19        let RedactionDeHelper { unsigned } = from_raw_json_value(&json)?;
20
21        if unsigned.and_then(|u| u.redacted_because).is_some() {
22            Ok(Self::Redacted(from_raw_json_value(&json)?))
23        } else {
24            Ok(Self::Original(from_raw_json_value(&json)?))
25        }
26    }
27}
28
29impl<'de> Deserialize<'de> for SyncRoomRedactionEvent {
30    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
31    where
32        D: Deserializer<'de>,
33    {
34        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
35        let RedactionDeHelper { unsigned } = from_raw_json_value(&json)?;
36
37        if unsigned.and_then(|u| u.redacted_because).is_some() {
38            Ok(Self::Redacted(from_raw_json_value(&json)?))
39        } else {
40            Ok(Self::Original(from_raw_json_value(&json)?))
41        }
42    }
43}
44
45#[derive(Deserialize)]
46struct OriginalRoomRedactionEventDeHelper {
47    content: RoomRedactionEventContent,
48    redacts: Option<OwnedEventId>,
49    event_id: OwnedEventId,
50    sender: OwnedUserId,
51    origin_server_ts: MilliSecondsSinceUnixEpoch,
52    room_id: Option<OwnedRoomId>,
53    #[serde(default)]
54    unsigned: RoomRedactionUnsigned,
55}
56
57impl<'de> Deserialize<'de> for OriginalRoomRedactionEvent {
58    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
59    where
60        D: Deserializer<'de>,
61    {
62        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
63        let OriginalRoomRedactionEventDeHelper {
64            content,
65            redacts,
66            event_id,
67            sender,
68            origin_server_ts,
69            room_id,
70            unsigned,
71        } = from_raw_json_value(&json)?;
72
73        let Some(room_id) = room_id else { return Err(de::Error::missing_field("room_id")) };
74
75        if redacts.is_none() && content.redacts.is_none() {
76            return Err(de::Error::missing_field("redacts"));
77        }
78
79        Ok(Self { content, redacts, event_id, sender, origin_server_ts, room_id, unsigned })
80    }
81}
82
83impl<'de> Deserialize<'de> for OriginalSyncRoomRedactionEvent {
84    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
85    where
86        D: Deserializer<'de>,
87    {
88        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
89        let OriginalRoomRedactionEventDeHelper {
90            content,
91            redacts,
92            event_id,
93            sender,
94            origin_server_ts,
95            unsigned,
96            ..
97        } = from_raw_json_value(&json)?;
98
99        if redacts.is_none() && content.redacts.is_none() {
100            return Err(de::Error::missing_field("redacts"));
101        }
102
103        Ok(Self { content, redacts, event_id, sender, origin_server_ts, unsigned })
104    }
105}