diff options
Diffstat (limited to 'app/controllers/comments_controller.rb')
| -rw-r--r-- | app/controllers/comments_controller.rb | 81 |
1 files changed, 81 insertions, 0 deletions
| diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..31fe411 --- /dev/null +++ b/app/controllers/comments_controller.rb | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | class CommentsController < ApplicationController | ||
| 2 | def create | ||
| 3 | @blog = Blog.find_by_slug(params[:slug]) | ||
| 4 | |||
| 5 | raise ActiveRecord::RecordNotFound unless @blog | ||
| 6 | raise ActiveRecord::RecordNotFound unless @blog.published | ||
| 7 | |||
| 8 | unless verify_recaptcha | ||
| 9 | flash.alert = "Error posting comment." | ||
| 10 | render "blogs/show" | ||
| 11 | return | ||
| 12 | end | ||
| 13 | |||
| 14 | @comment = @blog.comments.new(comment_params) | ||
| 15 | @comment.request_ip = request.ip | ||
| 16 | @comment.user_agent = request.user_agent | ||
| 17 | @comment.referrer = request.referrer | ||
| 18 | |||
| 19 | unless @comment.valid? | ||
| 20 | flash.alert = "Error posting comment." | ||
| 21 | render "blogs/show" | ||
| 22 | return | ||
| 23 | end | ||
| 24 | |||
| 25 | akismet_vars = %w{ HTTP_ACCEPT HTTP_ACCEPT_ENCODING } | ||
| 26 | akismet_params = { | ||
| 27 | type: "comment", | ||
| 28 | text: @comment.body, | ||
| 29 | created_at: DateTime.now, | ||
| 30 | author: @comment.username, | ||
| 31 | author_email: @comment.email, | ||
| 32 | author_url: @comment.website, | ||
| 33 | post_url: url_for(@comment.blog), | ||
| 34 | post_modified_at: @comment.blog.updated_at, | ||
| 35 | referrer: @comment.referrer, | ||
| 36 | env: request.env.slice(*akismet_vars) | ||
| 37 | } | ||
| 38 | |||
| 39 | is_spam, is_blatant = Akismet.check(@comment.request_ip, @comment.user_agent, akismet_params) | ||
| 40 | |||
| 41 | if is_blatant | ||
| 42 | # I am lying. | ||
| 43 | flash.notice = "Comment submitted successfully! It will need to be moderated before it shows up on the blog." | ||
| 44 | |||
| 45 | Global.increment_filtered_comments | ||
| 46 | else | ||
| 47 | if is_spam | ||
| 48 | @comment.status = :pending | ||
| 49 | flash_message = "Comment submitted successfully! It will need to be moderated before it shows up on the blog." | ||
| 50 | else | ||
| 51 | @comment.status = :published | ||
| 52 | flash_message = "Comment posted successfully!" | ||
| 53 | end | ||
| 54 | |||
| 55 | if @comment.save | ||
| 56 | flash.notice = flash_message | ||
| 57 | |||
| 58 | if @comment.status == :published | ||
| 59 | CommentMailer.with(comment: @comment).new_comment_email.deliver_later | ||
| 60 | |||
| 61 | if @comment.reply_to and @comment.reply_to.email != @comment.blog.user.email | ||
| 62 | CommentMailer.with(comment: @comment).reply_comment_email.deliver_later | ||
| 63 | end | ||
| 64 | else | ||
| 65 | # CommentMailer.with(comment: @comment).new_pending_comment_email.deliver_later | ||
| 66 | # I'm disabling pending comment emails, at least for now, because I am getting too many. | ||
| 67 | end | ||
| 68 | else | ||
| 69 | flash.alert = "Error posting comment." | ||
| 70 | end | ||
| 71 | end | ||
| 72 | |||
| 73 | redirect_to @comment.blog | ||
| 74 | end | ||
| 75 | |||
| 76 | private | ||
| 77 | |||
| 78 | def comment_params | ||
| 79 | params.require(:comment).permit(:username, :email, :website, :body, :reply_to_id) | ||
| 80 | end | ||
| 81 | end | ||
