Skip to content

Commit 17bc3b8

Browse files
committed
Switch to excluding by crate name instead
1 parent 4952046 commit 17bc3b8

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,
@@ -85,14 +85,10 @@ impl Default for Server {
8585
Some(s) => s.split(',').map(String::from).collect(),
8686
};
8787
let base = Base::from_environment();
88-
let excluded_crate_ids: Vec<i32> = match env_optional::<String>("EXCLUDED_CRATE_IDS") {
88+
let excluded_crate_names = match env_optional::<String>("EXCLUDED_CRATE_NAMES") {
8989
None => vec![],
9090
Some(s) if s.is_empty() => vec![],
91-
Some(s) => s
92-
.split(',')
93-
.map(|n| n.parse())
94-
.collect::<Result<Vec<_>, _>>()
95-
.unwrap_or_default(),
91+
Some(s) => s.split(',').map(String::from).collect(),
9692
};
9793
Server {
9894
db: DatabasePools::full_from_environment(&base),
@@ -107,7 +103,7 @@ impl Default for Server {
107103
blocked_traffic: blocked_traffic(),
108104
max_allowed_page_offset: env_optional("WEB_MAX_ALLOWED_PAGE_OFFSET").unwrap_or(200),
109105
page_offset_ua_blocklist,
110-
excluded_crate_ids,
106+
excluded_crate_names,
111107
domain_name: domain_name(),
112108
allowed_origins,
113109
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
@@ -24,7 +24,7 @@ use crate::models::krate::ALL_COLUMNS;
2424
/// Handles the `GET /summary` route.
2525
pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
2626
use crate::schema::crates::dsl::*;
27-
use diesel::dsl::any;
27+
use diesel::dsl::all;
2828

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

@@ -83,9 +83,9 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
8383
let mut most_recently_downloaded_query = crates
8484
.inner_join(recent_crate_downloads::table)
8585
.into_boxed();
86-
if !config.excluded_crate_ids.is_empty() {
87-
most_recently_downloaded_query = most_recently_downloaded_query
88-
.filter(recent_crate_downloads::crate_id.ne(any(&config.excluded_crate_ids)));
86+
if !config.excluded_crate_names.is_empty() {
87+
most_recently_downloaded_query =
88+
most_recently_downloaded_query.filter(name.ne(all(&config.excluded_crate_names)));
8989
}
9090
let most_recently_downloaded = most_recently_downloaded_query
9191
.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)