about summary refs log tree commit diff
path: root/src/render.rs
blob: ca1fd8ac3bc93adb7d07015e89be4bec8ff17ea6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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/>.

//! 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::*;
use md5;
use models::*;
use tera::{escape_html, Context, Tera};
use chrono::prelude::{DateTime, Utc};
use comrak::{ComrakOptions, markdown_to_html};

pub struct Renderer {
    pub tera: Tera,
    pub comrak: ComrakOptions,
}

impl Actor for Renderer {
    type Context = actix::Context<Self>;
}

/// 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")))
    }
}

/// Message used to render the index page.
pub struct IndexPage {
    pub threads: Vec<Thread>,
}

impl Message for IndexPage {
    type Result = Result<String>;
}

#[derive(Debug, Serialize)]
struct IndexThread {
    id: i32,
    title: String,
    posted: FormattedDate,
    author_name: String,
}

impl Handler<IndexPage> for Renderer {
    type Result = Result<String>;

    fn handle(&mut self, msg: IndexPage, _: &mut Self::Context) -> Self::Result {
        let threads: Vec<IndexThread> = msg.threads
            .into_iter()
            .map(|thread| IndexThread {
                id: thread.id,
                title: escape_html(&thread.title),
                posted: thread.posted.into(),
                author_name: thread.author_name,
            })
            .collect();

        let mut ctx = Context::new();
        ctx.add("threads", &threads);
        Ok(self.tera.render("index.html", &ctx)?)
    }
}

/// Message used to render a thread.
pub struct ThreadPage {
    pub thread: Thread,
    pub posts: Vec<Post>,
}

impl Message for ThreadPage {
    type Result = Result<String>;
}

// "Renderable" structures with data transformations applied.
#[derive(Debug, Serialize)]
struct RenderablePost {
    id: i32,
    body: String,
    posted: FormattedDate,
    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 {
    let posts = page.posts.into_iter().map(|post| {
        RenderablePost {
            id: post.id,
            body: markdown_to_html(&post.body, comrak),
            posted: post.posted.into(),
            author_name: post.author_name,
            author_gravatar: md5_hex(post.author_email.as_bytes()),
        }
    }).collect();

    RenderableThreadPage {
        posts,
        id: page.thread.id,
        title: escape_html(&page.thread.title),
    }
}

impl Handler<ThreadPage> for Renderer {
    type Result = Result<String>;

    fn handle(&mut self, msg: ThreadPage, _: &mut Self::Context) -> Self::Result {
        let renderable = prepare_thread(&self.comrak, msg);
        let mut ctx = Context::new();
        ctx.add("title", &renderable.title);
        ctx.add("posts", &renderable.posts);
        ctx.add("id", &renderable.id);
        Ok(self.tera.render("thread.html", &ctx)?)
    }
}

/// Message used to render new thread page.
///
/// 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>,
}

impl Message for NewThreadPage {
    type Result = Result<String>;
}

impl Handler<NewThreadPage> for Renderer {
    type Result = Result<String>;

    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)?)
    }
}