diff --git a/src/main.rs b/src/main.rs index 97677aa..66cf136 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,33 +158,48 @@ async fn logout(cookies: &CookieJar<'_>) -> status::Custom<&'static str> { } #[get("/adminpanel")] -async fn adminpanel(mut db: Connection, cookies: &CookieJar<'_>) -> status::Custom> { +async fn adminpanel( + mut db: Connection, + cookies: &CookieJar<'_>, +) -> status::Custom> { let token = cookies.get_private("token"); match token { Some(t) => match User::get_by_token(&mut db, t).await { Some(user) => match user.admin { - true => status::Custom(Status::Ok, RawHtml( - fs::read_to_string("/srv/web/adminpanel.html") - .unwrap() - .replace("{{username}}", &user.username[..])), + true => status::Custom( + Status::Ok, + RawHtml( + fs::read_to_string("/srv/web/adminpanel.html") + .unwrap() + .replace("{{username}}", &user.username[..]), + ), + ), + false => status::Custom( + Status::Unauthorized, + RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap()), ), - false => status::Custom(Status::Unauthorized, RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap())), }, - None => status::Custom(Status::Unauthorized, RawHtml( - fs::read_to_string("/srv/web/error.html") - .unwrap() - .replace("{{errorcode}}", "401")), + None => status::Custom( + Status::Unauthorized, + RawHtml( + fs::read_to_string("/srv/web/error.html") + .unwrap() + .replace("{{errorcode}}", "401"), + ), ), }, - None => status::Custom(Status::Unauthorized, RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap())), + None => status::Custom( + Status::Unauthorized, + RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap()), + ), } } -#[derive(Deserialize, Serialize)] -#[serde(crate = "rocket::serde")] -struct ApiPermsResult { - perms: Result, -} +// #[derive(Deserialize, Serialize)] +// #[serde(crate = "rocket::serde")] +// struct ApiPermsResult { +// perms: Result, +// } #[derive(Deserialize, Serialize)] #[serde(crate = "rocket::serde")] struct Perms { @@ -197,33 +212,26 @@ async fn api_perms( mut db: Connection, username: String, cookies: &CookieJar<'_>, -) -> Json { +) -> status::Custom>> { match cookies.get_private("token") { Some(t) => match User::get_by_token(&mut db, t).await { Some(user) => match user.admin { true => match User::get_by_username(&mut db, &username).await { - Some(user) => Json(ApiPermsResult { - perms: Ok(Perms { + Some(user) => status::Custom( + Status::Ok, + Json(Ok(Perms { admin: user.admin, make_posts: user.make_posts, comment: user.comment, - }), - }), - None => Json(ApiPermsResult { - perms: Err("User doesn't exist".to_string()), - }), + })), + ), + None => status::Custom(Status::NotFound, Json(Err("User doesn't exist"))), }, - false => Json(ApiPermsResult { - perms: Err("You don't have the permission to do this".to_string()), - }), + false => status::Custom(Status::Unauthorized, Json(Err("You don't have the permission to do this"))), }, - None => Json(ApiPermsResult { - perms: Err("Invalid token".to_string()), - }), + None => status::Custom(Status::Unauthorized, Json(Err("Invalid token"))), }, - None => Json(ApiPermsResult { - perms: Err("Not logged in".to_string()), - }), + None => status::Custom(Status::Unauthorized, Json(Err("Not logged in"))), } }