mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
The spec says that this behavior is optional, but it's what synapse does and we had a TODO for it.
253 lines
8.4 KiB
Rust
253 lines
8.4 KiB
Rust
use rand::seq::SliceRandom;
|
|
use ruma::{
|
|
api::{
|
|
appservice::query::query_room_alias,
|
|
client::{
|
|
alias::{create_alias, delete_alias, get_alias},
|
|
error::ErrorKind,
|
|
},
|
|
federation,
|
|
},
|
|
events::{
|
|
room::canonical_alias::RoomCanonicalAliasEventContent, StateEventType,
|
|
TimelineEventType,
|
|
},
|
|
OwnedRoomAliasId,
|
|
};
|
|
use serde_json::value::to_raw_value;
|
|
use tracing::info;
|
|
|
|
use crate::{service::pdu::PduBuilder, services, Ar, Error, Ra, Result};
|
|
|
|
/// # `PUT /_matrix/client/r0/directory/room/{roomAlias}`
|
|
///
|
|
/// Creates a new room alias on this server.
|
|
pub(crate) async fn create_alias_route(
|
|
body: Ar<create_alias::v3::Request>,
|
|
) -> Result<Ra<create_alias::v3::Response>> {
|
|
let sender_user =
|
|
body.sender_user.as_deref().expect("user is authenticated");
|
|
|
|
if body.room_alias.server_name() != services().globals.server_name() {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::InvalidParam,
|
|
"Alias is from another server.",
|
|
));
|
|
}
|
|
|
|
if let Some(info) = &body.appservice_info {
|
|
if !info.aliases.is_match(body.room_alias.as_str()) {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::Exclusive,
|
|
"Room alias is not in namespace.",
|
|
));
|
|
}
|
|
} else if services().appservice.is_exclusive_alias(&body.room_alias).await {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::Exclusive,
|
|
"Room alias reserved by appservice.",
|
|
));
|
|
}
|
|
|
|
if services().rooms.alias.resolve_local_alias(&body.room_alias)?.is_some() {
|
|
return Err(Error::Conflict("Alias already exists."));
|
|
}
|
|
|
|
services().rooms.alias.set_alias(
|
|
&body.room_alias,
|
|
&body.room_id,
|
|
sender_user,
|
|
)?;
|
|
|
|
Ok(Ra(create_alias::v3::Response::new()))
|
|
}
|
|
|
|
/// # `DELETE /_matrix/client/r0/directory/room/{roomAlias}`
|
|
///
|
|
/// Deletes a room alias from this server.
|
|
///
|
|
/// - TODO: additional access control checks
|
|
/// - TODO: Update canonical alias event
|
|
pub(crate) async fn delete_alias_route(
|
|
body: Ar<delete_alias::v3::Request>,
|
|
) -> Result<Ra<delete_alias::v3::Response>> {
|
|
let sender_user =
|
|
body.sender_user.as_deref().expect("user is authenticated");
|
|
|
|
if body.room_alias.server_name() != services().globals.server_name() {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::InvalidParam,
|
|
"Alias is from another server.",
|
|
));
|
|
}
|
|
|
|
let room_id = services()
|
|
.rooms
|
|
.alias
|
|
.resolve_local_alias(&body.room_alias)?
|
|
.ok_or_else(|| {
|
|
Error::BadRequest(ErrorKind::NotFound, "Alias is not assigned.")
|
|
})?;
|
|
|
|
if let Some(info) = &body.appservice_info {
|
|
if !info.aliases.is_match(body.room_alias.as_str()) {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::Exclusive,
|
|
"Room alias is not in namespace.",
|
|
));
|
|
}
|
|
} else if services().appservice.is_exclusive_alias(&body.room_alias).await {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::Exclusive,
|
|
"Room alias reserved by appservice.",
|
|
));
|
|
}
|
|
|
|
services().rooms.alias.remove_alias(&body.room_alias, sender_user)?;
|
|
|
|
// Attempt to remove the alias from the m.room.canonical_alias event, if it
|
|
// was present.
|
|
let canonical_alias: Option<RoomCanonicalAliasEventContent> = services()
|
|
.rooms
|
|
.state_accessor
|
|
.room_state_get(&room_id, &StateEventType::RoomCanonicalAlias, "")?
|
|
.and_then(|event| {
|
|
serde_json::from_str(event.content.get())
|
|
.inspect_err(|_| {
|
|
Error::bad_database("invalid m.room.canonical_alias event");
|
|
})
|
|
.ok()
|
|
});
|
|
if let Some(mut canonical_alias) = canonical_alias {
|
|
let mut changed = false;
|
|
if canonical_alias.alias.as_ref() == Some(&body.room_alias) {
|
|
canonical_alias.alias = None;
|
|
changed = true;
|
|
}
|
|
canonical_alias.alt_aliases.retain(|a| {
|
|
let equal = a == &body.room_alias;
|
|
changed |= equal;
|
|
!equal
|
|
});
|
|
|
|
if changed {
|
|
let room_token = services()
|
|
.globals
|
|
.roomid_mutex_state
|
|
.lock_key(room_id.clone())
|
|
.await;
|
|
|
|
if let Err(error) = services()
|
|
.rooms
|
|
.timeline
|
|
.build_and_append_pdu(
|
|
PduBuilder {
|
|
event_type: TimelineEventType::RoomCanonicalAlias,
|
|
content: to_raw_value(&canonical_alias)
|
|
.expect("event is valid"),
|
|
unsigned: None,
|
|
state_key: Some(String::new()),
|
|
redacts: None,
|
|
},
|
|
sender_user,
|
|
&room_token,
|
|
)
|
|
.await
|
|
{
|
|
// > Servers which choose to update the canonical alias event
|
|
// > are recommended to, in addition to their other relevant
|
|
// > permission checks, delete the alias and return a successful
|
|
// > response even if the user does not have permission to
|
|
// > update the m.room.canonical_alias event.
|
|
//
|
|
// <https://spec.matrix.org/v1.14/client-server-api/#delete_matrixclientv3directoryroomroomalias>
|
|
info!(%error, "Failed to remove canonical alias");
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(Ra(delete_alias::v3::Response::new()))
|
|
}
|
|
|
|
/// # `GET /_matrix/client/r0/directory/room/{roomAlias}`
|
|
///
|
|
/// Resolve an alias locally or over federation.
|
|
///
|
|
/// - TODO: Suggest more servers to join via
|
|
pub(crate) async fn get_alias_route(
|
|
body: Ar<get_alias::v3::Request>,
|
|
) -> Result<Ra<get_alias::v3::Response>> {
|
|
get_alias_helper(body.body.room_alias).await.map(Ra)
|
|
}
|
|
|
|
// Can't use `services().rooms.alias.resolve_alias` because we also need the set
|
|
// of servers from the remote get_room_information request.
|
|
pub(crate) async fn get_alias_helper(
|
|
room_alias: OwnedRoomAliasId,
|
|
) -> Result<get_alias::v3::Response> {
|
|
if room_alias.server_name() != services().globals.server_name() {
|
|
let response = services()
|
|
.sending
|
|
.send_federation_request(
|
|
room_alias.server_name(),
|
|
federation::query::get_room_information::v1::Request {
|
|
room_alias: room_alias.clone(),
|
|
},
|
|
)
|
|
.await?;
|
|
|
|
let mut servers = response.servers;
|
|
servers.shuffle(&mut rand::thread_rng());
|
|
|
|
return Ok(get_alias::v3::Response::new(response.room_id, servers));
|
|
}
|
|
|
|
let mut room_id = None;
|
|
match services().rooms.alias.resolve_local_alias(&room_alias)? {
|
|
Some(r) => room_id = Some(r),
|
|
None => {
|
|
for appservice in services().appservice.read().await.values() {
|
|
if appservice.aliases.is_match(room_alias.as_str())
|
|
&& matches!(
|
|
services()
|
|
.sending
|
|
.send_appservice_request(
|
|
appservice.registration.clone(),
|
|
query_room_alias::v1::Request {
|
|
room_alias: room_alias.clone(),
|
|
},
|
|
)
|
|
.await,
|
|
Ok(Some(_opt_result))
|
|
)
|
|
{
|
|
room_id = Some(
|
|
services()
|
|
.rooms
|
|
.alias
|
|
.resolve_local_alias(&room_alias)?
|
|
.ok_or_else(|| {
|
|
Error::bad_config(
|
|
"Appservice lied to us. Room does not \
|
|
exist.",
|
|
)
|
|
})?,
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
let Some(room_id) = room_id else {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::NotFound,
|
|
"Room with alias not found.",
|
|
));
|
|
};
|
|
|
|
Ok(get_alias::v3::Response::new(
|
|
room_id,
|
|
vec![services().globals.server_name().to_owned()],
|
|
))
|
|
}
|