diff --git a/.changes/config.json b/.changes/config.json index 844f7a4..17f27da 100644 --- a/.changes/config.json +++ b/.changes/config.json @@ -1,5 +1,12 @@ { "gitSiteUrl": "https://github.com/swpu-acm/online-judge/", + "changeTags": { + "feat": "New Features", + "fix": "Bug Fixes", + "perf": "Performance Improvements", + "chore": "Chores", + "refactor": "Refactors" + }, "pkgManagers": { "rust": { "version": true, diff --git a/.changes/refactor-profile.md b/.changes/refactor-profile.md new file mode 100644 index 0000000..2e4b039 --- /dev/null +++ b/.changes/refactor-profile.md @@ -0,0 +1,5 @@ +--- +"algohub-server": patch:refactor +--- + +Refactor api for getting user profile, allow get user profile anonymously diff --git a/src/models/shared.rs b/src/models/shared.rs index bd433c6..384f42d 100644 --- a/src/models/shared.rs +++ b/src/models/shared.rs @@ -24,3 +24,9 @@ pub struct OwnedCredentials { pub id: String, pub token: String, } + +#[derive(Serialize, Deserialize)] +#[serde(crate = "rocket::serde")] +pub struct Token<'r> { + pub token: &'r str, +} diff --git a/src/routes/account.rs b/src/routes/account.rs index 23f0a00..36c31af 100644 --- a/src/routes/account.rs +++ b/src/routes/account.rs @@ -19,7 +19,7 @@ use crate::{ account::Profile, error::{Error, ErrorResponse}, response::{Empty, Response}, - Record, + Record, Token, }, utils::{account, session}, Result, @@ -104,24 +104,8 @@ pub async fn profile(db: &State>, profile: Json> } } -#[derive(Serialize, Deserialize)] -#[serde(crate = "rocket::serde")] -pub struct Authenticate<'r> { - pub token: &'r str, -} - -#[post("/profile/", data = "")] -pub async fn get_profile( - db: &State>, - id: &str, - auth: Json>, -) -> Result { - if !session::verify(db, id, auth.token).await { - return Err(Error::Unauthorized(Json( - "Failed to grant access permission".into(), - ))); - } - +#[get("/profile/")] +pub async fn get_profile(db: &State>, id: &str) -> Result { let profile = account::get_by_id::(db, id) .await .map_err(|e| Error::NotFound(Json(e.to_string().into())))? @@ -209,11 +193,7 @@ pub async fn upload_content( } #[post("/delete/", data = "")] -pub async fn delete( - db: &State>, - id: &str, - auth: Json>, -) -> Result { +pub async fn delete(db: &State>, id: &str, auth: Json>) -> Result { if !session::verify(db, id, auth.token).await { return Err(Error::Unauthorized(Json( "Failed to grant access permission".into(), diff --git a/tests/account.rs b/tests/account.rs index 1a98f6e..72394db 100644 --- a/tests/account.rs +++ b/tests/account.rs @@ -1,13 +1,14 @@ use std::{fs::File, io::Read, path::Path}; -use anyhow::Result; use algohub_server::{ models::{ account::Profile, response::{Empty, Response}, + Token, }, - routes::account::{Authenticate, ProfileData, RegisterData, RegisterResponse, UploadResponse}, + routes::account::{ProfileData, RegisterData, RegisterResponse, UploadResponse}, }; +use anyhow::Result; use rocket::{http::ContentType, local::asynchronous::Client}; pub struct Upload { @@ -127,8 +128,7 @@ async fn test_register() -> Result<()> { assert!(empty_data.is_none()); let response = client - .post(format!("/account/profile/{}", &id)) - .json(&Authenticate { token: &token }) + .get(format!("/account/profile/{}", &id)) .dispatch() .await; @@ -169,7 +169,7 @@ async fn test_register() -> Result<()> { let response = client .post(format!("/account/delete/{}", id)) - .json(&Authenticate { token: &token }) + .json(&Token { token: &token }) .dispatch() .await; diff --git a/tests/problem.rs b/tests/problem.rs index 694f2ae..20352ec 100644 --- a/tests/problem.rs +++ b/tests/problem.rs @@ -2,10 +2,10 @@ use algohub_server::{ models::{ problem::{Mode, ProblemDetail}, response::{Empty, Response}, - OwnedCredentials, + OwnedCredentials, Token, }, routes::{ - account::{Authenticate, RegisterData, RegisterResponse}, + account::{RegisterData, RegisterResponse}, problem::{ListProblem, ProblemData, ProblemFilter, ProblemResponse}, }, }; @@ -131,7 +131,7 @@ async fn test_problem() -> Result<()> { client .post(format!("/account/delete/{}", id)) - .json(&Authenticate { token: &token }) + .json(&Token { token: &token }) .dispatch() .await .into_json::>()