about summary refs log tree commit diff
path: root/web/converse/src/handlers.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-04-05T20·09+0200
committertazjin <mail@tazj.in>2021-04-20T10·44+0000
commit929b38e8ae4d61340cbd70352663af8f6f2418cf (patch)
treeb2562c19bf3727bab14e11dabdffb3c8bd3569d0 /web/converse/src/handlers.rs
parent54f59a5cc5835932c62c0f2d58712e30c248da4d (diff)
refactor(web/converse): Refactor first handlers to rouille r/2530
This commit starts the refactoring process towards dropping actix (and
tokio, ...). It builds, but at this commit, Converse does *not* work.

I decided to commit to avoid more ridiculous diffs.

Included changes:

* Added dependency on rouille.

* Refactored DbExecutor (formerly actix actor) to simply be a type
  with a few methods. Most actor messages still exist as they are
  being referred to by handlers.

* Started refactoring two of the handlers (and their related renderer
  functions) into Rouille's call scheme.

Important note: Rouille does not have safe session management out of
the box, and it will need to be implemented as this progresses.

Change-Id: I3e3f203e0705e561e1a3392e8f75dbe273d5fa81
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2861
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'web/converse/src/handlers.rs')
-rw-r--r--web/converse/src/handlers.rs53
1 files changed, 27 insertions, 26 deletions
diff --git a/web/converse/src/handlers.rs b/web/converse/src/handlers.rs
index c4448c748365..0759cec5c146 100644
--- a/web/converse/src/handlers.rs
+++ b/web/converse/src/handlers.rs
@@ -30,12 +30,14 @@ use actix_web::middleware::identity::RequestIdentity;
 use actix_web::middleware::{Started, Middleware};
 use actix_web;
 use crate::db::*;
-use crate::errors::ConverseError;
+use crate::errors::{ConverseResult, ConverseError};
 use futures::Future;
 use crate::models::*;
 use crate::oidc::*;
 use crate::render::*;
 
+use rouille::{Request, Response};
+
 type ConverseResponse = Box<dyn Future<Item=HttpResponse, Error=ConverseError>>;
 
 const HTML: &'static str = "text/html";
@@ -54,15 +56,14 @@ pub struct AppState {
     pub renderer: Addr<Renderer>,
 }
 
-pub fn forum_index(state: State<AppState>) -> ConverseResponse {
-    state.db.send(ListThreads)
-        .flatten()
-        .and_then(move |res| state.renderer.send(IndexPage {
-            threads: res
-        }).from_err())
-        .flatten()
-        .map(|res| HttpResponse::Ok().content_type(HTML).body(res))
-        .responder()
+/// 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!()
 }
 
 /// Returns the ID of the currently logged in user. If there is no ID
@@ -78,23 +79,23 @@ pub fn get_user_id(req: &HttpRequest<AppState>) -> i32 {
     }
 }
 
-/// This handler retrieves and displays a single forum thread.
-pub fn forum_thread(state: State<AppState>,
-                    req: HttpRequest<AppState>,
-                    thread_id: Path<i32>) -> ConverseResponse {
-    let id = thread_id.into_inner();
-    let user = get_user_id(&req);
+pub fn get_user_id_rouille(_req: &Request) -> i32 {
+    // TODO(tazjin): Implement session support in rouille somehow.
+    ANONYMOUS
+}
 
-    state.db.send(GetThread(id))
-        .flatten()
-        .and_then(move |res| state.renderer.send(ThreadPage {
-            current_user: user,
-            thread: res.0,
-            posts: res.1,
-        }).from_err())
-        .flatten()
-        .map(|res| HttpResponse::Ok().content_type(HTML).body(res))
-        .responder()
+pub fn forum_thread_rouille(req: &Request, db: &DbExecutor, thread_id: i32)
+                            -> ConverseResult<Response> {
+    let user = get_user_id_rouille(&req);
+    let thread = db.get_thread(thread_id)?;
+    Ok(Response::html(thread_page(user, thread.0, thread.1)?))
+}
+
+/// This handler retrieves and displays a single forum thread.
+pub fn forum_thread(_: State<AppState>,
+                    _: HttpRequest<AppState>,
+                    _: Path<i32>) -> ConverseResponse {
+    unimplemented!()
 }
 
 /// This handler presents the user with the "New Thread" form.