|
1 | 1 | use chrono;
|
2 |
| -use futures; |
3 |
| -use futures::Future; |
4 | 2 | use my;
|
5 | 3 | use my::prelude::*;
|
| 4 | +use std::future::Future; |
6 | 5 | use trawler::{CommentId, StoryId, UserId};
|
7 | 6 |
|
8 |
| -pub(crate) fn handle<F>( |
| 7 | +pub(crate) async fn handle<F>( |
9 | 8 | c: F,
|
10 | 9 | acting_as: Option<UserId>,
|
11 | 10 | id: CommentId,
|
12 | 11 | story: StoryId,
|
13 | 12 | parent: Option<CommentId>,
|
14 |
| -) -> Box<dyn Future<Item = (my::Conn, bool), Error = my::error::Error> + Send> |
| 13 | +) -> Result<(my::Conn, bool), my::error::Error> |
15 | 14 | where
|
16 |
| - F: 'static + Future<Item = my::Conn, Error = my::error::Error> + Send, |
| 15 | + F: 'static + Future<Output = Result<my::Conn, my::error::Error>> + Send, |
17 | 16 | {
|
| 17 | + let c = c.await?; |
18 | 18 | let user = acting_as.unwrap();
|
19 |
| - Box::new( |
20 |
| - c.and_then(move |c| { |
21 |
| - c.first_exec::<_, _, my::Row>( |
22 |
| - "SELECT `stories`.* \ |
23 |
| - FROM `stories` \ |
24 |
| - WHERE `stories`.`short_id` = ?", |
25 |
| - (::std::str::from_utf8(&story[..]).unwrap(),), |
26 |
| - ) |
27 |
| - .map(|(c, story)| (c, story.unwrap())) |
28 |
| - }) |
29 |
| - .and_then(|(c, story)| { |
30 |
| - let author = story.get::<u32, _>("user_id").unwrap(); |
31 |
| - let id = story.get::<u32, _>("id").unwrap(); |
32 |
| - c.drop_exec( |
33 |
| - "SELECT `users`.* FROM `users` WHERE `users`.`id` = ?", |
34 |
| - (author,), |
35 |
| - ) |
36 |
| - .map(move |c| (c, id)) |
37 |
| - }) |
38 |
| - .and_then(move |(c, story)| { |
39 |
| - let fut = if let Some(parent) = parent { |
40 |
| - // check that parent exists |
41 |
| - futures::future::Either::A( |
42 |
| - c.first_exec::<_, _, my::Row>( |
43 |
| - "SELECT `comments`.* FROM `comments` \ |
44 |
| - WHERE `comments`.`story_id` = ? \ |
45 |
| - AND `comments`.`short_id` = ?", |
46 |
| - (story, ::std::str::from_utf8(&parent[..]).unwrap()), |
47 |
| - ) |
48 |
| - .map(move |(c, p)| { |
49 |
| - if let Some(p) = p { |
50 |
| - ( |
51 |
| - c, |
52 |
| - Some(( |
53 |
| - p.get::<u32, _>("id").unwrap(), |
54 |
| - p.get::<Option<u32>, _>("thread_id").unwrap(), |
55 |
| - )), |
56 |
| - ) |
57 |
| - } else { |
58 |
| - eprintln!( |
59 |
| - "failed to find parent comment {} in story {}", |
60 |
| - ::std::str::from_utf8(&parent[..]).unwrap(), |
61 |
| - story |
62 |
| - ); |
63 |
| - (c, None) |
64 |
| - } |
65 |
| - }), |
66 |
| - ) |
67 |
| - } else { |
68 |
| - futures::future::Either::B(futures::future::ok((c, None))) |
69 |
| - }; |
70 |
| - fut.map(move |(c, parent)| (c, story, parent)) |
71 |
| - }) |
72 |
| - .map(|c| { |
73 |
| - // TODO: real site checks for recent comments by same author with same |
74 |
| - // parent to ensure we don't double-post accidentally |
75 |
| - c |
76 |
| - }) |
77 |
| - .and_then(move |(c, story, parent)| { |
78 |
| - // check that short id is available |
79 |
| - c.drop_exec( |
80 |
| - "SELECT 1 AS one FROM `comments` \ |
81 |
| - WHERE `comments`.`short_id` = ?", |
82 |
| - (::std::str::from_utf8(&id[..]).unwrap(),), |
| 19 | + let (c, story) = c |
| 20 | + .first_exec::<_, _, my::Row>( |
| 21 | + "SELECT `stories`.* \ |
| 22 | + FROM `stories` \ |
| 23 | + WHERE `stories`.`short_id` = ?", |
| 24 | + (::std::str::from_utf8(&story[..]).unwrap(),), |
| 25 | + ) |
| 26 | + .await?; |
| 27 | + let story = story.unwrap(); |
| 28 | + let author = story.get::<u32, _>("user_id").unwrap(); |
| 29 | + let story = story.get::<u32, _>("id").unwrap(); |
| 30 | + c = c |
| 31 | + .drop_exec( |
| 32 | + "SELECT `users`.* FROM `users` WHERE `users`.`id` = ?", |
| 33 | + (author,), |
| 34 | + ) |
| 35 | + .await?; |
| 36 | + |
| 37 | + let parent = if let Some(parent) = parent { |
| 38 | + // check that parent exists |
| 39 | + let (x, p) = c |
| 40 | + .first_exec::<_, _, my::Row>( |
| 41 | + "SELECT `comments`.* FROM `comments` \ |
| 42 | + WHERE `comments`.`story_id` = ? \ |
| 43 | + AND `comments`.`short_id` = ?", |
| 44 | + (story, ::std::str::from_utf8(&parent[..]).unwrap()), |
83 | 45 | )
|
84 |
| - .map(move |c| (c, story, parent)) |
85 |
| - }) |
86 |
| - .and_then(move |(c, story, parent)| { |
87 |
| - // TODO: real impl checks *new* short_id *again* |
| 46 | + .await?; |
| 47 | + c = x; |
| 48 | + |
| 49 | + if let Some(p) = p { |
| 50 | + Some(( |
| 51 | + p.get::<u32, _>("id").unwrap(), |
| 52 | + p.get::<Option<u32>, _>("thread_id").unwrap(), |
| 53 | + )) |
| 54 | + } else { |
| 55 | + eprintln!( |
| 56 | + "failed to find parent comment {} in story {}", |
| 57 | + ::std::str::from_utf8(&parent[..]).unwrap(), |
| 58 | + story |
| 59 | + ); |
| 60 | + None |
| 61 | + } |
| 62 | + } else { |
| 63 | + None |
| 64 | + }; |
| 65 | + |
| 66 | + // TODO: real site checks for recent comments by same author with same |
| 67 | + // parent to ensure we don't double-post accidentally |
| 68 | + |
| 69 | + // check that short id is available |
| 70 | + c = c |
| 71 | + .drop_exec( |
| 72 | + "SELECT 1 AS one FROM `comments` \ |
| 73 | + WHERE `comments`.`short_id` = ?", |
| 74 | + (::std::str::from_utf8(&id[..]).unwrap(),), |
| 75 | + ) |
| 76 | + .await?; |
| 77 | + |
| 78 | + // TODO: real impl checks *new* short_id *again* |
| 79 | + |
| 80 | + // NOTE: MySQL technically does everything inside this and_then in a transaction, |
| 81 | + // but let's be nice to it |
| 82 | + let now = chrono::Local::now().naive_local(); |
| 83 | + let q = if let Some((parent, thread)) = parent { |
| 84 | + c.prep_exec( |
| 85 | + "INSERT INTO `comments` \ |
| 86 | + (`created_at`, `updated_at`, `short_id`, `story_id`, \ |
| 87 | + `user_id`, `parent_comment_id`, `thread_id`, \ |
| 88 | + `comment`, `markeddown_comment`) \ |
| 89 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 90 | + ( |
| 91 | + now, |
| 92 | + now, |
| 93 | + ::std::str::from_utf8(&id[..]).unwrap(), |
| 94 | + story, |
| 95 | + user, |
| 96 | + parent, |
| 97 | + thread, |
| 98 | + "moar benchmarking", // lorem ipsum? |
| 99 | + "<p>moar benchmarking</p>\n", |
| 100 | + ), |
| 101 | + ) |
| 102 | + .await? |
| 103 | + } else { |
| 104 | + c.prep_exec( |
| 105 | + "INSERT INTO `comments` \ |
| 106 | + (`created_at`, `updated_at`, `short_id`, `story_id`, \ |
| 107 | + `user_id`, `comment`, `markeddown_comment`) \ |
| 108 | + VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 109 | + ( |
| 110 | + now, |
| 111 | + now, |
| 112 | + ::std::str::from_utf8(&id[..]).unwrap(), |
| 113 | + story, |
| 114 | + user, |
| 115 | + "moar benchmarking", // lorem ipsum? |
| 116 | + "<p>moar benchmarking</p>\n", |
| 117 | + ), |
| 118 | + ) |
| 119 | + .await? |
| 120 | + }; |
| 121 | + let comment = q.last_insert_id().unwrap(); |
| 122 | + let c = q.drop_result().await?; |
| 123 | + // but why?! |
| 124 | + c = c |
| 125 | + .drop_exec( |
| 126 | + "SELECT `votes`.* FROM `votes` \ |
| 127 | + WHERE `votes`.`user_id` = ? \ |
| 128 | + AND `votes`.`story_id` = ? \ |
| 129 | + AND `votes`.`comment_id` = ?", |
| 130 | + (user, story, comment), |
| 131 | + ) |
| 132 | + .await?; |
| 133 | + c = c |
| 134 | + .drop_exec( |
| 135 | + "INSERT INTO `votes` \ |
| 136 | + (`user_id`, `story_id`, `comment_id`, `vote`) \ |
| 137 | + VALUES (?, ?, ?, ?)", |
| 138 | + (user, story, comment, 1), |
| 139 | + ) |
| 140 | + .await?; |
88 | 141 |
|
89 |
| - // NOTE: MySQL technically does everything inside this and_then in a transaction, |
90 |
| - // but let's be nice to it |
91 |
| - let now = chrono::Local::now().naive_local(); |
92 |
| - if let Some((parent, thread)) = parent { |
93 |
| - futures::future::Either::A(c.prep_exec( |
94 |
| - "INSERT INTO `comments` \ |
95 |
| - (`created_at`, `updated_at`, `short_id`, `story_id`, \ |
96 |
| - `user_id`, `parent_comment_id`, `thread_id`, \ |
97 |
| - `comment`, `markeddown_comment`) \ |
98 |
| - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
99 |
| - ( |
100 |
| - now, |
101 |
| - now, |
102 |
| - ::std::str::from_utf8(&id[..]).unwrap(), |
103 |
| - story, |
104 |
| - user, |
105 |
| - parent, |
106 |
| - thread, |
107 |
| - "moar benchmarking", // lorem ipsum? |
108 |
| - "<p>moar benchmarking</p>\n", |
109 |
| - ), |
110 |
| - )) |
111 |
| - } else { |
112 |
| - futures::future::Either::B(c.prep_exec( |
113 |
| - "INSERT INTO `comments` \ |
114 |
| - (`created_at`, `updated_at`, `short_id`, `story_id`, \ |
115 |
| - `user_id`, `comment`, `markeddown_comment`) \ |
116 |
| - VALUES (?, ?, ?, ?, ?, ?, ?)", |
117 |
| - ( |
118 |
| - now, |
119 |
| - now, |
120 |
| - ::std::str::from_utf8(&id[..]).unwrap(), |
121 |
| - story, |
122 |
| - user, |
123 |
| - "moar benchmarking", // lorem ipsum? |
124 |
| - "<p>moar benchmarking</p>\n", |
125 |
| - ), |
126 |
| - )) |
127 |
| - } |
128 |
| - .and_then(|q| { |
129 |
| - let comment = q.last_insert_id().unwrap(); |
130 |
| - q.drop_result().map(move |t| (t, comment)) |
131 |
| - }) |
132 |
| - .and_then(move |(t, comment)| { |
133 |
| - // but why?! |
134 |
| - t.drop_exec( |
135 |
| - "SELECT `votes`.* FROM `votes` \ |
136 |
| - WHERE `votes`.`user_id` = ? \ |
137 |
| - AND `votes`.`story_id` = ? \ |
138 |
| - AND `votes`.`comment_id` = ?", |
139 |
| - (user, story, comment), |
140 |
| - ) |
141 |
| - .map(move |t| (t, comment)) |
142 |
| - }) |
143 |
| - .and_then(move |(t, comment)| { |
144 |
| - t.drop_exec( |
145 |
| - "INSERT INTO `votes` \ |
146 |
| - (`user_id`, `story_id`, `comment_id`, `vote`) \ |
147 |
| - VALUES (?, ?, ?, ?)", |
148 |
| - (user, story, comment, 1), |
149 |
| - ) |
150 |
| - }) |
151 |
| - }) |
152 |
| - .map(|c| (c, false)), |
153 |
| - ) |
| 142 | + Ok((c, false)) |
154 | 143 | }
|
0 commit comments