From 88799359258e833396cf2f4b102b926c12db9812 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 10 Sep 2018 19:38:19 +0100 Subject: [PATCH] Add the possibility to save an article as draft --- Cargo.lock | 1 + Cargo.toml | 4 +++ src/main.rs | 1 + src/routes/posts.rs | 49 +++++++++++++++++++++---------- templates/macros.html.tera | 3 +- templates/posts/details.html.tera | 3 ++ templates/posts/new.html.tera | 13 +++++++- 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f143340c..8398ce09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1451,6 +1451,7 @@ version = "0.1.0" dependencies = [ "activitypub 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "atom_syndication 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 58e54cc2..08d9608a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,10 @@ validator_derive = "0.7" webfinger = "0.3" workerpool = "1.1" +[dependencies.chrono] +features = ["serde"] +version = "0.4" + [dependencies.diesel] features = ["postgres", "r2d2", "chrono"] version = "*" diff --git a/src/main.rs b/src/main.rs index 932e6e5f..7878ff30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate activitypub; extern crate atom_syndication; +extern crate chrono; extern crate colored; extern crate diesel; extern crate dotenv; diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 7558af47..864680a3 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -1,4 +1,5 @@ use activitypub::object::Article; +use chrono::Utc; use heck::{CamelCase, KebabCase}; use rocket::{State, request::LenientForm}; use rocket::response::{Redirect, Flash}; @@ -95,7 +96,8 @@ fn new(blog: String, user: User, conn: DbConn) -> Template { "instance": Instance::get_local(&*conn), "editing": false, "errors": null, - "form": null + "form": null, + "is_draft": true, })) } } @@ -131,7 +133,9 @@ fn edit(blog: String, slug: String, user: User, conn: DbConn) -> Template { .collect::>() .join(", "), license: post.license.clone(), - } + draft: true, + }, + "is_draft": !post.published })) } } @@ -172,6 +176,12 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor Instance::get_local(&*conn).map(|i| i.default_license).unwrap_or(String::from("CC-0")) }; + // update publication date if when this article is no longer a draft + if !post.published && !form.draft { + post.published = true; + post.creation_date = Utc::now().naive_utc(); + } + post.slug = new_slug.clone(); post.title = form.title.clone(); post.subtitle = form.subtitle.clone(); @@ -195,9 +205,11 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor }); } - let act = post.update_activity(&*conn); - let dest = User::one_by_instance(&*conn); - worker.execute(Thunk::of(move || broadcast(&user, act, dest))); + if post.published { + let act = post.update_activity(&*conn); + let dest = User::one_by_instance(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, act, dest))); + } Ok(Redirect::to(uri!(details: blog = blog, slug = new_slug))) } @@ -207,7 +219,8 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor "instance": Instance::get_local(&*conn), "editing": true, "errors": errors.inner(), - "form": form + "form": form, + "is_draft": form.draft, }))) } } @@ -219,7 +232,8 @@ struct NewPostForm { pub subtitle: String, pub content: String, pub tags: String, - pub license: String + pub license: String, + pub draft: bool, } fn valid_slug(title: &str) -> Result<(), ValidationError> { @@ -263,7 +277,7 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D slug: slug.to_string(), title: form.title.to_string(), content: SafeString::new(&content), - published: true, + published: !form.draft, license: if form.license.len() > 0 { form.license.to_string() } else { @@ -280,10 +294,6 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D author_id: user.id }); - for m in mentions.into_iter() { - Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true, true); - } - let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0); for tag in tags { Tag::insert(&*conn, NewTag { @@ -293,9 +303,15 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D }); } - let act = post.create_activity(&*conn); - let dest = User::one_by_instance(&*conn); - worker.execute(Thunk::of(move || broadcast(&user, act, dest))); + if post.published { + for m in mentions.into_iter() { + Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true, true); + } + + let act = post.create_activity(&*conn); + let dest = User::one_by_instance(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, act, dest))); + } Ok(Redirect::to(uri!(details: blog = blog_name, slug = slug))) } @@ -305,7 +321,8 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D "instance": Instance::get_local(&*conn), "editing": false, "errors": errors.inner(), - "form": form + "form": form, + "is_draft": form.draft }))) } } diff --git a/templates/macros.html.tera b/templates/macros.html.tera index 6ac6a4f8..0dfc8648 100644 --- a/templates/macros.html.tera +++ b/templates/macros.html.tera @@ -18,8 +18,9 @@ name=article.author.name, link_4="") }} - ⋅ {{ article.date | date(format="%B %e") }} + {% if article.post.published %}⋅ {{ article.date | date(format="%B %e") }}{% endif %} ⋅ {{ article.blog.title }} + {% if not article.post.published %}⋅ {{ "Draft" | _ }}{% endif %}

{% endmacro post_card %} diff --git a/templates/posts/details.html.tera b/templates/posts/details.html.tera index b723063d..449e8827 100644 --- a/templates/posts/details.html.tera +++ b/templates/posts/details.html.tera @@ -29,6 +29,9 @@ — {{ "Delete this article" | _ }} {% endif %} + {% if not article.post.published %} + {{ "Draft" }} + {% endif %}

{{ article.post.content | safe }} diff --git a/templates/posts/new.html.tera b/templates/posts/new.html.tera index 0f77e4b1..e8b4d701 100644 --- a/templates/posts/new.html.tera +++ b/templates/posts/new.html.tera @@ -35,10 +35,21 @@ {% set license_infos = "Default license will be {{ instance.default_license }}" | _(instance=instance) %} {{ macros::input(name="license", label="License", errors=errors, form=form, optional=true, details=license_infos) }} + {% if is_draft %} + + {% endif %} + {% if editing %} {% else %} - + {% if is_draft %} + + {% else %} + + {% endif %} {% endif %}