From af1e423b56af4d24d517025e20e1f4b2dd58062b Mon Sep 17 00:00:00 2001 From: reidlab Date: Mon, 4 Sep 2023 21:26:13 -0700 Subject: [PATCH] yeah you can update levels now cool --- readme.md | 8 ++--- src/endpoints/accounts/login_account.rs | 2 +- src/endpoints/accounts/register_account.rs | 2 +- src/endpoints/levels/upload_level.rs | 38 +++++++++++++++++----- src/helpers/clean.rs | 7 +++- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index 115f664..6f91c6f 100644 --- a/readme.md +++ b/readme.md @@ -34,10 +34,8 @@ _these features are implemented_ ## todo -- probably work on the code warnings we get hehe -- green name users... (add udid auth to auth function, use userName instead of accountID in uploading levels, and it goes on and on and on and on...) -- patch uploadlevel to fix color tags, use 1.9 [levelInfo](https://github.com/Cvolton/GMDprivateServer/blob/master/incl/levels/uploadGJLevel.php#L56)???, and use 2.2 unlisted?? +- green name users... +- patch uploadlevel to use 2.2 unlisted(honsetly idk what this is) - clean up uploadlevel -- add updating levels - add level parsing -- maybe swap to timestamp type instead of `(TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS'))` (thats REALLY ugly!!) \ No newline at end of file +- maybe swap to chrono instead of `(TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS'))` (thats REALLY ugly!!) \ No newline at end of file diff --git a/src/endpoints/accounts/login_account.rs b/src/endpoints/accounts/login_account.rs index bda02e3..87a084f 100644 --- a/src/endpoints/accounts/login_account.rs +++ b/src/endpoints/accounts/login_account.rs @@ -20,7 +20,7 @@ pub struct FromLoginAccount { pub fn login_account(input: Form) -> status::Custom<&'static str> { let connection = &mut db::establish_connection_pg(); - if input.userName != helpers::clean::clean(input.userName.as_ref()) { + if input.userName != helpers::clean::clean_no_space(input.userName.as_ref()) { return status::Custom(Status::Ok, "-4") } diff --git a/src/endpoints/accounts/register_account.rs b/src/endpoints/accounts/register_account.rs index 92c02f5..de04a33 100644 --- a/src/endpoints/accounts/register_account.rs +++ b/src/endpoints/accounts/register_account.rs @@ -26,7 +26,7 @@ pub fn register_account(input: Form) -> status::Custom<&'st return status::Custom(Status::Ok, "-1") } - if input.userName != helpers::clean::clean(input.userName.as_ref()) { + if input.userName != helpers::clean::clean_no_space(input.userName.as_ref()) { return status::Custom(Status::Ok, "-4") } diff --git a/src/endpoints/levels/upload_level.rs b/src/endpoints/levels/upload_level.rs index fb930cc..bae6579 100644 --- a/src/endpoints/levels/upload_level.rs +++ b/src/endpoints/levels/upload_level.rs @@ -96,17 +96,39 @@ pub fn upload_level(input: Form) -> status::Custom<&'static str return status::Custom(Status::Ok, "-1") } - // TODO: add level to db, file + let updated_level = diesel::update(levels) + .filter(id.eq(input.levelID)) + .set(( + description.eq(description_val.chars().take(140).collect::()), + password.eq(input.password.clone()), + requested_stars.eq(match input.requestedStars { + Some(requested_stars_val) => requested_stars_val.clamp(0, 10), + None => 0 + }), + version.eq(input.levelVersion), + extra_data.eq(extra_string.as_bytes().to_owned()), + level_info.eq(input.levelInfo.clone().into_bytes()), + editor_time.eq(input.wt.unwrap_or(0)), + editor_time_copies.eq(input.wt2.unwrap_or(0)), + song_id.eq(song_id_val), + length.eq(0), // unimplemeneted + objects.eq(0), // unimplemented + coins.eq(0), // unimplemented + has_ldm.eq(input.ldm.unwrap_or(0)), + two_player.eq(0) // unimplemented + )) + .get_result::(connection) + .expect("failed to update level"); + + fs::write(format!("{}/levels/{}.lvl", crate::CONFIG.db.data_folder, updated_level.id), general_purpose::URL_SAFE.decode(input.levelString.clone()).expect("user provided invalid level string")).expect("couldnt write level to file"); return status::Custom(Status::Ok, Box::leak(input.levelID.to_string().into_boxed_str())) } else { // upload level let new_level = NewLevel { - // TODO: clean and filter this to 20 charfacters - name: input.levelName.clone(), + name: helpers::clean::clean_basic(&input.levelName).chars().take(20).collect(), user_id: user_id_val, - // TODO: filter this to 140 characters - description: description_val, + description: description_val.chars().take(140).collect(), original: input.original.unwrap_or(0), game_version: input.gameVersion, binary_version: input.binaryVersion.unwrap_or(0), @@ -130,9 +152,9 @@ pub fn upload_level(input: Form) -> status::Custom<&'static str }; let inserted_level = diesel::insert_into(levels) - .values(&new_level) - .get_result::(connection) - .expect("failed to insert level"); + .values(&new_level) + .get_result::(connection) + .expect("failed to insert level"); fs::write(format!("{}/levels/{}.lvl", crate::CONFIG.db.data_folder, inserted_level.id), general_purpose::URL_SAFE.decode(input.levelString.clone()).expect("user provided invalid level string")).expect("couldnt write level to file"); diff --git a/src/helpers/clean.rs b/src/helpers/clean.rs index 855b39a..afd4161 100644 --- a/src/helpers/clean.rs +++ b/src/helpers/clean.rs @@ -1,6 +1,11 @@ use regex::Regex; -pub fn clean(string: &str) -> String { +pub fn clean_no_space(string: &str) -> String { let regex = Regex::new(r"[^a-zA-z0-9_-]").unwrap(); return regex.replace_all(string, "").to_string(); +} + +pub fn clean_basic(string: &str) -> String { + let regex = Regex::new(r"[^A-Za-z0-9\-_ ]").unwrap(); + return regex.replace_all(string, "").to_string(); } \ No newline at end of file