2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::CommentController < AjaxController
|
2014-12-07 05:31:20 -08:00
|
|
|
def create
|
|
|
|
params.require :answer
|
|
|
|
params.require :comment
|
|
|
|
|
|
|
|
answer = Answer.find(params[:answer])
|
|
|
|
|
|
|
|
begin
|
|
|
|
current_user.comment(answer, params[:comment])
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :rec_inv
|
2022-07-06 00:56:34 -07:00
|
|
|
@response[:message] = t(".invalid")
|
2014-12-07 05:31:20 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-03-11 10:48:04 -08:00
|
|
|
comments = Comment.where(answer:).includes([{user: :profile}, :smiles])
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
2022-07-06 00:56:34 -07:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2023-03-11 10:48:04 -08:00
|
|
|
@response[:render] = render_to_string(partial: 'answerbox/comments', locals: { a: answer, comments: })
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:count] = answer.comment_count
|
2014-12-07 05:31:20 -08:00
|
|
|
end
|
2014-12-28 12:14:01 -08:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require :comment
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2014-12-28 12:14:01 -08:00
|
|
|
comment = Comment.find(params[:comment])
|
|
|
|
|
|
|
|
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
2022-07-06 00:56:34 -07:00
|
|
|
@response[:message] = t(".nopriv")
|
2014-12-28 12:14:01 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:count] = comment.answer.comment_count - 1
|
2014-12-28 12:14:01 -08:00
|
|
|
comment.destroy
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
2022-07-06 00:56:34 -07:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2014-12-28 12:14:01 -08:00
|
|
|
end
|
2014-12-07 05:31:20 -08:00
|
|
|
end
|