ruma_client_api/media/
get_media_preview.rs1pub mod v3 {
6 use ruma_common::{
11 MilliSecondsSinceUnixEpoch,
12 api::{auth_scheme::AccessToken, request, response},
13 metadata,
14 };
15 use serde::Serialize;
16 use serde_json::value::{RawValue as RawJsonValue, to_raw_value as to_raw_json_value};
17
18 metadata! {
19 method: GET,
20 rate_limited: true,
21 authentication: AccessToken,
22 history: {
23 1.0 => "/_matrix/media/r0/preview_url",
24 1.1 => "/_matrix/media/v3/preview_url",
25 1.11 => deprecated,
26 }
27 }
28
29 #[request(error = crate::Error)]
31 #[deprecated = "\
32 Since Matrix 1.11, clients should use `authenticated_media::get_media_preview::v1::Request` \
33 instead if the homeserver supports it.\
34 "]
35 pub struct Request {
36 #[ruma_api(query)]
38 pub url: String,
39
40 #[ruma_api(query)]
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub ts: Option<MilliSecondsSinceUnixEpoch>,
44 }
45
46 #[response(error = crate::Error)]
48 #[derive(Default)]
49 pub struct Response {
50 #[ruma_api(body)]
55 pub data: Option<Box<RawJsonValue>>,
56 }
57
58 #[allow(deprecated)]
59 impl Request {
60 pub fn new(url: String) -> Self {
62 Self { url, ts: None }
63 }
64 }
65
66 impl Response {
67 pub fn new() -> Self {
69 Self { data: None }
70 }
71
72 pub fn from_raw_value(data: Box<RawJsonValue>) -> Self {
75 Self { data: Some(data) }
76 }
77
78 pub fn from_serialize<T: Serialize>(data: &T) -> serde_json::Result<Self> {
81 Ok(Self { data: Some(to_raw_json_value(data)?) })
82 }
83 }
84
85 #[cfg(test)]
86 mod tests {
87 use assert_matches2::assert_matches;
88 use serde_json::{
89 from_value as from_json_value, json,
90 value::{RawValue as RawJsonValue, to_raw_value as to_raw_json_value},
91 };
92
93 #[test]
96 fn raw_json_deserialize() {
97 type OptRawJson = Option<Box<RawJsonValue>>;
98
99 assert_matches!(from_json_value::<OptRawJson>(json!(null)).unwrap(), None);
100 from_json_value::<OptRawJson>(json!("test")).unwrap().unwrap();
101 from_json_value::<OptRawJson>(json!({ "a": "b" })).unwrap().unwrap();
102 }
103
104 #[test]
106 fn raw_json_serialize() {
107 to_raw_json_value(&json!(null)).unwrap();
108 to_raw_json_value(&json!("string")).unwrap();
109 to_raw_json_value(&json!({})).unwrap();
110 to_raw_json_value(&json!({ "a": "b" })).unwrap();
111 }
112 }
113}