Skip to content

Commit d8e4a7c

Browse files
author
Marco Napetti
committed
Allow crate deletion in the first 24 hours from creation
1 parent 51a3536 commit d8e4a7c

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/controllers/krate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod delete;
12
pub mod downloads;
23
pub mod follow;
34
pub mod metadata;

src/controllers/krate/delete.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Functionality related to deleting a crate.
2+
3+
use chrono::{Duration, Utc};
4+
5+
use crate::controllers::cargo_prelude::*;
6+
use crate::models::Crate;
7+
8+
use crate::schema::*;
9+
use crate::util::errors::internal;
10+
use crate::views::{EncodableCrate, GoodCrate, PublishWarnings};
11+
12+
/// Handles the `DELETE /crates/:crate_id` route.
13+
///
14+
/// Actually deletion is allowed only in the first 24 hours from creation
15+
pub async fn delete(app: AppState, Path(crate_name): Path<String>) -> AppResult<Json<GoodCrate>> {
16+
let conn = app.primary_database.get()?;
17+
let krate: Crate = Crate::by_name(&crate_name).first(&*conn)?;
18+
19+
if Utc::now()
20+
.naive_utc()
21+
.signed_duration_since(krate.created_at)
22+
> Duration::hours(24)
23+
{
24+
return Err(cargo_err(
25+
"Crate deletion is allowed only in the first 24 hours from creation",
26+
));
27+
}
28+
29+
// Create a transaction on the database, if there are no errors,
30+
// commit the transactions to delete the crate.
31+
conn.transaction(|| {
32+
diesel::delete(crates::table.find(krate.id)).execute(&*conn)?;
33+
34+
let uploader = app.config.uploader();
35+
uploader
36+
.delete_index(app.http_client(), &krate.name)
37+
.map_err(|e| internal(format_args!("failed to delete crate: {e}")))?;
38+
39+
Ok(Json(GoodCrate {
40+
krate: EncodableCrate::from_minimal(krate, None, None, true, None),
41+
warnings: PublishWarnings {
42+
invalid_categories: vec![],
43+
invalid_badges: vec![],
44+
other: vec![],
45+
},
46+
}))
47+
})
48+
}

src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub fn build_axum_router(state: AppState) -> Router {
9393
"/api/v1/crates/:crate_id/reverse_dependencies",
9494
get(krate::metadata::reverse_dependencies),
9595
)
96+
.route("/api/v1/crates/:crate_id", delete(krate::delete::delete))
9697
.route("/api/v1/keywords", get(keyword::index))
9798
.route("/api/v1/keywords/:keyword_id", get(keyword::show))
9899
.route("/api/v1/categories", get(category::index))

0 commit comments

Comments
 (0)