From b4e4b497eee2fe661295ff5b2000cae98dddd8e6 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Fri, 7 Dec 2018 12:05:01 +0100 Subject: [PATCH] Works on template Use uri! to generate links instead of hardcoded urls Fix #110 Fix invalid links needing to be POST forms Translate login message for boost and like directly from template Put js for search in its own file --- src/routes/posts.rs | 28 +++++++----- src/routes/session.rs | 2 +- static/js/search.js | 13 ++++++ templates/base.rs.html | 32 +++++++------- templates/blogs/details.rs.html | 11 ++--- templates/blogs/new.rs.html | 3 +- templates/instance/about.rs.html | 5 ++- templates/instance/admin.rs.html | 13 +++--- templates/instance/federated.rs.html | 17 ++++---- templates/instance/feed.rs.html | 11 ++--- templates/instance/index.rs.html | 25 +++++------ templates/instance/list.rs.html | 11 ++--- templates/instance/local.rs.html | 19 ++++---- templates/instance/users.rs.html | 13 +++--- templates/medias/details.rs.html | 9 ++-- templates/medias/index.rs.html | 11 +++-- templates/medias/new.rs.html | 3 +- templates/partials/comment.rs.html | 3 +- .../partials/instance_description.rs.html | 5 ++- templates/partials/post_card.rs.html | 11 ++--- templates/posts/details.rs.html | 43 ++++++++++--------- templates/posts/new.rs.html | 13 ++++-- templates/search/index.rs.html | 23 +++------- templates/session/login.rs.html | 3 +- templates/users/dashboard.rs.html | 7 +-- templates/users/details.rs.html | 7 +-- templates/users/edit.rs.html | 37 ++++++++-------- templates/users/followers.rs.html | 7 +-- templates/users/header.rs.html | 5 ++- templates/users/new.rs.html | 3 +- 30 files changed, 218 insertions(+), 175 deletions(-) create mode 100644 static/js/search.js diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 5ec78ca6..5a030bc8 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -121,12 +121,14 @@ pub fn new(blog: String, user: User, conn: DbConn, intl: I18n) -> Option let medias = Media::for_user(&*conn, user.id); Some(render!(posts::new( &(&*conn, &intl.catalog, Some(user)), + b, false, &NewPostForm::default(), + true, + None, ValidationErrors::default(), Instance::get_local(&*conn).expect("posts::new error: Local instance is null").default_license, - medias, - true + medias ))) } } @@ -143,7 +145,7 @@ pub fn edit(blog: String, slug: String, user: User, conn: DbConn, intl: I18n) -> ))) } else { let source = if !post.source.is_empty() { - post.source + post.source.clone() } else { post.content.get().clone() // fallback to HTML if the markdown was not stored }; @@ -151,6 +153,7 @@ pub fn edit(blog: String, slug: String, user: User, conn: DbConn, intl: I18n) -> let medias = Media::for_user(&*conn, user.id); Some(render!(posts::new( &(&*conn, &intl.catalog, Some(user)), + b, true, &NewPostForm { title: post.title.clone(), @@ -165,10 +168,11 @@ pub fn edit(blog: String, slug: String, user: User, conn: DbConn, intl: I18n) -> draft: true, cover: post.cover_id, }, + !post.published, + Some(post), ValidationErrors::default(), Instance::get_local(&*conn).expect("posts::new error: Local instance is null").default_license, - medias, - !post.published + medias ))) } } @@ -182,7 +186,7 @@ pub fn update(blog: String, slug: String, user: User, conn: DbConn, form: Lenien let new_slug = if !post.published { form.title.to_string().to_kebab_case() } else { - post.slug + post.slug.clone() }; let mut errors = match form.validate() { @@ -260,12 +264,14 @@ pub fn update(blog: String, slug: String, user: User, conn: DbConn, form: Lenien let medias = Media::for_user(&*conn, user.id); let temp = render!(posts::new( &(&*conn, &intl.catalog, Some(user)), + b, true, &*form, + form.draft.clone(), + Some(post), errors.clone(), Instance::get_local(&*conn).expect("posts::new error: Local instance is null").default_license, - medias.clone(), - form.draft.clone() + medias.clone() )); Err(Some(temp)) } @@ -378,12 +384,14 @@ pub fn create(blog_name: String, form: LenientForm, user: User, con let medias = Media::for_user(&*conn, user.id); Err(Some(render!(posts::new( &(&*conn, &intl.catalog, Some(user)), + blog, false, &*form, + form.draft, + None, errors.clone(), Instance::get_local(&*conn).expect("posts::new error: Local instance is null").default_license, - medias, - form.draft + medias )))) } } diff --git a/src/routes/session.rs b/src/routes/session.rs index bc058dcf..75533295 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -28,7 +28,7 @@ pub fn new(user: Option, conn: DbConn, intl: I18n) -> Ructe { pub fn new_message(user: Option, m: String, conn: DbConn, intl: I18n) -> Ructe { render!(session::login( &(&*conn, &intl.catalog, user), - Some(i18n!(intl.catalog, &m).to_string()), + Some(m), &LoginForm::default(), ValidationErrors::default() )) diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 00000000..3aae870e --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,13 @@ +window.onload = function(evt) { + var form = document.getElementById('form'); + form.addEventListener('submit', function () { + for (var input of form.getElementsByTagName('input')) { + if (input.name === '') { + input.name = input.id + } + if (input.name && !input.value) { + input.name = ''; + } + } + }); +} diff --git a/templates/base.rs.html b/templates/base.rs.html index 68281e81..43a4359b 100644 --- a/templates/base.rs.html +++ b/templates/base.rs.html @@ -1,5 +1,5 @@ @use template_utils::*; - +@use routes::*; @(ctx: BaseContext, title: &str, head: Content, header: Content, content: Content) @@ -8,10 +8,10 @@ @i18n!(ctx.1, title) ⋅ @i18n!(ctx.1, "Plume") - - - - + + + + @:head() @@ -21,8 +21,8 @@
diff --git a/templates/partials/post_card.rs.html b/templates/partials/post_card.rs.html index 9be8a571..db52a6e3 100644 --- a/templates/partials/post_card.rs.html +++ b/templates/partials/post_card.rs.html @@ -1,5 +1,6 @@ -@use template_utils::*; @use plume_models::posts::Post; +@use template_utils::*; +@use routes::*; @(ctx: BaseContext, article: Post) @@ -7,20 +8,20 @@ @if article.cover_id.is_some() {
} -

