|
| 1 | +var PostsDAO = require('../posts').PostsDAO |
| 2 | + , sanitize = require('validator').sanitize; // Helper to sanitize form input |
| 3 | + |
| 4 | +/* The ContentHandler must be constructed with a connected db */ |
| 5 | +function ContentHandler (db) { |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + var posts = new PostsDAO(db); |
| 9 | + |
| 10 | + this.displayMainPage = function(req, res, next) { |
| 11 | + "use strict"; |
| 12 | + |
| 13 | + posts.getPosts(10, function(err, results) { |
| 14 | + "use strict"; |
| 15 | + |
| 16 | + if (err) return next(err); |
| 17 | + |
| 18 | + return res.render('blog_template', { |
| 19 | + title: 'blog homepage', |
| 20 | + username: req.username, |
| 21 | + myposts: results |
| 22 | + }); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + this.displayMainPageByTag = function(req, res, next) { |
| 27 | + "use strict"; |
| 28 | + |
| 29 | + var tag = req.params.tag; |
| 30 | + |
| 31 | + posts.getPostsByTag(tag, 10, function(err, results) { |
| 32 | + "use strict"; |
| 33 | + |
| 34 | + if (err) return next(err); |
| 35 | + |
| 36 | + return res.render('blog_template', { |
| 37 | + title: 'blog homepage', |
| 38 | + username: req.username, |
| 39 | + myposts: results |
| 40 | + }); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + this.displayPostByPermalink = function(req, res, next) { |
| 45 | + "use strict"; |
| 46 | + |
| 47 | + var permalink = req.params.permalink; |
| 48 | + |
| 49 | + posts.getPostByPermalink(permalink, function(err, post) { |
| 50 | + "use strict"; |
| 51 | + |
| 52 | + if (err) return next(err); |
| 53 | + |
| 54 | + if (!post) return res.redirect("/post_not_found"); |
| 55 | + |
| 56 | + // init comment form fields for additional comment |
| 57 | + var comment = {'name': req.username, 'body': "", 'email': ""} |
| 58 | + |
| 59 | + return res.render('entry_template', { |
| 60 | + title: 'blog post', |
| 61 | + username: req.username, |
| 62 | + post: post, |
| 63 | + comment: comment, |
| 64 | + errors: "" |
| 65 | + }); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + this.handleNewComment = function(req, res, next) { |
| 70 | + "use strict"; |
| 71 | + var name = req.body.commentName; |
| 72 | + var email = req.body.commentEmail; |
| 73 | + var body = req.body.commentBody; |
| 74 | + var permalink = req.body.permalink; |
| 75 | + |
| 76 | + // Override the comment with our actual user name if found |
| 77 | + if (req.username) { |
| 78 | + name = req.username; |
| 79 | + } |
| 80 | + |
| 81 | + if (!name || !body) { |
| 82 | + // user did not fill in enough information |
| 83 | + |
| 84 | + posts.getPostByPermalink(permalink, function(err, post) { |
| 85 | + "use strict"; |
| 86 | + |
| 87 | + if (err) return next(err); |
| 88 | + |
| 89 | + if (!post) return res.redirect("/post_not_found"); |
| 90 | + |
| 91 | + // init comment form fields for additional comment |
| 92 | + var comment = {'name': name, 'body': "", 'email': ""} |
| 93 | + |
| 94 | + var errors = "Post must contain your name and an actual comment." |
| 95 | + return res.render('entry_template', { |
| 96 | + title: 'blog post', |
| 97 | + username: req.username, |
| 98 | + post: post, |
| 99 | + comment: comment, |
| 100 | + errors: errors |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // even if there is no logged in user, we can still post a comment |
| 108 | + posts.addComment(permalink, name, email, body, function(err, updated) { |
| 109 | + "use strict"; |
| 110 | + |
| 111 | + if (err) return next(err); |
| 112 | + |
| 113 | + if (updated == 0) return res.redirect("/post_not_found"); |
| 114 | + |
| 115 | + return res.redirect("/post/" + permalink); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + this.displayPostNotFound = function(req, res, next) { |
| 120 | + "use strict"; |
| 121 | + return res.send('Sorry, post not found', 404); |
| 122 | + } |
| 123 | + |
| 124 | + this.displayNewPostPage = function(req, res, next) { |
| 125 | + "use strict"; |
| 126 | + |
| 127 | + if (!req.username) return res.redirect("/login"); |
| 128 | + |
| 129 | + return res.render('newpost_template', { |
| 130 | + subject: "", |
| 131 | + body: "", |
| 132 | + errors: "", |
| 133 | + tags: "", |
| 134 | + username: req.username |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + function extract_tags(tags) { |
| 139 | + "use strict"; |
| 140 | + |
| 141 | + var cleaned = []; |
| 142 | + |
| 143 | + var tags_array = tags.split(','); |
| 144 | + |
| 145 | + for (var i = 0; i < tags_array.length; i++) { |
| 146 | + if ((cleaned.indexOf(tags_array[i]) == -1) && tags_array[i] != "") { |
| 147 | + cleaned.push(tags_array[i].replace(/\s/g,'')); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return cleaned |
| 152 | + } |
| 153 | + |
| 154 | + this.handleNewPost = function(req, res, next) { |
| 155 | + "use strict"; |
| 156 | + |
| 157 | + var title = req.body.subject |
| 158 | + var post = req.body.body |
| 159 | + var tags = req.body.tags |
| 160 | + |
| 161 | + if (!req.username) return res.redirect("/signup"); |
| 162 | + |
| 163 | + if (!title || !post) { |
| 164 | + var errors = "Post must contain a title and blog entry"; |
| 165 | + return res.render("newpost_template", {subject:title, username:req.username, body:post, tags:tags, errors:errors}); |
| 166 | + } |
| 167 | + |
| 168 | + var tags_array = extract_tags(tags) |
| 169 | + |
| 170 | + // looks like a good entry, insert it escaped |
| 171 | + var escaped_post = sanitize(post).escape(); |
| 172 | + |
| 173 | + // substitute some <br> for the paragraph breaks |
| 174 | + var formatted_post = escaped_post.replace(/\r?\n/g,'<br>'); |
| 175 | + |
| 176 | + posts.insertEntry(title, formatted_post, tags_array, req.username, function(err, permalink) { |
| 177 | + "use strict"; |
| 178 | + |
| 179 | + if (err) return next(err); |
| 180 | + |
| 181 | + // now redirect to the blog permalink |
| 182 | + return res.redirect("/post/" + permalink) |
| 183 | + }); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +module.exports = ContentHandler; |
0 commit comments