2018-04-14 16:40:56 +02:00
|
|
|
// Copyright (C) 2018 Vincent Ambo <mail@tazj.in>
|
|
|
|
//
|
|
|
|
// Converse is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see
|
|
|
|
// <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
|
|
|
}
|
|
|
|
|
|
|
|
impl Message for IndexPage {
|
|
|
|
type Result = Result<String>;
|
|
|
|
}
|
|
|
|
|
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-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-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 {
|
|
|
|
pub thread: Thread,
|
|
|
|
pub posts: Vec<Post>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message for ThreadPage {
|
|
|
|
type Result = Result<String>;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-14 16:33:45 +02:00
|
|
|
let posts = page.posts.into_iter().map(|post| {
|
2018-04-14 22:30:17 +02:00
|
|
|
let escaped_body = escape_html(&post.body);
|
2018-04-14 16:33:45 +02:00
|
|
|
RenderablePost {
|
2018-04-11 13:25:12 +02:00
|
|
|
id: post.id,
|
2018-04-14 22:30:17 +02:00
|
|
|
body: markdown_to_html(&escaped_body, comrak),
|
2018-04-11 16:16:37 +02:00
|
|
|
posted: post.posted.into(),
|
2018-04-11 13:25:12 +02:00
|
|
|
author_name: post.author_name,
|
|
|
|
author_gravatar: md5_hex(post.author_email.as_bytes()),
|
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-11 12:17:33 +02:00
|
|
|
let mut ctx = Context::new();
|
2018-04-11 13:25:12 +02:00
|
|
|
ctx.add("title", &renderable.title);
|
|
|
|
ctx.add("posts", &renderable.posts);
|
|
|
|
ctx.add("id", &renderable.id);
|
|
|
|
Ok(self.tera.render("thread.html", &ctx)?)
|
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>,
|
|
|
|
pub body: Option<String>,
|
|
|
|
}
|
2018-04-11 12:17:33 +02:00
|
|
|
|
|
|
|
impl Message for NewThreadPage {
|
|
|
|
type Result = Result<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.add("alerts", &msg.alerts);
|
|
|
|
ctx.add("title", &msg.title.map(|s| escape_html(&s)));
|
|
|
|
ctx.add("body", &msg.body.map(|s| escape_html(&s)));
|
|
|
|
Ok(self.tera.render("new-thread.html", &ctx)?)
|
2018-04-11 12:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 22:06:30 +02:00
|
|
|
|
|
|
|
/// Message used to render search results
|
|
|
|
pub struct SearchResultPage {
|
|
|
|
pub query: String,
|
|
|
|
pub results: Vec<SearchResult>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message for SearchResultPage {
|
|
|
|
type Result = Result<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<SearchResultPage> for Renderer {
|
|
|
|
type Result = Result<String>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: SearchResultPage, _: &mut Self::Context) -> Self::Result {
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.add("query", &msg.query);
|
|
|
|
ctx.add("results", &msg.results);
|
|
|
|
Ok(self.tera.render("search.html", &ctx)?)
|
|
|
|
}
|
|
|
|
}
|