conduit/src/client_server/alias.rs

143 lines
4.3 KiB
Rust
Raw Normal View History

2021-06-08 16:10:00 +00:00
use std::sync::Arc;
2020-07-30 16:14:47 +00:00
use super::State;
2020-12-19 15:00:11 +00:00
use crate::{ConduitResult, Database, Error, Ruma};
use regex::Regex;
use ruma::{
api::{
appservice,
client::{
error::ErrorKind,
r0::alias::{create_alias, delete_alias, get_alias},
},
federation,
2020-08-14 09:34:15 +00:00
},
RoomAliasId,
2020-08-14 09:34:15 +00:00
};
2020-07-30 16:14:47 +00:00
#[cfg(feature = "conduit_bin")]
use rocket::{delete, get, put};
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/directory/room/<_>", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn create_alias_route(
2021-06-08 16:10:00 +00:00
db: State<'_, Arc<Database>>,
2020-09-08 15:32:03 +00:00
body: Ruma<create_alias::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<create_alias::Response> {
if db.rooms.id_from_alias(&body.room_alias)?.is_some() {
return Err(Error::Conflict("Alias already exists."));
}
db.rooms
.set_alias(&body.room_alias, Some(&body.room_id), &db.globals)?;
db.flush().await?;
Ok(create_alias::Response::new().into())
2020-07-30 16:14:47 +00:00
}
#[cfg_attr(
feature = "conduit_bin",
delete("/_matrix/client/r0/directory/room/<_>", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn delete_alias_route(
2021-06-08 16:10:00 +00:00
db: State<'_, Arc<Database>>,
2020-09-08 15:32:03 +00:00
body: Ruma<delete_alias::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<delete_alias::Response> {
db.rooms.set_alias(&body.room_alias, None, &db.globals)?;
db.flush().await?;
Ok(delete_alias::Response::new().into())
2020-07-30 16:14:47 +00:00
}
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/client/r0/directory/room/<_>", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn get_alias_route(
2021-06-08 16:10:00 +00:00
db: State<'_, Arc<Database>>,
2020-09-08 15:32:03 +00:00
body: Ruma<get_alias::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<get_alias::Response> {
get_alias_helper(&db, &body.room_alias).await
}
pub async fn get_alias_helper(
db: &Database,
room_alias: &RoomAliasId,
) -> ConduitResult<get_alias::Response> {
if room_alias.server_name() != db.globals.server_name() {
2020-12-19 15:00:11 +00:00
let response = db
.sending
.send_federation_request(
&db.globals,
room_alias.server_name(),
2020-12-19 15:00:11 +00:00
federation::query::get_room_information::v1::Request { room_alias },
)
.await?;
return Ok(get_alias::Response::new(response.room_id, response.servers).into());
2020-07-30 16:14:47 +00:00
}
let mut room_id = None;
match db.rooms.id_from_alias(&room_alias)? {
Some(r) => room_id = Some(r),
None => {
2021-06-08 16:10:00 +00:00
let iter = db.appservice.iter_all()?;
for (_id, registration) in iter.filter_map(|r| r.ok()) {
let aliases = registration
.get("namespaces")
.and_then(|ns| ns.get("aliases"))
2021-03-18 11:03:15 +00:00
.and_then(|aliases| aliases.as_sequence())
.map_or_else(Vec::new, |aliases| {
aliases
.iter()
2021-06-17 18:34:14 +00:00
.filter_map(|aliases| {
2021-03-18 11:03:15 +00:00
aliases
.get("regex")
.and_then(|regex| regex.as_str())
.and_then(|regex| Regex::new(regex).ok())
})
.collect::<Vec<_>>()
});
2021-03-18 11:03:15 +00:00
if aliases
.iter()
.any(|aliases| aliases.is_match(room_alias.as_str()))
&& db
.sending
.send_appservice_request(
&db.globals,
registration,
appservice::query::query_room_alias::v1::Request { room_alias },
)
.await
.is_ok()
{
room_id = Some(db.rooms.id_from_alias(&room_alias)?.ok_or_else(|| {
Error::bad_config("Appservice lied to us. Room does not exist.")
})?);
break;
}
}
}
};
let room_id = match room_id {
Some(room_id) => room_id,
None => {
return Err(Error::BadRequest(
ErrorKind::NotFound,
"Room with alias not found.",
))
}
};
2020-07-30 16:14:47 +00:00
Ok(get_alias::Response::new(room_id, vec![db.globals.server_name().to_owned()]).into())
2020-07-30 16:14:47 +00:00
}