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