Skip to content

Commit 4952046

Browse files
committed
Add an environment variable to let us exclude crate IDs from the recent downloads list
1 parent 0cbb775 commit 4952046

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +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>,
2829
pub domain_name: String,
2930
pub allowed_origins: Vec<String>,
3031
pub downloads_persist_interval_ms: usize,
@@ -84,6 +85,15 @@ impl Default for Server {
8485
Some(s) => s.split(',').map(String::from).collect(),
8586
};
8687
let base = Base::from_environment();
88+
let excluded_crate_ids: Vec<i32> = match env_optional::<String>("EXCLUDED_CRATE_IDS") {
89+
None => vec![],
90+
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(),
96+
};
8797
Server {
8898
db: DatabasePools::full_from_environment(&base),
8999
base,
@@ -97,6 +107,7 @@ impl Default for Server {
97107
blocked_traffic: blocked_traffic(),
98108
max_allowed_page_offset: env_optional("WEB_MAX_ALLOWED_PAGE_OFFSET").unwrap_or(200),
99109
page_offset_ua_blocklist,
110+
excluded_crate_ids,
100111
domain_name: domain_name(),
101112
allowed_origins,
102113
downloads_persist_interval_ms: dotenv::var("DOWNLOADS_PERSIST_INTERVAL_MS")

src/controllers/krate/metadata.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ 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;
28+
29+
let config = &req.app().config;
2730

2831
let conn = req.db_read_only()?;
2932
let num_crates: i64 = crates.count().get_result(&*conn)?;
@@ -77,8 +80,14 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
7780
.limit(10)
7881
.load(&*conn)?;
7982

80-
let most_recently_downloaded = crates
83+
let mut most_recently_downloaded_query = crates
8184
.inner_join(recent_crate_downloads::table)
85+
.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)));
89+
}
90+
let most_recently_downloaded = most_recently_downloaded_query
8291
.then_order_by(recent_crate_downloads::downloads.desc())
8392
.select(selection)
8493
.limit(10)

src/tests/util/test_app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +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![],
303304
domain_name: "crates.io".into(),
304305
allowed_origins: Vec::new(),
305306
downloads_persist_interval_ms: 1000,

0 commit comments

Comments
 (0)