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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// Copyright (C) 2018 Vincent Ambo <mail@tazj.in>
//
// This file is part of Converse.
//
// Converse is free software: you can redistribute it and/or modify it
// 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.
//
// 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
// 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
// <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 askama::Template;
use errors::*;
use std::fmt;
use md5;
use models::*;
use tera::{escape_html, 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(DateTime<Utc>);
impl fmt::Display for FormattedDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.format("%a %d %B %Y, %R"))
}
}
/// Message used to render the index page.
pub struct IndexPage {
pub threads: Vec<ThreadIndex>,
}
message!(IndexPage, Result<String>);
#[derive(Debug, Serialize)]
struct IndexThread {
id: i32,
title: String,
sticky: bool,
posted: FormattedDate,
author_name: String,
post_author: String,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexPageTemplate {
threads: Vec<IndexThread>,
}
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.thread_id,
title: thread.title, // escape_html(&thread.title),
sticky: thread.sticky,
posted: FormattedDate(thread.posted),
author_name: thread.thread_author,
post_author: thread.post_author,
})
.collect();
let tpl = IndexPageTemplate {
threads
};
tpl.render().map_err(|e| e.into())
}
}
/// Message used to render a thread.
pub struct ThreadPage {
pub current_user: i32,
pub thread: Thread,
pub posts: Vec<SimplePost>,
}
message!(ThreadPage, 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,
editable: bool,
}
/// This structure represents the transformed thread data with
/// Markdown rendering and other changes applied.
#[derive(Template)]
#[template(path = "thread.html")]
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 user = page.current_user;
let posts = page.posts.into_iter().map(|post| {
let editable = user != 1 && post.user_id == user;
RenderablePost {
id: post.id,
body: markdown_to_html(&post.body, comrak),
posted: FormattedDate(post.posted),
author_name: post.author_name.clone(),
author_gravatar: md5_hex(post.author_email.as_bytes()),
editable,
}
}).collect();
RenderableThreadPage {
posts,
id: page.thread.id,
title: 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);
renderable.render().map_err(|e| e.into())
}
}
/// 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>,
}
/// 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 post: Option<String>,
}
message!(NewThreadPage, Result<String>);
impl Handler<NewThreadPage> for Renderer {
type Result = Result<String>;
fn handle(&mut self, msg: NewThreadPage, _: &mut Self::Context) -> Self::Result {
let ctx = FormContext {
alerts: msg.alerts,
title: msg.title,
post: msg.post,
..Default::default()
};
Ok(self.tera.render("post.html", &ctx)?)
}
}
/// 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)?)
}
}
/// Message used to render search results
#[derive(Serialize)]
pub struct SearchResultPage {
pub query: String,
pub results: Vec<SearchResult>,
}
message!(SearchResultPage, Result<String>);
impl Handler<SearchResultPage> for Renderer {
type Result = Result<String>;
fn handle(&mut self, msg: SearchResultPage, _: &mut Self::Context) -> Self::Result {
Ok(self.tera.render("search.html", &msg)?)
}
}
|