fix limit

This commit is contained in:
2026-02-27 18:44:59 +09:00
parent 8cba6b1dea
commit f7cce79f70
3 changed files with 78 additions and 21 deletions

View File

@@ -98,5 +98,27 @@
"3mfsyvyfq3x2d", "3mfsyvyfq3x2d",
"3mfsyvpe72g2b", "3mfsyvpe72g2b",
"3mfsycszf2y27", "3mfsycszf2y27",
"3mfsxkrpkcn25" "3mfsxkrpkcn25",
"3mfswma2hvj23",
"3mf6ej4dnle2h",
"3mf6ed2zvxb2f",
"3mf6duofofy2d",
"3mf6dmhxvbv2b",
"3mf6dg2mfm627",
"3mf6czldksn25",
"3mf6cjbkuq323",
"3mebae7z77x2b",
"3meb7ykzumg27",
"3meb7w4vuas25",
"3meb7bdy3ge23",
"3me6uf4masj2f",
"3me6uc233ib2d",
"3me6u76knjf2b",
"3me6u2g7izx27",
"3me6ttgsk2g25",
"3me6tlrez7l23",
"325sghcecgzco",
"2ks46vomw4s2i",
"2ivbc5b4um5bu",
"255k3bskheo6j"
] ]

View File

@@ -145,23 +145,37 @@ pub async fn sync_to_local(
} }
} }
// 3. Sync collection records // 3. Sync collection records (with pagination)
let records_url = format!( let collection_dir = format!("{}/{}", did_dir, collection);
fs::create_dir_all(&collection_dir)?;
let mut fetched_rkeys: Vec<String> = Vec::new();
let mut cursor: Option<String> = None;
let mut total_fetched = 0;
loop {
let mut records_url = format!(
"{}?repo={}&collection={}&limit=100", "{}?repo={}&collection={}&limit=100",
lexicons::url(pds_host, &com_atproto_repo::LIST_RECORDS), lexicons::url(pds_host, &com_atproto_repo::LIST_RECORDS),
did, did,
collection collection
); );
let res = client.get(&records_url).send().await?; if let Some(ref c) = cursor {
if res.status().is_success() { records_url.push_str(&format!("&cursor={}", c));
let list: ListRecordsResponse = res.json().await?; }
let collection_dir = format!("{}/{}", did_dir, collection);
fs::create_dir_all(&collection_dir)?; let res = client.get(&records_url).send().await?;
if !res.status().is_success() {
println!("Failed to fetch records: {}", res.status());
break;
}
let list: ListRecordsResponse = res.json().await?;
let count = list.records.len();
let mut rkeys: Vec<String> = Vec::new();
for record in &list.records { for record in &list.records {
let rkey = record.uri.split('/').next_back().unwrap_or("unknown"); let rkey = record.uri.split('/').next_back().unwrap_or("unknown");
rkeys.push(rkey.to_string()); fetched_rkeys.push(rkey.to_string());
let record_path = format!("{}/{}.json", collection_dir, rkey); let record_path = format!("{}/{}.json", collection_dir, rkey);
let record_json = serde_json::json!({ let record_json = serde_json::json!({
"uri": record.uri, "uri": record.uri,
@@ -172,15 +186,37 @@ pub async fn sync_to_local(
println!("Saved: {}", record_path); println!("Saved: {}", record_path);
} }
// Create index.json with list of rkeys total_fetched += count;
match list.cursor {
Some(c) if count > 0 => cursor = Some(c),
_ => break,
}
}
if total_fetched > 0 {
// Merge with existing index.json to preserve local-only entries
let index_path = format!("{}/index.json", collection_dir); let index_path = format!("{}/index.json", collection_dir);
fs::write(&index_path, serde_json::to_string_pretty(&rkeys)?)?; let mut merged_rkeys = fetched_rkeys.clone();
if let Ok(existing) = fs::read_to_string(&index_path) {
if let Ok(existing_rkeys) = serde_json::from_str::<Vec<String>>(&existing) {
let fetched_set: std::collections::HashSet<String> =
merged_rkeys.iter().cloned().collect();
for rkey in existing_rkeys {
if !fetched_set.contains(&rkey) {
merged_rkeys.push(rkey);
}
}
}
}
fs::write(&index_path, serde_json::to_string_pretty(&merged_rkeys)?)?;
println!("Saved: {}", index_path); println!("Saved: {}", index_path);
println!( println!(
"Synced {} records from {}", "Synced {} records from {}",
list.records.len(), total_fetched, collection
collection
); );
} }

View File

@@ -30,7 +30,6 @@ pub struct PutRecordResponse {
pub struct ListRecordsResponse { pub struct ListRecordsResponse {
pub records: Vec<Record>, pub records: Vec<Record>,
#[serde(default)] #[serde(default)]
#[allow(dead_code)]
pub cursor: Option<String>, pub cursor: Option<String>,
} }