2021-04-05 16:50:13 +02:00
|
|
|
// Copyright (C) 2018-2021 Vincent Ambo <tazjin@tvl.su>
|
2018-04-14 16:40:56 +02:00
|
|
|
//
|
2018-04-17 14:42:11 +02:00
|
|
|
// This file is part of Converse.
|
|
|
|
//
|
2021-04-05 16:50:13 +02:00
|
|
|
// This program is free software: you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2018-04-17 14:42:11 +02:00
|
|
|
// 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
|
2021-04-05 16:50:13 +02:00
|
|
|
// General Public License for more details.
|
2018-04-17 14:42:11 +02:00
|
|
|
//
|
2021-04-05 16:50:13 +02:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see
|
|
|
|
// <https://www.gnu.org/licenses/>.
|
2018-04-14 16:40:56 +02:00
|
|
|
|
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.
|
|
|
|
|
2021-04-05 20:00:13 +02:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::models::*;
|
2022-02-07 16:49:59 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
use askama::Template;
|
2018-04-11 13:25:12 +02:00
|
|
|
use chrono::prelude::{DateTime, Utc};
|
2022-02-07 16:49:59 +01:00
|
|
|
use comrak::{markdown_to_html, ComrakOptions};
|
|
|
|
use md5;
|
|
|
|
use std::fmt;
|
2018-04-11 12:17:33 +02:00
|
|
|
|
2018-04-11 13:25:12 +02:00
|
|
|
pub struct Renderer {
|
|
|
|
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
|
2018-05-22 19:31:07 +02:00
|
|
|
#[derive(Debug)]
|
2018-05-22 18:35:35 +02:00
|
|
|
struct FormattedDate(DateTime<Utc>);
|
2018-04-11 16:16:37 +02:00
|
|
|
|
2018-05-22 18:35:35 +02:00
|
|
|
impl fmt::Display for FormattedDate {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0.format("%a %d %B %Y, %R"))
|
2018-04-11 16:16:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 19:31:07 +02:00
|
|
|
#[derive(Debug)]
|
2018-04-11 13:52:37 +02:00
|
|
|
struct IndexThread {
|
|
|
|
id: i32,
|
|
|
|
title: String,
|
2018-04-14 17:55:57 +02:00
|
|
|
sticky: bool,
|
2018-05-25 18:44:05 +02:00
|
|
|
closed: 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-05-22 18:35:35 +02:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "index.html")]
|
|
|
|
struct IndexPageTemplate {
|
|
|
|
threads: Vec<IndexThread>,
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:25:12 +02:00
|
|
|
// "Renderable" structures with data transformations applied.
|
2018-05-22 19:31:07 +02:00
|
|
|
#[derive(Debug)]
|
2018-04-11 13:25:12 +02:00
|
|
|
struct RenderablePost {
|
|
|
|
id: i32,
|
|
|
|
body: String,
|
2018-05-22 18:56:22 +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.
|
2018-05-22 18:56:22 +02:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "thread.html")]
|
2018-04-11 13:25:12 +02:00
|
|
|
struct RenderableThreadPage {
|
|
|
|
id: i32,
|
|
|
|
title: String,
|
2018-05-25 18:44:05 +02:00
|
|
|
closed: bool,
|
2018-04-11 13:25:12 +02:00
|
|
|
posts: Vec<RenderablePost>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function for computing Gravatar links.
|
|
|
|
fn md5_hex(input: &[u8]) -> String {
|
|
|
|
format!("{:x}", md5::compute(input))
|
|
|
|
}
|
|
|
|
|
2018-04-15 21:13:20 +02:00
|
|
|
/// The different types of editing modes supported by the editing
|
|
|
|
/// template:
|
2018-05-22 19:31:07 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
2018-04-15 21:13:20 +02:00
|
|
|
pub enum EditingMode {
|
|
|
|
NewThread,
|
|
|
|
PostReply,
|
|
|
|
EditPost,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for EditingMode {
|
2022-02-07 16:49:59 +01:00
|
|
|
fn default() -> EditingMode {
|
|
|
|
EditingMode::NewThread
|
|
|
|
}
|
2018-04-15 21:13:20 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 19:31:07 +02:00
|
|
|
/// This is the template used for rendering the new thread, edit post
|
|
|
|
/// and reply to thread forms.
|
|
|
|
#[derive(Template, Default)]
|
|
|
|
#[template(path = "post.html")]
|
|
|
|
pub struct FormTemplate {
|
2018-04-15 21:13:20 +02:00
|
|
|
/// 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-05-22 19:31:07 +02:00
|
|
|
let ctx = FormTemplate {
|
2018-04-15 21:13:20 +02:00
|
|
|
alerts: msg.alerts,
|
|
|
|
title: msg.title,
|
|
|
|
post: msg.post,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2018-05-22 19:31:07 +02:00
|
|
|
ctx.render().map_err(|e| e.into())
|
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 {
|
2018-05-22 19:31:07 +02:00
|
|
|
let ctx = FormTemplate {
|
2018-04-15 23:09:44 +02:00
|
|
|
mode: EditingMode::EditPost,
|
|
|
|
id: Some(msg.id),
|
|
|
|
post: Some(msg.post),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2018-05-22 19:31:07 +02:00
|
|
|
ctx.render().map_err(|e| e.into())
|
2018-04-15 23:09:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:06:30 +02:00
|
|
|
/// Message used to render search results
|
2018-05-22 19:34:03 +02:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "search.html")]
|
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-05-22 19:34:03 +02:00
|
|
|
msg.render().map_err(|e| e.into())
|
2018-04-14 22:06:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 22:09:31 +02:00
|
|
|
|
|
|
|
// TODO: actor-free implementation below
|
|
|
|
|
|
|
|
/// Render the index page for the given thread list.
|
|
|
|
pub fn index_page(threads: Vec<ThreadIndex>) -> Result<String> {
|
|
|
|
let threads: Vec<IndexThread> = threads
|
|
|
|
.into_iter()
|
|
|
|
.map(|thread| IndexThread {
|
|
|
|
id: thread.thread_id,
|
|
|
|
title: thread.title, // escape_html(&thread.title),
|
|
|
|
sticky: thread.sticky,
|
|
|
|
closed: thread.closed,
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page of a given thread.
|
|
|
|
pub fn thread_page(user: i32, thread: Thread, posts: Vec<SimplePost>) -> Result<String> {
|
2022-02-07 16:49:59 +01:00
|
|
|
let posts = posts
|
|
|
|
.into_iter()
|
|
|
|
.map(|post| {
|
|
|
|
let editable = user != 1 && post.user_id == user;
|
|
|
|
|
|
|
|
let comrak = ComrakOptions::default(); // TODO(tazjin): cheddar
|
|
|
|
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();
|
2021-04-05 22:09:31 +02:00
|
|
|
|
|
|
|
let renderable = RenderableThreadPage {
|
|
|
|
posts,
|
|
|
|
closed: thread.closed,
|
|
|
|
id: thread.id,
|
|
|
|
title: thread.title,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(renderable.render()?)
|
|
|
|
}
|