ruma_events/room/message/
content_serde.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
//! `Deserialize` implementation for RoomMessageEventContent and MessageType.

use ruma_common::serde::from_raw_json_value;
use serde::{de, Deserialize};
use serde_json::value::RawValue as RawJsonValue;

use super::{
    relation_serde::deserialize_relation, MessageType, RoomMessageEventContent,
    RoomMessageEventContentWithoutRelation,
};
use crate::Mentions;

impl<'de> Deserialize<'de> for RoomMessageEventContent {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let json = Box::<RawJsonValue>::deserialize(deserializer)?;

        let mut deserializer = serde_json::Deserializer::from_str(json.get());
        let relates_to = deserialize_relation(&mut deserializer).map_err(de::Error::custom)?;

        let MentionsDeHelper { mentions } = from_raw_json_value(&json)?;

        Ok(Self { msgtype: from_raw_json_value(&json)?, relates_to, mentions })
    }
}

impl<'de> Deserialize<'de> for RoomMessageEventContentWithoutRelation {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let json = Box::<RawJsonValue>::deserialize(deserializer)?;

        let MentionsDeHelper { mentions } = from_raw_json_value(&json)?;

        Ok(Self { msgtype: from_raw_json_value(&json)?, mentions })
    }
}

#[derive(Deserialize)]
struct MentionsDeHelper {
    #[serde(rename = "m.mentions")]
    mentions: Option<Mentions>,
}

/// Helper struct to determine the msgtype from a `serde_json::value::RawValue`
#[derive(Debug, Deserialize)]
struct MessageTypeDeHelper {
    /// The message type field
    msgtype: String,
}

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

        Ok(match msgtype.as_ref() {
            "m.audio" => Self::Audio(from_raw_json_value(&json)?),
            "m.emote" => Self::Emote(from_raw_json_value(&json)?),
            "m.file" => Self::File(from_raw_json_value(&json)?),
            "m.image" => Self::Image(from_raw_json_value(&json)?),
            "m.location" => Self::Location(from_raw_json_value(&json)?),
            "m.notice" => Self::Notice(from_raw_json_value(&json)?),
            "m.server_notice" => Self::ServerNotice(from_raw_json_value(&json)?),
            "m.text" => Self::Text(from_raw_json_value(&json)?),
            "m.video" => Self::Video(from_raw_json_value(&json)?),
            "m.key.verification.request" => Self::VerificationRequest(from_raw_json_value(&json)?),
            _ => Self::_Custom(from_raw_json_value(&json)?),
        })
    }
}

#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/112615
#[cfg(feature = "unstable-msc3488")]
pub(in super::super) mod msc3488 {
    use ruma_common::MilliSecondsSinceUnixEpoch;
    use serde::{Deserialize, Serialize};

    use crate::{
        location::{AssetContent, LocationContent},
        message::historical_serde::MessageContentBlock,
        room::message::{LocationInfo, LocationMessageEventContent},
    };

    /// Deserialize helper type for `LocationMessageEventContent` with unstable fields from msc3488.
    #[derive(Serialize, Deserialize)]
    #[serde(tag = "msgtype", rename = "m.location")]
    pub(in super::super) struct LocationMessageEventContentSerDeHelper {
        pub body: String,

        pub geo_uri: String,

        #[serde(skip_serializing_if = "Option::is_none")]
        pub info: Option<Box<LocationInfo>>,

        #[serde(flatten)]
        pub message: Option<MessageContentBlock>,

        #[serde(rename = "org.matrix.msc3488.location", skip_serializing_if = "Option::is_none")]
        pub location: Option<LocationContent>,

        #[serde(rename = "org.matrix.msc3488.asset", skip_serializing_if = "Option::is_none")]
        pub asset: Option<AssetContent>,

        #[serde(rename = "org.matrix.msc3488.ts", skip_serializing_if = "Option::is_none")]
        pub ts: Option<MilliSecondsSinceUnixEpoch>,
    }

    impl From<LocationMessageEventContent> for LocationMessageEventContentSerDeHelper {
        fn from(value: LocationMessageEventContent) -> Self {
            let LocationMessageEventContent { body, geo_uri, info, message, location, asset, ts } =
                value;

            Self { body, geo_uri, info, message: message.map(Into::into), location, asset, ts }
        }
    }

    impl From<LocationMessageEventContentSerDeHelper> for LocationMessageEventContent {
        fn from(value: LocationMessageEventContentSerDeHelper) -> Self {
            let LocationMessageEventContentSerDeHelper {
                body,
                geo_uri,
                info,
                message,
                location,
                asset,
                ts,
            } = value;

            LocationMessageEventContent {
                body,
                geo_uri,
                info,
                message: message.map(Into::into),
                location,
                asset,
                ts,
            }
        }
    }
}