From f53f9a8b83f06ea0abb94e3215cdebfe39013077 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Wed, 3 Jul 2024 16:25:07 -0700 Subject: [PATCH] feat: Revert buffer save request --- src/handler/buffer.rs | 62 +++++++++++++++++++++++++++++++++++++++++++ src/handler/mod.rs | 1 + 2 files changed, 63 insertions(+) diff --git a/src/handler/buffer.rs b/src/handler/buffer.rs index 9c202ec..484ad55 100644 --- a/src/handler/buffer.rs +++ b/src/handler/buffer.rs @@ -60,6 +60,68 @@ pub mod update { } } +/// Save the buffer. +pub mod save { + use crate::handler::buffer::*; + + const METHOD: &str = "buffer::save"; + + pub async fn handle(channel: &mut Channel, room: &Arc>, json: &Value) { + let addr = &channel.get_connection().addr.clone(); + let mut room = room.lock().await; + let client = room.get_client(addr).unwrap(); + + if !check_entered(channel, client, METHOD).await { + return; + } + + let filename = data_str(json, "file"); + let contents = data_str(json, "contents"); + + if filename.is_none() { + missing_field(channel, METHOD, "file").await; + return; + } + + if contents.is_none() { + missing_field(channel, METHOD, "contents").await; + return; + } + + let rel_filename = no_client_path(&client, &filename); + + let filename = filename.unwrap(); + + if rel_filename.is_none() { + general_error( + channel, + METHOD, + format!("The file is not under the project path: {}", filename), + ) + .await; + return; + } + + let file = room.get_file_create_mut(addr, &filename, contents); + let file = file.unwrap(); + + file.save(); + + let rel_filename = rel_filename.unwrap(); + let contents = file.buffer(); + + room.broadcast_json_except( + &serde_json::json!({ + "method": METHOD, + "file": rel_filename, + "contents": contents, + "status": ST_SUCCESS, + }), + addr, + ); + } +} + /// Synce the buffer. /// /// This will only sync the view. diff --git a/src/handler/mod.rs b/src/handler/mod.rs index 793eda5..2b5727a 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -52,6 +52,7 @@ pub async fn handle(channel: &mut Channel, room: &Arc>, json: &str) "file::say" => file::say::handle(channel, room, &val).await, "buffer::update" => buffer::update::handle(channel, room, &val).await, "buffer::sync" => buffer::sync::handle(channel, room, &val).await, + "buffer::save" => buffer::save::handle(channel, room, &val).await, _ => { tracing::error!("Unkown method request: `{}`", method); }