Skip to content

Commit

Permalink
feat: Use new file instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 1, 2024
1 parent 3cb49d4 commit 036d3f9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 45 deletions.
44 changes: 0 additions & 44 deletions src/handler/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,6 @@ pub mod update {
}
}

/// Save buffer to file.
pub mod save {
use crate::channel::*;
use crate::constant::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "buffer::save";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
let addr = &channel.get_connection().addr;
let mut room = room.lock().await;
let client = room.get_client(addr).unwrap();

let filename = data_str(json, "file");
let contents = data_str(json, "contents");
let relative_path = no_client_path(&client, &filename);

let filename = filename.unwrap();

let file = room.get_file_create_mut(addr, &filename, contents);
let file = file.unwrap();

file.save();

let contents = file.buffer();

let relative_path = relative_path.unwrap();

room.broadcast_json_except(
&serde_json::json!({
"method": METHOD,
"file": relative_path,
"contents": contents,
"status": ST_SUCCESS,
}),
addr,
);
}
}

/// Synce the buffer.
///
/// This will only sync the view.
Expand Down
4 changes: 3 additions & 1 deletion src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &str)
"ping" => ping::handle(channel, room, &val).await,
"room::enter" => room::enter::handle(channel, room, &val).await,
"room::exit" => room::exit::handle(channel, room, &val).await,
"room::add_file" => room::add_file::handle(channel, room, &val).await,
"room::delete_file" => room::delete_file::handle(channel, room, &val).await,
"room::rename_file" => room::rename_file::handle(channel, room, &val).await,
"room::kick" => room::kick::handle(channel, room, &val).await,
"room::broadcast" => room::broadcast::handle(channel, room, &val).await,
"room::info" => room::info::handle(channel, room, &val).await,
Expand All @@ -44,7 +47,6 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &str)
"file::info" => file::info::handle(channel, room, &val).await,
"file::say" => file::say::handle(channel, room, &val).await,
"buffer::update" => buffer::update::handle(channel, room, &val).await,
"buffer::save" => buffer::save::handle(channel, room, &val).await,
"buffer::sync" => buffer::sync::handle(channel, room, &val).await,
_ => {
tracing::error!("Unkown method request: `{}`", method);
Expand Down
78 changes: 78 additions & 0 deletions src/handler/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,84 @@ pub mod exit {
}
}

/// Add a new file.
pub mod add_file {
use crate::channel::*;
use crate::constant::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "room::add_file";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
let addr = &channel.get_connection().addr;
let mut room = room.lock().await;
let client = room.get_client(addr).unwrap();

let filename = data_str(json, "file");
let contents = data_str(json, "contents");
let relative_path = no_client_path(&client, &filename);

let filename = filename.unwrap();

let file = room.get_file_create_mut(addr, &filename, contents);
let file = file.unwrap();

file.save();

let contents = file.buffer();

let relative_path = relative_path.unwrap();

room.broadcast_json_except(
&serde_json::json!({
"method": METHOD,
"file": relative_path,
"contents": contents,
"status": ST_SUCCESS,
}),
addr,
);
}
}

/// Delete a file.
pub mod delete_file {
use crate::channel::*;
use crate::constant::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "room::delete_file";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
// TODO: ..
}
}

/// Rename a file.
pub mod rename_file {
use crate::channel::*;
use crate::constant::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "room::rename_file";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
// TODO: ..
}
}

/// Kcik the user out of the room.
pub mod kick {
use crate::channel::*;
Expand Down

0 comments on commit 036d3f9

Please sign in to comment.