2021-07-13 15:44:25 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-05 20:34:31 +02:00
|
|
|
use crate::{service::pdu::PduBuilder, services, Error, Result, Ruma, RumaResponse};
|
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
|
|
|
},
|
2020-10-18 16:19:14 +02:00
|
|
|
events::{
|
2023-02-22 15:49:55 +01:00
|
|
|
room::canonical_alias::RoomCanonicalAliasEventContent, AnyStateEventContent, StateEventType,
|
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
|
|
|
};
|
2023-07-28 23:40:10 +00:00
|
|
|
use tracing::log::warn;
|
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-12-14 13:09:10 +01:00
|
|
|
body: Ruma<send_state_event::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<send_state_event::v3::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(
|
|
|
|
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(),
|
2020-08-21 17:19:18 -04:00
|
|
|
)
|
2020-10-21 21:28:02 +02:00
|
|
|
.await?;
|
|
|
|
|
2021-11-27 17:44:52 +01:00
|
|
|
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-12-14 13:09:10 +01:00
|
|
|
body: Ruma<send_state_event::v3::Request>,
|
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");
|
2020-08-21 17:19:18 -04:00
|
|
|
|
2021-09-24 22:44:26 +00:00
|
|
|
// Forbid m.room.encryption if encryption is disabled
|
2022-09-06 23:15:09 +02:00
|
|
|
if body.event_type == StateEventType::RoomEncryption && !services().globals.allow_encryption() {
|
2021-09-24 22:44:26 +00:00
|
|
|
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(
|
2021-04-22 11:26:20 +02:00
|
|
|
sender_user,
|
2020-10-21 21:28:02 +02:00
|
|
|
&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(),
|
2020-08-06 08:29:59 -04:00
|
|
|
)
|
2020-10-21 21:28:02 +02:00
|
|
|
.await?;
|
|
|
|
|
2021-11-27 17:44:52 +01:00
|
|
|
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
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_route(
|
2022-12-14 13:09:10 +01:00
|
|
|
body: Ruma<get_state_events::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_state_events::v3::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
|
|
|
|
2023-03-07 17:58:55 +01:00
|
|
|
if !services()
|
2022-10-05 20:34:31 +02:00
|
|
|
.rooms
|
2023-02-22 15:49:55 +01:00
|
|
|
.state_accessor
|
2023-12-23 19:48:14 -08:00
|
|
|
.user_can_see_state_events(sender_user, &body.room_id)?
|
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
|
|
|
}
|
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_state_events::v3::Response {
|
2022-09-06 23:15:09 +02:00
|
|
|
room_state: services()
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2022-10-05 09:34:25 +02:00
|
|
|
.state_accessor
|
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(),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
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
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_for_key_route(
|
2022-12-14 13:09:10 +01:00
|
|
|
body: Ruma<get_state_events_for_key::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_state_events_for_key::v3::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
|
|
|
|
2023-03-07 17:58:55 +01:00
|
|
|
if !services()
|
2022-10-05 20:34:31 +02:00
|
|
|
.rooms
|
2023-02-22 15:49:55 +01:00
|
|
|
.state_accessor
|
2023-12-23 19:48:14 -08:00
|
|
|
.user_can_see_state_events(sender_user, &body.room_id)?
|
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
|
|
|
}
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
let event = services()
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2022-10-05 20:34:31 +02:00
|
|
|
.state_accessor
|
|
|
|
.room_state_get(&body.room_id, &body.event_type, &body.state_key)?
|
2023-07-29 19:17:12 +00:00
|
|
|
.ok_or_else(|| {
|
2023-07-29 14:30:48 +00:00
|
|
|
warn!(
|
|
|
|
"State event {:?} not found in room {:?}",
|
|
|
|
&body.event_type, &body.room_id
|
|
|
|
);
|
|
|
|
Error::BadRequest(ErrorKind::NotFound, "State event not found.")
|
|
|
|
})?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_state_events_for_key::v3::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"))?,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
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
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_state_events_for_empty_key_route(
|
2022-12-14 13:09:10 +01:00
|
|
|
body: Ruma<get_state_events_for_key::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<RumaResponse<get_state_events_for_key::v3::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
|
|
|
|
2023-03-07 17:58:55 +01:00
|
|
|
if !services()
|
2022-10-05 20:34:31 +02:00
|
|
|
.rooms
|
2023-02-22 15:49:55 +01:00
|
|
|
.state_accessor
|
2023-12-23 19:48:14 -08:00
|
|
|
.user_can_see_state_events(sender_user, &body.room_id)?
|
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
|
|
|
}
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
let event = services()
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2022-10-05 20:34:31 +02:00
|
|
|
.state_accessor
|
|
|
|
.room_state_get(&body.room_id, &body.event_type, "")?
|
2023-07-29 19:17:12 +00:00
|
|
|
.ok_or_else(|| {
|
2023-07-29 14:30:48 +00:00
|
|
|
warn!(
|
|
|
|
"State event {:?} not found in room {:?}",
|
|
|
|
&body.event_type, &body.room_id
|
|
|
|
);
|
|
|
|
Error::BadRequest(ErrorKind::NotFound, "State event not found.")
|
|
|
|
})?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_state_events_for_key::v3::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
|
|
|
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,
|
2021-11-27 17:44:52 +01:00
|
|
|
) -> Result<Arc<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 {
|
2022-09-06 23:15:09 +02:00
|
|
|
if alias.server_name() != services().globals.server_name()
|
|
|
|
|| services()
|
2020-08-21 17:19:18 -04:00
|
|
|
.rooms
|
2022-10-05 20:34:31 +02:00
|
|
|
.alias
|
|
|
|
.resolve_local_alias(&alias)?
|
2020-08-21 17:19:18 -04:00
|
|
|
.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(
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.globals
|
2021-08-03 11:10:58 +02:00
|
|
|
.roomid_mutex_state
|
2021-07-13 15:44:25 +02:00
|
|
|
.write()
|
2024-03-05 14:22:54 +00:00
|
|
|
.await
|
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
|
|
|
|
2024-03-05 14:22:54 +00:00
|
|
|
let event_id = services()
|
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: event_type.to_string().into(),
|
|
|
|
content: serde_json::from_str(json.json().get()).expect("content is valid json"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(state_key),
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
sender_user,
|
|
|
|
room_id,
|
|
|
|
&state_lock,
|
|
|
|
)
|
|
|
|
.await?;
|
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
|
|
|
}
|