dedupe some code in state.rs
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
8e3918250d
commit
b17ccdadd2
1 changed files with 47 additions and 99 deletions
|
@ -42,7 +42,7 @@ pub(crate) async fn send_state_event_for_key_route(
|
||||||
sender_user,
|
sender_user,
|
||||||
&body.room_id,
|
&body.room_id,
|
||||||
&body.event_type,
|
&body.event_type,
|
||||||
&body.body.body, // Yes, I hate it too
|
&body.body.body,
|
||||||
body.state_key.clone(),
|
body.state_key.clone(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -64,22 +64,7 @@ pub(crate) async fn send_state_event_for_key_route(
|
||||||
pub(crate) async fn send_state_event_for_empty_key_route(
|
pub(crate) async fn send_state_event_for_empty_key_route(
|
||||||
body: Ruma<send_state_event::v3::Request>,
|
body: Ruma<send_state_event::v3::Request>,
|
||||||
) -> Result<RumaResponse<send_state_event::v3::Response>> {
|
) -> Result<RumaResponse<send_state_event::v3::Response>> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
send_state_event_for_key_route(body).await.map(RumaResponse)
|
||||||
|
|
||||||
let event_id = send_state_event_for_key_helper(
|
|
||||||
sender_user,
|
|
||||||
&body.room_id,
|
|
||||||
&body.event_type.to_string().into(),
|
|
||||||
&body.body.body,
|
|
||||||
body.state_key.clone(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let event_id = (*event_id).to_owned();
|
|
||||||
Ok(send_state_event::v3::Response {
|
|
||||||
event_id,
|
|
||||||
}
|
|
||||||
.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # `GET /_matrix/client/v3/rooms/{roomid}/state`
|
/// # `GET /_matrix/client/v3/rooms/{roomid}/state`
|
||||||
|
@ -182,57 +167,49 @@ pub(crate) async fn get_state_events_for_key_route(
|
||||||
pub(crate) async fn get_state_events_for_empty_key_route(
|
pub(crate) async fn get_state_events_for_empty_key_route(
|
||||||
body: Ruma<get_state_events_for_key::v3::Request>,
|
body: Ruma<get_state_events_for_key::v3::Request>,
|
||||||
) -> Result<RumaResponse<get_state_events_for_key::v3::Response>> {
|
) -> Result<RumaResponse<get_state_events_for_key::v3::Response>> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
get_state_events_for_key_route(body).await.map(RumaResponse)
|
||||||
|
|
||||||
if !services()
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.user_can_see_state_events(sender_user, &body.room_id)?
|
|
||||||
{
|
|
||||||
return Err(Error::BadRequest(
|
|
||||||
ErrorKind::forbidden(),
|
|
||||||
"You don't have permission to view the room state.",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let event = services()
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(&body.room_id, &body.event_type, "")?
|
|
||||||
.ok_or_else(|| {
|
|
||||||
warn!("State event {:?} not found in room {:?}", &body.event_type, &body.room_id);
|
|
||||||
Error::BadRequest(ErrorKind::NotFound, "State event not found.")
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if body
|
|
||||||
.format
|
|
||||||
.as_ref()
|
|
||||||
.is_some_and(|f| f.to_lowercase().eq("event"))
|
|
||||||
{
|
|
||||||
Ok(get_state_events_for_key::v3::Response {
|
|
||||||
content: None,
|
|
||||||
event: serde_json::from_str(event.to_state_event().json().get()).map_err(|e| {
|
|
||||||
error!("Invalid room state event in database: {}", e);
|
|
||||||
Error::bad_database("Invalid room state event in database")
|
|
||||||
})?,
|
|
||||||
}
|
|
||||||
.into())
|
|
||||||
} else {
|
|
||||||
Ok(get_state_events_for_key::v3::Response {
|
|
||||||
content: Some(serde_json::from_str(event.content.get()).map_err(|e| {
|
|
||||||
error!("Invalid room state event content in database: {}", e);
|
|
||||||
Error::bad_database("Invalid room state event content in database")
|
|
||||||
})?),
|
|
||||||
event: None,
|
|
||||||
}
|
|
||||||
.into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_state_event_for_key_helper(
|
async fn send_state_event_for_key_helper(
|
||||||
sender: &UserId, room_id: &RoomId, event_type: &StateEventType, json: &Raw<AnyStateEventContent>, state_key: String,
|
sender: &UserId, room_id: &RoomId, event_type: &StateEventType, json: &Raw<AnyStateEventContent>, state_key: String,
|
||||||
) -> Result<Arc<EventId>> {
|
) -> Result<Arc<EventId>> {
|
||||||
match *event_type {
|
allowed_to_send_state_event(room_id, event_type, json).await?;
|
||||||
|
|
||||||
|
let mutex_state = Arc::clone(
|
||||||
|
services()
|
||||||
|
.globals
|
||||||
|
.roomid_mutex_state
|
||||||
|
.write()
|
||||||
|
.await
|
||||||
|
.entry(room_id.to_owned())
|
||||||
|
.or_default(),
|
||||||
|
);
|
||||||
|
let state_lock = mutex_state.lock().await;
|
||||||
|
|
||||||
|
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,
|
||||||
|
room_id,
|
||||||
|
&state_lock,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(event_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn allowed_to_send_state_event(
|
||||||
|
room_id: &RoomId, event_type: &StateEventType, json: &Raw<AnyStateEventContent>,
|
||||||
|
) -> Result<()> {
|
||||||
|
match event_type {
|
||||||
// Forbid m.room.encryption if encryption is disabled
|
// Forbid m.room.encryption if encryption is disabled
|
||||||
StateEventType::RoomEncryption => {
|
StateEventType::RoomEncryption => {
|
||||||
if !services().globals.allow_encryption() {
|
if !services().globals.allow_encryption() {
|
||||||
|
@ -283,11 +260,11 @@ async fn send_state_event_for_key_helper(
|
||||||
for alias in aliases {
|
for alias in aliases {
|
||||||
if !server_is_ours(alias.server_name())
|
if !server_is_ours(alias.server_name())
|
||||||
|| services()
|
|| services()
|
||||||
.rooms
|
.rooms
|
||||||
.alias
|
.alias
|
||||||
.resolve_local_alias(&alias)?
|
.resolve_local_alias(&alias)?
|
||||||
.filter(|room| room == room_id) // Make sure it's the right room
|
.filter(|room| room == room_id) // Make sure it's the right room
|
||||||
.is_none()
|
.is_none()
|
||||||
{
|
{
|
||||||
return Err(Error::BadRequest(
|
return Err(Error::BadRequest(
|
||||||
ErrorKind::forbidden(),
|
ErrorKind::forbidden(),
|
||||||
|
@ -297,36 +274,7 @@ async fn send_state_event_for_key_helper(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => (),
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
let mutex_state = Arc::clone(
|
|
||||||
services()
|
|
||||||
.globals
|
|
||||||
.roomid_mutex_state
|
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.entry(room_id.to_owned())
|
|
||||||
.or_default(),
|
|
||||||
);
|
|
||||||
let state_lock = mutex_state.lock().await;
|
|
||||||
|
|
||||||
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,
|
|
||||||
room_id,
|
|
||||||
&state_lock,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(event_id)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue