about summary refs log tree commit diff
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-11T23·28+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-11T23·28+0200
commitbf02c70f74ea13452d9ee129ad047075e64cdfd6 (patch)
tree5b6a6f4f4f22cf5bd096935f238e3c37280f7294 /src/handlers.rs
parente7a54a5affd40f92f788f46ab64033d14860959a (diff)
feat(handlers/main): Add 'anonymous' mode for forum
Adds a `REQUIRE_LOGIN` environment variable which, if set to anything
other than true, will let users post anonymously on the forums.
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index 0848740bc1..c97e677311 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -64,6 +64,15 @@ pub fn new_thread(state: State<AppState>) -> ConverseResponse {
         .responder()
 }
 
+/// This function provides an anonymous "default" author if logins are
+/// not required.
+fn anonymous() -> Author {
+    Author {
+        name: "Anonymous".into(),
+        email: "anonymous@nothing.org".into(),
+    }
+}
+
 #[derive(Deserialize)]
 pub struct NewThreadForm {
     pub title: String,
@@ -90,9 +99,10 @@ pub fn submit_thread(state: State<AppState>,
             .responder();
     }
 
-    // Author is "unwrapped" because the RequireLogin middleware
-    // guarantees it to be present.
-    let author: Author = req.session().get(AUTHOR).unwrap().unwrap();
+    let author: Author = req.session().get(AUTHOR)
+        .unwrap_or_else(|_| Some(anonymous()))
+        .unwrap_or_else(anonymous);
+
     let new_thread = NewThread {
         title: input.0.title,
         body: input.0.body,
@@ -123,9 +133,10 @@ pub struct NewPostForm {
 pub fn reply_thread(state: State<AppState>,
                     input: Form<NewPostForm>,
                     mut req: HttpRequest<AppState>) -> ConverseResponse {
-    // Author is "unwrapped" because the RequireLogin middleware
-    // guarantees it to be present.
-    let author: Author = req.session().get(AUTHOR).unwrap().unwrap();
+    let author: Author = req.session().get(AUTHOR)
+        .unwrap_or_else(|_| Some(anonymous()))
+        .unwrap_or_else(anonymous);
+
     let new_post = NewPost {
         thread_id: input.thread_id,
         body: input.0.body,