Rough pass on transforming applied patches into API responses

This commit is contained in:
Lucien Greathouse
2019-10-01 14:45:24 -07:00
parent 2598ea3577
commit 099aa26ef8
3 changed files with 58 additions and 11 deletions

View File

@@ -99,6 +99,10 @@ impl<F: ImfsFetcher + Send + 'static> ServeSession<F> {
} }
impl<F: ImfsFetcher> ServeSession<F> { impl<F: ImfsFetcher> ServeSession<F> {
pub fn tree_handle(&self) -> Arc<Mutex<RojoTree>> {
Arc::clone(&self.tree)
}
pub fn tree(&self) -> MutexGuard<'_, RojoTree> { pub fn tree(&self) -> MutexGuard<'_, RojoTree> {
self.tree.lock().unwrap() self.tree.lock().unwrap()
} }

View File

@@ -13,8 +13,9 @@ use crate::{
serve_session::ServeSession, serve_session::ServeSession,
web::{ web::{
interface::{ interface::{
ErrorResponse, Instance, ReadResponse, ServerInfoResponse, SubscribeMessage, ErrorResponse, Instance, InstanceMetadata as WebInstanceMetadata, InstanceUpdate,
SubscribeResponse, PROTOCOL_VERSION, SERVER_VERSION, ReadResponse, ServerInfoResponse, SubscribeMessage, SubscribeResponse,
PROTOCOL_VERSION, SERVER_VERSION,
}, },
util::{json, json_ok}, util::{json, json_ok},
}, },
@@ -89,15 +90,46 @@ impl<F: ImfsFetcher> ApiService<F> {
message_queue.subscribe(input_cursor, sender); message_queue.subscribe(input_cursor, sender);
} }
let tree_handle = self.serve_session.tree_handle();
Box::new(receiver.then(move |result| match result { Box::new(receiver.then(move |result| match result {
Ok((message_cursor, messages)) => { Ok((message_cursor, messages)) => {
// TODO: Transform applied patch sets into subscribe responses let tree = tree_handle.lock().unwrap();
let api_messages = messages let api_messages = messages
.into_iter() .into_iter()
.map(|message| { .map(|message| {
let removed_instances = message.removed; let removed_instances = message.removed;
let added_instances = HashMap::new(); // TODO
let updated_instances = Vec::new(); // TODO let mut added_instances = HashMap::new();
for id in message.added {
let instance = tree.get_instance(id).unwrap();
added_instances.insert(id, Instance::from_rojo_instance(instance));
for instance in tree.descendants(id) {
added_instances
.insert(instance.id(), Instance::from_rojo_instance(instance));
}
}
let updated_instances = message
.updated
.into_iter()
.map(|update| {
let changed_metadata = update
.changed_metadata
.as_ref()
.map(WebInstanceMetadata::from_rojo_metadata);
InstanceUpdate {
id: update.id,
changed_name: update.changed_name,
changed_class_name: update.changed_class_name,
changed_properties: update.changed_properties,
changed_metadata,
}
})
.collect();
SubscribeMessage { SubscribeMessage {
removed_instances, removed_instances,

View File

@@ -8,7 +8,10 @@ use std::{
use rbx_dom_weak::{RbxId, RbxValue}; use rbx_dom_weak::{RbxId, RbxValue};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{session_id::SessionId, snapshot::InstanceWithMeta}; use crate::{
session_id::SessionId,
snapshot::{InstanceMetadata as RojoInstanceMetadata, InstanceWithMeta},
};
/// Server version to report over the API, not exposed outside this crate. /// Server version to report over the API, not exposed outside this crate.
pub(crate) const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION"); pub(crate) const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -31,7 +34,9 @@ pub struct InstanceUpdate {
pub id: RbxId, pub id: RbxId,
pub changed_name: Option<String>, pub changed_name: Option<String>,
pub changed_class_name: Option<String>, pub changed_class_name: Option<String>,
pub changed_properties: HashMap<String, RbxValue>, // TODO: Transform from HashMap<String, Option<_>> to something else, since
// null will get lost when decoding from JSON in some languages.
pub changed_properties: HashMap<String, Option<RbxValue>>,
pub changed_metadata: Option<InstanceMetadata>, pub changed_metadata: Option<InstanceMetadata>,
} }
@@ -41,6 +46,14 @@ pub struct InstanceMetadata {
pub ignore_unknown_instances: bool, pub ignore_unknown_instances: bool,
} }
impl InstanceMetadata {
pub(crate) fn from_rojo_metadata(meta: &RojoInstanceMetadata) -> Self {
Self {
ignore_unknown_instances: meta.ignore_unknown_instances,
}
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
pub struct Instance<'a> { pub struct Instance<'a> {
@@ -52,15 +65,13 @@ pub struct Instance<'a> {
} }
impl<'a> Instance<'a> { impl<'a> Instance<'a> {
pub fn from_rojo_instance<'b>(source: InstanceWithMeta<'b>) -> Instance<'b> { pub(crate) fn from_rojo_instance<'b>(source: InstanceWithMeta<'b>) -> Instance<'b> {
Instance { Instance {
name: Cow::Borrowed(source.name()), name: Cow::Borrowed(source.name()),
class_name: Cow::Borrowed(source.class_name()), class_name: Cow::Borrowed(source.class_name()),
properties: Cow::Borrowed(source.properties()), properties: Cow::Borrowed(source.properties()),
children: Cow::Borrowed(source.children()), children: Cow::Borrowed(source.children()),
metadata: Some(InstanceMetadata { metadata: Some(InstanceMetadata::from_rojo_metadata(source.metadata())),
ignore_unknown_instances: source.metadata().ignore_unknown_instances,
}),
} }
} }
} }