fix(discovery): hide restricted CurseForge projects
This commit is contained in:
1 parent
3f2f514097
commit
e77ae06bea
6 files changed
+104
-24
No files matched your search
@@ -84,7 +84,7 @@ impl ProgressTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fail(mut self, error: impl std::fmt::Display) {
|
pub fn fail(mut self, error: impl std::fmt::Display) {
|
||||||
tracing::warn!("Progress task failed: {error}");
|
tracing::debug!("Progress task failed: {error}");
|
||||||
self.remove();
|
self.remove();
|
||||||
self.finished = true;
|
self.finished = true;
|
||||||
}
|
}
|
||||||
|
|||||||
+46
-22
@@ -52,6 +52,7 @@ struct Mod {
|
|||||||
summary: String,
|
summary: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
download_count: u64,
|
download_count: u64,
|
||||||
|
allow_mod_distribution: Option<bool>,
|
||||||
logo: Option<Logo>,
|
logo: Option<Logo>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
categories: Vec<Category>,
|
categories: Vec<Category>,
|
||||||
@@ -230,13 +231,14 @@ async fn search(
|
|||||||
) -> Result<DiscoveryResults, NetError> {
|
) -> Result<DiscoveryResults, NetError> {
|
||||||
let loader = loader.and_then(loader_type);
|
let loader = loader.and_then(loader_type);
|
||||||
let mut projects = Vec::new();
|
let mut projects = Vec::new();
|
||||||
|
let mut received = 0;
|
||||||
let mut total_hits = 0;
|
let mut total_hits = 0;
|
||||||
while projects.len() < limit {
|
while received < limit {
|
||||||
let page_size = (limit - projects.len()).min(50);
|
let page_size = (limit - received).min(50);
|
||||||
let mut params = vec![
|
let mut params = vec![
|
||||||
format!("gameId={MINECRAFT_GAME_ID}"),
|
format!("gameId={MINECRAFT_GAME_ID}"),
|
||||||
format!("classId={class_id}"),
|
format!("classId={class_id}"),
|
||||||
format!("index={}", offset + projects.len()),
|
format!("index={}", offset + received),
|
||||||
format!("pageSize={page_size}"),
|
format!("pageSize={page_size}"),
|
||||||
"sortField=6".to_owned(),
|
"sortField=6".to_owned(),
|
||||||
"sortOrder=desc".to_owned(),
|
"sortOrder=desc".to_owned(),
|
||||||
@@ -257,32 +259,38 @@ async fn search(
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
total_hits = response.pagination.total_count;
|
total_hits = response.pagination.total_count;
|
||||||
let received = response.data.len();
|
let page_received = response.data.len();
|
||||||
|
received += page_received;
|
||||||
projects.extend(response.data);
|
projects.extend(response.data);
|
||||||
if received < page_size {
|
if page_received < page_size {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(DiscoveryResults {
|
Ok(DiscoveryResults {
|
||||||
|
received,
|
||||||
total_hits,
|
total_hits,
|
||||||
projects: projects
|
projects: projects.into_iter().filter_map(discovery_project).collect(),
|
||||||
.into_iter()
|
|
||||||
.map(|project| DiscoveryProject {
|
|
||||||
id: project.id.to_string(),
|
|
||||||
slug: project.slug,
|
|
||||||
title: project.name,
|
|
||||||
description: project.summary,
|
|
||||||
downloads: project.download_count,
|
|
||||||
icon_url: project
|
|
||||||
.logo
|
|
||||||
.map(|logo| logo.thumbnail_url)
|
|
||||||
.filter(|url| !url.trim().is_empty()),
|
|
||||||
icon_bytes: None,
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn discovery_project(project: Mod) -> Option<DiscoveryProject> {
|
||||||
|
project
|
||||||
|
.allow_mod_distribution
|
||||||
|
.unwrap_or(true)
|
||||||
|
.then(|| DiscoveryProject {
|
||||||
|
id: project.id.to_string(),
|
||||||
|
slug: project.slug,
|
||||||
|
title: project.name,
|
||||||
|
description: project.summary,
|
||||||
|
downloads: project.download_count,
|
||||||
|
icon_url: project
|
||||||
|
.logo
|
||||||
|
.map(|logo| logo.thumbnail_url)
|
||||||
|
.filter(|url| !url.trim().is_empty()),
|
||||||
|
icon_bytes: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn fetch_project(
|
pub async fn fetch_project(
|
||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
api_key: &str,
|
api_key: &str,
|
||||||
@@ -438,6 +446,15 @@ pub async fn ensure_download_url(
|
|||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
api_key: &str,
|
api_key: &str,
|
||||||
version: &mut VersionInfo,
|
version: &mut VersionInfo,
|
||||||
|
) -> Result<(), NetError> {
|
||||||
|
ensure_download_url_from(client, api_key, API_BASE, version).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ensure_download_url_from(
|
||||||
|
client: &HttpClient,
|
||||||
|
api_key: &str,
|
||||||
|
api_base: &str,
|
||||||
|
version: &mut VersionInfo,
|
||||||
) -> Result<(), NetError> {
|
) -> Result<(), NetError> {
|
||||||
let Some(file) = version.files.first_mut() else {
|
let Some(file) = version.files.first_mut() else {
|
||||||
return Err(NetError::Parse("No files in version".to_owned()));
|
return Err(NetError::Parse("No files in version".to_owned()));
|
||||||
@@ -449,12 +466,19 @@ pub async fn ensure_download_url(
|
|||||||
client,
|
client,
|
||||||
api_key,
|
api_key,
|
||||||
&format!(
|
&format!(
|
||||||
"{API_BASE}/mods/{}/files/{}/download-url",
|
"{api_base}/mods/{}/files/{}/download-url",
|
||||||
url_encode(&version.project_id),
|
url_encode(&version.project_id),
|
||||||
url_encode(&version.id)
|
url_encode(&version.id)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await
|
||||||
|
.map_err(|error| match error {
|
||||||
|
NetError::StatusError { status: 403, .. } => NetError::Parse(
|
||||||
|
"CurseForge does not permit this file to be downloaded by third-party launchers; use the CurseForge website or app"
|
||||||
|
.to_owned(),
|
||||||
|
),
|
||||||
|
error => error,
|
||||||
|
})?;
|
||||||
file.url = response.data;
|
file.url = response.data;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ pub struct DiscoveryProject {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct DiscoveryResults {
|
pub struct DiscoveryResults {
|
||||||
pub projects: Vec<DiscoveryProject>,
|
pub projects: Vec<DiscoveryProject>,
|
||||||
|
pub received: usize,
|
||||||
pub total_hits: usize,
|
pub total_hits: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,8 +188,10 @@ pub async fn search_discovery(
|
|||||||
url_encode(&facets),
|
url_encode(&facets),
|
||||||
);
|
);
|
||||||
let results: DiscoverySearchResponse = client.get_json(&url).await?;
|
let results: DiscoverySearchResponse = client.get_json(&url).await?;
|
||||||
|
let received = results.hits.len();
|
||||||
|
|
||||||
Ok(DiscoveryResults {
|
Ok(DiscoveryResults {
|
||||||
|
received,
|
||||||
total_hits: results.total_hits.max(0) as usize,
|
total_hits: results.total_hits.max(0) as usize,
|
||||||
projects: results
|
projects: results
|
||||||
.hits
|
.hits
|
||||||
@@ -217,7 +220,9 @@ pub async fn search_modpacks(
|
|||||||
url_encode(facets),
|
url_encode(facets),
|
||||||
);
|
);
|
||||||
let results: DiscoverySearchResponse = client.get_json(&url).await?;
|
let results: DiscoverySearchResponse = client.get_json(&url).await?;
|
||||||
|
let received = results.hits.len();
|
||||||
Ok(DiscoveryResults {
|
Ok(DiscoveryResults {
|
||||||
|
received,
|
||||||
total_hits: results.total_hits.max(0) as usize,
|
total_hits: results.total_hits.max(0) as usize,
|
||||||
projects: results
|
projects: results
|
||||||
.hits
|
.hits
|
||||||
|
|||||||
@@ -61,6 +61,21 @@ fn curseforge_library_category_maps_to_cleanup_metadata() {
|
|||||||
assert!(project_info(project, String::new()).is_library_only());
|
assert!(project_info(project, String::new()).is_library_only());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn discovery_hides_projects_that_block_third_party_downloads() {
|
||||||
|
let project: Mod = serde_json::from_str(
|
||||||
|
r#"{
|
||||||
|
"id": 7,
|
||||||
|
"name": "Restricted",
|
||||||
|
"slug": "restricted",
|
||||||
|
"allowModDistribution": false
|
||||||
|
}"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert!(discovery_project(project).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn datapack_discovery_uses_the_curseforge_data_packs_class() {
|
fn datapack_discovery_uses_the_curseforge_data_packs_class() {
|
||||||
assert_eq!(class_id(ContentKind::DataPack), 6945);
|
assert_eq!(class_id(ContentKind::DataPack), 6945);
|
||||||
@@ -109,3 +124,32 @@ async fn curseforge_versions_follow_pagination() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(versions.len(), 51);
|
assert_eq!(versions.len(), 51);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn restricted_curseforge_download_has_an_actionable_error() {
|
||||||
|
use wiremock::matchers::{method, path};
|
||||||
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||||
|
|
||||||
|
let server = MockServer::start().await;
|
||||||
|
Mock::given(method("GET"))
|
||||||
|
.and(path("/mods/7/files/9/download-url"))
|
||||||
|
.respond_with(ResponseTemplate::new(403))
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
let mut version = version_info(
|
||||||
|
serde_json::from_value(serde_json::json!({
|
||||||
|
"id": 9,
|
||||||
|
"modId": 7,
|
||||||
|
"displayName": "Restricted",
|
||||||
|
"fileName": "restricted.jar"
|
||||||
|
}))
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let error =
|
||||||
|
ensure_download_url_from(&HttpClient::new(), "test-key", &server.uri(), &mut version)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
|
||||||
|
assert!(matches!(error, NetError::Parse(message) if message.contains("third-party launchers")));
|
||||||
|
}
|
||||||
@@ -126,6 +126,7 @@ fn provider_merge_preserves_ranking_and_appends_fallbacks() {
|
|||||||
"modrinth",
|
"modrinth",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![modrinth_first, modrinth_second],
|
projects: vec![modrinth_first, modrinth_second],
|
||||||
|
received: 2,
|
||||||
total_hits: 2,
|
total_hits: 2,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -133,6 +134,7 @@ fn provider_merge_preserves_ranking_and_appends_fallbacks() {
|
|||||||
"curseforge",
|
"curseforge",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![curseforge_duplicate, curseforge_fallback],
|
projects: vec![curseforge_duplicate, curseforge_fallback],
|
||||||
|
received: 2,
|
||||||
total_hits: 2,
|
total_hits: 2,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -165,6 +167,7 @@ fn provider_merge_keeps_same_provider_title_collisions() {
|
|||||||
"modrinth",
|
"modrinth",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![first, second],
|
projects: vec![first, second],
|
||||||
|
received: 2,
|
||||||
total_hits: 2,
|
total_hits: 2,
|
||||||
},
|
},
|
||||||
)],
|
)],
|
||||||
@@ -188,6 +191,7 @@ fn provider_merge_keeps_the_preferred_project_across_pages() {
|
|||||||
"curseforge",
|
"curseforge",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![fallback],
|
projects: vec![fallback],
|
||||||
|
received: 1,
|
||||||
total_hits: 200,
|
total_hits: 200,
|
||||||
},
|
},
|
||||||
)],
|
)],
|
||||||
@@ -208,6 +212,7 @@ fn provider_merge_uses_the_longest_provider_result_range() {
|
|||||||
"modrinth",
|
"modrinth",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![],
|
projects: vec![],
|
||||||
|
received: 20,
|
||||||
total_hits: 20,
|
total_hits: 20,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -215,6 +220,7 @@ fn provider_merge_uses_the_longest_provider_result_range() {
|
|||||||
"curseforge",
|
"curseforge",
|
||||||
DiscoveryResults {
|
DiscoveryResults {
|
||||||
projects: vec![],
|
projects: vec![],
|
||||||
|
received: 50,
|
||||||
total_hits: 200,
|
total_hits: 200,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -224,6 +230,7 @@ fn provider_merge_uses_the_longest_provider_result_range() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(merged.total_hits, 200);
|
assert_eq!(merged.total_hits, 200);
|
||||||
|
assert_eq!(merged.received, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1736,7 +1736,7 @@ pub(crate) fn merge_provider_results(
|
|||||||
pages.sort_by_key(|(provider, _)| *provider != preferred);
|
pages.sort_by_key(|(provider, _)| *provider != preferred);
|
||||||
let received = pages
|
let received = pages
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, page)| page.projects.len())
|
.map(|(_, page)| page.received)
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
let total_hits = pages
|
let total_hits = pages
|
||||||
|
|||||||
Reference in new issue
Block a user