about summary refs log tree commit diff stats
path: root/.gitignore
blob: a7cadc7da8f501ee355f89fcdb69d6bfcb8c287e (plain) (blame)
1
2
3
4
5
build/
builds/
assets/LL1.yaml
.DS_Store
.vs
*/ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
class CommentsController < ApplicationController
  def create
    @blog = Blog.find_by_slug(params[:slug])

    raise ActiveRecord::RecordNotFound unless @blog
    raise ActiveRecord::RecordNotFound unless @blog.published

    @comment = @blog.comments.new(comment_params)
    @comment.request_ip = request.ip
    @comment.user_agent = request.user_agent
    @comment.referrer = request.referrer

    unless @comment.valid?
      flash.alert = "Error posting comment."
      render "blogs/show"
      return
    end

    akismet_vars = %w{ HTTP_ACCEPT HTTP_ACCEPT_ENCODING }
    akismet_params = {
      type: "comment",
      text: @comment.body,
      created_at: DateTime.now,
      author: @comment.username,
      author_email: @comment.email,
      author_url: @comment.website,
      post_url: url_for(@comment.blog),
      post_modified_at: @comment.blog.updated_at,
      referrer: @comment.referrer,
      env: request.env.slice(*akismet_vars)
    }

    is_spam, is_blatant = Akismet.check(@comment.request_ip, @comment.user_agent, akismet_params)

    if is_blatant
      # I am lying.
      flash.notice = "Comment submitted successfully! It will need to be moderated before it shows up on the blog."

      Global.increment_filtered_comments
    else
      if is_spam
        @comment.status = :pending
        flash_message = "Comment submitted successfully! It will need to be moderated before it shows up on the blog."
      else
        @comment.status = :published
        flash_message = "Comment posted successfully!"
      end

      if @comment.save
        flash.notice = flash_message

        if @comment.status == :published
          CommentMailer.with(comment: @comment).new_comment_email.deliver_later

          if @comment.reply_to and @comment.reply_to.email != @comment.blog.user.email
            CommentMailer.with(comment: @comment).reply_comment_email.deliver_later
          end
        else
          # CommentMailer.with(comment: @comment).new_pending_comment_email.deliver_later
          # I'm disabling pending comment emails, at least for now, because I am getting too many.
        end
      else
        flash.alert = "Error posting comment."
      end
    end

    redirect_to @comment.blog
  end

  private

    def comment_params
      params.require(:comment).permit(:username, :email, :website, :body, :reply_to_id)
    end
end