Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/controllers/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use crate::util::errors::AppResult;
use axum::Json;
use axum::response::{IntoResponse, Response};
use axum_extra::json;

pub mod authorization;
pub(crate) mod pagination;

pub(crate) use self::pagination::Paginate;

pub fn ok_true() -> AppResult<Response> {
let json = json!({ "ok": true });
Ok(json.into_response())
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct OkResponse {
#[schema(example = true)]
ok: bool,
}

impl Default for OkResponse {
fn default() -> Self {
Self::new()
}
}

impl OkResponse {
pub fn new() -> Self {
Self { ok: true }
}
}

impl IntoResponse for OkResponse {
fn into_response(self) -> Response {
Json(self).into_response()
}
}
15 changes: 7 additions & 8 deletions src/controllers/krate/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
use crate::app::AppState;
use crate::auth::AuthCheck;
use crate::controllers::helpers::ok_true;
use crate::controllers::helpers::OkResponse;
use crate::controllers::krate::CratePath;
use crate::models::{Crate, Follow};
use crate::schema::*;
use crate::util::errors::{AppResult, crate_not_found};
use axum::response::Response;
use axum_extra::json;
use axum_extra::response::ErasedJson;
use diesel::prelude::*;
Expand Down Expand Up @@ -39,9 +38,9 @@ async fn follow_target(
("cookie" = []),
),
tag = "crates",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<Response> {
pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<OkResponse> {
let mut conn = app.db_write().await?;
let user_id = AuthCheck::default().check(&req, &mut conn).await?.user_id();
let follow = follow_target(&path.name, &mut conn, user_id).await?;
Expand All @@ -51,7 +50,7 @@ pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResu
.execute(&mut conn)
.await?;

ok_true()
Ok(OkResponse::new())
}

/// Unfollow a crate.
Expand All @@ -64,15 +63,15 @@ pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResu
("cookie" = []),
),
tag = "crates",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn unfollow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<Response> {
pub async fn unfollow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<OkResponse> {
let mut conn = app.db_write().await?;
let user_id = AuthCheck::default().check(&req, &mut conn).await?.user_id();
let follow = follow_target(&path.name, &mut conn, user_id).await?;
diesel::delete(&follow).execute(&mut conn).await?;

ok_true()
Ok(OkResponse::new())
}

/// Check if a crate is followed.
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/user/email_notifications.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::app::AppState;
use crate::auth::AuthCheck;
use crate::controllers::helpers::ok_true;
use crate::controllers::helpers::OkResponse;
use crate::models::{CrateOwner, OwnerKind};
use crate::schema::crate_owners;
use crate::util::errors::AppResult;
use axum::Json;
use axum::response::Response;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use http::request::Parts;
Expand All @@ -29,14 +28,14 @@ pub struct CrateEmailNotifications {
("cookie" = []),
),
tag = "users",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
#[deprecated]
pub async fn update_email_notifications(
app: AppState,
parts: Parts,
Json(updates): Json<Vec<CrateEmailNotifications>>,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
use diesel::pg::upsert::excluded;

let updates: HashMap<i32, bool> = updates
Expand Down Expand Up @@ -89,5 +88,5 @@ pub async fn update_email_notifications(
.execute(&mut conn)
.await?;

ok_true()
Ok(OkResponse::new())
}
18 changes: 10 additions & 8 deletions src/controllers/user/email_verification.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::update::UserConfirmEmail;
use crate::app::AppState;
use crate::auth::AuthCheck;
use crate::controllers::helpers::ok_true;
use crate::controllers::helpers::OkResponse;
use crate::models::Email;
use crate::util::errors::AppResult;
use crate::util::errors::{BoxedAppError, bad_request};
use axum::extract::Path;
use axum::response::Response;
use crates_io_database::schema::emails;
use diesel::dsl::sql;
use diesel::prelude::*;
Expand All @@ -22,9 +21,12 @@ use http::request::Parts;
("email_token" = String, Path, description = "Secret verification token sent to the user's email address"),
),
tag = "users",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> AppResult<Response> {
pub async fn confirm_user_email(
state: AppState,
Path(token): Path<String>,
) -> AppResult<OkResponse> {
let mut conn = state.db_write().await?;

let updated_rows = diesel::update(emails::table.filter(emails::token.eq(&token)))
Expand All @@ -36,7 +38,7 @@ pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> A
return Err(bad_request("Email belonging to token not found."));
}

ok_true()
Ok(OkResponse::new())
}

/// Regenerate and send an email verification token.
Expand All @@ -51,13 +53,13 @@ pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> A
("cookie" = []),
),
tag = "users",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn resend_email_verification(
state: AppState,
Path(param_user_id): Path<i32>,
req: Parts,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
let mut conn = state.db_write().await?;
let auth = AuthCheck::default().check(&req, &mut conn).await?;

Expand Down Expand Up @@ -91,7 +93,7 @@ pub async fn resend_email_verification(
})
.await?;

ok_true()
Ok(OkResponse::new())
}

#[cfg(test)]
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/user/update.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::app::AppState;
use crate::auth::AuthCheck;
use crate::controllers::helpers::ok_true;
use crate::controllers::helpers::OkResponse;
use crate::models::NewEmail;
use crate::schema::users;
use crate::util::errors::{AppResult, bad_request, server_error};
use axum::Json;
use axum::extract::Path;
use axum::response::Response;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use http::request::Parts;
Expand Down Expand Up @@ -40,14 +39,14 @@ pub struct User {
("cookie" = []),
),
tag = "users",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn update_user(
state: AppState,
Path(param_user_id): Path<i32>,
req: Parts,
Json(user_update): Json<UserUpdate>,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
let mut conn = state.db_write().await?;
let auth = AuthCheck::default().check(&req, &mut conn).await?;

Expand Down Expand Up @@ -116,7 +115,7 @@ pub async fn update_user(
let _ = state.emails.send(user_email, email).await;
}

ok_true()
Ok(OkResponse::new())
}

pub struct UserConfirmEmail<'a> {
Expand Down
15 changes: 7 additions & 8 deletions src/controllers/version/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use super::CrateVersionPath;
use super::update::{authenticate, perform_version_yank_update};
use crate::app::AppState;
use crate::controllers::helpers::ok_true;
use crate::controllers::helpers::OkResponse;
use crate::rate_limiter::LimitedAction;
use crate::util::errors::AppResult;
use axum::response::Response;
use http::request::Parts;

/// Yank a crate version.
Expand All @@ -29,13 +28,13 @@ use http::request::Parts;
("cookie" = []),
),
tag = "versions",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn yank_version(
app: AppState,
path: CrateVersionPath,
req: Parts,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
modify_yank(path, app, req, true).await
}

Expand All @@ -49,13 +48,13 @@ pub async fn yank_version(
("cookie" = []),
),
tag = "versions",
responses((status = 200, description = "Successful Response")),
responses((status = 200, description = "Successful Response", body = inline(OkResponse))),
)]
pub async fn unyank_version(
app: AppState,
path: CrateVersionPath,
req: Parts,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
modify_yank(path, app, req, false).await
}

Expand All @@ -65,7 +64,7 @@ async fn modify_yank(
state: AppState,
req: Parts,
yanked: bool,
) -> AppResult<Response> {
) -> AppResult<OkResponse> {
// FIXME: Should reject bad requests before authentication, but can't due to
// lifetime issues with `req`.

Expand All @@ -89,5 +88,5 @@ async fn modify_yank(
)
.await?;

ok_true()
Ok(OkResponse::new())
}
Loading