From f097313e532ddeb117f0a21f18bfb3b5da4e425e Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Mon, 23 Oct 2017 16:10:19 +0200 Subject: [PATCH] Add links in footer to article and author history --- src/resources/article_resource.rs | 22 ++++++++++++++++++---- src/resources/changes_resource.rs | 12 ++++++------ src/resources/new_article_resource.rs | 1 + templates/article_revision.html | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index 43713f2..da3fb83 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -13,6 +13,8 @@ use site::Layout; use state::State; use web::{Resource, ResponseFuture}; +use super::changes_resource::QueryParameters; + pub struct ArticleResource { state: State, article_id: i32, @@ -26,17 +28,27 @@ impl ArticleResource { } } -pub fn last_updated(created: &DateTime, author: Option<&str>) -> String { +pub fn last_updated(article_id: i32, created: &DateTime, author: Option<&str>) -> String { + struct Author<'a> { + author: &'a str, + history: String, + } + #[derive(BartDisplay)] - #[template_string = "Last updated {{created}}{{#author}} by {{.}}{{/author}}"] + #[template_string = "Last updated {{created}}{{#author}} by {{.author}}{{/author}}"] struct Template<'a> { created: &'a str, - author: Option<&'a str>, + article_history: &'a str, + author: Option>, } Template { created: &created.to_rfc2822(), - author + article_history: &format!("_changes{}", QueryParameters::default().article_id(Some(article_id)).into_link()), + author: author.map(|author| Author { + author: &author, + history: format!("_changes{}", QueryParameters::default().author(Some(author.to_owned())).into_link()), + }), }.to_string() } @@ -82,6 +94,7 @@ impl Resource for ArticleResource { body: &Template { revision: data.revision, last_updated: Some(&last_updated( + data.article_id, &Local.from_utc_datetime(&data.created), data.author.as_ref().map(|x| &**x) )), @@ -148,6 +161,7 @@ impl Resource for ArticleResource { rendered: render_markdown(&updated.body), }.to_string(), last_updated: &last_updated( + updated.article_id, &Local.from_utc_datetime(&updated.created), updated.author.as_ref().map(|x| &**x) ), diff --git a/src/resources/changes_resource.rs b/src/resources/changes_resource.rs index 541ee28..b96da11 100644 --- a/src/resources/changes_resource.rs +++ b/src/resources/changes_resource.rs @@ -26,7 +26,7 @@ pub struct ChangesLookup { } #[derive(Serialize, Deserialize, Default)] -struct QueryParameters { +pub struct QueryParameters { after: Option, before: Option, @@ -37,7 +37,7 @@ struct QueryParameters { } impl QueryParameters { - fn pagination(self, pagination: Pagination) -> Self { + pub fn pagination(self, pagination: Pagination) -> Self { Self { after: if let Pagination::After(x) = pagination { Some(x) } else { None }, before: if let Pagination::Before(x) = pagination { Some(x) } else { None }, @@ -45,22 +45,22 @@ impl QueryParameters { } } - fn article_id(self, article_id: Option) -> Self { + pub fn article_id(self, article_id: Option) -> Self { Self { article_id, ..self } } - fn author(self, author: Option) -> Self { + pub fn author(self, author: Option) -> Self { Self { author, ..self } } - fn limit(self, limit: i32) -> Self { + pub fn limit(self, limit: i32) -> Self { Self { limit: if limit != DEFAULT_LIMIT { Some(limit) } else { None }, ..self } } - fn into_link(self) -> String { + pub fn into_link(self) -> String { let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail"); if args.len() > 0 { format!("?{}", args) diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index 25d831d..41a7935 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -150,6 +150,7 @@ impl Resource for NewArticleResource { rendered: render_markdown(&updated.body), }.to_string(), last_updated: &super::article_resource::last_updated( + updated.article_id, &Local.from_utc_datetime(&updated.created), updated.author.as_ref().map(|x| &**x) ), diff --git a/templates/article_revision.html b/templates/article_revision.html index 6a3626f..3c4d00b 100644 --- a/templates/article_revision.html +++ b/templates/article_revision.html @@ -33,7 +33,7 @@