feat: query param to return full state event or event content

query parameter `?format=event|content`

defaults to normal behaviour which is the event's content.

ruma impl: 788ea6b00f

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-02-18 16:07:11 -05:00 committed by June
parent b80be6c709
commit a92f291bbf
3 changed files with 62 additions and 28 deletions

24
Cargo.lock generated
View file

@ -2071,7 +2071,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.9.4" version = "0.9.4"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@ -2090,7 +2090,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -2102,7 +2102,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.17.4" version = "0.17.4"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"assign", "assign",
@ -2121,7 +2121,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.12.1" version = "0.12.1"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"base64", "base64",
@ -2149,7 +2149,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.27.11" version = "0.27.11"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"indexmap", "indexmap",
@ -2171,7 +2171,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.8.0" version = "0.8.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -2183,7 +2183,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.9.3" version = "0.9.3"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"js_int", "js_int",
"thiserror", "thiserror",
@ -2192,7 +2192,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identity-service-api" name = "ruma-identity-service-api"
version = "0.8.0" version = "0.8.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -2202,7 +2202,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-macros" name = "ruma-macros"
version = "0.12.0" version = "0.12.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"proc-macro-crate", "proc-macro-crate",
@ -2217,7 +2217,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-push-gateway-api" name = "ruma-push-gateway-api"
version = "0.8.0" version = "0.8.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -2229,7 +2229,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.14.0" version = "0.14.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"base64", "base64",
"ed25519-dalek", "ed25519-dalek",
@ -2245,7 +2245,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-state-res" name = "ruma-state-res"
version = "0.10.0" version = "0.10.0"
source = "git+https://github.com/ruma/ruma?rev=4d9f754657a099df8e61533787b8eebd12946435#4d9f754657a099df8e61533787b8eebd12946435" source = "git+https://github.com/girlbossceo/ruma?rev=788ea6b00fab49b04a17d88caa0c840b7d74aa13#788ea6b00fab49b04a17d88caa0c840b7d74aa13"
dependencies = [ dependencies = [
"itertools 0.11.0", "itertools 0.11.0",
"js_int", "js_int",

View file

@ -23,8 +23,8 @@ tower-http = { version = "0.4.4", features = ["add-extension", "cors", "sensitiv
# Used for matrix spec type definitions and helpers # Used for matrix spec type definitions and helpers
#ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/ruma/ruma", rev = "4d9f754657a099df8e61533787b8eebd12946435", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified", "unstable-msc2870", "unstable-msc3061", "unstable-msc2867", "unstable-extensible-events"] } #ruma = { git = "https://github.com/ruma/ruma", rev = "4d9f754657a099df8e61533787b8eebd12946435", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified", "unstable-msc2870", "unstable-msc3061", "unstable-msc2867", "unstable-extensible-events"] }
#ruma = { git = "https://github.com/girlbossceo/ruma", rev = "b111ec442df34c4d6518e0ba98c6d10a451fa8a1", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified", "unstable-msc2870", "unstable-msc3061", "unstable-msc2867", "unstable-extensible-events"] } ruma = { git = "https://github.com/girlbossceo/ruma", rev = "788ea6b00fab49b04a17d88caa0c840b7d74aa13", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified", "unstable-msc2870", "unstable-msc3061", "unstable-msc2867", "unstable-extensible-events"] }
#ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified" ] } #ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-msc3575", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified" ] }
# Async runtime and utilities # Async runtime and utilities

View file

@ -107,7 +107,9 @@ pub async fn get_state_events_route(
/// # `GET /_matrix/client/v3/rooms/{roomid}/state/{eventType}/{stateKey}` /// # `GET /_matrix/client/v3/rooms/{roomid}/state/{eventType}/{stateKey}`
/// ///
/// Get single state event of a room. /// Get single state event of a room with the specified state key.
/// The optional query parameter `?format=event|content` allows returning the full room state event
/// or just the state event's content (default behaviour)
/// ///
/// - If not joined: Only works if current room history visibility is world readable /// - If not joined: Only works if current room history visibility is world readable
pub async fn get_state_events_for_key_route( pub async fn get_state_events_for_key_route(
@ -137,18 +139,34 @@ pub async fn get_state_events_for_key_route(
); );
Error::BadRequest(ErrorKind::NotFound, "State event not found.") Error::BadRequest(ErrorKind::NotFound, "State event not found.")
})?; })?;
if body
Ok(get_state_events_for_key::v3::Response { .format
content: serde_json::from_str(event.content.get()).map_err(|e| { .as_ref()
error!("Invalid event content in database: {}", e); .is_some_and(|f| f.to_lowercase().eq("event"))
Error::bad_database("Invalid event content in database") {
})?, 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")
})?,
})
} 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,
})
}
} }
/// # `GET /_matrix/client/v3/rooms/{roomid}/state/{eventType}` /// # `GET /_matrix/client/v3/rooms/{roomid}/state/{eventType}`
/// ///
/// Get single state event of a room. /// Get single state event of a room.
/// The optional query parameter `?format=event|content` allows returning the full room state event
/// or just the state event's content (default behaviour)
/// ///
/// - If not joined: Only works if current room history visibility is world readable /// - If not joined: Only works if current room history visibility is world readable
pub async fn get_state_events_for_empty_key_route( pub async fn get_state_events_for_empty_key_route(
@ -179,13 +197,29 @@ pub async fn get_state_events_for_empty_key_route(
Error::BadRequest(ErrorKind::NotFound, "State event not found.") Error::BadRequest(ErrorKind::NotFound, "State event not found.")
})?; })?;
Ok(get_state_events_for_key::v3::Response { if body
content: serde_json::from_str(event.content.get()).map_err(|e| { .format
error!("Invalid event content in database: {}", e); .as_ref()
Error::bad_database("Invalid event content in database") .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())
} }
.into())
} }
async fn send_state_event_for_key_helper( async fn send_state_event_for_key_helper(