From 313e617181065b64d232b56c83273e6554a27134 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 30 May 2018 23:14:14 -0700 Subject: [PATCH] Add (broken) implementation of /read endpoint --- server/src/rbx.rs | 2 ++ server/src/web.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/server/src/rbx.rs b/server/src/rbx.rs index b85a7319..685db55f 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -19,4 +19,6 @@ pub struct RbxInstance { /// Contains all other properties of an Instance. pub properties: HashMap, + + // TODO: children property, Vec } diff --git a/server/src/web.rs b/server/src/web.rs index 762683a5..9f185882 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -22,9 +22,9 @@ pub struct WebConfig { #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct ServerInfoResponse<'a> { + server_id: &'a str, server_version: &'static str, protocol_version: u64, - server_id: &'a str, partitions: &'a HashMap, } @@ -32,16 +32,24 @@ struct ServerInfoResponse<'a> { #[serde(rename_all = "camelCase")] struct ReadAllResponse<'a> { server_id: &'a str, - instances: &'a HashMap, message_cursor: i32, + instances: &'a HashMap, +} + +#[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] +struct ReadResponse<'a> { + server_id: &'a str, + message_cursor: i32, + instances: HashMap, } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct SubscribeResponse<'a> { server_id: &'a str, - messages: &'a [Message], message_cursor: i32, + messages: &'a [Message], } /// Start the Rojo web server and park our current thread. @@ -139,12 +147,41 @@ pub fn start(config: WebConfig) { }, (POST) (/read) => { - let body = match read_json::>>(request) { + let requested_ids = match read_json::>(request) { Some(body) => body, None => return rouille::Response::text("Malformed JSON").with_status_code(400), }; - Response::json(&body) + let rbx_session = config.rbx_session.read().unwrap(); + + let message_cursor = { + let messages = config.message_session.messages.read().unwrap(); + messages.len() as i32 - 1 + }; + + let mut instances = HashMap::new(); + + for requested_id in &requested_ids { + let requested_instance = match rbx_session.instances.get(requested_id) { + Some(instance) => instance, + None => continue, + }; + + instances.insert(*requested_id, requested_instance); + + // Oops; this needs to be recursive. + for (child_id, instance) in &rbx_session.instances { + if instance.parent == Some(*requested_id) { + instances.insert(*child_id, instance); + } + } + } + + Response::json(&ReadResponse { + server_id: &server_id, + message_cursor, + instances, + }) }, _ => Response::empty_404()