summaryrefslogtreecommitdiff
path: root/app/controllers/posts/comments_controller.rb
blob: b91f69001c677becf4d0db084f68afe21308c9a9 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true

class Posts::CommentsController < ApplicationController
  include RateLimitable

  before_action :load_post
  before_action :check_auth
  before_action :load_comment, only: [:show, :edit, :update, :destroy]
  before_action :filter_banned_users, only: [:create, :edit, :update]

  before_action -> { ratelimit 1, 30.seconds, t('booru.errors.comment_flooding', seconds: 30) }, only: [:create], unless: :user_signed_in?

  skip_authorization_check only: [:show]

  def index
    respond_to do |format|
      @comments = CommentQuery.paginate(post: @post, current_user: current_user, page: params[:page], comment_id: params[:comment_id])
      params[:comment_id] = nil
      format.html { render partial: 'posts/post_comments', layout: false, locals: { comments: @comments, post: @post } }
      format.json { render json: { comments: @comments, total: @post.comments_count } }
    end
  end

  def create
    @comment = @post.comments.new(comment_params.merge(request_attributes))
    @comment.name_at_post_time = current_user.name if current_user

    authorize! :create, @comment

    Notification.watch(@post, current_user) if current_user && current_user.watch_on_reply

    @post.update_index

    if @comment.save
      inc_user_stat :comments_posted
      # NewCommentLogger.log(@comment)
      respond_to do |format|
        format.html do
          flash[:notice] = t('comments.create.success')
          redirect_to post_path(@post, anchor: "comment_#{@comment.id}")
        end
        format.js do
          params[:comment]    = nil
          params[:comment_id] = @comment.id
          @comments = CommentQuery.paginate(post: @post, current_user: current_user, page: nil, comment_id: params[:comment_id])
          render partial: 'posts/post_comments', locals: { post: @post, comments: @comments, comment: @comment }, layout: false
        end
      end
      return
    else
      flash[:error] = t('comments.create.error')
    end

    respond_to do |format|
      format.html { redirect_to post_path(@post) }
      format.js   { head 400 }
    end
  end

  def show
    respond_to do |format|
      format.json { render json: @comment }
      format.all  { render layout: false }
    end
  end

  def edit
    authorize! :edit, @comment
    @title = t('comments.edit.title', target: @comment.parent_title)
  end

  def update
    authorize! :edit, @comment
    @comment.paper_trail.save_with_version if @comment.versions.count < 1
    if @comment.update(params.require(:comment).permit(:body, :edit_reason).merge(edited_at: Time.zone.now))
      @comment.update_index
      # CommentEditLogger.log(@comment, current_user)
      respond_to do |format|
        format.html { redirect_to post_path(@post, anchor: "comment_#{@comment.id}"), flash: { notice: t('comments.update.success') } }
        format.json { render head: :ok }
      end
    else
      respond_to do |format|
        format.html { render :edit }
        format.json { render head: :error }
      end
    end
  end

  def destroy
    authorize! :destroy, @comment

    CommentDestroyer.new(@comment).save
    flash[:notice] = t('comments.destroy.content_destroyed')

    redirect_to post_path(@post, anchor: "comment_#{@comment.id}")
  end

  private

  def load_post
    @post = Post.find(params[:post_id])
  end

  def load_comment
    @comment = Comment.find(params[:id])
  end

  def check_auth
    authorize! :read, @post
  end

  def comment_params
    cmt_params = params.require(:comment).permit(:body, :anonymous, :show_staff_tag)

    if force_anonymous?
      cmt_params = cmt_params.merge(anonymous: true)
    end

    # Only allow staff tag to be shown if current user is staff.
    unless current_user&.staff?
      cmt_params = cmt_params.merge(show_staff_tag: false)
    end

    cmt_params
  end
end