2021-07-13 15:44:25 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
use crate::{
|
|
|
|
database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma,
|
|
|
|
};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
2021-04-05 21:25:10 +02:00
|
|
|
r0::state::{get_state_events, get_state_events_for_key, send_state_event},
|
2020-07-30 18:14:47 +02:00
|
|
|
},
|
2020-10-18 16:19:14 +02:00
|
|
|
events::{
|
2021-04-22 11:26:20 +02:00
|
|
|
room::{
|
2021-10-13 10:16:45 +02:00
|
|
|
canonical_alias::RoomCanonicalAliasEventContent,
|
|
|
|
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
|
2021-04-22 11:26:20 +02:00
|
|
|
},
|
|
|
|
AnyStateEventContent, EventType,
|
2020-10-18 16:19:14 +02:00
|
|
|
},
|
2021-04-22 11:26:20 +02:00
|
|
|
serde::Raw,
|
2020-08-26 11:15:52 -04:00
|
|
|
EventId, RoomId, UserId,
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, put};
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}`
|
|
|
|
///
|
|
|
|
/// Sends a state event into the room.
|
|
|
|
///
|
|
|
|
/// - The only requirement for the content is that it has to be valid json
|
|
|
|
/// - Tries to send the event into the room, auth rules will determine if it is allowed
|
|
|
|
/// - If event is new canonical_alias: Rejects if alias is incorrect
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 20:23:19 +02:00
|
|
|
pub async fn send_state_event_for_key_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2021-04-05 21:25:10 +02:00
|
|
|
body: Ruma<send_state_event::Request<'_>>,
|
|
|
|
) -> ConduitResult<send_state_event::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-10-21 21:28:02 +02:00
|
|
|
let event_id = send_state_event_for_key_helper(
|
|
|
|
&db,
|
|
|
|
sender_user,
|
|
|
|
&body.room_id,
|
2021-04-22 11:26:20 +02:00
|
|
|
EventType::from(&body.event_type),
|
|
|
|
&body.body.body, // Yes, I hate it too
|
|
|
|
body.state_key.to_owned(),
|
2020-08-21 17:19:18 -04:00
|
|
|
)
|
2020-10-21 21:28:02 +02:00
|
|
|
.await?;
|
|
|
|
|
2021-08-02 10:13:34 +02:00
|
|
|
db.flush()?;
|
2020-10-21 21:28:02 +02:00
|
|
|
|
2021-04-05 21:25:10 +02:00
|
|
|
Ok(send_state_event::Response { event_id }.into())
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}`
|
|
|
|
///
|
|
|
|
/// Sends a state event into the room.
|
|
|
|
///
|
|
|
|
/// - The only requirement for the content is that it has to be valid json
|
|
|
|
/// - Tries to send the event into the room, auth rules will determine if it is allowed
|
|
|
|
/// - If event is new canonical_alias: Rejects if alias is incorrect
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 20:23:19 +02:00
|
|
|
pub async fn send_state_event_for_empty_key_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2021-04-05 21:25:10 +02:00
|
|
|
body: Ruma<send_state_event::Request<'_>>,
|
|
|
|
) -> ConduitResult<send_state_event::Response> {
|
2021-04-22 11:26:20 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-08-21 17:19:18 -04:00
|
|
|
|
2021-09-24 22:44:26 +00:00
|
|
|
// Forbid m.room.encryption if encryption is disabled
|
|
|
|
if &body.event_type == "m.room.encryption" && !db.globals.allow_encryption() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"Encryption has been disabled",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:28:02 +02:00
|
|
|
let event_id = send_state_event_for_key_helper(
|
|
|
|
&db,
|
2021-04-22 11:26:20 +02:00
|
|
|
sender_user,
|
2020-10-21 21:28:02 +02:00
|
|
|
&body.room_id,
|
2021-04-22 11:26:20 +02:00
|
|
|
EventType::from(&body.event_type),
|
|
|
|
&body.body.body,
|
|
|
|
body.state_key.to_owned(),
|
2020-08-06 08:29:59 -04:00
|
|
|
)
|
2020-10-21 21:28:02 +02:00
|
|
|
.await?;
|
|
|
|
|
2021-08-02 10:13:34 +02:00
|
|
|
db.flush()?;
|
2020-10-21 21:28:02 +02:00
|
|
|
|
2021-04-05 21:25:10 +02:00
|
|
|
Ok(send_state_event::Response { event_id }.into())
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/rooms/{roomid}/state`
|
|
|
|
///
|
|
|
|
/// Get all state events for a room.
|
|
|
|
///
|
|
|
|
/// - If not joined: Only works if current room history visibility is world readable
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-10-27 19:10:09 -04:00
|
|
|
body: Ruma<get_state_events::Request<'_>>,
|
2020-07-30 18:14:47 +02:00
|
|
|
) -> ConduitResult<get_state_events::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-11-08 14:45:52 -05:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 16:19:14 +02:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 19:10:09 -04:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 16:19:14 +02:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2021-03-17 22:30:25 +01:00
|
|
|
.map(|event| {
|
2021-10-13 11:51:30 +02:00
|
|
|
serde_json::from_str(event.content.get())
|
|
|
|
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
|
2020-10-18 16:19:14 +02:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 19:10:09 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 18:14:47 +02: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())
|
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}/{stateKey}`
|
|
|
|
///
|
|
|
|
/// Get single state event of a room.
|
|
|
|
///
|
|
|
|
/// - If not joined: Only works if current room history visibility is world readable
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_for_key_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-10-27 19:10:09 -04:00
|
|
|
body: Ruma<get_state_events_for_key::Request<'_>>,
|
2020-07-30 18:14:47 +02:00
|
|
|
) -> ConduitResult<get_state_events_for_key::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-11-08 14:45:52 -05:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 16:19:14 +02:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 19:10:09 -04:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 16:19:14 +02:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2021-03-17 22:30:25 +01:00
|
|
|
.map(|event| {
|
2021-10-13 11:51:30 +02:00
|
|
|
serde_json::from_str(event.content.get())
|
|
|
|
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
|
2020-10-18 16:19:14 +02:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 19:10:09 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 18:14:47 +02: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 22:30:25 +01:00
|
|
|
))?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
|
|
|
Ok(get_state_events_for_key::Response {
|
2021-10-13 10:16:45 +02:00
|
|
|
content: serde_json::from_str(event.content.get())
|
2020-07-30 18:14:47 +02:00
|
|
|
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}`
|
|
|
|
///
|
|
|
|
/// Get single state event of a room.
|
|
|
|
///
|
|
|
|
/// - If not joined: Only works if current room history visibility is world readable
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_for_empty_key_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2021-04-05 21:25:10 +02:00
|
|
|
body: Ruma<get_state_events_for_key::Request<'_>>,
|
|
|
|
) -> ConduitResult<get_state_events_for_key::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-11-08 14:45:52 -05:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)]
|
2020-10-18 16:19:14 +02:00
|
|
|
// Users not in the room should not be able to access the state unless history_visibility is
|
|
|
|
// WorldReadable
|
2020-10-27 19:10:09 -04:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
|
|
|
&& !matches!(
|
2020-10-18 16:19:14 +02:00
|
|
|
db.rooms
|
|
|
|
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
2021-03-17 22:30:25 +01:00
|
|
|
.map(|event| {
|
2021-10-13 11:51:30 +02:00
|
|
|
serde_json::from_str(event.content.get())
|
|
|
|
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
|
2020-10-18 16:19:14 +02:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database(
|
|
|
|
"Invalid room history visibility event in database.",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
Some(Ok(HistoryVisibility::WorldReadable))
|
2020-10-27 19:10:09 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view the room state.",
|
|
|
|
));
|
2020-07-30 18:14:47 +02: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 22:30:25 +01:00
|
|
|
))?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-04-05 21:25:10 +02:00
|
|
|
Ok(get_state_events_for_key::Response {
|
2021-10-13 10:16:45 +02:00
|
|
|
content: serde_json::from_str(event.content.get())
|
2020-07-30 18:14:47 +02:00
|
|
|
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
2020-08-21 17:19:18 -04:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
async fn send_state_event_for_key_helper(
|
2020-08-21 17:19:18 -04:00
|
|
|
db: &Database,
|
|
|
|
sender: &UserId,
|
|
|
|
room_id: &RoomId,
|
2021-04-22 11:26:20 +02:00
|
|
|
event_type: EventType,
|
|
|
|
json: &Raw<AnyStateEventContent>,
|
|
|
|
state_key: String,
|
2021-11-26 20:36:40 +01:00
|
|
|
) -> Result<Box<EventId>> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = sender;
|
2020-08-21 17:19:18 -04:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
// TODO: Review this check, error if event is unparsable, use event type, allow alias if it
|
|
|
|
// previously existed
|
2021-04-22 11:26:20 +02:00
|
|
|
if let Ok(canonical_alias) =
|
2021-10-13 10:16:45 +02:00
|
|
|
serde_json::from_str::<RoomCanonicalAliasEventContent>(json.json().get())
|
2021-04-22 11:26:20 +02:00
|
|
|
{
|
2020-08-21 17:19:18 -04:00
|
|
|
let mut aliases = canonical_alias.alt_aliases.clone();
|
|
|
|
|
2021-04-22 11:26:20 +02:00
|
|
|
if let Some(alias) = canonical_alias.alias {
|
2020-08-21 17:19:18 -04:00
|
|
|
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",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 11:10:58 +02:00
|
|
|
let mutex_state = Arc::clone(
|
2021-07-13 15:44:25 +02:00
|
|
|
db.globals
|
2021-08-03 11:10:58 +02:00
|
|
|
.roomid_mutex_state
|
2021-07-13 15:44:25 +02:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
2021-11-26 20:36:40 +01:00
|
|
|
.entry(room_id.to_owned())
|
2021-07-13 15:44:25 +02:00
|
|
|
.or_default(),
|
|
|
|
);
|
2021-08-03 11:10:58 +02:00
|
|
|
let state_lock = mutex_state.lock().await;
|
2021-07-13 15:44:25 +02:00
|
|
|
|
2020-10-05 22:19:22 +02:00
|
|
|
let event_id = db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
2021-04-22 11:26:20 +02:00
|
|
|
event_type,
|
|
|
|
content: serde_json::from_str(json.json().get()).expect("content is valid json"),
|
2020-10-05 22:19:22 +02:00
|
|
|
unsigned: None,
|
2021-04-22 11:26:20 +02:00
|
|
|
state_key: Some(state_key),
|
2020-10-05 22:19:22 +02:00
|
|
|
redacts: None,
|
|
|
|
},
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
|
|
|
room_id,
|
|
|
|
db,
|
2021-08-03 11:10:58 +02:00
|
|
|
&state_lock,
|
2020-10-05 22:19:22 +02:00
|
|
|
)?;
|
2020-08-21 17:19:18 -04:00
|
|
|
|
2020-08-26 11:15:52 -04:00
|
|
|
Ok(event_id)
|
2020-08-21 17:19:18 -04:00
|
|
|
}
|