about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-09T07·20+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-09T07·20+0200
commit7c73949066cf1b730a2c8c3408fa016048538716 (patch)
treea9cc78f0b8fb147c9f91ff8a9b8b20626065c3bc /src
parent64453ec6834f657de8c63276fb93ff8b87cf6017 (diff)
feat(handlers): Extract & add author to thread and post information
Diffstat (limited to 'src')
-rw-r--r--src/handlers.rs44
-rw-r--r--src/main.rs4
-rw-r--r--src/models.rs4
3 files changed, 46 insertions, 6 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index e709fdd2023c..c31cdf679bb7 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -81,10 +81,28 @@ pub fn forum_thread(state: State<AppState>, thread_id: Path<i32>) -> ConverseRes
         .responder()
 }
 
+#[derive(Deserialize)]
+pub struct NewThreadForm {
+    pub title: String,
+    pub body: String,
+}
+
 /// This handler receives a "New thread"-form and redirects the user
 /// to the new thread after creation.
-pub fn submit_thread(state: State<AppState>, input: Form<NewThread>) -> ConverseResponse {
-    state.db.send(CreateThread(input.0))
+pub fn submit_thread(state: State<AppState>,
+                     input: Form<NewThreadForm>,
+                     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 new_thread = NewThread {
+        title: input.0.title,
+        body: input.0.body,
+        author_name: author.name,
+        author_email: author.email,
+    };
+
+    state.db.send(CreateThread(new_thread))
         .from_err()
         .and_then(move |res| {
             let thread = res?;
@@ -96,10 +114,28 @@ pub fn submit_thread(state: State<AppState>, input: Form<NewThread>) -> Converse
         .responder()
 }
 
+#[derive(Deserialize)]
+pub struct NewPostForm {
+    pub thread_id: i32,
+    pub body: String,
+}
+
 /// This handler receives a "Reply"-form and redirects the user to the
 /// new post after creation.
-pub fn reply_thread(state: State<AppState>, input: Form<NewPost>) -> ConverseResponse {
-    state.db.send(CreatePost(input.0))
+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 new_post = NewPost {
+        thread_id: input.thread_id,
+        body: input.0.body,
+        author_name: author.name,
+        author_email: author.email,
+    };
+
+    state.db.send(CreatePost(new_post))
         .from_err()
         .and_then(move |res| {
             let post = res?;
diff --git a/src/main.rs b/src/main.rs
index 59e711ad4312..8400f570a9be 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -113,8 +113,8 @@ fn main() {
             .middleware(sessions)
             .middleware(RequireLogin)
             .resource("/", |r| r.method(Method::GET).with(forum_index))
-            .resource("/thread/submit", |r| r.method(Method::POST).with2(submit_thread))
-            .resource("/thread/reply", |r| r.method(Method::POST).with2(reply_thread))
+            .resource("/thread/submit", |r| r.method(Method::POST).with3(submit_thread))
+            .resource("/thread/reply", |r| r.method(Method::POST).with3(reply_thread))
             .resource("/thread/{id}", |r| r.method(Method::GET).with2(forum_thread))
             .resource("/oidc/login", |r| r.method(Method::GET).with(login))
             .resource("/oidc/callback", |r| r.method(Method::POST).with3(callback))})
diff --git a/src/models.rs b/src/models.rs
index daeccf852a67..388dab48534d 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -27,6 +27,8 @@ pub struct Post {
 pub struct NewThread {
     pub title: String,
     pub body: String,
+    pub author_name: String,
+    pub author_email: String,
 }
 
 #[derive(Deserialize, Insertable)]
@@ -34,4 +36,6 @@ pub struct NewThread {
 pub struct NewPost {
     pub thread_id: i32,
     pub body: String,
+    pub author_name: String,
+    pub author_email: String,
 }