2018-04-14 16:40:56 +02:00
|
|
|
// Copyright (C) 2018 Vincent Ambo <mail@tazj.in>
|
|
|
|
//
|
2018-04-17 14:42:11 +02:00
|
|
|
// This file is part of Converse.
|
|
|
|
//
|
2018-04-14 16:40:56 +02:00
|
|
|
// Converse is free software: you can redistribute it and/or modify it
|
2018-04-17 14:42:11 +02:00
|
|
|
// under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
2018-04-14 16:40:56 +02:00
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2018-04-17 14:42:11 +02:00
|
|
|
// Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public
|
|
|
|
// License along with this program. If not, see
|
2018-04-14 16:40:56 +02:00
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
//! This module defines a rendering actor used for processing Converse
|
|
|
|
//! data into whatever format is needed by the templates and rendering
|
|
|
|
//! them.
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
|
|
|
use errors::*;
|
2018-04-11 13:25:12 +02:00
|
|
|
use md5;
|
|
|
|
use models::*;
|
|
|
|
use tera::{escape_html, Context, Tera};
|
|
|
|
use chrono::prelude::{DateTime, Utc};
|
|
|
|
use comrak::{ComrakOptions, markdown_to_html};
|
2018-04-11 12:17:33 +02:00
|
|
|
|
2018-04-11 13:25:12 +02:00
|
|
|
pub struct Renderer {
|
|
|
|
pub tera: Tera,
|
|
|
|
pub comrak: ComrakOptions,
|
|
|
|
}
|
2018-04-11 12:17:33 +02:00
|
|
|
|
|
|
|
impl Actor for Renderer {
|
|
|
|
type Context = actix::Context<Self>;
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:16:37 +02:00
|
|
|
/// Represents a data formatted for human consumption
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct FormattedDate(String);
|
|
|
|
|
|
|
|
impl From<DateTime<Utc>> for FormattedDate {
|
|
|
|
fn from(date: DateTime<Utc>) -> Self {
|
|
|
|
FormattedDate(format!("{}", date.format("%a %d %B %Y, %R")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
/// Message used to render the index page.
|
|
|
|
pub struct IndexPage {
|
2018-04-14 17:15:27 +02:00
|
|
|
pub threads: Vec<ThreadIndex>,
|
2018-04-11 12:17:33 +02:00
|
|
|
}
|
2018-04-15 21:31:14 +02:00
|
|
|
message!(IndexPage, Result<String>);
|
2018-04-11 12:17:33 +02:00
|
|
|
|
2018-04-11 13:52:37 +02:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct IndexThread {
|
|
|
|
id: i32,
|
|
|
|
title: String,
|
2018-04-14 17:55:57 +02:00
|
|
|
sticky: bool,
|
2018-04-11 16:16:37 +02:00
|
|
|
posted: FormattedDate,
|
2018-04-11 13:52:37 +02:00
|
|
|
author_name: String,
|
2018-04-16 15:56:00 +02:00
|
|
|
post_author: String,
|
2018-04-11 13:52:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
impl Handler<IndexPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: IndexPage, _: &mut Self::Context) -> Self::Result {
|
2018-04-11 13:52:37 +02:00
|
|
|
let threads: Vec<IndexThread> = msg.threads
|
|
|
|
.into_iter()
|
|
|
|
.map(|thread| IndexThread {
|
2018-04-14 17:15:27 +02:00
|
|
|
id: thread.thread_id,
|
2018-04-11 13:52:37 +02:00
|
|
|
title: escape_html(&thread.title),
|
2018-04-14 17:55:57 +02:00
|
|
|
sticky: thread.sticky,
|
2018-04-11 16:16:37 +02:00
|
|
|
posted: thread.posted.into(),
|
2018-04-14 17:47:31 +02:00
|
|
|
author_name: thread.thread_author,
|
2018-04-16 15:56:00 +02:00
|
|
|
post_author: thread.post_author,
|
2018-04-11 13:52:37 +02:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
let mut ctx = Context::new();
|
2018-04-11 13:52:37 +02:00
|
|
|
ctx.add("threads", &threads);
|
2018-04-11 13:25:12 +02:00
|
|
|
Ok(self.tera.render("index.html", &ctx)?)
|
2018-04-11 12:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Message used to render a thread.
|
|
|
|
pub struct ThreadPage {
|
2018-04-15 12:38:12 +02:00
|
|
|
pub current_user: Option<String>,
|
2018-04-11 12:17:33 +02:00
|
|
|
pub thread: Thread,
|
|
|
|
pub posts: Vec<Post>,
|
|
|
|
}
|
2018-04-15 21:31:14 +02:00
|
|
|
message!(ThreadPage, Result<String>);
|
2018-04-11 12:17:33 +02:00
|
|
|
|
2018-04-11 13:25:12 +02:00
|
|
|
// "Renderable" structures with data transformations applied.
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct RenderablePost {
|
|
|
|
id: i32,
|
|
|
|
body: String,
|
2018-04-11 16:16:37 +02:00
|
|
|
posted: FormattedDate,
|
2018-04-11 13:25:12 +02:00
|
|
|
author_name: String,
|
|
|
|
author_gravatar: String,
|
2018-04-15 12:38:12 +02:00
|
|
|
editable: bool,
|
2018-04-11 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This structure represents the transformed thread data with
|
|
|
|
/// Markdown rendering and other changes applied.
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct RenderableThreadPage {
|
|
|
|
id: i32,
|
|
|
|
title: String,
|
|
|
|
posts: Vec<RenderablePost>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function for computing Gravatar links.
|
|
|
|
fn md5_hex(input: &[u8]) -> String {
|
|
|
|
format!("{:x}", md5::compute(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_thread(comrak: &ComrakOptions, page: ThreadPage) -> RenderableThreadPage {
|
2018-04-15 12:38:12 +02:00
|
|
|
let user = page.current_user;
|
|
|
|
|
2018-04-14 16:33:45 +02:00
|
|
|
let posts = page.posts.into_iter().map(|post| {
|
2018-04-15 12:38:12 +02:00
|
|
|
let editable = user.clone()
|
|
|
|
.map(|c| post.author_email.eq(&c))
|
|
|
|
.unwrap_or_else(|| false);
|
|
|
|
|
2018-04-14 16:33:45 +02:00
|
|
|
RenderablePost {
|
2018-04-11 13:25:12 +02:00
|
|
|
id: post.id,
|
2018-04-16 03:15:50 +02:00
|
|
|
body: markdown_to_html(&post.body, comrak),
|
2018-04-11 16:16:37 +02:00
|
|
|
posted: post.posted.into(),
|
2018-04-15 12:38:12 +02:00
|
|
|
author_name: post.author_name.clone(),
|
2018-04-11 13:25:12 +02:00
|
|
|
author_gravatar: md5_hex(post.author_email.as_bytes()),
|
2018-04-15 12:38:12 +02:00
|
|
|
editable,
|
2018-04-14 16:33:45 +02:00
|
|
|
}
|
|
|
|
}).collect();
|
2018-04-11 13:25:12 +02:00
|
|
|
|
|
|
|
RenderableThreadPage {
|
|
|
|
posts,
|
|
|
|
id: page.thread.id,
|
|
|
|
title: escape_html(&page.thread.title),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
impl Handler<ThreadPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: ThreadPage, _: &mut Self::Context) -> Self::Result {
|
2018-04-11 13:25:12 +02:00
|
|
|
let renderable = prepare_thread(&self.comrak, msg);
|
2018-04-15 21:18:27 +02:00
|
|
|
Ok(self.tera.render("thread.html", &renderable)?)
|
2018-04-11 12:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-15 21:13:20 +02:00
|
|
|
/// The different types of editing modes supported by the editing
|
|
|
|
/// template:
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub enum EditingMode {
|
|
|
|
NewThread,
|
|
|
|
PostReply,
|
|
|
|
EditPost,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for EditingMode {
|
|
|
|
fn default() -> EditingMode { EditingMode::NewThread }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This struct represents the context submitted to the template used
|
|
|
|
/// for rendering the new thread, edit post and reply to thread forms.
|
|
|
|
#[derive(Default, Serialize)]
|
|
|
|
pub struct FormContext {
|
|
|
|
/// Which editing mode is to be used by the template?
|
|
|
|
pub mode: EditingMode,
|
|
|
|
|
|
|
|
/// Potential alerts to display to the user (e.g. input validation
|
|
|
|
/// results)
|
|
|
|
pub alerts: Vec<&'static str>,
|
|
|
|
|
|
|
|
/// Either the title to be used in the subject field or the title
|
|
|
|
/// of the thread the user is responding to.
|
|
|
|
pub title: Option<String>,
|
|
|
|
|
|
|
|
/// Body of the post being edited, if present.
|
|
|
|
pub post: Option<String>,
|
|
|
|
|
|
|
|
/// ID of the thread being replied to or the post being edited.
|
|
|
|
pub id: Option<i32>,
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:17:33 +02:00
|
|
|
/// Message used to render new thread page.
|
2018-04-12 01:07:25 +02:00
|
|
|
///
|
|
|
|
/// It can optionally contain a vector of warnings to display to the
|
|
|
|
/// user in alert boxes, such as input validation errors.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NewThreadPage {
|
|
|
|
pub alerts: Vec<&'static str>,
|
|
|
|
pub title: Option<String>,
|
2018-04-15 21:13:20 +02:00
|
|
|
pub post: Option<String>,
|
2018-04-12 01:07:25 +02:00
|
|
|
}
|
2018-04-15 21:31:14 +02:00
|
|
|
message!(NewThreadPage, Result<String>);
|
2018-04-11 12:17:33 +02:00
|
|
|
|
|
|
|
impl Handler<NewThreadPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
2018-04-12 01:07:25 +02:00
|
|
|
fn handle(&mut self, msg: NewThreadPage, _: &mut Self::Context) -> Self::Result {
|
2018-04-15 23:09:44 +02:00
|
|
|
let ctx = FormContext {
|
2018-04-15 21:13:20 +02:00
|
|
|
alerts: msg.alerts,
|
|
|
|
title: msg.title,
|
|
|
|
post: msg.post,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
Ok(self.tera.render("post.html", &ctx)?)
|
2018-04-11 12:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 22:06:30 +02:00
|
|
|
|
2018-04-15 23:09:44 +02:00
|
|
|
/// Message used to render post editing page.
|
|
|
|
pub struct EditPostPage {
|
|
|
|
pub id: i32,
|
|
|
|
pub post: String,
|
|
|
|
}
|
|
|
|
message!(EditPostPage, Result<String>);
|
|
|
|
|
|
|
|
impl Handler<EditPostPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: EditPostPage, _: &mut Self::Context) -> Self::Result {
|
|
|
|
let ctx = FormContext {
|
|
|
|
mode: EditingMode::EditPost,
|
|
|
|
id: Some(msg.id),
|
|
|
|
post: Some(msg.post),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(self.tera.render("post.html", &ctx)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:06:30 +02:00
|
|
|
/// Message used to render search results
|
2018-04-15 21:18:27 +02:00
|
|
|
#[derive(Serialize)]
|
2018-04-14 22:06:30 +02:00
|
|
|
pub struct SearchResultPage {
|
|
|
|
pub query: String,
|
|
|
|
pub results: Vec<SearchResult>,
|
|
|
|
}
|
2018-04-15 21:31:14 +02:00
|
|
|
message!(SearchResultPage, Result<String>);
|
2018-04-14 22:06:30 +02:00
|
|
|
|
|
|
|
impl Handler<SearchResultPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: SearchResultPage, _: &mut Self::Context) -> Self::Result {
|
2018-04-15 21:18:27 +02:00
|
|
|
Ok(self.tera.render("search.html", &msg)?)
|
2018-04-14 22:06:30 +02:00
|
|
|
}
|
|
|
|
}
|