Skip to content

Commit

Permalink
feat: Revert buffer save request
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 3, 2024
1 parent ab8167f commit f53f9a8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/handler/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Room>>, 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.
Expand Down
1 change: 1 addition & 0 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, 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);
}
Expand Down

0 comments on commit f53f9a8

Please sign in to comment.