2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
class CommentsController < ApplicationController
|
2019-11-26 17:03:43 -05:00
|
|
|
respond_to :html, :json
|
2023-10-15 12:09:54 -04:00
|
|
|
before_action :member_only, except: %i[index search show for_post]
|
2024-12-08 22:28:31 -05:00
|
|
|
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]
|
2018-04-02 13:51:26 -04:00
|
|
|
skip_before_action :api_check
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05: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
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-03-28 18:48:02 -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)
|
2022-11-24 19:58:55 -05:00
|
|
|
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
|
2018-09-29 13:16:29 -04:00
|
|
|
@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
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def update
|
2010-04-29 17:32:15 -04:00
|
|
|
@comment = Comment.find(params[:id])
|
2022-12-23 10:35:37 -05:00
|
|
|
check_editable(@comment)
|
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))
|
2010-04-29 17:32:15 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-04-29 17:32:15 -04:00
|
|
|
def create
|
2018-04-02 13:51:26 -04:00
|
|
|
@comment = Comment.create(comment_params(:create))
|
2018-04-19 00:58:35 -04:00
|
|
|
flash[:notice] = @comment.valid? ? "Comment posted" : @comment.errors.full_messages.join("; ")
|
2010-04-29 17:32:15 -04:00
|
|
|
respond_with(@comment) do |format|
|
|
|
|
format.html do
|
2023-12-03 09:36:37 -05:00
|
|
|
redirect_back fallback_location: @comment.post || comments_path
|
2010-04-29 17:32:15 -04:00
|
|
|
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])
|
2020-05-20 21:32:31 -04:00
|
|
|
check_visible(@comment)
|
2019-04-10 20:21:27 -04:00
|
|
|
@comment_votes = CommentVote.for_comments_and_user([@comment.id], CurrentUser.id)
|
2018-09-29 13:16:29 -04:00
|
|
|
respond_with(@comment)
|
2011-09-13 18:41:50 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-14 12:52:49 -04:00
|
|
|
def destroy
|
2019-09-05 08:35:27 -04:00
|
|
|
@comment = Comment.find(params[:id])
|
|
|
|
@comment.destroy
|
|
|
|
respond_with(@comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def hide
|
2011-09-14 12:52:49 -04:00
|
|
|
@comment = Comment.find(params[:id])
|
2022-12-23 10:35:37 -05:00
|
|
|
check_hidable(@comment)
|
2019-09-05 08:35:27 -04:00
|
|
|
@comment.hide!
|
2018-09-29 13:16:29 -04:00
|
|
|
respond_with(@comment)
|
2011-09-14 12:52:49 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-09-05 08:35:27 -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)
|
2019-09-05 08:35:27 -04:00
|
|
|
@comment.unhide!
|
2018-09-29 13:16:29 -04:00
|
|
|
respond_with(@comment)
|
2015-07-11 13:26:55 -04:00
|
|
|
end
|
|
|
|
|
2021-04-05 01:06:06 -04:00
|
|
|
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)
|
2021-04-05 01:06:06 -04:00
|
|
|
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 }
|
2021-04-05 01:06:06 -04:00
|
|
|
end
|
|
|
|
|
2011-01-13 18:16:39 -05:00
|
|
|
private
|
|
|
|
def index_by_post
|
2019-03-28 17:04:01 -04:00
|
|
|
tags = params[:tags] || ""
|
2020-02-01 10:45:17 -05:00
|
|
|
@posts = Post.tag_match(tags + " order:comment_bumped").paginate(params[:page], :limit => 5, :search_count => params[:search])
|
2019-04-10 20:21:27 -04:00
|
|
|
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
|
2024-01-22 13:26:00 -05:00
|
|
|
@comments = Comment.visible(CurrentUser.user)
|
2020-05-20 21:20:41 -04:00
|
|
|
@comments = @comments.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
|
2019-04-10 20:21:27 -04:00
|
|
|
@comment_votes = CommentVote.for_comments_and_user(@comments.map(&:id), CurrentUser.id)
|
2021-10-02 06:13:27 -04:00
|
|
|
respond_with(@comments)
|
2010-03-10 18:21:43 -05:00
|
|
|
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)
|
2011-09-14 12:52:49 -04:00
|
|
|
end
|
2016-12-26 23:24:01 -05:00
|
|
|
|
2020-05-20 21:32:31 -04:00
|
|
|
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)
|
2020-05-20 21:32:31 -04:00
|
|
|
end
|
|
|
|
|
2021-10-30 13:46:55 -04:00
|
|
|
def search_params
|
2024-02-14 10:10:45 -05:00
|
|
|
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]
|
2022-11-28 10:21:40 -05:00
|
|
|
permitted_params += %i[is_hidden] if CurrentUser.is_moderator?
|
|
|
|
permitted_params += %i[ip_addr] if CurrentUser.is_admin?
|
2022-02-06 09:19:08 -05:00
|
|
|
permit_search_params permitted_params
|
2021-10-30 13:46:55 -04:00
|
|
|
end
|
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
def comment_params(context)
|
2022-01-09 11:53:17 -05:00
|
|
|
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?
|
2016-12-26 23:24:01 -05:00
|
|
|
|
2018-09-29 15:24:17 -04:00
|
|
|
params.fetch(:comment, {}).permit(permitted_params)
|
2016-12-26 23:24:01 -05:00
|
|
|
end
|
2024-12-08 22:28:31 -05:00
|
|
|
|
|
|
|
def ensure_lockdown_disabled
|
|
|
|
access_denied if Security::Lockdown.comments_disabled? && !CurrentUser.is_staff?
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|