|
|
|
@ -9,8 +9,8 @@ use ruma::{ |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
events::{AnyStateEventContent, EventContent}, |
|
|
|
events::{AnyStateEventContent, EventContent}, |
|
|
|
|
|
|
|
RoomId, UserId, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::convert::TryFrom; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")] |
|
|
|
#[cfg(feature = "conduit_bin")] |
|
|
|
use rocket::{get, put}; |
|
|
|
use rocket::{get, put}; |
|
|
|
@ -33,45 +33,14 @@ pub fn send_state_event_for_key_route( |
|
|
|
) |
|
|
|
) |
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; |
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; |
|
|
|
|
|
|
|
|
|
|
|
if let AnyStateEventContent::RoomCanonicalAlias(canonical_alias) = &body.content { |
|
|
|
send_state_event_for_key_helper( |
|
|
|
let mut aliases = canonical_alias.alt_aliases.clone(); |
|
|
|
&db, |
|
|
|
|
|
|
|
sender_id, |
|
|
|
if let Some(alias) = canonical_alias.alias.clone() { |
|
|
|
&body.content, |
|
|
|
aliases.push(alias); |
|
|
|
content, |
|
|
|
} |
|
|
|
&body.room_id, |
|
|
|
|
|
|
|
Some(body.state_key.clone()), |
|
|
|
for alias in aliases { |
|
|
|
) |
|
|
|
if alias.server_name() != db.globals.server_name() |
|
|
|
|
|
|
|
|| db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.id_from_alias(&alias)? |
|
|
|
|
|
|
|
.filter(|room| room == &body.room_id) // Make sure it's the right room
|
|
|
|
|
|
|
|
.is_none() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return Err(Error::BadRequest( |
|
|
|
|
|
|
|
ErrorKind::Forbidden, |
|
|
|
|
|
|
|
"You are only allowed to send canonical_alias \ |
|
|
|
|
|
|
|
events when it's aliases already exists", |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let event_id = db.rooms.build_and_append_pdu( |
|
|
|
|
|
|
|
PduBuilder { |
|
|
|
|
|
|
|
room_id: body.room_id.clone(), |
|
|
|
|
|
|
|
sender: sender_id.clone(), |
|
|
|
|
|
|
|
event_type: body.content.event_type().into(), |
|
|
|
|
|
|
|
content, |
|
|
|
|
|
|
|
unsigned: None, |
|
|
|
|
|
|
|
state_key: Some(body.state_key.clone()), |
|
|
|
|
|
|
|
redacts: None, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(send_state_event_for_key::Response::new(event_id).into()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
#[cfg_attr(
|
|
|
|
@ -84,34 +53,30 @@ pub fn send_state_event_for_empty_key_route( |
|
|
|
) -> ConduitResult<send_state_event_for_empty_key::Response> { |
|
|
|
) -> ConduitResult<send_state_event_for_empty_key::Response> { |
|
|
|
// This just calls send_state_event_for_key_route
|
|
|
|
// This just calls send_state_event_for_key_route
|
|
|
|
let Ruma { |
|
|
|
let Ruma { |
|
|
|
body: |
|
|
|
body, |
|
|
|
send_state_event_for_empty_key::IncomingRequest { |
|
|
|
|
|
|
|
room_id, content, .. |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
sender_id, |
|
|
|
sender_id, |
|
|
|
device_id, |
|
|
|
device_id: _, |
|
|
|
json_body, |
|
|
|
json_body, |
|
|
|
} = body; |
|
|
|
} = body; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let json = serde_json::from_str::<serde_json::Value>( |
|
|
|
|
|
|
|
json_body |
|
|
|
|
|
|
|
.as_ref() |
|
|
|
|
|
|
|
.ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? |
|
|
|
|
|
|
|
.get(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; |
|
|
|
|
|
|
|
|
|
|
|
Ok(send_state_event_for_empty_key::Response::new( |
|
|
|
Ok(send_state_event_for_empty_key::Response::new( |
|
|
|
send_state_event_for_key_route( |
|
|
|
send_state_event_for_key_helper( |
|
|
|
db, |
|
|
|
&db, |
|
|
|
Ruma { |
|
|
|
sender_id |
|
|
|
body: send_state_event_for_key::IncomingRequest::try_from(http::Request::new( |
|
|
|
.as_ref() |
|
|
|
serde_json::json!({ |
|
|
|
.expect("no user for send state empty key rout"), |
|
|
|
"room_id": room_id, |
|
|
|
&body.content, |
|
|
|
"state_key": "", |
|
|
|
json, |
|
|
|
"content": content, |
|
|
|
&body.room_id, |
|
|
|
}) |
|
|
|
None, |
|
|
|
.to_string() |
|
|
|
|
|
|
|
.as_bytes() |
|
|
|
|
|
|
|
.to_vec(), |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
sender_id, |
|
|
|
|
|
|
|
device_id, |
|
|
|
|
|
|
|
json_body, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
)? |
|
|
|
)? |
|
|
|
.0 |
|
|
|
.0 |
|
|
|
.event_id, |
|
|
|
.event_id, |
|
|
|
@ -210,3 +175,54 @@ pub fn get_state_events_for_empty_key_route( |
|
|
|
} |
|
|
|
} |
|
|
|
.into()) |
|
|
|
.into()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn send_state_event_for_key_helper( |
|
|
|
|
|
|
|
db: &Database, |
|
|
|
|
|
|
|
sender: &UserId, |
|
|
|
|
|
|
|
content: &AnyStateEventContent, |
|
|
|
|
|
|
|
json: serde_json::Value, |
|
|
|
|
|
|
|
room_id: &RoomId, |
|
|
|
|
|
|
|
state_key: Option<String>, |
|
|
|
|
|
|
|
) -> ConduitResult<send_state_event_for_key::Response> { |
|
|
|
|
|
|
|
let sender_id = sender; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let AnyStateEventContent::RoomCanonicalAlias(canonical_alias) = content { |
|
|
|
|
|
|
|
let mut aliases = canonical_alias.alt_aliases.clone(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(alias) = canonical_alias.alias.clone() { |
|
|
|
|
|
|
|
aliases.push(alias); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for alias in aliases { |
|
|
|
|
|
|
|
if alias.server_name() != db.globals.server_name() |
|
|
|
|
|
|
|
|| db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.id_from_alias(&alias)? |
|
|
|
|
|
|
|
.filter(|room| room == room_id) // Make sure it's the right room
|
|
|
|
|
|
|
|
.is_none() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return Err(Error::BadRequest( |
|
|
|
|
|
|
|
ErrorKind::Forbidden, |
|
|
|
|
|
|
|
"You are only allowed to send canonical_alias \ |
|
|
|
|
|
|
|
events when it's aliases already exists", |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let event_id = db.rooms.build_and_append_pdu( |
|
|
|
|
|
|
|
PduBuilder { |
|
|
|
|
|
|
|
room_id: room_id.clone(), |
|
|
|
|
|
|
|
sender: sender_id.clone(), |
|
|
|
|
|
|
|
event_type: content.event_type().into(), |
|
|
|
|
|
|
|
content: json, |
|
|
|
|
|
|
|
unsigned: None, |
|
|
|
|
|
|
|
state_key, |
|
|
|
|
|
|
|
redacts: None, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(send_state_event_for_key::Response::new(event_id).into()) |
|
|
|
|
|
|
|
} |
|
|
|
|