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]]
name = "ruma"
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 = [
"assign",
"js_int",
@ -2090,7 +2090,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
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 = [
"js_int",
"ruma-common",
@ -2102,7 +2102,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
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 = [
"as_variant",
"assign",
@ -2121,7 +2121,7 @@ dependencies = [
[[package]]
name = "ruma-common"
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 = [
"as_variant",
"base64",
@ -2149,7 +2149,7 @@ dependencies = [
[[package]]
name = "ruma-events"
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 = [
"as_variant",
"indexmap",
@ -2171,7 +2171,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
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 = [
"js_int",
"ruma-common",
@ -2183,7 +2183,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
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 = [
"js_int",
"thiserror",
@ -2192,7 +2192,7 @@ dependencies = [
[[package]]
name = "ruma-identity-service-api"
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 = [
"js_int",
"ruma-common",
@ -2202,7 +2202,7 @@ dependencies = [
[[package]]
name = "ruma-macros"
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 = [
"once_cell",
"proc-macro-crate",
@ -2217,7 +2217,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
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 = [
"js_int",
"ruma-common",
@ -2229,7 +2229,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
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 = [
"base64",
"ed25519-dalek",
@ -2245,7 +2245,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
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 = [
"itertools 0.11.0",
"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
#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/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/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 = "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" ] }
# 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 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
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.")
})?;
if body
.format
.as_ref()
.is_some_and(|f| f.to_lowercase().eq("event"))
{
Ok(get_state_events_for_key::v3::Response {
content: serde_json::from_str(event.content.get()).map_err(|e| {
error!("Invalid event content in database: {}", e);
Error::bad_database("Invalid event content in database")
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 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
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.")
})?;
if body
.format
.as_ref()
.is_some_and(|f| f.to_lowercase().eq("event"))
{
Ok(get_state_events_for_key::v3::Response {
content: serde_json::from_str(event.content.get()).map_err(|e| {
error!("Invalid event content in database: {}", e);
Error::bad_database("Invalid event content in database")
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(