Add the possibility to save an article as draft

This commit is contained in:
Bat 2018-09-10 19:38:19 +01:00
parent fcdd3d4c1a
commit 8879935925
7 changed files with 56 additions and 18 deletions

1
Cargo.lock generated
View File

@ -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)",

View File

@ -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 = "*"

View File

@ -3,6 +3,7 @@
extern crate activitypub;
extern crate atom_syndication;
extern crate chrono;
extern crate colored;
extern crate diesel;
extern crate dotenv;

View File

@ -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::<Vec<String>>()
.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
});
}
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<NewPostForm>, 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<NewPostForm>, 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<NewPostForm>, user: User, conn: D
});
}
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<NewPostForm>, user: User, conn: D
"instance": Instance::get_local(&*conn),
"editing": false,
"errors": errors.inner(),
"form": form
"form": form,
"is_draft": form.draft
})))
}
}

View File

@ -18,8 +18,9 @@
name=article.author.name,
link_4="</a>")
}}
⋅ {{ article.date | date(format="%B %e") }}
{% if article.post.published %}⋅ {{ article.date | date(format="%B %e") }}{% endif %}
⋅ <a href="/~/{{ article.blog.fqn }}/">{{ article.blog.title }}</a>
{% if not article.post.published %}⋅ {{ "Draft" | _ }}{% endif %}
</p>
</div>
{% endmacro post_card %}

View File

@ -29,6 +29,9 @@
&mdash;
<a href="{{ article.url}}delete" onclick="return confirm('Are you sure you?')">{{ "Delete this article" | _ }}</a>
{% endif %}
{% if not article.post.published %}
<span class="badge">{{ "Draft" }}</span>
{% endif %}
</p>
<article>
{{ article.post.content | safe }}

View File

@ -35,11 +35,22 @@
{% 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 %}
<label for="draft">
<input type="checkbox" name="draft" id="draft" checked>
{{ "This is a draft, don't publish it yet." | _ }}
</label>
{% endif %}
{% if editing %}
<input type="submit" value="{{ "Update" | _ }}" />
{% else %}
{% if is_draft %}
<input type="submit" value="{{ "Update or publish" | _ }}" />
{% else %}
<input type="submit" value="{{ "Publish" | _ }}" />
{% endif %}
{% endif %}
</form>
<script src="/static/js/autoExpand.js"></script>
{% endblock content %}