2020-07-30 16:14:47 +00:00
|
|
|
use super::State;
|
2020-08-14 09:34:15 +00:00
|
|
|
use crate::{server_server, ConduitResult, Database, Error, Ruma};
|
2020-08-20 16:12:02 +00:00
|
|
|
use ruma::{
|
|
|
|
api::{
|
|
|
|
client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::alias::{create_alias, delete_alias, get_alias},
|
|
|
|
},
|
|
|
|
federation,
|
2020-08-14 09:34:15 +00:00
|
|
|
},
|
2020-08-20 16:12:02 +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>")
|
|
|
|
)]
|
|
|
|
pub fn create_alias_route(
|
|
|
|
db: State<'_, 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)?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
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>")
|
|
|
|
)]
|
|
|
|
pub fn delete_alias_route(
|
|
|
|
db: State<'_, 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)?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
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>")
|
|
|
|
)]
|
2020-08-14 09:31:31 +00:00
|
|
|
pub async fn get_alias_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, 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> {
|
2020-08-21 21:19:18 +00:00
|
|
|
get_alias_helper(&db, &body.room_alias).await
|
2020-08-20 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_alias_helper(
|
2020-08-21 21:19:18 +00:00
|
|
|
db: &Database,
|
2020-08-20 16:12:02 +00:00
|
|
|
room_alias: &RoomAliasId,
|
|
|
|
) -> ConduitResult<get_alias::Response> {
|
|
|
|
if room_alias.server_name() != db.globals.server_name() {
|
2020-08-14 09:31:31 +00:00
|
|
|
let response = server_server::send_request(
|
|
|
|
&db,
|
2020-08-20 16:12:02 +00:00
|
|
|
room_alias.server_name().to_string(),
|
2020-08-14 09:31:31 +00:00
|
|
|
federation::query::get_room_information::v1::Request {
|
2020-09-08 15:32:03 +00:00
|
|
|
room_alias,
|
2020-08-14 09:31:31 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
return Ok(get_alias::Response::new(response.room_id, response.servers).into());
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let room_id = db
|
|
|
|
.rooms
|
2020-08-20 16:12:02 +00:00
|
|
|
.id_from_alias(&room_alias)?
|
2020-07-30 16:14:47 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Room with alias not found.",
|
|
|
|
))?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(get_alias::Response::new(room_id, vec![db.globals.server_name().to_string()]).into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|