@article.title

+

@article.title

@article.subtitle

@Html(i18n!(ctx.1, "By {0}"; format!( - "{}", - escape(&article.get_authors(ctx.0)[0].get_fqn(ctx.0)), + "{}", + uri!(user::details: name = article.get_authors(ctx.0)[0].get_fqn(ctx.0)), escape(&article.get_authors(ctx.0)[0].name(ctx.0)) ))) @if article.published { ⋅ @article.creation_date.format("%B %e, %Y") } - ⋅ @article.get_blog(ctx.0).title + ⋅ @article.get_blog(ctx.0).title @if !article.published { ⋅ @i18n!(ctx.1, "Draft") } diff --git a/templates/posts/details.rs.html b/templates/posts/details.rs.html index 0f77b1bb..f5c3078b 100644 --- a/templates/posts/details.rs.html +++ b/templates/posts/details.rs.html @@ -7,33 +7,36 @@ @use plume_models::users::User; @use validator::ValidationErrors; @use routes::comments::NewCommentForm; +@use routes::*; @(ctx: BaseContext, article: Post, blog: Blog, comment_form: &NewCommentForm, comment_errors: ValidationErrors, tags: Vec, comments: Vec, previous_comment: Option, n_likes: usize, n_reshares: usize, has_liked: bool, has_reshared: bool, is_following: bool, author: User) @:base(ctx, &article.title.clone(), { - + @if article.cover_id.is_some() { } - + }, { - @blog.title + @blog.title }, {

@&article.title

@&article.subtitle

} @@ -120,7 +121,7 @@

@i18n!(ctx.1, "Comments")

@if ctx.2.is_some() { - + @input!(ctx.1, warning (optional text), "Content warning", comment_form, comment_errors, "") diff --git a/templates/posts/new.rs.html b/templates/posts/new.rs.html index 46438b03..a4fb7194 100644 --- a/templates/posts/new.rs.html +++ b/templates/posts/new.rs.html @@ -3,9 +3,12 @@ @use validator::{ValidationErrors, ValidationErrorsKind}; @use std::borrow::Cow; @use plume_models::medias::*; +@use plume_models::blogs::Blog; +@use plume_models::posts::Post; @use routes::posts::NewPostForm; +@use routes::*; -@(ctx: BaseContext, editing: bool, form: &NewPostForm, errors: ValidationErrors, default_license: String, medias: Vec, is_draft: bool) +@(ctx: BaseContext, blog: Blog, editing: bool, form: &NewPostForm, is_draft: bool, article: Option, errors: ValidationErrors, default_license: String, medias: Vec) @:base(ctx, &i18n!(ctx.1, if editing { "Edit {0}" } else { "New post" }; &form.title), {}, {}, {

@@ -15,7 +18,11 @@ @i18n!(ctx.1, "Create a new post") }

- + @if let Some(article) = article { + + } else { + + } @input!(ctx.1, title (text), "Title", form, errors.clone(), "required") @input!(ctx.1, subtitle (optional text), "Subtitle", form, errors.clone(), "") @@ -63,5 +70,5 @@ } } - + }) diff --git a/templates/search/index.rs.html b/templates/search/index.rs.html index aff6f964..cbe4a935 100644 --- a/templates/search/index.rs.html +++ b/templates/search/index.rs.html @@ -1,34 +1,20 @@ @use templates::base; @use template_utils::*; +@use routes::*; @(ctx: BaseContext, now: &str) -@:base(ctx, "Search", { - -}, {}, { +@:base(ctx, "Search", {}, {}, {

@i18n!(ctx.1, "Search")

+
Advanced search @input!(ctx.1, title (text), "Title matching these words", "placeholder=\"Title\"") @input!(ctx.1, subtitle (text), "Subtitle matching these words", "placeholder=\"Subtitle\"") @input!(ctx.1, content (text), "Content matching these words", "placeholder=\"Content\"") - @input!(ctx.1, after (date), "From this date", &format!("max={}", now))) + @input!(ctx.1, after (date), "From this date", &format!("max={}", now)) @input!(ctx.1, before (date), "To this date", &format!("max={}", now)) @input!(ctx.1, tag (text), "Containing these tags", "placeholder=\"Tags\"") @@ -40,4 +26,5 @@
+ }) diff --git a/templates/session/login.rs.html b/templates/session/login.rs.html index 0e4432c3..e58f8b8d 100644 --- a/templates/session/login.rs.html +++ b/templates/session/login.rs.html @@ -2,6 +2,7 @@ @use templates::base; @use validator::ValidationErrors; @use routes::session::LoginForm; +@use routes::*; @(ctx: BaseContext, message: Option, form: &LoginForm, errors: ValidationErrors) @@ -10,7 +11,7 @@ @if let Some(message) = message {

@message

} -
+ @input!(ctx.1, email_or_name (text), "Username or email", form, errors.clone(), "minlenght=\"1\"") @input!(ctx.1, password (password), "Password", form, errors, "minlenght=\"1\"") diff --git a/templates/users/dashboard.rs.html b/templates/users/dashboard.rs.html index 678fcd31..4883a479 100644 --- a/templates/users/dashboard.rs.html +++ b/templates/users/dashboard.rs.html @@ -2,6 +2,7 @@ @use template_utils::*; @use plume_models::blogs::Blog; @use plume_models::posts::Post; +@use routes::*; @(ctx: BaseContext, blogs: Vec, drafts: Vec) @@ -16,11 +17,11 @@
@for blog in blogs { }
- @i18n!(ctx.1, "Start a new blog") + @i18n!(ctx.1, "Start a new blog") @if !drafts.is_empty() { @@ -36,6 +37,6 @@

@i18n!(ctx.1, "Your media")

- @i18n!(ctx.1, "Go to your gallery") + @i18n!(ctx.1, "Go to your gallery")
}) diff --git a/templates/users/details.rs.html b/templates/users/details.rs.html index 90940a3f..4ad4a401 100644 --- a/templates/users/details.rs.html +++ b/templates/users/details.rs.html @@ -2,6 +2,7 @@ @use template_utils::*; @use plume_models::posts::Post; @use plume_models::users::User; +@use routes::*; @(ctx: BaseContext, user: User, follows: bool, is_remote: bool, remote_url: String, recents: Vec, reshares: Vec) @@ -9,14 +10,14 @@ @:header(ctx, &user, follows, is_remote, remote_url) @tabs(&[ - (&format!("/@/{}", user.get_fqn(ctx.0)), i18n!(ctx.1, "Articles"), true), - (&format!("/@/{}/followers", user.get_fqn(ctx.0)), i18n!(ctx.1, "Followers"), false) + (&uri!(user::details: name= user.get_fqn(ctx.0)).to_string(), i18n!(ctx.1, "Articles"), true), + (&uri!(user::followers: name = user.get_fqn(ctx.0)).to_string(), i18n!(ctx.1, "Followers"), false) ]) @if !recents.is_empty() {

@i18n!(ctx.1, "Latest articles") - @icon!("rss") + @icon!("rss")

@for article in recents { diff --git a/templates/users/edit.rs.html b/templates/users/edit.rs.html index 8d013602..35d8bab1 100644 --- a/templates/users/edit.rs.html +++ b/templates/users/edit.rs.html @@ -2,29 +2,32 @@ @use template_utils::*; @use routes::user::UpdateUserForm; @use validator::ValidationErrors; +@use routes::*; @(ctx: BaseContext, form: UpdateUserForm, errors: ValidationErrors) @:base(ctx, "Edit your account", {}, {}, { -

@i18n!(ctx.1, "Your Profile")

- - - + @if let Some(u) = ctx.2.clone() { +

@i18n!(ctx.1, "Your Profile")

+ + + - @input!(ctx.1, display_name (text), "Display name", form, errors.clone()) - @input!(ctx.1, email (text), "Email", form, errors.clone()) - @input!(ctx.1, summary (text), "Summary", form, errors) + @input!(ctx.1, display_name (text), "Display name", form, errors.clone()) + @input!(ctx.1, email (text), "Email", form, errors.clone()) + @input!(ctx.1, summary (text), "Summary", form, errors) - - - -

@i18n!(ctx.1, "Danger zone")

-

@i18n!(ctx.1, "Be very careful, any action taken here can't be cancelled.") - @if !ctx.2.clone().expect("Editing profile while not connected").is_admin { -

- +
- } else { -

@i18n!(ctx.1, "Sorry, but as an admin, you can't leave your instance.")

+ +

@i18n!(ctx.1, "Danger zone")

+

@i18n!(ctx.1, "Be very careful, any action taken here can't be cancelled.") + @if !u.is_admin { +

+ +
+ } else { +

@i18n!(ctx.1, "Sorry, but as an admin, you can't leave your instance.")

+ } } }) diff --git a/templates/users/followers.rs.html b/templates/users/followers.rs.html index 058022fd..31502af0 100644 --- a/templates/users/followers.rs.html +++ b/templates/users/followers.rs.html @@ -1,6 +1,7 @@ @use templates::{base, users::header}; @use template_utils::*; @use plume_models::users::User; +@use routes::*; @(ctx: BaseContext, user: User, follows: bool, is_remote: bool, remote_url: String, followers: Vec, page: i32, n_pages: i32) @@ -8,14 +9,14 @@ @:header(ctx, &user, follows, is_remote, remote_url) @tabs(&[ - (&format!("/@/{}", user.get_fqn(ctx.0)), i18n!(ctx.1, "Articles"), false), - (&format!("/@/{}/followers", user.get_fqn(ctx.0)), i18n!(ctx.1, "Followers"), true) + (&uri!(user::details: name= user.get_fqn(ctx.0)).to_string(), i18n!(ctx.1, "Articles"), false), + (&uri!(user::followers: name = user.get_fqn(ctx.0)).to_string(), i18n!(ctx.1, "Followers"), true) ])
@for follower in followers {
-

@follower.name(ctx.0) @format!("@{}", follower.get_fqn(ctx.0))

+

@follower.name(ctx.0) @format!("@{}", follower.get_fqn(ctx.0))

@Html(follower.summary)

} diff --git a/templates/users/header.rs.html b/templates/users/header.rs.html index ec1c3a7e..510af500 100644 --- a/templates/users/header.rs.html +++ b/templates/users/header.rs.html @@ -1,5 +1,6 @@ @use template_utils::*; @use plume_models::users::User; +@use routes::*; @(ctx: BaseContext, user: &User, follows: bool, is_remote: bool, instance_url: String) @@ -19,7 +20,7 @@ @if ctx.2.clone().map(|u| u.id == user.id).unwrap_or(false) { @i18n!(ctx.1, "It is you") - @i18n!(ctx.1, "Edit your profile") + @i18n!(ctx.1, "Edit your profile") }

@@ -29,7 +30,7 @@ } @if ctx.2.clone().map(|u| u.id != user.id).unwrap_or(false) { -
+ @if follows { } else { diff --git a/templates/users/new.rs.html b/templates/users/new.rs.html index b10fa8ee..5e4f18b2 100644 --- a/templates/users/new.rs.html +++ b/templates/users/new.rs.html @@ -2,13 +2,14 @@ @use template_utils::*; @use routes::user::NewUserForm; @use validator::ValidationErrors; +@use routes::*; @(ctx: BaseContext, enabled: bool, form: &NewUserForm, errors: ValidationErrors) @:base(ctx, "Edit your account", {}, {}, { @if enabled {

@i18n!(ctx.1, "Create an account")

- + @input!(ctx.1, username (text), "Username", form, errors.clone(), "minlenght=\"1\"") @input!(ctx.1, email (text), "Email", form, errors.clone()) @input!(ctx.1, password (password), "Password", form, errors.clone(), "minlenght=\"8\"")