Merge branch 'send_join_v1' into 'master'

Implement federation/v1/send_join

See merge request famedly/conduit!141
next
Timo Kösters 2021-08-22 12:06:26 +00:00
commit 4f8cf3bed4
2 changed files with 71 additions and 51 deletions

View File

@ -160,7 +160,8 @@ fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<
server_server::get_room_state_route,
server_server::get_room_state_ids_route,
server_server::create_join_event_template_route,
server_server::create_join_event_route,
server_server::create_join_event_v1_route,
server_server::create_join_event_v2_route,
server_server::create_invite_route,
server_server::get_devices_route,
server_server::get_room_information_route,

View File

@ -2342,23 +2342,19 @@ pub fn create_join_event_template_route(
.into())
}
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/federation/v2/send_join/<_>/<_>", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn create_join_event_route(
db: DatabaseGuard,
body: Ruma<create_join_event::v2::Request<'_>>,
) -> ConduitResult<create_join_event::v2::Response> {
async fn create_join_event(
db: &DatabaseGuard,
room_id: &RoomId,
pdu: &Raw<ruma::events::pdu::Pdu>,
) -> Result<RoomState> {
if !db.globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled."));
}
// We need to return the state prior to joining, let's keep a reference to that here
let shortstatehash =
db.rooms
.current_shortstatehash(&body.room_id)?
let shortstatehash = db
.rooms
.current_shortstatehash(&room_id)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"Pdu state not found.",
@ -2368,7 +2364,7 @@ pub async fn create_join_event_route(
// let mut auth_cache = EventMap::new();
// We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&body.pdu) {
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&pdu) {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json
@ -2393,19 +2389,11 @@ pub async fn create_join_event_route(
.roomid_mutex_federation
.write()
.unwrap()
.entry(body.room_id.clone())
.entry(room_id.clone())
.or_default(),
);
let mutex_lock = mutex.lock().await;
let pdu_id = handle_incoming_pdu(
&origin,
&event_id,
&body.room_id,
value,
true,
&db,
&pub_key_map,
)
let pdu_id = handle_incoming_pdu(&origin, &event_id, &room_id, value, true, &db, &pub_key_map)
.await
.map_err(|e| {
warn!("Error while handling incoming send join PDU: {}", e);
@ -2425,7 +2413,7 @@ pub async fn create_join_event_route(
for server in db
.rooms
.room_servers(&body.room_id)
.room_servers(&room_id)
.filter_map(|r| r.ok())
.filter(|server| &**server != db.globals.server_name())
{
@ -2434,8 +2422,7 @@ pub async fn create_join_event_route(
db.flush()?;
Ok(create_join_event::v2::Response {
room_state: RoomState {
Ok(RoomState {
auth_chain: auth_chain_ids
.filter_map(|id| db.rooms.get_pdu_json(&id).ok().flatten())
.map(PduEvent::convert_to_outgoing_federation_event)
@ -2445,7 +2432,39 @@ pub async fn create_join_event_route(
.filter_map(|id| db.rooms.get_pdu_json(&id).ok().flatten())
.map(PduEvent::convert_to_outgoing_federation_event)
.collect(),
},
})
}
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/federation/v1/send_join/<_>/<_>", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn create_join_event_v1_route(
db: DatabaseGuard,
body: Ruma<create_join_event::v1::Request<'_>>,
) -> ConduitResult<create_join_event::v1::Response> {
let room_state = create_join_event(&db, &body.room_id, &body.pdu).await?;
Ok(create_join_event::v1::Response {
room_state: room_state,
}
.into())
}
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/federation/v2/send_join/<_>/<_>", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn create_join_event_v2_route(
db: DatabaseGuard,
body: Ruma<create_join_event::v2::Request<'_>>,
) -> ConduitResult<create_join_event::v2::Response> {
let room_state = create_join_event(&db, &body.room_id, &body.pdu).await?;
Ok(create_join_event::v2::Response {
room_state: room_state,
}
.into())
}