2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Ruma};
|
2021-03-03 20:41:26 +00:00
|
|
|
use regex::Regex;
|
2020-08-20 16:12:02 +00:00
|
|
|
use ruma::{
|
|
|
|
api::{
|
2020-12-08 09:33:44 +00:00
|
|
|
appservice,
|
2020-08-20 16:12:02 +00:00
|
|
|
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>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn create_alias_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
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)?;
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
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>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn delete_alias_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
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)?;
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
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>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-08-14 09:31:31 +00:00
|
|
|
pub async fn get_alias_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
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-12-19 15:00:11 +00:00
|
|
|
let response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
room_alias.server_name(),
|
2020-12-19 15:00:11 +00:00
|
|
|
federation::query::get_room_information::v1::Request { room_alias },
|
|
|
|
)
|
|
|
|
.await?;
|
2020-08-14 09:31:31 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 09:33:44 +00:00
|
|
|
let mut room_id = None;
|
|
|
|
match db.rooms.id_from_alias(&room_alias)? {
|
|
|
|
Some(r) => room_id = Some(r),
|
|
|
|
None => {
|
2021-08-02 08:13:34 +00:00
|
|
|
for (_id, registration) in db.appservice.all()? {
|
2021-03-03 20:41:26 +00:00
|
|
|
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:37:07 +00:00
|
|
|
.filter_map(|aliases| Regex::new(aliases.get("regex")?.as_str()?).ok())
|
2021-03-18 11:03:15 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
});
|
2021-03-03 20:41:26 +00:00
|
|
|
|
2021-03-18 11:03:15 +00:00
|
|
|
if aliases
|
|
|
|
.iter()
|
|
|
|
.any(|aliases| aliases.is_match(room_alias.as_str()))
|
2021-03-03 20:41:26 +00:00
|
|
|
&& db
|
|
|
|
.sending
|
|
|
|
.send_appservice_request(
|
|
|
|
&db.globals,
|
|
|
|
registration,
|
|
|
|
appservice::query::query_room_alias::v1::Request { room_alias },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.is_ok()
|
2020-12-08 09:33:44 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-09-14 09:00:31 +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
|
|
|
}
|