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-08 18:48:50 +02:00
|
|
|
//! This module contains the implementation of converse's actix-web
|
|
|
|
//! HTTP handlers.
|
|
|
|
//!
|
|
|
|
//! Most handlers have an associated rendering function using one of
|
|
|
|
//! the tera templates stored in the `/templates` directory in the
|
|
|
|
//! project root.
|
|
|
|
|
2021-04-05 20:00:13 +02:00
|
|
|
use crate::db::*;
|
2022-02-07 16:49:59 +01:00
|
|
|
use crate::errors::{ConverseError, ConverseResult};
|
2021-04-05 20:00:13 +02:00
|
|
|
use crate::models::*;
|
|
|
|
use crate::oidc::*;
|
|
|
|
use crate::render::*;
|
2022-02-07 16:49:59 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
use actix_web;
|
|
|
|
use actix_web::http::Method;
|
|
|
|
use actix_web::middleware::identity::RequestIdentity;
|
|
|
|
use actix_web::middleware::{Middleware, Started};
|
|
|
|
use actix_web::*;
|
|
|
|
use futures::Future;
|
2018-04-08 18:48:50 +02:00
|
|
|
|
2021-04-05 22:09:31 +02:00
|
|
|
use rouille::{Request, Response};
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
type ConverseResponse = Box<dyn Future<Item = HttpResponse, Error = ConverseError>>;
|
2018-04-08 18:48:50 +02:00
|
|
|
|
2018-04-11 12:18:08 +02:00
|
|
|
const HTML: &'static str = "text/html";
|
2018-05-01 23:00:15 +02:00
|
|
|
const ANONYMOUS: i32 = 1;
|
|
|
|
const NEW_THREAD_LENGTH_ERR: &'static str = "Title and body can not be empty!";
|
2018-04-11 12:18:08 +02:00
|
|
|
|
2018-04-08 18:48:50 +02:00
|
|
|
/// Represents the state carried by the web server actors.
|
|
|
|
pub struct AppState {
|
|
|
|
/// Address of the database actor
|
2021-04-05 17:22:48 +02:00
|
|
|
pub db: Addr<DbExecutor>,
|
2018-04-08 18:48:50 +02:00
|
|
|
|
2018-04-08 22:37:36 +02:00
|
|
|
/// Address of the OIDC actor
|
2021-04-05 17:22:48 +02:00
|
|
|
pub oidc: Addr<OidcExecutor>,
|
2018-04-08 22:37:36 +02:00
|
|
|
|
2018-04-11 12:18:08 +02:00
|
|
|
/// Address of the rendering actor
|
2021-04-05 17:22:48 +02:00
|
|
|
pub renderer: Addr<Renderer>,
|
2018-04-08 18:48:50 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 22:09:31 +02:00
|
|
|
/// Serve the forum's index page.
|
|
|
|
pub fn forum_index_rouille(db: &DbExecutor) -> ConverseResult<Response> {
|
|
|
|
let threads = db.list_threads()?;
|
|
|
|
Ok(Response::html(index_page(threads)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forum_index(_: State<AppState>) -> ConverseResponse {
|
|
|
|
unimplemented!()
|
2018-04-08 18:48:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:00:15 +02:00
|
|
|
/// Returns the ID of the currently logged in user. If there is no ID
|
|
|
|
/// present, the ID of the anonymous user will be returned.
|
|
|
|
pub fn get_user_id(req: &HttpRequest<AppState>) -> i32 {
|
|
|
|
if let Some(id) = req.identity() {
|
|
|
|
// If this .expect() call is triggered, someone is likely
|
|
|
|
// attempting to mess with their cookies. These requests can
|
|
|
|
// be allowed to fail without further ado.
|
|
|
|
id.parse().expect("Session cookie contained invalid data!")
|
|
|
|
} else {
|
|
|
|
ANONYMOUS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:09:31 +02:00
|
|
|
pub fn get_user_id_rouille(_req: &Request) -> i32 {
|
|
|
|
// TODO(tazjin): Implement session support in rouille somehow.
|
|
|
|
ANONYMOUS
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn forum_thread_rouille(
|
|
|
|
req: &Request,
|
|
|
|
db: &DbExecutor,
|
|
|
|
thread_id: i32,
|
|
|
|
) -> ConverseResult<Response> {
|
2021-04-05 22:09:31 +02:00
|
|
|
let user = get_user_id_rouille(&req);
|
|
|
|
let thread = db.get_thread(thread_id)?;
|
|
|
|
Ok(Response::html(thread_page(user, thread.0, thread.1)?))
|
|
|
|
}
|
|
|
|
|
2018-04-08 18:48:50 +02:00
|
|
|
/// This handler retrieves and displays a single forum thread.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn forum_thread(
|
|
|
|
_: State<AppState>,
|
|
|
|
_: HttpRequest<AppState>,
|
|
|
|
_: Path<i32>,
|
|
|
|
) -> ConverseResponse {
|
2021-04-05 22:09:31 +02:00
|
|
|
unimplemented!()
|
2018-04-08 18:48:50 +02:00
|
|
|
}
|
2018-04-08 20:01:32 +02:00
|
|
|
|
2018-04-09 23:37:41 +02:00
|
|
|
/// This handler presents the user with the "New Thread" form.
|
2018-04-11 12:18:08 +02:00
|
|
|
pub fn new_thread(state: State<AppState>) -> ConverseResponse {
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.send(NewThreadPage::default())
|
|
|
|
.flatten()
|
2018-04-12 00:04:37 +02:00
|
|
|
.map(|res| HttpResponse::Ok().content_type(HTML).body(res))
|
2018-04-11 12:18:08 +02:00
|
|
|
.responder()
|
2018-04-09 23:37:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 09:20:27 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct NewThreadForm {
|
|
|
|
pub title: String,
|
2018-04-15 21:13:20 +02:00
|
|
|
pub post: String,
|
2018-04-09 09:20:27 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 20:01:32 +02:00
|
|
|
/// This handler receives a "New thread"-form and redirects the user
|
|
|
|
/// to the new thread after creation.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn submit_thread(
|
|
|
|
(state, input, req): (State<AppState>, Form<NewThreadForm>, HttpRequest<AppState>),
|
|
|
|
) -> ConverseResponse {
|
2018-04-15 12:59:43 +02:00
|
|
|
// Trim whitespace out of inputs:
|
|
|
|
let input = NewThreadForm {
|
|
|
|
title: input.title.trim().into(),
|
2018-04-15 21:13:20 +02:00
|
|
|
post: input.post.trim().into(),
|
2018-04-15 12:59:43 +02:00
|
|
|
};
|
|
|
|
|
2018-04-12 01:07:25 +02:00
|
|
|
// Perform simple validation and abort here if it fails:
|
2018-04-15 21:13:20 +02:00
|
|
|
if input.title.is_empty() || input.post.is_empty() {
|
2022-02-07 16:49:59 +01:00
|
|
|
return state
|
|
|
|
.renderer
|
2018-04-12 01:07:25 +02:00
|
|
|
.send(NewThreadPage {
|
|
|
|
alerts: vec![NEW_THREAD_LENGTH_ERR],
|
2018-04-15 12:59:43 +02:00
|
|
|
title: Some(input.title),
|
2018-04-15 21:13:20 +02:00
|
|
|
post: Some(input.post),
|
2018-04-12 01:07:25 +02:00
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.map(|res| HttpResponse::Ok().content_type(HTML).body(res))
|
|
|
|
.responder();
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:00:15 +02:00
|
|
|
let user_id = get_user_id(&req);
|
2018-04-12 01:28:37 +02:00
|
|
|
|
2018-04-09 09:20:27 +02:00
|
|
|
let new_thread = NewThread {
|
2018-05-01 23:00:15 +02:00
|
|
|
user_id,
|
2018-04-15 12:59:43 +02:00
|
|
|
title: input.title,
|
2018-04-09 09:20:27 +02:00
|
|
|
};
|
|
|
|
|
2018-04-14 16:33:45 +02:00
|
|
|
let msg = CreateThread {
|
|
|
|
new_thread,
|
2018-04-15 21:13:20 +02:00
|
|
|
post: input.post,
|
2018-04-14 16:33:45 +02:00
|
|
|
};
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.db
|
|
|
|
.send(msg)
|
2018-04-08 20:01:32 +02:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |res| {
|
|
|
|
let thread = res?;
|
2022-02-07 16:49:59 +01:00
|
|
|
info!(
|
|
|
|
"Created new thread \"{}\" with ID {}",
|
|
|
|
thread.title, thread.id
|
|
|
|
);
|
2018-04-08 20:20:05 +02:00
|
|
|
Ok(HttpResponse::SeeOther()
|
2022-02-07 16:49:59 +01:00
|
|
|
.header("Location", format!("/thread/{}", thread.id))
|
|
|
|
.finish())
|
2018-04-08 20:01:32 +02:00
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
}
|
2018-04-08 20:20:05 +02:00
|
|
|
|
2018-04-09 09:20:27 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct NewPostForm {
|
|
|
|
pub thread_id: i32,
|
2018-04-15 21:13:20 +02:00
|
|
|
pub post: String,
|
2018-04-09 09:20:27 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 20:20:05 +02:00
|
|
|
/// This handler receives a "Reply"-form and redirects the user to the
|
|
|
|
/// new post after creation.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn reply_thread(
|
|
|
|
state: State<AppState>,
|
|
|
|
input: Form<NewPostForm>,
|
|
|
|
req: HttpRequest<AppState>,
|
|
|
|
) -> ConverseResponse {
|
2018-05-01 23:00:15 +02:00
|
|
|
let user_id = get_user_id(&req);
|
2018-04-12 01:28:37 +02:00
|
|
|
|
2018-04-09 09:20:27 +02:00
|
|
|
let new_post = NewPost {
|
2018-05-01 23:00:15 +02:00
|
|
|
user_id,
|
2018-04-09 09:20:27 +02:00
|
|
|
thread_id: input.thread_id,
|
2018-04-15 21:13:20 +02:00
|
|
|
body: input.post.trim().into(),
|
2018-04-09 09:20:27 +02:00
|
|
|
};
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.db
|
|
|
|
.send(CreatePost(new_post))
|
2018-04-14 20:29:46 +02:00
|
|
|
.flatten()
|
2018-04-08 20:20:05 +02:00
|
|
|
.from_err()
|
2018-04-14 20:29:46 +02:00
|
|
|
.and_then(move |post| {
|
2018-04-08 20:20:05 +02:00
|
|
|
info!("Posted reply {} to thread {}", post.id, post.thread_id);
|
|
|
|
Ok(HttpResponse::SeeOther()
|
2022-02-07 16:49:59 +01:00
|
|
|
.header(
|
|
|
|
"Location",
|
|
|
|
format!("/thread/{}#post-{}", post.thread_id, post.id),
|
|
|
|
)
|
|
|
|
.finish())
|
2018-04-08 20:20:05 +02:00
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
}
|
2018-04-08 22:37:36 +02:00
|
|
|
|
2018-04-15 23:09:44 +02:00
|
|
|
/// This handler presents the user with the form to edit a post. If
|
|
|
|
/// the user attempts to edit a post that they do not have access to,
|
|
|
|
/// they are currently ungracefully redirected back to the post
|
|
|
|
/// itself.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn edit_form(
|
|
|
|
state: State<AppState>,
|
|
|
|
req: HttpRequest<AppState>,
|
|
|
|
query: Path<GetPost>,
|
|
|
|
) -> ConverseResponse {
|
2018-05-01 23:00:15 +02:00
|
|
|
let user_id = get_user_id(&req);
|
2018-04-15 23:09:44 +02:00
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.db
|
|
|
|
.send(query.into_inner())
|
2018-04-15 23:09:44 +02:00
|
|
|
.flatten()
|
|
|
|
.from_err()
|
|
|
|
.and_then(move |post| {
|
2018-05-01 23:00:15 +02:00
|
|
|
if user_id != 1 && post.user_id == user_id {
|
|
|
|
return Ok(post);
|
2018-04-15 23:09:44 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 10:53:31 +02:00
|
|
|
Err(ConverseError::PostEditForbidden {
|
|
|
|
user: user_id,
|
|
|
|
id: post.id,
|
|
|
|
})
|
2018-04-15 23:09:44 +02:00
|
|
|
})
|
|
|
|
.and_then(move |post| {
|
|
|
|
let edit_msg = EditPostPage {
|
|
|
|
id: post.id,
|
|
|
|
post: post.body,
|
|
|
|
};
|
|
|
|
|
|
|
|
state.renderer.send(edit_msg).from_err()
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.map(|page| HttpResponse::Ok().content_type(HTML).body(page))
|
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
2018-04-15 23:30:22 +02:00
|
|
|
/// This handler "executes" an edit to a post if the current user owns
|
|
|
|
/// the edited post.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn edit_post(
|
|
|
|
state: State<AppState>,
|
|
|
|
req: HttpRequest<AppState>,
|
|
|
|
update: Form<UpdatePost>,
|
|
|
|
) -> ConverseResponse {
|
2018-05-01 23:00:15 +02:00
|
|
|
let user_id = get_user_id(&req);
|
2018-04-15 23:30:22 +02:00
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.db
|
|
|
|
.send(GetPost { id: update.post_id })
|
2018-04-15 23:30:22 +02:00
|
|
|
.flatten()
|
|
|
|
.from_err()
|
|
|
|
.and_then(move |post| {
|
2018-05-01 23:00:15 +02:00
|
|
|
if user_id != 1 && post.user_id == user_id {
|
2022-02-07 16:49:59 +01:00
|
|
|
Ok(())
|
2018-05-01 23:00:15 +02:00
|
|
|
} else {
|
2018-05-26 10:53:31 +02:00
|
|
|
Err(ConverseError::PostEditForbidden {
|
|
|
|
user: user_id,
|
|
|
|
id: post.id,
|
|
|
|
})
|
2018-04-15 23:30:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.and_then(move |_| state.db.send(update.0).from_err())
|
|
|
|
.flatten()
|
2022-02-07 16:49:59 +01:00
|
|
|
.map(|updated| {
|
|
|
|
HttpResponse::SeeOther()
|
|
|
|
.header(
|
|
|
|
"Location",
|
|
|
|
format!("/thread/{}#post-{}", updated.thread_id, updated.id),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
})
|
2018-04-15 23:30:22 +02:00
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:06:30 +02:00
|
|
|
/// This handler executes a full-text search on the forum database and
|
|
|
|
/// displays the results to the user.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn search_forum(state: State<AppState>, query: Query<SearchPosts>) -> ConverseResponse {
|
2018-04-14 23:28:34 +02:00
|
|
|
let query_string = query.query.clone();
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.db
|
|
|
|
.send(query.into_inner())
|
2018-04-14 22:06:30 +02:00
|
|
|
.flatten()
|
2022-02-07 16:49:59 +01:00
|
|
|
.and_then(move |results| {
|
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.send(SearchResultPage {
|
|
|
|
results,
|
|
|
|
query: query_string,
|
|
|
|
})
|
|
|
|
.from_err()
|
|
|
|
})
|
2018-04-14 22:06:30 +02:00
|
|
|
.flatten()
|
|
|
|
.map(|res| HttpResponse::Ok().content_type(HTML).body(res))
|
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
2018-04-08 22:37:36 +02:00
|
|
|
/// This handler initiates an OIDC login.
|
|
|
|
pub fn login(state: State<AppState>) -> ConverseResponse {
|
2022-02-07 16:49:59 +01:00
|
|
|
state
|
|
|
|
.oidc
|
|
|
|
.send(GetLoginUrl)
|
2018-04-08 22:37:36 +02:00
|
|
|
.from_err()
|
2022-02-07 16:49:59 +01:00
|
|
|
.and_then(|url| {
|
|
|
|
Ok(HttpResponse::TemporaryRedirect()
|
|
|
|
.header("Location", url)
|
|
|
|
.finish())
|
|
|
|
})
|
2018-04-08 22:37:36 +02:00
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:00:15 +02:00
|
|
|
/// This handler handles an OIDC callback (i.e. completed login).
|
|
|
|
///
|
|
|
|
/// Upon receiving the callback, a token is retrieved from the OIDC
|
|
|
|
/// provider and a user lookup is performed. If a user with a matching
|
|
|
|
/// email-address is found in the database, it is logged in -
|
|
|
|
/// otherwise a new user is created.
|
2022-02-07 16:49:59 +01:00
|
|
|
pub fn callback(
|
|
|
|
state: State<AppState>,
|
|
|
|
data: Form<CodeResponse>,
|
|
|
|
req: HttpRequest<AppState>,
|
|
|
|
) -> ConverseResponse {
|
|
|
|
state
|
|
|
|
.oidc
|
|
|
|
.send(RetrieveToken(data.0))
|
|
|
|
.flatten()
|
2018-05-01 23:00:15 +02:00
|
|
|
.map(|author| LookupOrCreateUser {
|
|
|
|
email: author.email,
|
|
|
|
name: author.name,
|
|
|
|
})
|
2022-02-07 16:49:59 +01:00
|
|
|
.and_then(move |msg| state.db.send(msg).from_err())
|
|
|
|
.flatten()
|
2018-05-01 23:00:15 +02:00
|
|
|
.and_then(move |user| {
|
|
|
|
info!("Completed login for user {} ({})", user.email, user.id);
|
|
|
|
req.remember(user.id.to_string());
|
2022-02-07 16:49:59 +01:00
|
|
|
Ok(HttpResponse::SeeOther().header("Location", "/").finish())
|
|
|
|
})
|
2018-04-08 22:37:36 +02:00
|
|
|
.responder()
|
|
|
|
}
|
2018-04-09 09:10:48 +02:00
|
|
|
|
2018-05-19 00:11:10 +02:00
|
|
|
/// This is an extension trait to enable easy serving of embedded
|
|
|
|
/// static content.
|
|
|
|
///
|
|
|
|
/// It is intended to be called with `include_bytes!()` when setting
|
|
|
|
/// up the actix-web application.
|
|
|
|
pub trait EmbeddedFile {
|
|
|
|
fn static_file(self, path: &'static str, content: &'static [u8]) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmbeddedFile for App<AppState> {
|
|
|
|
fn static_file(self, path: &'static str, content: &'static [u8]) -> Self {
|
|
|
|
self.route(path, Method::GET, move |_: HttpRequest<_>| {
|
2021-04-05 20:30:29 +02:00
|
|
|
let mime = format!("{}", mime_guess::from_path(path).first_or_octet_stream());
|
2022-02-07 16:49:59 +01:00
|
|
|
HttpResponse::Ok().content_type(mime.as_str()).body(content)
|
2018-05-19 00:11:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 09:10:48 +02:00
|
|
|
|
2018-04-12 01:07:25 +02:00
|
|
|
/// Middleware used to enforce logins unceremoniously.
|
2018-04-09 09:10:48 +02:00
|
|
|
pub struct RequireLogin;
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
impl<S> Middleware<S> for RequireLogin {
|
2021-04-05 17:22:48 +02:00
|
|
|
fn start(&self, req: &HttpRequest<S>) -> actix_web::Result<Started> {
|
2018-05-01 23:00:15 +02:00
|
|
|
let logged_in = req.identity().is_some();
|
2018-04-09 09:10:48 +02:00
|
|
|
let is_oidc_req = req.path().starts_with("/oidc");
|
|
|
|
|
2018-05-01 23:00:15 +02:00
|
|
|
if !is_oidc_req && !logged_in {
|
2018-04-09 09:10:48 +02:00
|
|
|
Ok(Started::Response(
|
|
|
|
HttpResponse::SeeOther()
|
|
|
|
.header("Location", "/oidc/login")
|
2022-02-07 16:49:59 +01:00
|
|
|
.finish(),
|
2018-04-09 09:10:48 +02:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(Started::Done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|