#[cfg(feature = "unstable-msc4143")]
use std::borrow::Cow;
#[cfg(feature = "unstable-msc4143")]
use ruma_common::serde::JsonObject;
use ruma_common::{
api::{auth_scheme::NoAuthentication, request, response},
metadata,
};
#[cfg(feature = "unstable-msc4143")]
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
#[cfg(feature = "unstable-msc4143")]
use serde_json::Value as JsonValue;
metadata! {
method: GET,
rate_limited: false,
authentication: NoAuthentication,
path: "/.well-known/matrix/client",
}
#[request(error = crate::Error)]
#[derive(Default)]
pub struct Request {}
#[response(error = crate::Error)]
pub struct Response {
#[serde(rename = "m.homeserver")]
pub homeserver: HomeserverInfo,
#[serde(rename = "m.identity_server", skip_serializing_if = "Option::is_none")]
pub identity_server: Option<IdentityServerInfo>,
#[cfg(feature = "unstable-msc3488")]
#[serde(
rename = "org.matrix.msc3488.tile_server",
alias = "m.tile_server",
skip_serializing_if = "Option::is_none"
)]
pub tile_server: Option<TileServerInfo>,
#[cfg(feature = "unstable-msc4143")]
#[serde(
rename = "org.matrix.msc4143.rtc_foci",
alias = "m.rtc_foci",
default,
skip_serializing_if = "Vec::is_empty"
)]
pub rtc_foci: Vec<RtcFocusInfo>,
}
impl Request {
pub fn new() -> Self {
Self {}
}
}
impl Response {
pub fn new(homeserver: HomeserverInfo) -> Self {
Self {
homeserver,
identity_server: None,
#[cfg(feature = "unstable-msc3488")]
tile_server: None,
#[cfg(feature = "unstable-msc4143")]
rtc_foci: Default::default(),
}
}
}
#[derive(Clone, Debug, Deserialize, Hash, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct HomeserverInfo {
pub base_url: String,
}
impl HomeserverInfo {
pub fn new(base_url: String) -> Self {
Self { base_url }
}
}
#[derive(Clone, Debug, Deserialize, Hash, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct IdentityServerInfo {
pub base_url: String,
}
impl IdentityServerInfo {
pub fn new(base_url: String) -> Self {
Self { base_url }
}
}
#[cfg(feature = "unstable-msc3488")]
#[derive(Clone, Debug, Deserialize, Hash, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct TileServerInfo {
pub map_style_url: String,
}
#[cfg(feature = "unstable-msc3488")]
impl TileServerInfo {
pub fn new(map_style_url: String) -> Self {
Self { map_style_url }
}
}
#[cfg(feature = "unstable-msc4143")]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[serde(tag = "type")]
pub enum RtcFocusInfo {
#[serde(rename = "livekit")]
LiveKit(LiveKitRtcFocusInfo),
#[doc(hidden)]
#[serde(untagged)]
_Custom(CustomRtcFocusInfo),
}
#[cfg(feature = "unstable-msc4143")]
impl RtcFocusInfo {
pub fn new(focus_type: &str, data: JsonObject) -> serde_json::Result<Self> {
fn deserialize_variant<T: DeserializeOwned>(obj: JsonObject) -> serde_json::Result<T> {
serde_json::from_value(JsonValue::Object(obj))
}
Ok(match focus_type {
"livekit" => Self::LiveKit(deserialize_variant(data)?),
_ => Self::_Custom(CustomRtcFocusInfo { focus_type: focus_type.to_owned(), data }),
})
}
pub fn livekit(service_url: String) -> Self {
Self::LiveKit(LiveKitRtcFocusInfo { service_url })
}
pub fn focus_type(&self) -> &str {
match self {
Self::LiveKit(_) => "livekit",
Self::_Custom(custom) => &custom.focus_type,
}
}
pub fn data(&self) -> Cow<'_, JsonObject> {
fn serialize<T: Serialize>(object: &T) -> JsonObject {
match serde_json::to_value(object).expect("rtc focus type serialization to succeed") {
JsonValue::Object(object) => object,
_ => panic!("all rtc focus types must serialize to objects"),
}
}
match self {
Self::LiveKit(info) => Cow::Owned(serialize(info)),
Self::_Custom(info) => Cow::Borrowed(&info.data),
}
}
}
#[cfg(feature = "unstable-msc4143")]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct LiveKitRtcFocusInfo {
#[serde(rename = "livekit_service_url")]
pub service_url: String,
}
#[doc(hidden)]
#[cfg(feature = "unstable-msc4143")]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct CustomRtcFocusInfo {
#[serde(rename = "type")]
focus_type: String,
#[serde(flatten)]
data: JsonObject,
}
#[cfg(test)]
mod tests {
#[cfg(feature = "unstable-msc4143")]
use assert_matches2::assert_matches;
#[cfg(feature = "unstable-msc4143")]
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[cfg(feature = "unstable-msc4143")]
use super::RtcFocusInfo;
#[test]
#[cfg(feature = "unstable-msc4143")]
fn test_livekit_rtc_focus_deserialization() {
let json = json!({
"type": "livekit",
"livekit_service_url": "https://livekit.example.com"
});
let focus: RtcFocusInfo = from_json_value(json).unwrap();
assert_matches!(focus, RtcFocusInfo::LiveKit(info));
assert_eq!(info.service_url, "https://livekit.example.com");
}
#[test]
#[cfg(feature = "unstable-msc4143")]
fn test_livekit_rtc_focus_serialization() {
let focus = RtcFocusInfo::livekit("https://livekit.example.com".to_owned());
let json = to_json_value(&focus).unwrap();
assert_eq!(
json,
json!({
"type": "livekit",
"livekit_service_url": "https://livekit.example.com"
})
);
}
#[test]
#[cfg(feature = "unstable-msc4143")]
fn test_custom_rtc_focus_serialization() {
let json = json!({
"type": "some-focus-type",
"additional-type-specific-field": "https://my_focus.domain",
"another-additional-type-specific-field": ["with", "Array", "type"]
});
let focus: RtcFocusInfo = from_json_value(json.clone()).unwrap();
assert_eq!(focus.focus_type(), "some-focus-type");
let data = &focus.data();
assert_eq!(data["additional-type-specific-field"], "https://my_focus.domain");
let array_values: Vec<&str> = data["another-additional-type-specific-field"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert_eq!(array_values, vec!["with", "Array", "type"]);
assert!(!data.contains_key("type"));
let serialized = to_json_value(&focus).unwrap();
assert_eq!(serialized, json);
}
}