about summary refs log tree commit diff stats
path: root/app/controllers/admin/comments_controller.rb
blob: 4d9502d4513afd3e5add15d5ae29e5514cd8d255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Admin::CommentsController < Admin::AdminController
  before_action :set_section

  def index
    @comments = Comment.where(status: :published).order(updated_at: :desc).paginate(page: params[:page], per_page: 20)
  end

  def pending
    @comments = Comment.where(status: :pending).order(updated_at: :desc).paginate(page: params[:page], per_page: 20)
  end

  def accept
    @comment = Comment.find(params[:id])
    @comment.status = :published
    @comment.save!

    if @comment.reply_to and @comment.reply_to.email != @comment.blog.user.email and @comment.reply_to.email != current_user.email
      CommentMailer.with(comment: @comment).reply_comment_email.deliver_later
    end

    flash.notice = "Comment successfully published."
    redirect_to pending_admin_comments_url
  end

  def reject
    @comment = Comment.find(params[:id])
    @comment.status = :rejected
    @comment.save!

    flash.notice = "Comment successfully rejected."
    redirect_to pending_admin_comments_url
  end

  def mark_spam
    @comment = Comment.find(params[:id])
    perform_mark_spam(@comment)

    flash.notice = "Comment successfully marked as spam."
    redirect_back fallback_location: pending_admin_comments_url
  end

  def destroy
    if Comment.destroy(params[:id])
      flash.notice = "Comment successfully deleted."
    else
      flash.alert = "Could not delete comment."
    end

    redirect_to admin_comments_url
  end

  def mass
    if params[:mass_action] == "Delete"
      Comment.destroy_by(id: params[:comment_ids])

      flash.notice = "Comments successfully deleted."
    elsif params[:mass_action] == "Mark Spam"
      @comments = Comment.find(params[:comment_ids])

      @comments.each do |comment|
        perform_mark_spam(comment)
      end

      flash.notice = "Comments successfully marked as spam."
    end

    redirect_back fallback_location: pending_admin_comments_url
  end

  private

    def set_section
      @section = "comments"
    end

    def perform_mark_spam(comment)
      akismet_params = {
        type: "comment",
        text: comment.body,
        created_at: comment.created_at,
        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
      }

      Akismet.spam comment.request_ip, comment.user_agent, akismet_params

      comment.destroy!
    end
end