eBooru/app/controllers/comments_controller.rb

149 lines
4.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class CommentsController < ApplicationController
2019-11-26 17:03:43 -05:00
respond_to :html, :json
before_action :member_only, except: %i[index search show for_post]
before_action :moderator_only, only: %i[unhide warning]
before_action :admin_only, only: %i[destroy]
before_action :ensure_lockdown_disabled, except: %i[index search show for_post]
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
skip_before_action :api_check
2013-03-19 08:10:10 -04:00
def index
2021-10-02 06:13:27 -04:00
if params[:group_by] == "comment"
2011-11-11 15:56:52 -05:00
index_by_comment
2011-01-13 18:16:39 -05:00
else
2011-11-11 15:56:52 -05:00
index_by_post
2011-01-13 18:16:39 -05:00
end
end
2013-03-19 08:10:10 -04:00
def search
end
2013-03-19 08:10:10 -04:00
2020-02-17 13:47:31 -05:00
def for_post
@post = Post.find(params[:id])
@comments = @post.comments
@comment_votes = CommentVote.for_comments_and_user(@comments.map(&:id), CurrentUser.id)
comment_html = render_to_string partial: 'comments/partials/show/comment', collection: @comments, locals: { post: @post }, formats: [:html]
respond_with do |format|
format.json do
render json: {html: comment_html, posts: deferred_posts}
end
end
2020-02-17 13:47:31 -05:00
end
2013-02-22 21:42:50 -05:00
def new
@comment = Comment.new(comment_params(:create))
respond_with(@comment)
2013-02-22 21:42:50 -05:00
end
2013-03-19 08:10:10 -04:00
def update
@comment = Comment.find(params[:id])
2022-12-23 10:35:37 -05:00
check_editable(@comment)
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
@comment.update(comment_params(:update))
2011-03-08 18:26:10 -05:00
respond_with(@comment, :location => post_path(@comment.post_id))
end
2013-03-19 08:10:10 -04:00
def create
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
@comment = Comment.create(comment_params(:create))
flash[:notice] = @comment.valid? ? "Comment posted" : @comment.errors.full_messages.join("; ")
respond_with(@comment) do |format|
format.html do
2023-12-03 09:36:37 -05:00
redirect_back fallback_location: @comment.post || comments_path
end
2011-01-13 18:16:39 -05:00
end
end
2013-03-19 08:10:10 -04:00
2011-03-08 18:26:10 -05:00
def edit
@comment = Comment.find(params[:id])
2022-12-23 10:35:37 -05:00
check_editable(@comment)
2011-03-08 18:26:10 -05:00
respond_with(@comment)
end
2013-03-19 08:10:10 -04:00
2011-09-13 18:41:50 -04:00
def show
@comment = Comment.find(params[:id])
check_visible(@comment)
@comment_votes = CommentVote.for_comments_and_user([@comment.id], CurrentUser.id)
respond_with(@comment)
2011-09-13 18:41:50 -04:00
end
2013-03-19 08:10:10 -04:00
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
respond_with(@comment)
end
def hide
@comment = Comment.find(params[:id])
2022-12-23 10:35:37 -05:00
check_hidable(@comment)
@comment.hide!
respond_with(@comment)
end
2013-03-19 08:10:10 -04:00
def unhide
2015-07-11 13:26:55 -04:00
@comment = Comment.find(params[:id])
2022-12-23 10:35:37 -05:00
check_hidable(@comment)
@comment.unhide!
respond_with(@comment)
2015-07-11 13:26:55 -04:00
end
def warning
@comment = Comment.find(params[:id])
if params[:record_type] == 'unmark'
@comment.remove_user_warning!
else
2022-09-26 14:15:35 -04:00
@comment.user_warned!(params[:record_type], CurrentUser.user)
end
2022-09-27 15:01:41 -04:00
@comment_votes = CommentVote.for_comments_and_user([@comment.id], CurrentUser.id)
html = render_to_string partial: "comments/partials/show/comment", locals: { comment: @comment, post: nil }, formats: [:html]
render json: { html: html, posts: deferred_posts }
end
2011-01-13 18:16:39 -05:00
private
def index_by_post
tags = params[:tags] || ""
@posts = Post.tag_match(tags + " order:comment_bumped").paginate(params[:page], :limit => 5, :search_count => params[:search])
comment_ids = @posts.flat_map {|post| post.comments.visible(CurrentUser.user).recent.reverse.map(&:id)} if CurrentUser.id
@comment_votes = CommentVote.for_comments_and_user(comment_ids || [], CurrentUser.id)
2019-11-26 17:16:06 -05:00
respond_with(@posts)
2011-01-13 18:16:39 -05:00
end
2013-03-19 08:10:10 -04:00
2011-01-13 18:16:39 -05:00
def index_by_comment
@comments = Comment.visible(CurrentUser.user)
@comments = @comments.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@comment_votes = CommentVote.for_comments_and_user(@comments.map(&:id), CurrentUser.id)
2021-10-02 06:13:27 -04:00
respond_with(@comments)
end
2013-03-19 08:10:10 -04:00
2022-12-23 10:35:37 -05:00
def check_editable(comment)
raise User::PrivilegeError unless comment.editable_by?(CurrentUser.user)
end
def check_visible(comment)
2022-12-23 10:35:37 -05:00
raise User::PrivilegeError unless comment.visible_to?(CurrentUser.user)
end
def check_hidable(comment)
raise User::PrivilegeError unless comment.can_hide?(CurrentUser.user)
end
def search_params
permitted_params = %i[body_matches post_id post_tags_match creator_name creator_id post_note_updater_name post_note_updater_id poster_id poster_name is_sticky do_not_bump_post order]
permitted_params += %i[is_hidden] if CurrentUser.is_moderator?
permitted_params += %i[ip_addr] if CurrentUser.is_admin?
permit_search_params permitted_params
end
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
def comment_params(context)
permitted_params = %i[body]
permitted_params += %i[do_not_bump_post post_id] if context == :create
2024-01-22 11:52:48 -05:00
permitted_params += %i[is_sticky] if CurrentUser.is_janitor?
permitted_params += %i[is_hidden] if CurrentUser.is_moderator?
params.fetch(:comment, {}).permit(permitted_params)
end
def ensure_lockdown_disabled
access_denied if Security::Lockdown.comments_disabled? && !CurrentUser.is_staff?
end
end