conduit/src/client_server/state.rs

274 lines
8.7 KiB
Rust
Raw Normal View History

2020-07-30 16:14:47 +00:00
use super::State;
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::{get_state_events, get_state_events_for_key, send_state_event},
2020-07-30 16:14:47 +00:00
},
events::{
2021-04-22 09:26:20 +00:00
room::{
canonical_alias::CanonicalAliasEventContent,
history_visibility::{HistoryVisibility, HistoryVisibilityEventContent},
},
AnyStateEventContent, EventType,
},
2021-04-22 09:26:20 +00:00
serde::Raw,
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>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, 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>,
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 16:14:47 +00:00
let event_id = send_state_event_for_key_helper(
&db,
sender_user,
&body.room_id,
2021-04-22 09:26:20 +00:00
EventType::from(&body.event_type),
&body.body.body, // Yes, I hate it too
body.state_key.to_owned(),
)
.await?;
db.flush().await?;
Ok(send_state_event::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>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, 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>,
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
2021-04-22 09:26:20 +00:00
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event_id = send_state_event_for_key_helper(
&db,
2021-04-22 09:26:20 +00:00
sender_user,
&body.room_id,
2021-04-22 09:26:20 +00:00
EventType::from(&body.event_type),
&body.body.body,
body.state_key.to_owned(),
)
.await?;
db.flush().await?;
Ok(send_state_event::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>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn get_state_events_route(
2020-07-30 16:14:47 +00:00
db: State<'_, Database>,
body: Ruma<get_state_events::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<get_state_events::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 16:14:47 +00:00
#[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable
if !db.rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
2021-03-17 21:30:25 +00:00
.map(|event| {
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))
)
{
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>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn get_state_events_for_key_route(
2020-07-30 16:14:47 +00:00
db: State<'_, Database>,
body: Ruma<get_state_events_for_key::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<get_state_events_for_key::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 16:14:47 +00:00
#[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable
if !db.rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
2021-03-17 21:30:25 +00:00
.map(|event| {
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))
)
{
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.",
2021-03-17 21:30:25 +00:00
))?;
2020-07-30 16:14:47 +00:00
Ok(get_state_events_for_key::Response {
2021-04-22 09:26:20 +00:00
content: serde_json::from_value(event.content)
2020-07-30 16:14:47 +00:00
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}
.into())
}
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn get_state_events_for_empty_key_route(
2020-07-30 16:14:47 +00:00
db: State<'_, Database>,
body: Ruma<get_state_events_for_key::Request<'_>>,
) -> ConduitResult<get_state_events_for_key::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 16:14:47 +00:00
#[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable
if !db.rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
2021-03-17 21:30:25 +00:00
.map(|event| {
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))
)
{
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.",
2021-03-17 21:30:25 +00:00
))?;
2020-07-30 16:14:47 +00:00
Ok(get_state_events_for_key::Response {
2021-04-22 09:26:20 +00:00
content: serde_json::from_value(event.content)
2020-07-30 16:14:47 +00:00
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}
.into())
}
2020-09-14 18:23:19 +00:00
pub async fn send_state_event_for_key_helper(
db: &Database,
sender: &UserId,
room_id: &RoomId,
2021-04-22 09:26:20 +00:00
event_type: EventType,
json: &Raw<AnyStateEventContent>,
state_key: String,
) -> Result<EventId> {
let sender_user = sender;
2021-04-22 09:26:20 +00:00
if let Ok(canonical_alias) =
serde_json::from_str::<CanonicalAliasEventContent>(json.json().get())
{
let mut aliases = canonical_alias.alt_aliases.clone();
2021-04-22 09:26:20 +00:00
if let Some(alias) = canonical_alias.alias {
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 {
2021-04-22 09:26:20 +00:00
event_type,
content: serde_json::from_str(json.json().get()).expect("content is valid json"),
2020-10-05 20:19:22 +00:00
unsigned: None,
2021-04-22 09:26:20 +00:00
state_key: Some(state_key),
2020-10-05 20:19:22 +00:00
redacts: None,
},
&sender_user,
2020-10-05 20:19:22 +00:00
&room_id,
2021-01-15 16:05:57 +00:00
&db,
2020-10-05 20:19:22 +00:00
)?;
Ok(event_id)
}