Add helper function for get_alias route

next
Devin Ragotzy 2020-08-20 12:12:02 -04:00
parent ea20032404
commit 5ccdd3694b
2 changed files with 28 additions and 35 deletions

View File

@ -1,11 +1,14 @@
use super::State;
use crate::{server_server, ConduitResult, Database, Error, Ruma};
use ruma::api::{
client::{
error::ErrorKind,
r0::alias::{create_alias, delete_alias, get_alias},
use ruma::{
api::{
client::{
error::ErrorKind,
r0::alias::{create_alias, delete_alias, get_alias},
},
federation,
},
federation,
RoomAliasId,
};
#[cfg(feature = "conduit_bin")]
@ -50,12 +53,19 @@ pub async fn get_alias_route(
db: State<'_, Database>,
body: Ruma<get_alias::IncomingRequest>,
) -> ConduitResult<get_alias::Response> {
if body.room_alias.server_name() != db.globals.server_name() {
get_alias_helper(db, &body.room_alias).await
}
pub async fn get_alias_helper(
db: State<'_, Database>,
room_alias: &RoomAliasId,
) -> ConduitResult<get_alias::Response> {
if room_alias.server_name() != db.globals.server_name() {
let response = server_server::send_request(
&db,
body.room_alias.server_name().to_string(),
room_alias.server_name().to_string(),
federation::query::get_room_information::v1::Request {
room_alias: body.room_alias.to_string(),
room_alias: room_alias.to_string(),
},
)
.await?;
@ -65,7 +75,7 @@ pub async fn get_alias_route(
let room_id = db
.rooms
.id_from_alias(&body.room_alias)?
.id_from_alias(&room_alias)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"Room with alias not found.",

View File

@ -8,13 +8,10 @@ use ruma::{
api::{
client::{
error::ErrorKind,
r0::{
alias,
membership::{
ban_user, forget_room, get_member_events, invite_user, join_room_by_id,
join_room_by_id_or_alias, joined_members, joined_rooms, kick_user, leave_room,
unban_user, IncomingThirdPartySigned,
},
r0::membership::{
ban_user, forget_room, get_member_events, invite_user, join_room_by_id,
join_room_by_id_or_alias, joined_members, joined_rooms, kick_user, leave_room,
unban_user, IncomingThirdPartySigned,
},
},
federation,
@ -58,24 +55,10 @@ pub async fn join_room_by_id_or_alias_route(
let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) {
Ok(room_id) => room_id,
Err(room_alias) => {
client_server::get_alias_route(
db,
Ruma {
body: alias::get_alias::IncomingRequest::try_from(http::Request::new(
serde_json::json!({ "room_alias": room_alias, })
.to_string()
.as_bytes()
.to_vec(),
))
.unwrap(),
sender_id: body.sender_id.clone(),
device_id: body.device_id.clone(),
json_body: None,
},
)
.await?
.0
.room_id
client_server::get_alias_helper(db, &room_alias)
.await?
.0
.room_id
}
};
@ -495,7 +478,7 @@ async fn join_room_by_id_helper(
room_id: room_id.clone(),
event_id,
pdu_stub: serde_json::from_value(join_event_stub_value)
.expect("Raw::from_value always works"),
.expect("we just created this event"),
},
)
.await?;