diff --git a/entry/src/github.rs b/entry/src/github.rs index 47fab5d..607d27b 100644 --- a/entry/src/github.rs +++ b/entry/src/github.rs @@ -1,8 +1,9 @@ -use crate::data::CurrentVersion; -use crate::{ending_point, OPT_CONFIG, STORE, UA}; +use crate::data::{CurrentVersion, EventCard}; +use crate::{OPT_CONFIG, STORE, UA, ending_point}; use anyhow::anyhow; use reqwest::Client; use serde::Deserialize; +use std::collections::HashMap; use std::sync::OnceLock; const COMMIT_URL: &str = @@ -59,6 +60,24 @@ pub fn raw_url(path: &str) -> String { + path } +pub fn deduplicate_event_cards(cards: Vec) -> Vec { + let mut map: HashMap = HashMap::new(); + + for card in cards { + map.entry(card.card_id) + .and_modify(|existing| { + if card.id < existing.id { + *existing = card.clone(); + } + }) + .or_insert(card); + } + + let mut res: Vec = map.into_values().collect(); + res.sort_by_key(|c| c.id); + res +} + pub async fn sync_data() -> anyhow::Result<()> { let client = http_client()?; let (events_cards, event_stories, cards) = tokio::try_join!( @@ -68,15 +87,13 @@ pub async fn sync_data() -> anyhow::Result<()> { .send() .await? .text(), - client - .get(raw_url("cards.json")) - .send() - .await? - .text() + client.get(raw_url("cards.json")).send().await?.text() )?; ending_point!(ok); let mut store = STORE.write().map_err(|e| anyhow!("{}", e))?; - store.set_event_cards(serde_json::from_str(&events_cards)?); + store.set_event_cards(deduplicate_event_cards(serde_json::from_str( + &events_cards, + )?)); store.set_event_stories(serde_json::from_str(&event_stories)?); store.set_cards(serde_json::from_str(&cards)?);