Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace BytesRequest with Json and Parts extractors #10230

Merged
merged 3 commits into from
Dec 17, 2024
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
12 changes: 4 additions & 8 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::controllers::helpers::pagination::{Page, PaginationOptions};
use crate::models::{Crate, CrateOwnerInvitation, Rights, User};
use crate::schema::{crate_owner_invitations, crates, users};
use crate::util::errors::{bad_request, forbidden, internal, AppResult};
use crate::util::{BytesRequest, RequestUtils};
use crate::util::RequestUtils;
use crate::views::{
EncodableCrateOwnerInvitation, EncodableCrateOwnerInvitationV1, EncodablePublicUser,
InvitationResponse,
Expand Down Expand Up @@ -279,7 +279,7 @@ struct ResponseMeta {
}

#[derive(Deserialize)]
struct OwnerInvitation {
pub struct OwnerInvitation {
crate_owner_invite: InvitationResponse,
}

Expand All @@ -295,13 +295,9 @@ struct OwnerInvitation {
)]
pub async fn handle_crate_owner_invitation(
state: AppState,
req: BytesRequest,
parts: Parts,
Json(crate_invite): Json<OwnerInvitation>,
) -> AppResult<ErasedJson> {
let (parts, body) = req.0.into_parts();

let crate_invite: OwnerInvitation =
serde_json::from_slice(&body).map_err(|_| bad_request("invalid json request"))?;

let crate_invite = crate_invite.crate_owner_invite;

let mut conn = state.db_write().await?;
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/user/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::models::krate::CrateName;
use crate::models::{CrateOwner, Follow, OwnerKind, User, Version, VersionOwnerAction};
use crate::schema::{crate_owners, crates, emails, follows, users, versions};
use crate::util::errors::{bad_request, AppResult};
use crate::util::BytesRequest;
use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCrate};

/// Get the currently authenticated user.
Expand Down Expand Up @@ -140,6 +139,12 @@ pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> A
ok_true()
}

#[derive(Deserialize)]
pub struct CrateEmailNotifications {
id: i32,
email_notifications: bool,
}

/// Update email notification settings for the authenticated user.
///
/// This endpoint was implemented for an experimental feature that was never
Expand All @@ -151,19 +156,14 @@ pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> A
responses((status = 200, description = "Successful Response")),
)]
#[deprecated]
pub async fn update_email_notifications(app: AppState, req: BytesRequest) -> AppResult<Response> {
pub async fn update_email_notifications(
app: AppState,
parts: Parts,
Json(updates): Json<Vec<CrateEmailNotifications>>,
) -> AppResult<Response> {
use diesel::pg::upsert::excluded;

let (parts, body) = req.0.into_parts();

#[derive(Deserialize)]
struct CrateEmailNotifications {
id: i32,
email_notifications: bool,
}

let updates: HashMap<i32, bool> = serde_json::from_slice::<Vec<CrateEmailNotifications>>(&body)
.map_err(|_| bad_request("invalid json request"))?
let updates: HashMap<i32, bool> = updates
.iter()
.map(|c| (c.id, c.email_notifications))
.collect();
Expand Down
19 changes: 0 additions & 19 deletions src/controllers/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::middleware::app::RequestApp;
use crate::middleware::log_request::RequestLogExt;
use crate::util::errors::{forbidden, AppResult};
use crate::util::BytesRequest;
use http::request::Parts;
use http::{header, Extensions, HeaderMap, HeaderValue, Method, Request, Uri, Version};

Expand Down Expand Up @@ -73,21 +72,3 @@ impl<B> RequestPartsExt for Request<B> {
self.extensions()
}
}

impl RequestPartsExt for BytesRequest {
fn method(&self) -> &Method {
self.0.method()
}
fn uri(&self) -> &Uri {
self.0.uri()
}
fn version(&self) -> Version {
self.0.version()
}
fn headers(&self) -> &HeaderMap<HeaderValue> {
self.0.headers()
}
fn extensions(&self) -> &Extensions {
self.0.extensions()
}
}
2 changes: 0 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub use self::bytes_request::BytesRequest;
pub use self::io_util::{read_fill, read_le_u32};
pub use self::request_helpers::*;

mod bytes_request;
pub mod diesel;
pub mod errors;
mod io_util;
Expand Down
89 changes: 0 additions & 89 deletions src/util/bytes_request.rs

This file was deleted.