-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10231 from Turbo87/controller-modules
Extract more controller modules
- Loading branch information
Showing
18 changed files
with
492 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::app::AppState; | ||
use crate::controllers::helpers::pagination::PaginationOptions; | ||
use crate::controllers::krate::CratePath; | ||
use crate::models::{CrateName, User, Version, VersionOwnerAction}; | ||
use crate::util::errors::AppResult; | ||
use crate::views::{EncodableDependency, EncodableVersion}; | ||
use axum_extra::json; | ||
use axum_extra::response::ErasedJson; | ||
use crates_io_database::schema::{crates, users, versions}; | ||
use diesel::prelude::*; | ||
use diesel_async::RunQueryDsl; | ||
use http::request::Parts; | ||
|
||
/// List reverse dependencies of a crate. | ||
#[utoipa::path( | ||
get, | ||
path = "/api/v1/crates/{name}/reverse_dependencies", | ||
params(CratePath), | ||
tag = "crates", | ||
responses((status = 200, description = "Successful Response")), | ||
)] | ||
pub async fn list_reverse_dependencies( | ||
app: AppState, | ||
path: CratePath, | ||
req: Parts, | ||
) -> AppResult<ErasedJson> { | ||
let mut conn = app.db_read().await?; | ||
|
||
let pagination_options = PaginationOptions::builder().gather(&req)?; | ||
|
||
let krate = path.load_crate(&mut conn).await?; | ||
|
||
let (rev_deps, total) = krate | ||
.reverse_dependencies(&mut conn, pagination_options) | ||
.await?; | ||
|
||
let rev_deps: Vec<_> = rev_deps | ||
.into_iter() | ||
.map(|dep| EncodableDependency::from_reverse_dep(dep, &krate.name)) | ||
.collect(); | ||
|
||
let version_ids: Vec<i32> = rev_deps.iter().map(|dep| dep.version_id).collect(); | ||
|
||
let versions_and_publishers: Vec<(Version, CrateName, Option<User>)> = versions::table | ||
.filter(versions::id.eq_any(version_ids)) | ||
.inner_join(crates::table) | ||
.left_outer_join(users::table) | ||
.select(<(Version, CrateName, Option<User>)>::as_select()) | ||
.load(&mut conn) | ||
.await?; | ||
|
||
let versions = versions_and_publishers | ||
.iter() | ||
.map(|(v, ..)| v) | ||
.collect::<Vec<_>>(); | ||
|
||
let actions = VersionOwnerAction::for_versions(&mut conn, &versions).await?; | ||
|
||
let versions = versions_and_publishers | ||
.into_iter() | ||
.zip(actions) | ||
.map(|((version, krate_name, published_by), actions)| { | ||
EncodableVersion::from(version, &krate_name.name, published_by, actions) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(json!({ | ||
"dependencies": rev_deps, | ||
"versions": versions, | ||
"meta": { "total": total }, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
pub mod email_notifications; | ||
pub mod email_verification; | ||
pub mod me; | ||
pub mod other; | ||
pub mod resend; | ||
pub mod session; | ||
pub mod update; | ||
|
||
pub use resend::resend_email_verification; | ||
pub use email_verification::resend_email_verification; | ||
pub use update::update_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use crate::app::AppState; | ||
use crate::auth::AuthCheck; | ||
use crate::controllers::helpers::ok_true; | ||
use crate::models::{CrateOwner, OwnerKind}; | ||
use crate::schema::crate_owners; | ||
use crate::util::errors::AppResult; | ||
use axum::response::Response; | ||
use axum::Json; | ||
use diesel::prelude::*; | ||
use diesel_async::RunQueryDsl; | ||
use http::request::Parts; | ||
use std::collections::HashMap; | ||
|
||
#[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 | ||
/// fully implemented. It is now deprecated and will be removed in the future. | ||
#[utoipa::path( | ||
put, | ||
path = "/api/v1/me/email_notifications", | ||
tag = "users", | ||
responses((status = 200, description = "Successful Response")), | ||
)] | ||
#[deprecated] | ||
pub async fn update_email_notifications( | ||
app: AppState, | ||
parts: Parts, | ||
Json(updates): Json<Vec<CrateEmailNotifications>>, | ||
) -> AppResult<Response> { | ||
use diesel::pg::upsert::excluded; | ||
|
||
let updates: HashMap<i32, bool> = updates | ||
.iter() | ||
.map(|c| (c.id, c.email_notifications)) | ||
.collect(); | ||
|
||
let mut conn = app.db_write().await?; | ||
let user_id = AuthCheck::default() | ||
.check(&parts, &mut conn) | ||
.await? | ||
.user_id(); | ||
|
||
// Build inserts from existing crates belonging to the current user | ||
let to_insert = CrateOwner::by_owner_kind(OwnerKind::User) | ||
.filter(crate_owners::owner_id.eq(user_id)) | ||
.select(( | ||
crate_owners::crate_id, | ||
crate_owners::owner_id, | ||
crate_owners::owner_kind, | ||
crate_owners::email_notifications, | ||
)) | ||
.load(&mut conn) | ||
.await? | ||
.into_iter() | ||
// Remove records whose `email_notifications` will not change from their current value | ||
.map( | ||
|(c_id, o_id, o_kind, e_notifications): (i32, i32, i32, bool)| { | ||
let current_e_notifications = *updates.get(&c_id).unwrap_or(&e_notifications); | ||
( | ||
crate_owners::crate_id.eq(c_id), | ||
crate_owners::owner_id.eq(o_id), | ||
crate_owners::owner_kind.eq(o_kind), | ||
crate_owners::email_notifications.eq(current_e_notifications), | ||
) | ||
}, | ||
) | ||
.collect::<Vec<_>>(); | ||
|
||
// Upsert crate owners; this should only actually execute updates | ||
diesel::insert_into(crate_owners::table) | ||
.values(&to_insert) | ||
.on_conflict(( | ||
crate_owners::crate_id, | ||
crate_owners::owner_id, | ||
crate_owners::owner_kind, | ||
)) | ||
.do_update() | ||
.set(crate_owners::email_notifications.eq(excluded(crate_owners::email_notifications))) | ||
.execute(&mut conn) | ||
.await?; | ||
|
||
ok_true() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.