From 7de121299315533e3b957a14ee3e76770e066a62 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Wed, 8 Apr 2020 18:10:13 +0800 Subject: [PATCH] also remove post from list if unlist is set --- src/blog.rs | 10 +++++++++- src/lib.rs | 2 ++ src/sn.rs | 14 ++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/blog.rs b/src/blog.rs index b94466e..589aa4c 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -42,10 +42,18 @@ impl PostsList { if self.has_post(uuid) { return Ok(()); } - + self.0.insert(0, uuid.into()); store::put_obj_pretty("posts_list", self.0).await } + + // Remove a post from published list + // may be used when deleting / unpublishing a post + // Does nothing if uuid not found in list + pub async fn remove_post(mut self, uuid: &str) -> MyResult<()> { + self.0.remove_item(&uuid); + store::put_obj_pretty("posts_list", self.0).await + } } #[derive(Serialize, Deserialize)] diff --git a/src/lib.rs b/src/lib.rs index 79d91ea..144a698 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(vec_remove_item)] + #[macro_use] extern crate lazy_static; diff --git a/src/sn.rs b/src/sn.rs index 4423637..80ff5a2 100644 --- a/src/sn.rs +++ b/src/sn.rs @@ -75,11 +75,10 @@ async fn get_actions(_req: Request, url: Url) -> MyResult { #[derive(Deserialize)] struct CustomMetadata { - // If unlist is set to TRUE upon first publication, - // the post won't be listed publicly on the blog. - // However, this has no effect if set to TRUE after first publication. - // (setting to FALSE after first publication MAY work) - // You will have to first delete the blog before changing this value. + // If unlist is set to TRUE, + // the post won't be listed publicly on the blog, + // and if the post were published, it will + // be removed from the public list unlist: Option, url: Option, timestamp: Option // Should be something `js_sys::Date::parse` could handle @@ -203,8 +202,11 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult { // As you may have seen by now, the process is far from atomic // This is fine because we don't expect users to update posts from // multiple endpoints simultaneously all the time + let list = blog::PostsList::load().await; if !metadata.unlist { - blog::PostsList::load().await.add_post(&post.uuid).await?; + list.add_post(&post.uuid).await?; + } else { + list.remove_post(&post.uuid).await?; } post.write_to_kv().await?;