From 9cee48f2dac7400fc0df78b8790ff1e1600a632e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 Apr 2018 12:38:12 +0200 Subject: feat(handlers): Determine whether current user can edit a post --- src/handlers.rs | 9 ++++++++- src/main.rs | 2 +- src/render.rs | 11 ++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/handlers.rs b/src/handlers.rs index ccb01eb3c803..f4346f26c6c3 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -60,11 +60,18 @@ pub fn forum_index(state: State) -> ConverseResponse { } /// This handler retrieves and displays a single forum thread. -pub fn forum_thread(state: State, thread_id: Path) -> ConverseResponse { +pub fn forum_thread(state: State, + mut req: HttpRequest, + thread_id: Path) -> ConverseResponse { let id = thread_id.into_inner(); + let user = req.session().get(AUTHOR) + .unwrap_or_else(|_| None) + .map(|a: Author| a.email); + state.db.send(GetThread(id)) .flatten() .and_then(move |res| state.renderer.send(ThreadPage { + current_user: user, thread: res.0, posts: res.1, }).from_err()) diff --git a/src/main.rs b/src/main.rs index 851e810412ee..f748be1a7bc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,7 +152,7 @@ fn main() { .resource("/thread/new", |r| r.method(Method::GET).with(new_thread)) .resource("/thread/submit", |r| r.method(Method::POST).with3(submit_thread)) .resource("/thread/reply", |r| r.method(Method::POST).with3(reply_thread)) - .resource("/thread/{id}", |r| r.method(Method::GET).with2(forum_thread)) + .resource("/thread/{id}", |r| r.method(Method::GET).with3(forum_thread)) .resource("/search", |r| r.method(Method::GET).with2(search_forum)) .resource("/oidc/login", |r| r.method(Method::GET).with(login)) .resource("/oidc/callback", |r| r.method(Method::POST).with3(callback)); diff --git a/src/render.rs b/src/render.rs index 3254d480b5d2..9cc9efd4d10e 100644 --- a/src/render.rs +++ b/src/render.rs @@ -86,6 +86,7 @@ impl Handler for Renderer { /// Message used to render a thread. pub struct ThreadPage { + pub current_user: Option, pub thread: Thread, pub posts: Vec, } @@ -102,6 +103,7 @@ struct RenderablePost { posted: FormattedDate, author_name: String, author_gravatar: String, + editable: bool, } /// This structure represents the transformed thread data with @@ -119,14 +121,21 @@ fn md5_hex(input: &[u8]) -> String { } fn prepare_thread(comrak: &ComrakOptions, page: ThreadPage) -> RenderableThreadPage { + let user = page.current_user; + let posts = page.posts.into_iter().map(|post| { let escaped_body = escape_html(&post.body); + let editable = user.clone() + .map(|c| post.author_email.eq(&c)) + .unwrap_or_else(|| false); + RenderablePost { id: post.id, body: markdown_to_html(&escaped_body, comrak), posted: post.posted.into(), - author_name: post.author_name, + author_name: post.author_name.clone(), author_gravatar: md5_hex(post.author_email.as_bytes()), + editable, } }).collect(); -- cgit 1.4.1