From 56dd6cb16ff8bd290d15595381b45087f790ee60 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Mon, 18 May 2020 09:22:07 +0200 Subject: [PATCH] improvement: get state --- Cargo.toml | 2 +- src/client_server.rs | 85 +++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 3 ++ 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d1b4b1..e6ed01e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ log = "0.4.8" sled = "0.31.0" directories = "2.0.2" js_int = "0.1.5" -serde_json = "1.0.53" +serde_json = { version = "1.0.53", features = ["raw_value"] } serde = "1.0.110" tokio = { version = "0.2.21", features = ["macros"] } rand = "0.7.3" diff --git a/src/client_server.rs b/src/client_server.rs index d7e129c..6030952 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -30,7 +30,10 @@ use ruma_client_api::{ read_marker::set_read_marker, room::create_room, session::{get_login_types, login}, - state::{create_state_event_for_empty_key, create_state_event_for_key}, + state::{ + create_state_event_for_empty_key, create_state_event_for_key, get_state_events, + get_state_events_for_empty_key, get_state_events_for_key, + }, sync::sync_events, thirdparty::get_protocols, to_device::{self, send_event_to_device}, @@ -1233,10 +1236,10 @@ pub fn get_protocols_route() -> MatrixResult { )] pub fn create_message_event_route( db: State<'_, Database>, + body: Ruma, _room_id: String, _event_type: String, _txn_id: String, - body: Ruma, ) -> MatrixResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); @@ -1267,10 +1270,10 @@ pub fn create_message_event_route( )] pub fn create_state_event_for_key_route( db: State<'_, Database>, + body: Ruma, _room_id: String, _event_type: String, _state_key: String, - body: Ruma, ) -> MatrixResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); @@ -1299,9 +1302,9 @@ pub fn create_state_event_for_key_route( )] pub fn create_state_event_for_empty_key_route( db: State<'_, Database>, + body: Ruma, _room_id: String, _event_type: String, - body: Ruma, ) -> MatrixResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); @@ -1324,6 +1327,80 @@ pub fn create_state_event_for_empty_key_route( })) } +#[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "")] +pub fn get_state_events_route( + db: State<'_, Database>, + body: Ruma, + _room_id: String, +) -> MatrixResult { + MatrixResult(Ok(get_state_events::Response { + room_state: db + .rooms + .room_state(&body.room_id) + .unwrap() + .values() + .map(|pdu| pdu.to_state_event()) + .collect(), + })) +} + +#[get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", + data = "" +)] +pub fn get_state_events_for_key_route( + db: State<'_, Database>, + body: Ruma, + _room_id: String, + _event_type: String, + _state_key: String, +) -> MatrixResult { + if let Some(event) = db + .rooms + .room_state(&body.room_id) + .unwrap() + .get(&(body.event_type.clone(), body.state_key.clone())) + { + MatrixResult(Ok(get_state_events_for_key::Response { + content: serde_json::value::to_raw_value(event).unwrap(), + })) + } else { + MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "State event not found.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + })) + } +} + +#[get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", + data = "" +)] +pub fn get_state_events_for_empty_key_route( + db: State<'_, Database>, + body: Ruma, + _room_id: String, + _event_type: String, +) -> MatrixResult { + if let Some(event) = db + .rooms + .room_state(&body.room_id) + .unwrap() + .get(&(body.event_type.clone(), "".to_owned())) + { + MatrixResult(Ok(get_state_events_for_key::Response { + content: serde_json::value::to_raw_value(event).unwrap(), + })) + } else { + MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "State event not found.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + })) + } +} + #[get("/_matrix/client/r0/sync", data = "")] pub fn sync_route( db: State<'_, Database>, diff --git a/src/main.rs b/src/main.rs index 717ef96..7581da1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,9 @@ fn setup_rocket() -> rocket::Rocket { client_server::create_message_event_route, client_server::create_state_event_for_key_route, client_server::create_state_event_for_empty_key_route, + client_server::get_state_events_route, + client_server::get_state_events_for_key_route, + client_server::get_state_events_for_empty_key_route, client_server::sync_route, client_server::get_message_events_route, client_server::turn_server_route,