add: modify codes
This commit is contained in:
parent
6c9a4cbd77
commit
c7bc6bb475
5 changed files with 131 additions and 94 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -607,7 +607,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "fluffle"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"axum-extra",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "fluffle"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
authors = ["trisuaso"]
|
||||
repository = "https://trisua.com/t/fluffle"
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
("style" "margin-top: var(--pad-2)")
|
||||
("onsubmit" "edit_entry(event)")
|
||||
(div
|
||||
("class" "flex gap-2")
|
||||
("class" "w-full flex gap-2")
|
||||
(input
|
||||
("class" "w-full")
|
||||
("type" "text")
|
||||
|
@ -51,12 +51,22 @@
|
|||
("name" "edit_code")
|
||||
("required" "")
|
||||
("placeholder" "Enter edit code"))
|
||||
(input ("class" "w-full") ("style" "visibility: hidden") ("aria-hidden" "true") ("disabled" "true"))
|
||||
(input ("class" "w-full") ("style" "visibility: hidden") ("aria-hidden" "true") ("disabled" "true")))
|
||||
(div
|
||||
("class" "flex gap-2")
|
||||
(input
|
||||
("class" "w-full")
|
||||
("type" "text")
|
||||
("minlength" "2")
|
||||
("name" "new_edit_code")
|
||||
("placeholder" "New edit code"))
|
||||
(input
|
||||
("class" "w-full")
|
||||
("type" "text")
|
||||
("minlength" "2")
|
||||
("name" "new_modify_code")
|
||||
("placeholder" "New modify code"))
|
||||
(input
|
||||
("class" "w-full")
|
||||
("type" "text")
|
||||
|
@ -116,6 +126,7 @@
|
|||
edit_code: e.target.edit_code.value,
|
||||
new_slug: e.target.new_slug.value || undefined,
|
||||
new_edit_code: e.target.new_edit_code.value || undefined,
|
||||
new_modify_code: e.target.new_modify_code.value || undefined,
|
||||
metadata: globalThis.metadata_editor.getValue(),
|
||||
\"delete\": rm,
|
||||
}),
|
||||
|
|
|
@ -16,6 +16,9 @@ pub struct Entry {
|
|||
/// The IP address of the last editor of the entry.
|
||||
#[serde(default)]
|
||||
pub last_edit_from: String,
|
||||
/// An edit code that can only be used to change the entry's content.
|
||||
#[serde(default)]
|
||||
pub modify_code: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||
|
|
|
@ -492,6 +492,7 @@ async fn create_request(
|
|||
content: req.content,
|
||||
metadata: req.metadata,
|
||||
last_edit_from: real_ip,
|
||||
modify_code: String::new(),
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
|
@ -533,6 +534,8 @@ struct EditEntry {
|
|||
#[serde(default)]
|
||||
new_edit_code: Option<String>,
|
||||
#[serde(default)]
|
||||
new_modify_code: Option<String>,
|
||||
#[serde(default)]
|
||||
metadata: String,
|
||||
#[serde(default)]
|
||||
delete: bool,
|
||||
|
@ -601,11 +604,22 @@ async fn edit_request(
|
|||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
let edit_code = hash(req.edit_code.clone() + &entry.salt);
|
||||
let using_modify_code = edit_code == entry.modify_code;
|
||||
|
||||
// check edit code
|
||||
if hash(req.edit_code.clone() + &entry.salt) != entry.edit_code {
|
||||
if edit_code
|
||||
!= *if using_modify_code {
|
||||
&entry.modify_code
|
||||
} else {
|
||||
&entry.edit_code
|
||||
}
|
||||
{
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
// ...
|
||||
if !using_modify_code {
|
||||
// handle delete
|
||||
if req.delete {
|
||||
let views_id = match data
|
||||
|
@ -705,11 +719,20 @@ async fn edit_request(
|
|||
entry.edit_code = hash(new_edit_code + &entry.salt);
|
||||
}
|
||||
|
||||
// update modify code
|
||||
if let Some(new_modify_code) = req.new_modify_code {
|
||||
entry.modify_code = hash(new_modify_code + &entry.salt);
|
||||
}
|
||||
}
|
||||
|
||||
// update
|
||||
entry.content = req.content;
|
||||
entry.edited = unix_epoch_timestamp();
|
||||
|
||||
if !using_modify_code {
|
||||
entry.metadata = req.metadata;
|
||||
entry.last_edit_from = real_ip;
|
||||
entry.edited = unix_epoch_timestamp();
|
||||
}
|
||||
|
||||
if let Err(e) = data
|
||||
.update(id, serde_json::to_string(&entry).unwrap())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue