1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/api/client_server/state.rs

287 lines
9.9 KiB
Rust
Raw Normal View History

2021-07-13 15:44:25 +02:00
use std::sync::Arc;
2021-07-14 07:07:08 +00:00
use crate::{
Error, Result, Ruma, RumaResponse, services, service::pdu::PduBuilder,
2021-07-14 07:07:08 +00:00
};
2020-07-30 18:14:47 +02:00
use ruma::{
api::client::{
error::ErrorKind,
2022-02-18 15:33:14 +01:00
state::{get_state_events, get_state_events_for_key, send_state_event},
2020-07-30 18:14:47 +02:00
},
events::{
2021-04-22 11:26:20 +02:00
room::{
canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
2021-04-22 11:26:20 +02:00
},
2022-04-07 13:22:10 +02:00
AnyStateEventContent, StateEventType,
},
2021-04-22 11:26:20 +02:00
serde::Raw,
EventId, RoomId, UserId,
2020-07-30 18:14:47 +02:00
};
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-09-14 20:23:19 +02:00
pub async fn send_state_event_for_key_route(
2022-04-06 21:31:29 +02:00
body: Ruma<send_state_event::v3::IncomingRequest>,
2022-02-18 15:33:14 +01:00
) -> Result<send_state_event::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
let event_id = send_state_event_for_key_helper(
sender_user,
&body.room_id,
2022-04-06 21:31:29 +02:00
&body.event_type,
2021-04-22 11:26:20 +02:00
&body.body.body, // Yes, I hate it too
body.state_key.to_owned(),
)
.await?;
let event_id = (*event_id).to_owned();
2022-02-18 15:33:14 +01:00
Ok(send_state_event::v3::Response { event_id })
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-09-14 20:23:19 +02:00
pub async fn send_state_event_for_empty_key_route(
2022-04-06 21:31:29 +02:00
body: Ruma<send_state_event::v3::IncomingRequest>,
2022-02-18 15:33:14 +01:00
) -> Result<RumaResponse<send_state_event::v3::Response>> {
2021-04-22 11:26:20 +02:00
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
// Forbid m.room.encryption if encryption is disabled
if body.event_type == StateEventType::RoomEncryption && !services().globals.allow_encryption() {
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Encryption has been disabled",
));
}
let event_id = send_state_event_for_key_helper(
2021-04-22 11:26:20 +02:00
sender_user,
&body.room_id,
2022-04-06 21:31:29 +02:00
&body.event_type.to_string().into(),
2021-04-22 11:26:20 +02:00
&body.body.body,
body.state_key.to_owned(),
)
.await?;
let event_id = (*event_id).to_owned();
2022-02-18 15:33:14 +01:00
Ok(send_state_event::v3::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
pub async fn get_state_events_route(
2022-04-06 21:31:29 +02:00
body: Ruma<get_state_events::v3::IncomingRequest>,
2022-02-18 15:33:14 +01:00
) -> Result<get_state_events::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02: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 !services().rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
services().rooms
2022-04-06 21:31:29 +02:00
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
2021-03-17 22:30:25 +01:00
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
{
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"You don't have permission to view the room state.",
));
2020-07-30 18:14:47 +02:00
}
2022-02-18 15:33:14 +01:00
Ok(get_state_events::v3::Response {
room_state: services()
2020-07-30 18:14:47 +02:00
.rooms
2022-06-18 16:38:41 +02:00
.room_state_full(&body.room_id)
.await?
2020-07-30 18:14:47 +02:00
.values()
.map(|pdu| pdu.to_state_event())
.collect(),
})
2020-07-30 18:14:47 +02:00
}
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
pub async fn get_state_events_for_key_route(
2022-04-06 21:31:29 +02:00
body: Ruma<get_state_events_for_key::v3::IncomingRequest>,
2022-02-18 15:33:14 +01:00
) -> Result<get_state_events_for_key::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02: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 !services().rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
services().rooms
2022-04-06 21:31:29 +02:00
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
2021-03-17 22:30:25 +01:00
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
{
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 = services()
2020-07-30 18:14:47 +02:00
.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
2022-02-18 15:33:14 +01:00
Ok(get_state_events_for_key::v3::Response {
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"))?,
})
2020-07-30 18:14:47 +02:00
}
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
pub async fn get_state_events_for_empty_key_route(
2022-04-06 21:31:29 +02:00
body: Ruma<get_state_events_for_key::v3::IncomingRequest>,
2022-02-18 15:33:14 +01:00
) -> Result<RumaResponse<get_state_events_for_key::v3::Response>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02: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 !services().rooms.is_joined(sender_user, &body.room_id)?
&& !matches!(
services().rooms
2022-04-06 21:31:29 +02:00
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
2021-03-17 22:30:25 +01:00
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
{
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 = services()
2020-07-30 18:14:47 +02:00
.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
2022-02-18 15:33:14 +01:00
Ok(get_state_events_for_key::v3::Response {
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
async fn send_state_event_for_key_helper(
sender: &UserId,
room_id: &RoomId,
2022-04-06 21:31:29 +02:00
event_type: &StateEventType,
2021-04-22 11:26:20 +02:00
json: &Raw<AnyStateEventContent>,
state_key: String,
) -> Result<Arc<EventId>> {
let sender_user = sender;
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) =
serde_json::from_str::<RoomCanonicalAliasEventContent>(json.json().get())
2021-04-22 11:26:20 +02:00
{
let mut aliases = canonical_alias.alt_aliases.clone();
2021-04-22 11:26:20 +02:00
if let Some(alias) = canonical_alias.alias {
aliases.push(alias);
}
for alias in aliases {
if alias.server_name() != services().globals.server_name()
|| services()
.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(
services().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
let event_id = services().rooms.build_and_append_pdu(
2020-10-05 22:19:22 +02:00
PduBuilder {
2022-04-06 21:31:29 +02:00
event_type: event_type.to_string().into(),
2021-04-22 11:26:20 +02:00
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,
},
sender_user,
room_id,
2021-08-03 11:10:58 +02:00
&state_lock,
2020-10-05 22:19:22 +02:00
)?;
Ok(event_id)
}