Skip to content

Commit 0b39c70

Browse files
committed
Switch to excluding by crate name instead
1 parent 2761002 commit 0b39c70

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/config.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Server {
2525
pub blocked_traffic: Vec<(String, Vec<String>)>,
2626
pub max_allowed_page_offset: u32,
2727
pub page_offset_ua_blocklist: Vec<String>,
28-
pub excluded_crate_ids: Vec<i32>,
28+
pub excluded_crate_names: Vec<String>,
2929
pub domain_name: String,
3030
pub allowed_origins: Vec<String>,
3131
pub downloads_persist_interval_ms: usize,
@@ -84,14 +84,10 @@ impl Default for Server {
8484
Some(s) if s.is_empty() => vec![],
8585
Some(s) => s.split(',').map(String::from).collect(),
8686
};
87-
let excluded_crate_ids: Vec<i32> = match env_optional::<String>("EXCLUDED_CRATE_IDS") {
87+
let excluded_crate_names = match env_optional::<String>("EXCLUDED_CRATE_NAMES") {
8888
None => vec![],
8989
Some(s) if s.is_empty() => vec![],
90-
Some(s) => s
91-
.split(',')
92-
.map(|n| n.parse())
93-
.collect::<Result<Vec<_>, _>>()
94-
.unwrap_or_default(),
90+
Some(s) => s.split(',').map(String::from).collect(),
9591
};
9692
Server {
9793
db: DatabasePools::full_from_environment(),
@@ -106,7 +102,7 @@ impl Default for Server {
106102
blocked_traffic: blocked_traffic(),
107103
max_allowed_page_offset: env_optional("WEB_MAX_ALLOWED_PAGE_OFFSET").unwrap_or(200),
108104
page_offset_ua_blocklist,
109-
excluded_crate_ids,
105+
excluded_crate_names,
110106
domain_name: domain_name(),
111107
allowed_origins,
112108
downloads_persist_interval_ms: dotenv::var("DOWNLOADS_PERSIST_INTERVAL_MS")

src/controllers/krate/metadata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::models::krate::ALL_COLUMNS;
2323
/// Handles the `GET /summary` route.
2424
pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
2525
use crate::schema::crates::dsl::*;
26-
use diesel::dsl::any;
26+
use diesel::dsl::all;
2727

2828
let config = &req.app().config;
2929

@@ -82,9 +82,9 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
8282
let mut most_recently_downloaded_query = crates
8383
.inner_join(recent_crate_downloads::table)
8484
.into_boxed();
85-
if !config.excluded_crate_ids.is_empty() {
86-
most_recently_downloaded_query = most_recently_downloaded_query
87-
.filter(recent_crate_downloads::crate_id.ne(any(&config.excluded_crate_ids)));
85+
if !config.excluded_crate_names.is_empty() {
86+
most_recently_downloaded_query =
87+
most_recently_downloaded_query.filter(name.ne(all(&config.excluded_crate_names)));
8888
}
8989
let most_recently_downloaded = most_recently_downloaded_query
9090
.then_order_by(recent_crate_downloads::downloads.desc())

src/tests/krate/summary.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,42 @@ fn summary_new_crates() {
107107

108108
assert_eq!(json.new_crates.len(), 5);
109109
}
110+
111+
#[test]
112+
fn excluded_crate_id() {
113+
let (app, anon, user) = TestApp::init()
114+
.with_config(|config| {
115+
config.excluded_crate_names = vec![
116+
"most_recent_downloads".into(),
117+
// make sure no error occurs with a crate name that doesn't exist and that the name
118+
// matches are exact, not substrings
119+
"downloads".into(),
120+
];
121+
})
122+
.with_user();
123+
let user = user.as_model();
124+
app.db(|conn| {
125+
CrateBuilder::new("some_downloads", user.id)
126+
.version(VersionBuilder::new("0.1.0"))
127+
.description("description")
128+
.keyword("popular")
129+
.category("cat1")
130+
.downloads(20)
131+
.recent_downloads(10)
132+
.expect_build(conn);
133+
134+
CrateBuilder::new("most_recent_downloads", user.id)
135+
.version(VersionBuilder::new("0.2.0"))
136+
.keyword("popular")
137+
.category("cat1")
138+
.downloads(5000)
139+
.recent_downloads(50)
140+
.expect_build(conn);
141+
});
142+
143+
let json: SummaryResponse = anon.get("/api/v1/summary").good();
144+
145+
assert_eq!(json.most_recently_downloaded.len(), 1);
146+
assert_eq!(json.most_recently_downloaded[0].name, "some_downloads");
147+
assert_eq!(json.most_recently_downloaded[0].recent_downloads, Some(10));
148+
}

src/tests/util/test_app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ fn simple_config() -> config::Server {
300300
blocked_traffic: Default::default(),
301301
max_allowed_page_offset: 200,
302302
page_offset_ua_blocklist: vec![],
303-
excluded_crate_ids: vec![],
303+
excluded_crate_names: vec![],
304304
domain_name: "crates.io".into(),
305305
allowed_origins: Vec::new(),
306306
downloads_persist_interval_ms: 1000,

0 commit comments

Comments
 (0)