Skip to content

Commit 3a18a82

Browse files
committed
Implement refresh_group endpoint to refresh group data and files
1 parent 1cb316a commit 3a18a82

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/groups.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn scope() -> actix_web::Scope {
1919
web::scope("/{group_id}")
2020
.service(delete_group)
2121
.service(get_group)
22+
.service(refresh_group)
2223
.service(repos::scope())
2324
)
2425
}
@@ -109,3 +110,57 @@ async fn join_group_from_url(request_url: web::Json<RequestUrl>) -> AppResult<im
109110

110111
Ok(HttpResponse::Ok().json(snowbird_group))
111112
}
113+
114+
#[post("/refresh")]
115+
async fn refresh_group(group_id: web::Path<String>) -> AppResult<impl Responder> {
116+
let backend = get_backend().await?;
117+
log_debug!(TAG, "Starting group refresh");
118+
119+
let group_id = group_id.into_inner();
120+
let key = create_veilid_cryptokey_from_base64(group_id.as_str())?;
121+
log_debug!(TAG, "Got key {}", key);
122+
123+
let group = backend.get_group(&key).await?;
124+
log_debug!(TAG, "Got group");
125+
126+
// Get all repos in the group
127+
let locked_repos = group.repos.lock().await;
128+
let repos: Vec<_> = locked_repos.values().cloned().collect();
129+
drop(locked_repos); // Release the lock before async operations
130+
131+
let mut refreshed_files = Vec::new();
132+
133+
// For each repo, refresh its collection and files
134+
for repo in repos {
135+
log_debug!(TAG, "Refreshing repo {}", repo.id());
136+
137+
// Refresh collection hash
138+
let collection_hash = repo.get_hash_from_dht().await?;
139+
if !group.has_hash(&collection_hash).await? {
140+
group.download_hash_from_peers(&collection_hash).await?;
141+
log_debug!(TAG, "Downloaded collection hash {}", collection_hash);
142+
}
143+
144+
// Get and refresh all files
145+
let files = repo.list_files().await?;
146+
for file_name in files {
147+
match repo.get_file_hash(&file_name).await {
148+
Ok(file_hash) => {
149+
if !group.has_hash(&file_hash).await? {
150+
group.download_hash_from_peers(&file_hash).await?;
151+
log_debug!(TAG, "Downloaded file hash {} for {}", file_hash, file_name);
152+
refreshed_files.push(file_name);
153+
}
154+
}
155+
Err(e) => {
156+
log_debug!(TAG, "Error getting hash for file {}: {}", file_name, e);
157+
}
158+
}
159+
}
160+
}
161+
162+
Ok(HttpResponse::Ok().json(json!({
163+
"status": "success",
164+
"refreshed_files": refreshed_files
165+
})))
166+
}

0 commit comments

Comments
 (0)