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