Skip to main content

ruma_appservice_api/media/
preview_url.rs

1//! `GET /_matrix/app/*/media/preview_url`
2//!
3//! Endpoint to query a preview for a given url.
4
5pub mod unstable {
6    //! `msc4417` ([MSC])
7    //!
8    //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4417
9
10    use ruma_common::{
11        OwnedUserId,
12        api::{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    use crate::HomeserverToken;
19
20    metadata! {
21        method: GET,
22        rate_limited: false,
23        authentication: HomeserverToken,
24        path: "/_matrix/app/unstable/uk.half-shot.msc4417/preview_url"
25    }
26
27    /// Request type for the `preview_url` endpoint.
28    #[request]
29    pub struct Request {
30        /// URL to get a preview of.
31        #[ruma_api(query)]
32        pub url: String,
33
34        /// UserID of the user requesting this preview.
35        #[ruma_api(query)]
36        #[serde(skip_serializing_if = "Option::is_none")]
37        pub user_id: Option<OwnedUserId>,
38    }
39
40    /// Response type for the `get_media_preview` endpoint.
41    ///
42    /// Equivalent to `ruma_client_api::authenticated_media::get_media_preview::v1::Response`
43    #[response]
44    #[derive(Default)]
45    pub struct Response {
46        /// OpenGraph-like data for the URL.
47        ///
48        /// Differences from OpenGraph: the image size in bytes is added to the `matrix:image:size`
49        /// field, and `og:image` returns the MXC URI to the image, if any.
50        #[ruma_api(body)]
51        pub data: Option<Box<RawJsonValue>>,
52    }
53
54    impl Request {
55        /// Creates a new `Request` with the given URL.
56        pub fn new(url: String) -> Self {
57            Self { url, user_id: None }
58        }
59    }
60
61    impl Response {
62        /// Creates an empty `Response`.
63        pub fn new() -> Self {
64            Self { data: None }
65        }
66
67        /// Creates a new `Response` with the given OpenGraph data (in a
68        /// `serde_json::value::RawValue`).
69        pub fn from_raw_value(data: Box<RawJsonValue>) -> Self {
70            Self { data: Some(data) }
71        }
72
73        /// Creates a new `Response` with the given OpenGraph data (in any kind of serializable
74        /// object).
75        pub fn from_serialize<T: Serialize>(data: &T) -> serde_json::Result<Self> {
76            Ok(Self { data: Some(to_raw_json_value(data)?) })
77        }
78    }
79}