add resolve_with_servers() to alias service; simplify api

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-14 01:01:12 +00:00 committed by strawberry
parent b4ec1e9d3c
commit d0ee4b6d25
5 changed files with 131 additions and 123 deletions

View file

@ -381,13 +381,18 @@ pub(super) async fn force_join_room(
&self, user_id: String, room_id: OwnedRoomOrAliasId, &self, user_id: String, room_id: OwnedRoomOrAliasId,
) -> Result<RoomMessageEventContent> { ) -> Result<RoomMessageEventContent> {
let user_id = parse_local_user_id(self.services, &user_id)?; let user_id = parse_local_user_id(self.services, &user_id)?;
let room_id = self.services.rooms.alias.resolve(&room_id).await?; let (room_id, servers) = self
.services
.rooms
.alias
.resolve_with_servers(&room_id, None)
.await?;
assert!( assert!(
self.services.globals.user_is_local(&user_id), self.services.globals.user_is_local(&user_id),
"Parsed user_id must be a local user" "Parsed user_id must be a local user"
); );
join_room_by_id_helper(self.services, &user_id, &room_id, None, &[], None, &None).await?; join_room_by_id_helper(self.services, &user_id, &room_id, None, &servers, None, &None).await?;
Ok(RoomMessageEventContent::notice_markdown(format!( Ok(RoomMessageEventContent::notice_markdown(format!(
"{user_id} has been joined to {room_id}.", "{user_id} has been joined to {room_id}.",

View file

@ -86,25 +86,19 @@ pub(crate) async fn get_alias_route(
State(services): State<crate::State>, body: Ruma<get_alias::v3::Request>, State(services): State<crate::State>, body: Ruma<get_alias::v3::Request>,
) -> Result<get_alias::v3::Response> { ) -> Result<get_alias::v3::Response> {
let room_alias = body.body.room_alias; let room_alias = body.body.room_alias;
let servers = None;
let Ok((room_id, pre_servers)) = services let Ok((room_id, servers)) = services.rooms.alias.resolve_alias(&room_alias, None).await else {
.rooms
.alias
.resolve_alias(&room_alias, servers.as_ref())
.await
else {
return Err!(Request(NotFound("Room with alias not found."))); return Err!(Request(NotFound("Room with alias not found.")));
}; };
let servers = room_available_servers(&services, &room_id, &room_alias, &pre_servers).await; let servers = room_available_servers(&services, &room_id, &room_alias, servers).await;
debug!(?room_alias, ?room_id, "available servers: {servers:?}"); debug!(?room_alias, ?room_id, "available servers: {servers:?}");
Ok(get_alias::v3::Response::new(room_id, servers)) Ok(get_alias::v3::Response::new(room_id, servers))
} }
async fn room_available_servers( async fn room_available_servers(
services: &Services, room_id: &RoomId, room_alias: &RoomAliasId, pre_servers: &Option<Vec<OwnedServerName>>, services: &Services, room_id: &RoomId, room_alias: &RoomAliasId, pre_servers: Vec<OwnedServerName>,
) -> Vec<OwnedServerName> { ) -> Vec<OwnedServerName> {
// find active servers in room state cache to suggest // find active servers in room state cache to suggest
let mut servers: Vec<OwnedServerName> = services let mut servers: Vec<OwnedServerName> = services
@ -117,9 +111,7 @@ async fn room_available_servers(
// push any servers we want in the list already (e.g. responded remote alias // push any servers we want in the list already (e.g. responded remote alias
// servers, room alias server itself) // servers, room alias server itself)
if let Some(pre_servers) = pre_servers { servers.extend(pre_servers);
servers.extend(pre_servers.clone());
};
servers.sort_unstable(); servers.sort_unstable();
servers.dedup(); servers.dedup();

View file

@ -9,8 +9,9 @@ use axum_client_ip::InsecureClientIp;
use conduit::{ use conduit::{
debug, debug_info, debug_warn, err, error, info, pdu, debug, debug_info, debug_warn, err, error, info, pdu,
pdu::{gen_event_id_canonical_json, PduBuilder}, pdu::{gen_event_id_canonical_json, PduBuilder},
result::FlatOk,
trace, utils, trace, utils,
utils::{IterStream, ReadyExt}, utils::{shuffle, IterStream, ReadyExt},
warn, Err, Error, PduEvent, Result, warn, Err, Error, PduEvent, Result,
}; };
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
@ -188,6 +189,10 @@ pub(crate) async fn join_room_by_id_route(
servers.push(server.into()); servers.push(server.into());
} }
servers.sort_unstable();
servers.dedup();
shuffle(&mut servers);
join_room_by_id_helper( join_room_by_id_helper(
&services, &services,
sender_user, sender_user,
@ -251,45 +256,48 @@ pub(crate) async fn join_room_by_id_or_alias_route(
servers.push(server.to_owned()); servers.push(server.to_owned());
} }
servers.sort_unstable();
servers.dedup();
shuffle(&mut servers);
(servers, room_id) (servers, room_id)
}, },
Err(room_alias) => { Err(room_alias) => {
let response = services let (room_id, mut servers) = services
.rooms .rooms
.alias .alias
.resolve_alias(&room_alias, Some(&body.via.clone())) .resolve_alias(&room_alias, Some(body.via.clone()))
.await?; .await?;
let (room_id, mut pre_servers) = response;
banned_room_check(&services, sender_user, Some(&room_id), Some(room_alias.server_name()), client).await?; banned_room_check(&services, sender_user, Some(&room_id), Some(room_alias.server_name()), client).await?;
let mut servers = body.via; let addl_via_servers = services
if let Some(pre_servers) = &mut pre_servers { .rooms
servers.append(pre_servers); .state_cache
} .servers_invite_via(&room_id)
.map(ToOwned::to_owned);
servers.extend( let addl_state_servers = services
services .rooms
.rooms .state_cache
.state_cache .invite_state(sender_user, &room_id)
.servers_invite_via(&room_id) .await
.map(ToOwned::to_owned) .unwrap_or_default();
.collect::<Vec<_>>()
.await,
);
servers.extend( let mut addl_servers: Vec<_> = addl_state_servers
services .iter()
.rooms .map(|event| event.get_field("sender"))
.state_cache .filter_map(FlatOk::flat_ok)
.invite_state(sender_user, &room_id) .map(|user: &UserId| user.server_name().to_owned())
.await .stream()
.unwrap_or_default() .chain(addl_via_servers)
.iter() .collect()
.filter_map(|event| event.get_field("sender").ok().flatten()) .await;
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()), addl_servers.sort_unstable();
); addl_servers.dedup();
shuffle(&mut addl_servers);
servers.append(&mut addl_servers);
(servers, room_id) (servers, room_id)
}, },

View file

@ -112,40 +112,51 @@ impl Service {
Ok(()) Ok(())
} }
#[inline]
pub async fn resolve(&self, room: &RoomOrAliasId) -> Result<OwnedRoomId> { pub async fn resolve(&self, room: &RoomOrAliasId) -> Result<OwnedRoomId> {
self.resolve_with_servers(room, None)
.await
.map(|(room_id, _)| room_id)
}
pub async fn resolve_with_servers(
&self, room: &RoomOrAliasId, servers: Option<Vec<OwnedServerName>>,
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
if room.is_room_id() { if room.is_room_id() {
let room_id: &RoomId = &RoomId::parse(room).expect("valid RoomId"); let room_id = RoomId::parse(room).expect("valid RoomId");
Ok(room_id.to_owned()) Ok((room_id, servers.unwrap_or_default()))
} else { } else {
let alias: &RoomAliasId = &RoomAliasId::parse(room).expect("valid RoomAliasId"); let alias = &RoomAliasId::parse(room).expect("valid RoomAliasId");
Ok(self.resolve_alias(alias, None).await?.0) self.resolve_alias(alias, servers).await
} }
} }
#[tracing::instrument(skip(self), name = "resolve")] #[tracing::instrument(skip(self), name = "resolve")]
pub async fn resolve_alias( pub async fn resolve_alias(
&self, room_alias: &RoomAliasId, servers: Option<&Vec<OwnedServerName>>, &self, room_alias: &RoomAliasId, servers: Option<Vec<OwnedServerName>>,
) -> Result<(OwnedRoomId, Option<Vec<OwnedServerName>>)> { ) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
if !self let server_name = room_alias.server_name();
.services let server_is_ours = self.services.globals.server_is_ours(server_name);
.globals let servers_contains_ours = || {
.server_is_ours(room_alias.server_name()) servers
&& (!servers
.as_ref() .as_ref()
.is_some_and(|servers| servers.contains(&self.services.globals.server_name().to_owned())) .is_some_and(|servers| servers.contains(&self.services.globals.config.server_name))
|| servers.as_ref().is_none()) };
{
return self.remote_resolve(room_alias, servers).await; if !server_is_ours && !servers_contains_ours() {
return self
.remote_resolve(room_alias, servers.unwrap_or_default())
.await;
} }
let room_id: Option<OwnedRoomId> = match self.resolve_local_alias(room_alias).await { let room_id = match self.resolve_local_alias(room_alias).await {
Ok(r) => Some(r), Ok(r) => Some(r),
Err(_) => self.resolve_appservice_alias(room_alias).await?, Err(_) => self.resolve_appservice_alias(room_alias).await?,
}; };
room_id.map_or_else( room_id.map_or_else(
|| Err(Error::BadRequest(ErrorKind::NotFound, "Room with alias not found.")), || Err!(Request(NotFound("Room with alias not found."))),
|room_id| Ok((room_id, None)), |room_id| Ok((room_id, Vec::new())),
) )
} }

View file

@ -1,75 +1,67 @@
use conduit::{debug, debug_warn, Error, Result}; use std::iter::once;
use ruma::{
api::{client::error::ErrorKind, federation},
OwnedRoomId, OwnedServerName, RoomAliasId,
};
impl super::Service { use conduit::{debug, debug_error, err, implement, Result};
pub(super) async fn remote_resolve( use federation::query::get_room_information::v1::Response;
&self, room_alias: &RoomAliasId, servers: Option<&Vec<OwnedServerName>>, use ruma::{api::federation, OwnedRoomId, OwnedServerName, RoomAliasId, ServerName};
) -> Result<(OwnedRoomId, Option<Vec<OwnedServerName>>)> {
debug!(?room_alias, ?servers, "resolve");
let mut response = self #[implement(super::Service)]
.services pub(super) async fn remote_resolve(
.sending &self, room_alias: &RoomAliasId, servers: Vec<OwnedServerName>,
.send_federation_request( ) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
room_alias.server_name(), debug!(?room_alias, servers = ?servers, "resolve");
federation::query::get_room_information::v1::Request { let servers = once(room_alias.server_name())
room_alias: room_alias.to_owned(), .map(ToOwned::to_owned)
}, .chain(servers.into_iter());
)
.await;
debug!("room alias server_name get_alias_helper response: {response:?}"); let mut resolved_servers = Vec::new();
let mut resolved_room_id: Option<OwnedRoomId> = None;
for server in servers {
match self.remote_request(room_alias, &server).await {
Err(e) => debug_error!("Failed to query for {room_alias:?} from {server}: {e}"),
Ok(Response {
room_id,
servers,
}) => {
debug!("Server {server} answered with {room_id:?} for {room_alias:?} servers: {servers:?}");
if let Err(ref e) = response { resolved_room_id.get_or_insert(room_id);
debug_warn!( add_server(&mut resolved_servers, server);
"Server {} of the original room alias failed to assist in resolving room alias: {e}",
room_alias.server_name(),
);
}
if response.as_ref().is_ok_and(|resp| resp.servers.is_empty()) || response.as_ref().is_err() { if !servers.is_empty() {
if let Some(servers) = servers { add_servers(&mut resolved_servers, servers);
for server in servers { break;
response = self
.services
.sending
.send_federation_request(
server,
federation::query::get_room_information::v1::Request {
room_alias: room_alias.to_owned(),
},
)
.await;
debug!("Got response from server {server} for room aliases: {response:?}");
if let Ok(ref response) = response {
if !response.servers.is_empty() {
break;
}
debug_warn!(
"Server {server} responded with room aliases, but was empty? Response: {response:?}"
);
}
} }
} },
} }
}
if let Ok(response) = response { resolved_room_id
let room_id = response.room_id; .map(|room_id| (room_id, resolved_servers))
.ok_or_else(|| err!(Request(NotFound("No servers could assist in resolving the room alias"))))
}
let mut pre_servers = response.servers; #[implement(super::Service)]
// since the room alis server responded, insert it into the list async fn remote_request(&self, room_alias: &RoomAliasId, server: &ServerName) -> Result<Response> {
pre_servers.push(room_alias.server_name().into()); use federation::query::get_room_information::v1::Request;
return Ok((room_id, Some(pre_servers))); let request = Request {
} room_alias: room_alias.to_owned(),
};
Err(Error::BadRequest( self.services
ErrorKind::NotFound, .sending
"No servers could assist in resolving the room alias", .send_federation_request(server, request)
)) .await
}
fn add_servers(servers: &mut Vec<OwnedServerName>, new: Vec<OwnedServerName>) {
for server in new {
add_server(servers, server);
}
}
fn add_server(servers: &mut Vec<OwnedServerName>, server: OwnedServerName) {
if !servers.contains(&server) {
servers.push(server);
} }
} }