2020-07-30 16:14:47 +00:00
|
|
|
use super::State;
|
2020-08-26 15:15:52 +00:00
|
|
|
use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::state::{
|
2020-08-12 21:32:39 +00:00
|
|
|
get_state_events, get_state_events_for_empty_key, get_state_events_for_key,
|
|
|
|
send_state_event_for_empty_key, send_state_event_for_key,
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-18 14:19:14 +00:00
|
|
|
events::{
|
2020-11-08 19:45:52 +00:00
|
|
|
room::history_visibility::{HistoryVisibility, HistoryVisibilityEventContent},
|
|
|
|
AnyStateEventContent, EventContent, EventType,
|
2020-10-18 14:19:14 +00:00
|
|
|
},
|
2020-08-26 15:15:52 +00:00
|
|
|
EventId, RoomId, UserId,
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, put};
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn send_state_event_for_key_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<send_state_event_for_key::Request<'_>>,
|
2020-08-12 21:32:39 +00:00
|
|
|
) -> ConduitResult<send_state_event_for_key::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let content = serde_json::from_str::<serde_json::Value>(
|
|
|
|
body.json_body
|
|
|
|
.as_ref()
|
|
|
|
.ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?
|
|
|
|
.get(),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?;
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
let event_id = send_state_event_for_key_helper(
|
|
|
|
&db,
|
|
|
|
sender_user,
|
|
|
|
&body.content,
|
|
|
|
content,
|
|
|
|
&body.room_id,
|
|
|
|
Some(body.state_key.to_owned()),
|
2020-08-21 21:19:18 +00:00
|
|
|
)
|
2020-10-21 19:28:02 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(send_state_event_for_key::Response { event_id }.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
|
|
|
)]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn send_state_event_for_empty_key_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<send_state_event_for_empty_key::Request<'_>>,
|
2020-08-12 21:32:39 +00:00
|
|
|
) -> ConduitResult<send_state_event_for_empty_key::Response> {
|
|
|
|
// This just calls send_state_event_for_key_route
|
2020-07-30 16:14:47 +00:00
|
|
|
let Ruma {
|
2020-08-21 21:19:18 +00:00
|
|
|
body,
|
2020-10-18 18:33:12 +00:00
|
|
|
sender_user,
|
|
|
|
sender_device: _,
|
2020-07-30 16:14:47 +00:00
|
|
|
json_body,
|
|
|
|
} = body;
|
|
|
|
|
2020-08-21 21:19:18 +00:00
|
|
|
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."))?;
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
let event_id = send_state_event_for_key_helper(
|
|
|
|
&db,
|
|
|
|
sender_user
|
|
|
|
.as_ref()
|
|
|
|
.expect("no user for send state empty key rout"),
|
|
|
|
&body.content,
|
|
|
|
json,
|
|
|
|
&body.room_id,
|
|
|
|
Some("".into()),
|
2020-08-06 12:29:59 +00:00
|
|
|
)
|
2020-10-21 19:28:02 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(send_state_event_for_empty_key::Response { event_id }.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state", data = "<body>")
|
|
|
|
)]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_state_events_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-10-27 23:10:09 +00:00
|
|
|
body: Ruma<get_state_events::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_state_events::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-11-08 19:45:52 +00:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 14:19:14 +00:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 23:10:09 +00:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 14:19:14 +00:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2020-10-27 19:25:43 +00:00
|
|
|
.map(|(_, event)| {
|
2020-10-18 14:19:14 +00:00
|
|
|
serde_json::from_value::<HistoryVisibilityEventContent>(event.content)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|e| e.history_visibility)
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 23:10:09 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(get_state_events::Response {
|
|
|
|
room_state: db
|
|
|
|
.rooms
|
|
|
|
.room_state_full(&body.room_id)?
|
|
|
|
.values()
|
|
|
|
.map(|pdu| pdu.to_state_event())
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_state_events_for_key_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-10-27 23:10:09 +00:00
|
|
|
body: Ruma<get_state_events_for_key::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_state_events_for_key::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-11-08 19:45:52 +00:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 14:19:14 +00:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 23:10:09 +00:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 14:19:14 +00:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2020-10-27 19:25:43 +00:00
|
|
|
.map(|(_, event)| {
|
2020-10-18 14:19:14 +00:00
|
|
|
serde_json::from_value::<HistoryVisibilityEventContent>(event.content)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|e| e.history_visibility)
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 23:10:09 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let event = db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(&body.room_id, &body.event_type, &body.state_key)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"State event not found.",
|
2020-10-27 19:25:43 +00:00
|
|
|
))?
|
|
|
|
.1;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
Ok(get_state_events_for_key::Response {
|
|
|
|
content: serde_json::value::to_raw_value(&event.content)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
|
|
|
)]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_state_events_for_empty_key_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-10-27 23:10:09 +00:00
|
|
|
body: Ruma<get_state_events_for_empty_key::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_state_events_for_empty_key::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-11-08 19:45:52 +00:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 14:19:14 +00:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 23:10:09 +00:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 14:19:14 +00:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2020-10-27 19:25:43 +00:00
|
|
|
.map(|(_, event)| {
|
2020-10-18 14:19:14 +00:00
|
|
|
serde_json::from_value::<HistoryVisibilityEventContent>(event.content)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|e| e.history_visibility)
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 23:10:09 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let event = db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(&body.room_id, &body.event_type, "")?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"State event not found.",
|
2020-10-27 19:25:43 +00:00
|
|
|
))?
|
|
|
|
.1;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
Ok(get_state_events_for_empty_key::Response {
|
|
|
|
content: serde_json::value::to_raw_value(&event)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
2020-08-21 21:19:18 +00:00
|
|
|
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn send_state_event_for_key_helper(
|
2020-08-21 21:19:18 +00:00
|
|
|
db: &Database,
|
|
|
|
sender: &UserId,
|
|
|
|
content: &AnyStateEventContent,
|
|
|
|
json: serde_json::Value,
|
|
|
|
room_id: &RoomId,
|
|
|
|
state_key: Option<String>,
|
2020-08-26 15:15:52 +00:00
|
|
|
) -> Result<EventId> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = sender;
|
2020-08-21 21:19:18 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 20:19:22 +00:00
|
|
|
let event_id = db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: content.event_type().into(),
|
|
|
|
content: json,
|
|
|
|
unsigned: None,
|
|
|
|
state_key,
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-10-05 20:19:22 +00:00
|
|
|
&room_id,
|
|
|
|
&db.globals,
|
|
|
|
&db.sending,
|
2020-11-09 11:21:04 +00:00
|
|
|
&db.admin,
|
2020-10-05 20:19:22 +00:00
|
|
|
&db.account_data,
|
|
|
|
)?;
|
2020-08-21 21:19:18 +00:00
|
|
|
|
2020-08-26 15:15:52 +00:00
|
|
|
Ok(event_id)
|
2020-08-21 21:19:18 +00:00
|
|
|
}
|