|
|
|
@ -9,8 +9,8 @@ use ruma::{
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
events::{AnyStateEventContent, EventContent},
|
|
|
|
|
RoomId, UserId,
|
|
|
|
|
};
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
|
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."))?;
|
|
|
|
|
|
|
|
|
|
if let AnyStateEventContent::RoomCanonicalAlias(canonical_alias) = &body.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 == &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())
|
|
|
|
|
send_state_event_for_key_helper(
|
|
|
|
|
&db,
|
|
|
|
|
sender_id,
|
|
|
|
|
&body.content,
|
|
|
|
|
content,
|
|
|
|
|
&body.room_id,
|
|
|
|
|
Some(body.state_key.clone()),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
@ -84,34 +53,30 @@ pub fn send_state_event_for_empty_key_route(
|
|
|
|
|
) -> ConduitResult<send_state_event_for_empty_key::Response> {
|
|
|
|
|
// This just calls send_state_event_for_key_route
|
|
|
|
|
let Ruma {
|
|
|
|
|
body:
|
|
|
|
|
send_state_event_for_empty_key::IncomingRequest {
|
|
|
|
|
room_id, content, ..
|
|
|
|
|
},
|
|
|
|
|
body,
|
|
|
|
|
sender_id,
|
|
|
|
|
device_id,
|
|
|
|
|
device_id: _,
|
|
|
|
|
json_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(
|
|
|
|
|
send_state_event_for_key_route(
|
|
|
|
|
db,
|
|
|
|
|
Ruma {
|
|
|
|
|
body: send_state_event_for_key::IncomingRequest::try_from(http::Request::new(
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"room_id": room_id,
|
|
|
|
|
"state_key": "",
|
|
|
|
|
"content": content,
|
|
|
|
|
})
|
|
|
|
|
.to_string()
|
|
|
|
|
.as_bytes()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
sender_id,
|
|
|
|
|
device_id,
|
|
|
|
|
json_body,
|
|
|
|
|
},
|
|
|
|
|
send_state_event_for_key_helper(
|
|
|
|
|
&db,
|
|
|
|
|
sender_id
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("no user for send state empty key rout"),
|
|
|
|
|
&body.content,
|
|
|
|
|
json,
|
|
|
|
|
&body.room_id,
|
|
|
|
|
None,
|
|
|
|
|
)?
|
|
|
|
|
.0
|
|
|
|
|
.event_id,
|
|
|
|
@ -210,3 +175,54 @@ pub fn get_state_events_for_empty_key_route(
|
|
|
|
|
}
|
|
|
|
|
.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())
|
|
|
|
|
}
|
|
|
|
|