ruma_client_api/session/
login_fallback.rs

1//! `GET /_matrix/static/client/login/` ([spec])
2//!
3//! Get login fallback web page.
4//!
5//! [spec]: https://spec.matrix.org/latest/client-server-api/#login-fallback
6
7use ruma_common::{api::request, metadata, OwnedDeviceId};
8
9metadata! {
10    method: GET,
11    rate_limited: false,
12    authentication: NoAuthentication,
13    history: {
14        1.0 => "/_matrix/static/client/login/",
15    }
16}
17
18/// Request type for the `login_fallback` endpoint.
19#[request(error = crate::Error)]
20#[derive(Default)]
21pub struct Request {
22    /// ID of the client device.
23    #[ruma_api(query)]
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub device_id: Option<OwnedDeviceId>,
26
27    /// A display name to assign to the newly-created device.
28    ///
29    /// Ignored if `device_id` corresponds to a known device.
30    #[ruma_api(query)]
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub initial_device_display_name: Option<String>,
33}
34
35impl Request {
36    /// Creates a new `Request` with the given auth type and session ID.
37    pub fn new(
38        device_id: Option<OwnedDeviceId>,
39        initial_device_display_name: Option<String>,
40    ) -> Self {
41        Self { device_id, initial_device_display_name }
42    }
43}
44
45/// Response type for the `login_fallback` endpoint.
46#[derive(Debug, Clone)]
47#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
48pub struct Response {
49    /// HTML to return to client.
50    pub body: Vec<u8>,
51}
52
53impl Response {
54    /// Creates a new `Response` with the given HTML body.
55    pub fn new(body: Vec<u8>) -> Self {
56        Self { body }
57    }
58}
59
60#[cfg(feature = "server")]
61impl ruma_common::api::OutgoingResponse for Response {
62    fn try_into_http_response<T: Default + bytes::BufMut>(
63        self,
64    ) -> Result<http::Response<T>, ruma_common::api::error::IntoHttpError> {
65        Ok(http::Response::builder()
66            .status(http::StatusCode::OK)
67            .header(http::header::CONTENT_TYPE, "text/html")
68            .body(ruma_common::serde::slice_to_buf(&self.body))?)
69    }
70}
71
72#[cfg(feature = "client")]
73impl ruma_common::api::IncomingResponse for Response {
74    type EndpointError = crate::Error;
75
76    fn try_from_http_response<T: AsRef<[u8]>>(
77        response: http::Response<T>,
78    ) -> Result<Self, ruma_common::api::error::FromHttpResponseError<Self::EndpointError>> {
79        use ruma_common::api::{error::FromHttpResponseError, EndpointError};
80
81        if response.status().as_u16() >= 400 {
82            return Err(FromHttpResponseError::Server(Self::EndpointError::from_http_response(
83                response,
84            )));
85        }
86
87        let body = response.into_body().as_ref().to_owned();
88        Ok(Self { body })
89    }
90}