Edit posts

This commit is contained in:
Bat 2018-09-07 18:51:53 +01:00
parent 413e34ac0e
commit 3918bd4501
12 changed files with 67 additions and 21 deletions

View file

@ -281,29 +281,32 @@ impl Post {
act
}
pub fn handle_update(&mut self, conn: &PgConnection, updated: Article) {
pub fn handle_update(conn: &PgConnection, updated: Article) {
let id = updated.object_props.id_string().expect("Post::handle_update: id error");
let mut post = Post::find_by_ap_url(conn, id).unwrap();
if let Ok(title) = updated.object_props.name_string() {
self.slug = title.to_kebab_case();
self.title = title;
post.slug = title.to_kebab_case();
post.title = title;
}
if let Ok(content) = updated.object_props.content_string() {
self.content = SafeString::new(&content);
post.content = SafeString::new(&content);
}
if let Ok(subtitle) = updated.object_props.summary_string() {
self.subtitle = subtitle;
post.subtitle = subtitle;
}
if let Ok(ap_url) = updated.object_props.url_string() {
self.ap_url = ap_url;
post.ap_url = ap_url;
}
if let Ok(source) = updated.ap_object_props.source_object::<Source>() {
self.source = source.content;
post.source = source.content;
}
self.update(conn);
post.update(conn);
}
pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value {

View file

@ -549,5 +549,8 @@ msgstr ""
msgid "Articles tagged \"{{ tag }}\""
msgstr "Über {{ instance_name }}"
msgid "Edit"
msgstr ""
#~ msgid "Your password should be at least 8 characters long"
#~ msgstr "Das Passwort sollte mindestens 8 Zeichen lang sein"

View file

@ -535,3 +535,6 @@ msgstr ""
#, fuzzy
msgid "Articles tagged \"{{ tag }}\""
msgstr "Welcome on {{ instance_name }}"
msgid "Edit"
msgstr ""

View file

@ -543,3 +543,6 @@ msgstr "Tout afficher"
#, fuzzy
msgid "Articles tagged \"{{ tag }}\""
msgstr "Articles de {{ instance.name }}"
msgid "Edit"
msgstr ""

View file

@ -539,3 +539,6 @@ msgstr ""
#, fuzzy
msgid "Articles tagged \"{{ tag }}\""
msgstr "Acerca de {{ instance_name }}"
msgid "Edit"
msgstr ""

View file

@ -553,6 +553,9 @@ msgstr ""
msgid "Articles tagged \"{{ tag }}\""
msgstr "Om {{ instance_name }}"
msgid "Edit"
msgstr ""
#~ msgid "One reshare"
#~ msgid_plural "{{ count }} reshares"
#~ msgstr[0] "Én deling"

View file

@ -552,6 +552,9 @@ msgstr ""
msgid "Articles tagged \"{{ tag }}\""
msgstr "O {{ instance_name }}"
msgid "Edit"
msgstr ""
#~ msgid "One reshare"
#~ msgid_plural "{{ count }} reshares"
#~ msgstr[0] "Jedno udostępnienie"

View file

@ -522,3 +522,6 @@ msgstr ""
msgid "Articles tagged \"{{ tag }}\""
msgstr ""
msgid "Edit"
msgstr ""

View file

@ -86,6 +86,8 @@ fn main() {
routes::posts::details,
routes::posts::details_response,
routes::posts::activity_details,
routes::posts::edit,
routes::posts::update,
routes::posts::new,
routes::posts::new_auth,
routes::posts::create,

View file

@ -107,6 +107,7 @@ fn edit(blog: String, slug: String, user: User, conn: DbConn) -> Template {
"error_message": "You are not author in this blog."
}))
} else {
println!("Source: {}", post.source.clone());
Template::render("posts/new", json!({
"account": user.to_json(&*conn),
"instance": Instance::get_local(&*conn),
@ -130,7 +131,7 @@ fn edit(blog: String, slug: String, user: User, conn: DbConn) -> Template {
#[post("/~/<blog>/<slug>/edit", data = "<data>")]
fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientForm<NewPostForm>, worker: State<Pool<ThunkWorker<()>>>) -> Result<Redirect, Template> {
let b = Blog::find_by_fqn(&*conn, blog.to_string());
let mut post = b.clone().and_then(|blog| Post::find_by_slug(&*conn, slug, blog.id)).expect("Post to update not found");
let mut post = b.clone().and_then(|blog| Post::find_by_slug(&*conn, slug.clone(), blog.id)).expect("Post to update not found");
let form = data.get();
let new_slug = form.title.to_string().to_kebab_case();
@ -139,12 +140,15 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor
Ok(_) => ValidationErrors::new(),
Err(e) => e
};
if let Some(_) = Post::find_by_slug(&*conn, new_slug.clone(), b.unwrap().id) {
errors.add("title", ValidationError {
code: Cow::from("existing_slug"),
message: Some(Cow::from("A post with the same title already exists.")),
params: HashMap::new()
});
if new_slug != slug {
if let Some(_) = Post::find_by_slug(&*conn, new_slug.clone(), b.clone().unwrap().id) {
errors.add("title", ValidationError {
code: Cow::from("existing_slug"),
message: Some(Cow::from("A post with the same title already exists.")),
params: HashMap::new()
});
}
}
if errors.is_empty() {
@ -187,13 +191,13 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor
let followers = user.get_followers(&*conn);
worker.execute(Thunk::of(move || broadcast(&user, act, followers)));
Ok(Redirect::to(uri!(details: blog = blog, slug = slug)))
Ok(Redirect::to(uri!(details: blog = blog, slug = new_slug)))
}
} else {
Err(Template::render("posts/new", json!({
"account": user.to_json(&*conn),
"instance": Instance::get_local(&*conn),
"editing": false,
"editing": true,
"errors": errors.inner(),
"form": form
})))

View file

@ -24,6 +24,8 @@
&mdash;
<span class="date">{{ date | date(format="%B %e, %Y") }}</span>
{% if is_author %}
&mdash;
<a href="{{ article.url}}edit">{{ "Edit" | _ }}</a>
&mdash;
<a href="{{ article.url}}delete" onclick="return confirm('Are you sure you?')">{{ "Delete this article" | _ }}</a>
{% endif %}

View file

@ -2,11 +2,21 @@
{% import "macros" as macros %}
{% block title %}
{{ "New post" | _ }}
{% if editing %}
{{ "Edit {{ post }}" | _(post=form.title) }}
{% else %}
{{ "New post" | _ }}
{% endif %}
{% endblock title %}
{% block content %}
<h1>{{ "Create a post" | _ }}</h1>
<h1>
{% if editing %}
{{ "Edit {{ post }}" | _(post=form.title) }}
{% else %}
{{ "Create a post" | _ }}
{% endif %}
</h1>
<form class="new-post" method="post">
{{ macros::input(name="title", label="Title", errors=errors, form=form, props="required") }}
{{ macros::input(name="subtitle", label="Subtitle", errors=errors, form=form, optional=true) }}
@ -18,14 +28,18 @@
{% endif %}
<label for="content">{{ "Content" | _ }}<small>{{ "Markdown is supported" | _ }}</small></label>
<textarea id="content" name="content" value="{{ form.content | default(value="") }}" rows="20"></textarea>
<textarea id="content" name="content" rows="20">{{ form.content | default(value="") }}</textarea>
{{ macros::input(name="tags", label="Tags, separated by commas", errors=errors, form=form, optional=true) }}
{% 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) }}
<input type="submit" value="{{ "Publish" | _ }}" />
{% if editing %}
<input type="submit" value="{{ "Update" | _ }}" />
{% else %}
<input type="submit" value="{{ "Publish" | _ }}" />
{% endif %}
</form>
<script src="/static/js/autoExpand.js"></script>
{% endblock content %}