Skip to content

Commit 2761002

Browse files
committed
Add an environment variable to let us exclude crate IDs from the recent downloads list
1 parent 11ef962 commit 2761002

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,
@@ -83,6 +84,15 @@ impl Default for Server {
8384
Some(s) if s.is_empty() => vec![],
8485
Some(s) => s.split(',').map(String::from).collect(),
8586
};
87+
let excluded_crate_ids: Vec<i32> = match env_optional::<String>("EXCLUDED_CRATE_IDS") {
88+
None => vec![],
89+
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(),
95+
};
8696
Server {
8797
db: DatabasePools::full_from_environment(),
8898
base: Base::from_environment(),
@@ -96,6 +106,7 @@ impl Default for Server {
96106
blocked_traffic: blocked_traffic(),
97107
max_allowed_page_offset: env_optional("WEB_MAX_ALLOWED_PAGE_OFFSET").unwrap_or(200),
98108
page_offset_ua_blocklist,
109+
excluded_crate_ids,
99110
domain_name: domain_name(),
100111
allowed_origins,
101112
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
@@ -23,6 +23,9 @@ 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;
27+
28+
let config = &req.app().config;
2629

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

79-
let most_recently_downloaded = crates
82+
let mut most_recently_downloaded_query = crates
8083
.inner_join(recent_crate_downloads::table)
84+
.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)));
88+
}
89+
let most_recently_downloaded = most_recently_downloaded_query
8190
.then_order_by(recent_crate_downloads::downloads.desc())
8291
.select(selection)
8392
.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)