2023-02-05 11:18:51 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cgi"
|
2023-02-05 10:17:01 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::AnswerController < AjaxController
|
2023-10-18 11:01:59 -07:00
|
|
|
include SocialHelper
|
2023-02-05 10:17:01 -08:00
|
|
|
|
2015-01-03 09:40:39 -08:00
|
|
|
def create
|
|
|
|
params.require :id
|
|
|
|
params.require :answer
|
|
|
|
params.require :inbox
|
|
|
|
|
2023-02-05 11:18:51 -08:00
|
|
|
inbox = (params[:inbox] == "true")
|
2015-01-03 09:40:39 -08:00
|
|
|
|
|
|
|
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
|
2022-07-06 03:09:58 -07:00
|
|
|
@response[:message] = t(".error")
|
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
|
2022-07-06 03:09:58 -07:00
|
|
|
@response[:message] = t(".privacy")
|
2015-01-03 13:37:10 -08:00
|
|
|
return
|
|
|
|
end
|
2015-01-03 09:40:39 -08:00
|
|
|
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
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
2022-07-06 03:09:58 -07:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2023-02-05 10:17:01 -08:00
|
|
|
|
2023-02-19 11:58:47 -08:00
|
|
|
@response[:sharing] = sharing_hash(answer) if current_user.sharing_enabled
|
2023-02-05 10:17:01 -08:00
|
|
|
|
2023-02-05 11:18:51 -08:00
|
|
|
return if inbox
|
|
|
|
|
|
|
|
@question = 1
|
2023-12-09 09:05:50 -08:00
|
|
|
@response[:render] = render_to_string(partial: "answerbox", locals: { a: answer, show_question: false })
|
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
|
|
|
|
2023-02-05 11:18:51 -08:00
|
|
|
unless (current_user == answer.user) || (privileged? answer.user)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
2022-07-06 03:09:58 -07:00
|
|
|
@response[:message] = t(".nopriv")
|
2014-11-26 08:05:46 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-02-05 11:18:51 -08:00
|
|
|
Inbox.create!(user: answer.user, question: answer.question, new: true, returning: true) if answer.user == current_user
|
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
|
2022-07-06 03:09:58 -07:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2014-11-26 08:05:46 -08:00
|
|
|
end
|
2023-02-19 11:58:47 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sharing_hash(answer) = {
|
2023-10-18 11:01:59 -07:00
|
|
|
url: answer_share_url(answer),
|
|
|
|
text: prepare_tweet(answer, nil, true),
|
2023-02-19 11:58:47 -08:00
|
|
|
twitter: twitter_share_url(answer),
|
|
|
|
tumblr: tumblr_share_url(answer),
|
|
|
|
telegram: telegram_share_url(answer),
|
|
|
|
custom: CGI.escape(prepare_tweet(answer)),
|
|
|
|
}
|
2014-11-26 08:05:46 -08:00
|
|
|
end
|