ruma_client_api/media/
get_media_preview.rs

1//! `GET /_matrix/media/*/preview_url`
2//!
3//! Get a preview for a URL.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3preview_url
9
10    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 type for the `get_media_preview` endpoint.
30    #[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        /// URL to get a preview of.
37        #[ruma_api(query)]
38        pub url: String,
39
40        /// Preferred point in time (in milliseconds) to return a preview for.
41        #[ruma_api(query)]
42        #[serde(skip_serializing_if = "Option::is_none")]
43        pub ts: Option<MilliSecondsSinceUnixEpoch>,
44    }
45
46    /// Response type for the `get_media_preview` endpoint.
47    #[response(error = crate::Error)]
48    #[derive(Default)]
49    pub struct Response {
50        /// OpenGraph-like data for the URL.
51        ///
52        /// Differences from OpenGraph: the image size in bytes is added to the `matrix:image:size`
53        /// field, and `og:image` returns the MXC URI to the image, if any.
54        #[ruma_api(body)]
55        pub data: Option<Box<RawJsonValue>>,
56    }
57
58    #[allow(deprecated)]
59    impl Request {
60        /// Creates a new `Request` with the given url.
61        pub fn new(url: String) -> Self {
62            Self { url, ts: None }
63        }
64    }
65
66    impl Response {
67        /// Creates an empty `Response`.
68        pub fn new() -> Self {
69            Self { data: None }
70        }
71
72        /// Creates a new `Response` with the given OpenGraph data (in a
73        /// `serde_json::value::RawValue`).
74        pub fn from_raw_value(data: Box<RawJsonValue>) -> Self {
75            Self { data: Some(data) }
76        }
77
78        /// Creates a new `Response` with the given OpenGraph data (in any kind of serializable
79        /// object).
80        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        // Since BTreeMap<String, Box<RawJsonValue>> deserialization doesn't seem to
94        // work, test that Option<RawJsonValue> works
95        #[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        // For completeness sake, make sure serialization works too
105        #[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}