sn: implement the Delete action
This commit is contained in:
parent
bcd52491c9
commit
a9e3490478
|
@ -118,6 +118,10 @@ impl Post {
|
||||||
Self::create_url_mapping(&self.url, &self.uuid).await?;
|
Self::create_url_mapping(&self.url, &self.uuid).await?;
|
||||||
store::put_obj(&Self::uuid_to_post_key(&self.uuid), self).await
|
store::put_obj(&Self::uuid_to_post_key(&self.uuid), self).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_uuid(uuid: &str) -> MyResult<()> {
|
||||||
|
store::delete(&Self::uuid_to_post_key(uuid)).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -342,4 +346,8 @@ impl PostContentCache {
|
||||||
pub async fn save(&self) -> MyResult<()> {
|
pub async fn save(&self) -> MyResult<()> {
|
||||||
store::put_obj(&Self::uuid_to_cache_key(&self.uuid), self).await
|
store::put_obj(&Self::uuid_to_cache_key(&self.uuid), self).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_uuid(uuid: &str) -> MyResult<()> {
|
||||||
|
store::delete(&Self::uuid_to_cache_key(uuid)).await
|
||||||
|
}
|
||||||
}
|
}
|
56
src/sn.rs
56
src/sn.rs
|
@ -11,6 +11,7 @@ use web_sys::*;
|
||||||
pub fn build_routes(router: &mut Router) {
|
pub fn build_routes(router: &mut Router) {
|
||||||
router.add_route("/actions", &get_actions);
|
router.add_route("/actions", &get_actions);
|
||||||
router.add_route("/post", &create_or_update_post);
|
router.add_route("/post", &create_or_update_post);
|
||||||
|
router.add_route("/delete", &delete_post);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! verify_secret {
|
macro_rules! verify_secret {
|
||||||
|
@ -54,6 +55,15 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
||||||
});
|
});
|
||||||
|
|
||||||
if post_exists {
|
if post_exists {
|
||||||
|
actions.push(Action {
|
||||||
|
label: "Delete".into(),
|
||||||
|
url: format!("{}/delete?secret={}", origin, CONFIG.secret.clone()),
|
||||||
|
verb: Verb::Post,
|
||||||
|
context: Context::Item,
|
||||||
|
content_types: vec![ContentType::Note],
|
||||||
|
access_type: Some(AccessType::Decrypted)
|
||||||
|
});
|
||||||
|
|
||||||
actions.push(Action {
|
actions.push(Action {
|
||||||
label: "Open Post".into(),
|
label: "Open Post".into(),
|
||||||
url: format!("{}/{}/", origin, post.unwrap().url),
|
url: format!("{}/{}/", origin, post.unwrap().url),
|
||||||
|
@ -61,7 +71,7 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
||||||
context: Context::Item,
|
context: Context::Item,
|
||||||
content_types: vec![ContentType::Note],
|
content_types: vec![ContentType::Note],
|
||||||
access_type: None
|
access_type: None
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let info = ActionsExtension {
|
let info = ActionsExtension {
|
||||||
|
@ -173,6 +183,19 @@ fn build_metadata(custom: Option<CustomMetadata>, uuid: &str, title: &str) -> Me
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! load_post_body {
|
||||||
|
($data:ident, $req:expr) => {
|
||||||
|
let $data: ActionsPostData = serde_json::from_str(
|
||||||
|
&JsFuture::from($req.text().internal_err()?)
|
||||||
|
.await.internal_err()?
|
||||||
|
.as_string().ok_or(Error::BadRequest("Unable to parse POST body".into()))?
|
||||||
|
).internal_err()?;
|
||||||
|
if $data.items.len() == 0 {
|
||||||
|
return Err(Error::BadRequest("At least one item must be supplied".into()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async fn create_or_update_post(req: Request, url: Url) -> MyResult<Response> {
|
async fn create_or_update_post(req: Request, url: Url) -> MyResult<Response> {
|
||||||
verify_secret!(url, params);
|
verify_secret!(url, params);
|
||||||
if req.method() != "POST" {
|
if req.method() != "POST" {
|
||||||
|
@ -180,14 +203,7 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult<Response> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the information sent as POST body
|
// Load the information sent as POST body
|
||||||
let data: ActionsPostData = serde_json::from_str(
|
load_post_body!(data, req);
|
||||||
&JsFuture::from(req.text().internal_err()?)
|
|
||||||
.await.internal_err()?
|
|
||||||
.as_string().ok_or(Error::BadRequest("Unable to parse POST body".into()))?
|
|
||||||
).internal_err()?;
|
|
||||||
if data.items.len() == 0 {
|
|
||||||
return Err(Error::BadRequest("At least one item must be supplied".into()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let uuid = data.items[0].uuid.clone();
|
let uuid = data.items[0].uuid.clone();
|
||||||
let text = data.items[0].content.text.clone();
|
let text = data.items[0].content.text.clone();
|
||||||
|
@ -247,6 +263,28 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult<Response> {
|
||||||
).internal_err()
|
).internal_err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn delete_post(req: Request, url: Url) -> MyResult<Response> {
|
||||||
|
verify_secret!(url, params);
|
||||||
|
if req.method() != "POST" {
|
||||||
|
return Err(Error::BadRequest("Unsupported method".into()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the information sent as POST body
|
||||||
|
load_post_body!(data, req);
|
||||||
|
|
||||||
|
let uuid = &data.items[0].uuid;
|
||||||
|
blog::PostsList::load().await.remove_post(uuid).await?;
|
||||||
|
blog::Post::delete_by_uuid(uuid).await?;
|
||||||
|
blog::PostContentCache::delete_by_uuid(uuid).await?;
|
||||||
|
|
||||||
|
Response::new_with_opt_str_and_init(
|
||||||
|
None,
|
||||||
|
ResponseInit::new()
|
||||||
|
.status(200)
|
||||||
|
.headers(headers!().add_cors().as_ref())
|
||||||
|
).internal_err()
|
||||||
|
}
|
||||||
|
|
||||||
pub enum Verb {
|
pub enum Verb {
|
||||||
Show,
|
Show,
|
||||||
Post,
|
Post,
|
||||||
|
|
|
@ -12,6 +12,8 @@ extern "C" {
|
||||||
fn kv_get(key: &str) -> Promise;
|
fn kv_get(key: &str) -> Promise;
|
||||||
#[wasm_bindgen(js_namespace = PAPRIKA, js_name = "put")]
|
#[wasm_bindgen(js_namespace = PAPRIKA, js_name = "put")]
|
||||||
fn kv_put_str(key: &str, value: &str) -> Promise;
|
fn kv_put_str(key: &str, value: &str) -> Promise;
|
||||||
|
#[wasm_bindgen(js_namespace = PAPRIKA, js_name = "delete")]
|
||||||
|
fn kv_delete(key: &str) -> Promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns empty string ("") if the key is not found
|
// Returns empty string ("") if the key is not found
|
||||||
|
@ -37,4 +39,9 @@ pub async fn put_obj<T: Serialize>(key: &str, value: T) -> MyResult<()> {
|
||||||
// For example, the user may want to manually edit the order in which posts appear
|
// For example, the user may want to manually edit the order in which posts appear
|
||||||
pub async fn put_obj_pretty<T: Serialize>(key: &str, value: T) -> MyResult<()> {
|
pub async fn put_obj_pretty<T: Serialize>(key: &str, value: T) -> MyResult<()> {
|
||||||
put_str(key, &serde_json::to_string_pretty(&value).internal_err()?).await
|
put_str(key, &serde_json::to_string_pretty(&value).internal_err()?).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(key: &str) -> MyResult<()> {
|
||||||
|
JsFuture::from(kv_delete(key)).await.internal_err()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
Loading…
Reference in New Issue