From 3918bd450161921be7a87dc2c7e6443928b167e9 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 7 Sep 2018 18:51:53 +0100 Subject: [PATCH] Edit posts --- plume-models/src/posts.rs | 19 +++++++++++-------- po/de.po | 3 +++ po/en.po | 3 +++ po/fr.po | 3 +++ po/gl.po | 3 +++ po/nb.po | 3 +++ po/pl.po | 3 +++ po/plume.pot | 3 +++ src/main.rs | 2 ++ src/routes/posts.rs | 22 +++++++++++++--------- templates/posts/details.html.tera | 2 ++ templates/posts/new.html.tera | 22 ++++++++++++++++++---- 12 files changed, 67 insertions(+), 21 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index a55f1135..4367fee7 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -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::() { - self.source = source.content; + post.source = source.content; } - self.update(conn); + post.update(conn); } pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { diff --git a/po/de.po b/po/de.po index dc916e9b..c4906639 100644 --- a/po/de.po +++ b/po/de.po @@ -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" diff --git a/po/en.po b/po/en.po index fb54ba7d..54e79172 100644 --- a/po/en.po +++ b/po/en.po @@ -535,3 +535,6 @@ msgstr "" #, fuzzy msgid "Articles tagged \"{{ tag }}\"" msgstr "Welcome on {{ instance_name }}" + +msgid "Edit" +msgstr "" diff --git a/po/fr.po b/po/fr.po index f6e33c27..abe8d34c 100644 --- a/po/fr.po +++ b/po/fr.po @@ -543,3 +543,6 @@ msgstr "Tout afficher" #, fuzzy msgid "Articles tagged \"{{ tag }}\"" msgstr "Articles de {{ instance.name }}" + +msgid "Edit" +msgstr "" diff --git a/po/gl.po b/po/gl.po index 1937b200..32f2d64b 100644 --- a/po/gl.po +++ b/po/gl.po @@ -539,3 +539,6 @@ msgstr "" #, fuzzy msgid "Articles tagged \"{{ tag }}\"" msgstr "Acerca de {{ instance_name }}" + +msgid "Edit" +msgstr "" diff --git a/po/nb.po b/po/nb.po index 046a17fd..981bedf9 100644 --- a/po/nb.po +++ b/po/nb.po @@ -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" diff --git a/po/pl.po b/po/pl.po index 8184bbfd..5b662c14 100644 --- a/po/pl.po +++ b/po/pl.po @@ -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" diff --git a/po/plume.pot b/po/plume.pot index 49228b02..5f35daca 100644 --- a/po/plume.pot +++ b/po/plume.pot @@ -522,3 +522,6 @@ msgstr "" msgid "Articles tagged \"{{ tag }}\"" msgstr "" + +msgid "Edit" +msgstr "" diff --git a/src/main.rs b/src/main.rs index 4c463b70..956146f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/routes/posts.rs b/src/routes/posts.rs index e7614c7c..1a40de43 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -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("/~///edit", data = "")] fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientForm, worker: State>>) -> Result { 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 }))) diff --git a/templates/posts/details.html.tera b/templates/posts/details.html.tera index 9667ba59..1a0bf59a 100644 --- a/templates/posts/details.html.tera +++ b/templates/posts/details.html.tera @@ -24,6 +24,8 @@ — {{ date | date(format="%B %e, %Y") }} {% if is_author %} + — + {{ "Edit" | _ }}{{ "Delete this article" | _ }} {% endif %} diff --git a/templates/posts/new.html.tera b/templates/posts/new.html.tera index fe3c2728..0f77e4b1 100644 --- a/templates/posts/new.html.tera +++ b/templates/posts/new.html.tera @@ -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 %} -

{{ "Create a post" | _ }}

+

+ {% if editing %} + {{ "Edit {{ post }}" | _(post=form.title) }} + {% else %} + {{ "Create a post" | _ }} + {% endif %} +

{{ 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 %} - + {{ 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) }} - + {% if editing %} + + {% else %} + + {% endif %}
{% endblock content %}