Skip to content

Commit

Permalink
Merge pull request #10298 from eth3lbert/tidy-crate-metadata
Browse files Browse the repository at this point in the history
Tidy crate metadata
  • Loading branch information
Turbo87 authored Jan 3, 2025
2 parents 1d1c699 + 416534c commit 8e55b04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
32 changes: 18 additions & 14 deletions src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use crate::app::AppState;
use crate::controllers::krate::CratePath;
use crate::models::{
Category, Crate, CrateCategory, CrateKeyword, Keyword, RecentCrateDownloads, User, Version,
VersionOwnerAction,
Category, Crate, CrateCategory, CrateKeyword, Keyword, RecentCrateDownloads, TopVersions, User,
Version, VersionOwnerAction,
};
use crate::schema::*;
use crate::util::errors::{bad_request, crate_not_found, AppResult, BoxedAppError};
Expand All @@ -18,7 +18,6 @@ use axum_extra::json;
use axum_extra::response::ErasedJson;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use std::cmp::Reverse;
use std::str::FromStr;

#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
Expand Down Expand Up @@ -91,15 +90,12 @@ pub async fn find_crate(
.ok_or_else(|| crate_not_found(&path.name))?;

let mut versions_publishers_and_audit_actions = if include.versions {
let mut versions_and_publishers: Vec<(Version, Option<User>)> =
Version::belonging_to(&krate)
.left_outer_join(users::table)
.select(<(Version, Option<User>)>::as_select())
.load(&mut conn)
.await?;

versions_and_publishers
.sort_by_cached_key(|(version, _)| Reverse(semver::Version::parse(&version.num).ok()));
let versions_and_publishers: Vec<(Version, Option<User>)> = Version::belonging_to(&krate)
.left_outer_join(users::table)
.select(<(Version, Option<User>)>::as_select())
.order_by(versions::id)
.load(&mut conn)
.await?;

let versions = versions_and_publishers
.iter()
Expand Down Expand Up @@ -164,8 +160,16 @@ pub async fn find_crate(
None
};

let top_versions = if include.versions {
Some(krate.top_versions(&mut conn).await?)
let top_versions = if let Some(versions) = versions_publishers_and_audit_actions
.as_ref()
.filter(|_| include.versions)
{
let pairs = versions
.iter()
.filter(|(v, _, _)| !v.yanked)
.cloned()
.map(|(v, _, _)| (v.created_at, v.num));
Some(TopVersions::from_date_version_pairs(pairs))
} else {
None
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ snapshot_kind: text
"updated_at": "[datetime]",
"versions": [
1,
3,
2
2,
3
],
"yanked": false
},
Expand Down Expand Up @@ -90,30 +90,30 @@ snapshot_kind: text
"crate_size": 0,
"created_at": "[datetime]",
"description": null,
"dl_path": "/api/v1/crates/foo_show/0.5.1/download",
"dl_path": "/api/v1/crates/foo_show/0.5.0/download",
"documentation": null,
"downloads": 0,
"edition": null,
"features": {},
"has_lib": null,
"homepage": null,
"id": 3,
"id": 2,
"lib_links": null,
"license": null,
"links": {
"authors": "/api/v1/crates/foo_show/0.5.1/authors",
"dependencies": "/api/v1/crates/foo_show/0.5.1/dependencies",
"version_downloads": "/api/v1/crates/foo_show/0.5.1/downloads"
"authors": "/api/v1/crates/foo_show/0.5.0/authors",
"dependencies": "/api/v1/crates/foo_show/0.5.0/dependencies",
"version_downloads": "/api/v1/crates/foo_show/0.5.0/downloads"
},
"num": "0.5.1",
"num": "0.5.0",
"published_by": {
"avatar": null,
"id": 1,
"login": "foo",
"name": null,
"url": "https://github.com/foo"
},
"readme_path": "/api/v1/crates/foo_show/0.5.1/readme",
"readme_path": "/api/v1/crates/foo_show/0.5.0/readme",
"repository": null,
"rust_version": null,
"updated_at": "[datetime]",
Expand All @@ -128,30 +128,30 @@ snapshot_kind: text
"crate_size": 0,
"created_at": "[datetime]",
"description": null,
"dl_path": "/api/v1/crates/foo_show/0.5.0/download",
"dl_path": "/api/v1/crates/foo_show/0.5.1/download",
"documentation": null,
"downloads": 0,
"edition": null,
"features": {},
"has_lib": null,
"homepage": null,
"id": 2,
"id": 3,
"lib_links": null,
"license": null,
"links": {
"authors": "/api/v1/crates/foo_show/0.5.0/authors",
"dependencies": "/api/v1/crates/foo_show/0.5.0/dependencies",
"version_downloads": "/api/v1/crates/foo_show/0.5.0/downloads"
"authors": "/api/v1/crates/foo_show/0.5.1/authors",
"dependencies": "/api/v1/crates/foo_show/0.5.1/dependencies",
"version_downloads": "/api/v1/crates/foo_show/0.5.1/downloads"
},
"num": "0.5.0",
"num": "0.5.1",
"published_by": {
"avatar": null,
"id": 1,
"login": "foo",
"name": null,
"url": "https://github.com/foo"
},
"readme_path": "/api/v1/crates/foo_show/0.5.0/readme",
"readme_path": "/api/v1/crates/foo_show/0.5.1/readme",
"repository": null,
"rust_version": null,
"updated_at": "[datetime]",
Expand Down

0 comments on commit 8e55b04

Please sign in to comment.