add: stack clones

This commit is contained in:
trisua 2025-08-15 19:09:37 -04:00
parent 3a48ef2969
commit eec81f5718
5 changed files with 55 additions and 0 deletions

View file

@ -256,6 +256,7 @@ version = "1.0.0"
"stacks:label.remove" = "Remove" "stacks:label.remove" = "Remove"
"stacks:label.block_all" = "Block all" "stacks:label.block_all" = "Block all"
"stacks:label.unblock_all" = "Unblock all" "stacks:label.unblock_all" = "Unblock all"
"stacks:label.clone" = "Clone"
"forge:label.forges" = "Forges" "forge:label.forges" = "Forges"
"forge:label.my_forges" = "My forges" "forge:label.my_forges" = "My forges"

View file

@ -37,6 +37,14 @@
(text "{{ icon \"pencil\" }}") (text "{{ icon \"pencil\" }}")
(span (span
(text "{{ text \"general:action.manage\" }}"))) (text "{{ text \"general:action.manage\" }}")))
(text "{% elif user -%}")
; clone button for non-owner users
(button
("class" "lowered small")
("onclick" "clone_stack()")
(icon (text "book-copy"))
(span
(str (text "stacks:label.clone"))))
(text "{%- endif %}"))) (text "{%- endif %}")))
(div (div
("class" "card w_full flex flex_col gap_2") ("class" "card w_full flex flex_col gap_2")
@ -114,5 +122,22 @@
window.location.href = \"/settings#/account/blocks\"; window.location.href = \"/settings#/account/blocks\";
} }
}); });
}
async function clone_stack() {
fetch(\"/api/v1/stacks/{{ stack.id }}/clone\", {
method: \"POST\",
})
.then((res) => res.json())
.then((res) => {
trigger(\"atto::toast\", [
res.ok ? \"success\" : \"error\",
res.message,
]);
if (res.ok) {
window.location.href = `/stacks/${res.payload}`;
}
});
}")) }"))
(text "{% endblock %}") (text "{% endblock %}")

View file

@ -665,6 +665,7 @@ pub fn routes() -> Router {
.route("/stacks", get(stacks::list_request)) .route("/stacks", get(stacks::list_request))
.route("/stacks", post(stacks::create_request)) .route("/stacks", post(stacks::create_request))
.route("/stacks/{id}", get(stacks::get_request)) .route("/stacks/{id}", get(stacks::get_request))
.route("/stacks/{id}/clone", post(stacks::clone_request))
.route("/stacks/{id}/name", post(stacks::update_name_request)) .route("/stacks/{id}/name", post(stacks::update_name_request))
.route("/stacks/{id}/privacy", post(stacks::update_privacy_request)) .route("/stacks/{id}/privacy", post(stacks::update_privacy_request))
.route("/stacks/{id}/mode", post(stacks::update_mode_request)) .route("/stacks/{id}/mode", post(stacks::update_mode_request))

View file

@ -91,6 +91,27 @@ pub async fn create_request(
} }
} }
pub async fn clone_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserCreateStacks) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.clone_stack(user.id, id).await {
Ok(s) => Json(ApiReturn {
ok: true,
message: "Stack created".to_string(),
payload: s.id.to_string(),
}),
Err(e) => Json(e.into()),
}
}
pub async fn update_name_request( pub async fn update_name_request(
jar: CookieJar, jar: CookieJar,
Extension(data): Extension<State>, Extension(data): Extension<State>,

View file

@ -247,6 +247,13 @@ impl DataManager {
Ok(()) Ok(())
} }
/// Clone the given stack.
pub async fn clone_stack(&self, owner: usize, stack: usize) -> Result<UserStack> {
let stack = self.get_stack_by_id(stack).await?;
self.create_stack(UserStack::new(stack.name, owner, stack.users))
.await
}
auto_method!(update_stack_name(&str)@get_stack_by_id:FinePermission::MANAGE_STACKS; -> "UPDATE stacks SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.stack:{}"); auto_method!(update_stack_name(&str)@get_stack_by_id:FinePermission::MANAGE_STACKS; -> "UPDATE stacks SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.stack:{}");
auto_method!(update_stack_users(Vec<usize>)@get_stack_by_id:FinePermission::MANAGE_STACKS; -> "UPDATE stacks SET users = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}"); auto_method!(update_stack_users(Vec<usize>)@get_stack_by_id:FinePermission::MANAGE_STACKS; -> "UPDATE stacks SET users = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.stack:{}");