2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::AnswerController < AjaxController
|
2015-01-03 09:40:39 -08:00
|
|
|
def create
|
|
|
|
params.require :id
|
|
|
|
params.require :answer
|
|
|
|
params.require :share
|
|
|
|
params.require :inbox
|
|
|
|
|
|
|
|
inbox = (params[:inbox] == 'true')
|
|
|
|
|
|
|
|
if inbox
|
|
|
|
inbox_entry = Inbox.find(params[:id])
|
|
|
|
|
|
|
|
unless current_user == inbox_entry.user
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :fail
|
|
|
|
@response[:message] = I18n.t('messages.answer.create.fail')
|
2015-01-03 09:40:39 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
question = Question.find(params[:id])
|
2015-01-03 13:37:10 -08:00
|
|
|
|
|
|
|
unless question.user.privacy_allow_stranger_answers
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :privacy_stronk
|
|
|
|
@response[:message] = I18n.t('messages.answer.create.privacy_stronk')
|
2015-01-03 13:37:10 -08:00
|
|
|
return
|
|
|
|
end
|
2015-01-03 09:40:39 -08:00
|
|
|
end
|
|
|
|
|
2015-05-08 16:31:31 -07:00
|
|
|
# this should never trigger because empty params throw ParameterMissing
|
|
|
|
unless params[:answer].length > 0
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :peter_dinklage
|
|
|
|
@response[:message] = I18n.t('messages.answer.create.peter_dinklage')
|
2015-05-08 16:31:31 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-11 14:15:24 -07:00
|
|
|
answer = if inbox
|
|
|
|
inbox_entry.answer params[:answer], current_user
|
|
|
|
else
|
|
|
|
current_user.answer question, params[:answer]
|
|
|
|
end
|
2015-01-03 09:40:39 -08:00
|
|
|
|
|
|
|
services = JSON.parse params[:share]
|
2021-12-25 04:40:21 -08:00
|
|
|
services.each do |service|
|
|
|
|
ShareWorker.perform_async(current_user.id, answer.id, service)
|
|
|
|
end
|
2015-01-03 09:40:39 -08:00
|
|
|
|
2015-01-03 10:14:26 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.answer.create.okay')
|
|
|
|
@response[:success] = true
|
2015-01-03 10:14:26 -08:00
|
|
|
unless inbox
|
2020-04-28 11:27:59 -07:00
|
|
|
# this assign is needed because shared/_answerbox relies on it, I think
|
2015-01-03 10:14:26 -08:00
|
|
|
@question = 1
|
2020-04-29 04:33:53 -07:00
|
|
|
@response[:render] = render_to_string(partial: 'answerbox', locals: { a: answer, show_question: false })
|
2015-01-03 10:14:26 -08:00
|
|
|
end
|
2015-01-03 09:40:39 -08:00
|
|
|
end
|
|
|
|
|
2014-11-26 08:05:46 -08:00
|
|
|
def destroy
|
|
|
|
params.require :answer
|
|
|
|
|
|
|
|
answer = Answer.find(params[:answer])
|
2014-12-28 12:14:01 -08:00
|
|
|
|
|
|
|
unless (current_user == answer.user) or (privileged? answer.user)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
|
|
|
@response[:message] = I18n.t('messages.answer.destroy.nopriv')
|
2014-11-26 08:05:46 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-11-28 05:09:12 -08:00
|
|
|
if answer.user == current_user
|
|
|
|
Inbox.create!(user: answer.user, question: answer.question, new: true)
|
|
|
|
end # TODO: decide what happens with the question
|
2014-12-28 12:34:42 -08:00
|
|
|
answer.destroy
|
2014-11-26 08:05:46 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.answer.destroy.okay')
|
|
|
|
@response[:success] = true
|
2014-11-26 08:05:46 -08:00
|
|
|
end
|
|
|
|
end
|