diff --git a/crates/coa-core/src/catalogue.rs b/crates/coa-core/src/catalogue.rs index 5022932..d9bcfaa 100644 --- a/crates/coa-core/src/catalogue.rs +++ b/crates/coa-core/src/catalogue.rs @@ -290,6 +290,60 @@ fn respond(items: Vec, overlay: &CoaOverlayDocument, cached } } +/// Last push (`pushed_at`) of GitHub repositories given as `owner/name`, for modules the public catalogue does +/// not list. Kept in `cache_file` for 12 hours; a repository GitHub does not answer for is `None` and asked +/// again next time. +pub fn repo_last_push(cache_file: &Path, repos: &[String]) -> Result>> { + #[derive(Serialize, Deserialize, Default)] + struct Cache { + at: i64, + pushed: BTreeMap, + } + let now = Utc::now().timestamp(); + let mut cache: Cache = fs::read(cache_file).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default(); + if now - cache.at > 12 * 3600 { + cache = Cache { at: now, pushed: BTreeMap::new() }; + } + let name_ok = |s: Option<&str>| { + s.is_some_and(|s| !s.is_empty() && s.len() <= 100 && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))) + }; + let valid = |r: &str| { + let mut parts = r.split('/'); + name_ok(parts.next()) && name_ok(parts.next()) && parts.next().is_none() + }; + let client = reqwest::blocking::Client::builder() + .connect_timeout(Duration::from_secs(CONNECT_TIMEOUT_SECS)) + .timeout(Duration::from_secs(10)) + .build() + .map_err(|e| Error::Invalid(e.to_string()))?; + let mut out = BTreeMap::new(); + for repo in repos.iter().map(|r| r.trim().to_ascii_lowercase()).filter(|r| valid(r)) { + if let Some(v) = cache.pushed.get(&repo) { + out.insert(repo, Some(v.clone())); + continue; + } + let pushed = client + .get(format!("https://api.github.com/repos/{repo}")) + .header(reqwest::header::USER_AGENT, "CSM-Modules-Preview") + .header(reqwest::header::ACCEPT, "application/vnd.github+json") + .send() + .ok() + .filter(|r| r.status().is_success()) + .and_then(|r| r.text().ok()) + .and_then(|t| serde_json::from_str::(&t).ok()) + .and_then(|v| v.get("pushed_at")?.as_str().map(str::to_string)); + if let Some(p) = &pushed { + cache.pushed.insert(repo.clone(), p.clone()); + } + out.insert(repo, pushed); + } + if let Some(dir) = cache_file.parent() { + let _ = fs::create_dir_all(dir); + } + let _ = fsx::atomic_write_json(cache_file, &cache); + Ok(out) +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CachedDocument { pub schema_version: u32, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4929f0c..70b5eb0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -3083,6 +3083,14 @@ async fn analyze_github_module( .await } +/// Last push of GitHub repositories (`owner/name`), for modules that are not in the public catalogue (CoA's +/// own). Answers are kept for 12 hours in the source cache; a repository GitHub does not answer for gets none. +#[tauri::command] +async fn repo_last_push(repos: Vec) -> std::result::Result>, UiError> { + let cache_file = data_dir().join("github_cache").join("repo-last-push.json"); + blocking(move || coa_core::catalogue::repo_last_push(&cache_file, &repos)).await +} + /// What the module build cache of a server (and the shared source downloads) take on disk. #[tauri::command] async fn build_cache_summary( @@ -3634,6 +3642,7 @@ pub fn run() { is_classic_server, build_cache_summary, build_cache_clear, + repo_last_push, cancel_install, create_account, check_update, diff --git a/src/lib/api.ts b/src/lib/api.ts index b0a922b..736cc3d 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -843,6 +843,7 @@ export const api = { installNew: (dest: string, pkg?: string, gameData?: string, copyFrom?: string) => invoke("install_new", { dest, package: pkg ?? null, gameData: gameData ?? null, copyFrom: copyFrom ?? null }), isClassicServer: (path: string) => invoke("is_classic_server", { path }), + repoLastPush: (repos: string[]) => invoke>("repo_last_push", { repos }), buildCacheSummary: (id: string) => invoke("build_cache_summary", { id }), buildCacheClear: (id: string, module: string | null, sources: boolean) => invoke("build_cache_clear", { id, module, sources }), diff --git a/src/screens/ModulesPage.tsx b/src/screens/ModulesPage.tsx index bfa6972..34b6eeb 100644 --- a/src/screens/ModulesPage.tsx +++ b/src/screens/ModulesPage.tsx @@ -664,6 +664,13 @@ export function ModulesPage({ const [showUrlBar, setShowUrlBar] = useState(false); const [menuOpen, setMenuOpen] = useState(false); const [cacheOpen, setCacheOpen] = useState(false); + // Last push of CoA's own module repositories (the public catalogue does not list them). + const [curatedPushed, setCuratedPushed] = useState | null>(null); + useEffect(() => { + if (activeTab !== "catalog" || curatedPushed !== null) return; + const repos = [...new Set(VERIFIED_CATALOG.map((m) => normalizeUrlSlug(m.repo)))]; + void api.repoLastPush(repos).then(setCuratedPushed).catch(() => setCuratedPushed({})); + }, [activeTab, curatedPushed]); const [cacheBytes, setCacheBytes] = useState(null); useEffect(() => { if (menuOpen) { @@ -2033,7 +2040,7 @@ export function ModulesPage({ }; const cat = (publicCatalog?.categories ?? []).find((c) => c.code === curatedCategory(item.id)); return ( -
+
{cat ? (locale === "ru" ? cat.ru : cat.en) : ""} - + {(() => { + const pushed = curatedPushed?.[normalizeUrlSlug(item.repo)] ?? null; + return ( + + {curatedPushed === null ? "…" : timeAgo(pushed, locale)} + + ); + })()}
{isInstalled ? ( @@ -2092,7 +2109,7 @@ export function ModulesPage({ const isIncompatible = item.coa_status === "Incompatible"; const cat = (publicCatalog?.categories ?? []).find((c) => c.code === (item.category ?? "other")); return ( -
+