2023-02-05 20:18:51 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cgi"
|
2023-02-05 19:17:01 +01:00
|
|
|
|
2020-04-28 20:27:59 +02:00
|
|
|
class Ajax::AnswerController < AjaxController
|
2023-10-18 20:01:59 +02:00
|
|
|
include SocialHelper
|
2023-02-05 19:17:01 +01:00
|
|
|
|
2015-01-03 18:40:39 +01:00
|
|
|
def create
|
|
|
|
params.require :id
|
|
|
|
params.require :answer
|
|
|
|
params.require :inbox
|
|
|
|
|
2023-02-05 20:18:51 +01:00
|
|
|
inbox = (params[:inbox] == "true")
|
2015-01-03 18:40:39 +01:00
|
|
|
|
|
|
|
if inbox
|
2024-01-27 13:02:58 +01:00
|
|
|
inbox_entry = InboxEntry.find(params[:id])
|
2015-01-03 18:40:39 +01:00
|
|
|
|
|
|
|
unless current_user == inbox_entry.user
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:status] = :fail
|
2022-07-06 12:09:58 +02:00
|
|
|
@response[:message] = t(".error")
|
2015-01-03 18:40:39 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
question = Question.find(params[:id])
|
2015-01-03 22:37:10 +01:00
|
|
|
|
|
|
|
unless question.user.privacy_allow_stranger_answers
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:status] = :privacy_stronk
|
2022-07-06 12:09:58 +02:00
|
|
|
@response[:message] = t(".privacy")
|
2015-01-03 22:37:10 +01:00
|
|
|
return
|
|
|
|
end
|
2015-01-03 18:40:39 +01:00
|
|
|
end
|
|
|
|
|
2022-06-11 23:15:24 +02:00
|
|
|
answer = if inbox
|
|
|
|
inbox_entry.answer params[:answer], current_user
|
|
|
|
else
|
|
|
|
current_user.answer question, params[:answer]
|
|
|
|
end
|
2015-01-03 18:40:39 +01:00
|
|
|
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:status] = :okay
|
2022-07-06 12:09:58 +02:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:success] = true
|
2023-02-05 19:17:01 +01:00
|
|
|
|
2023-02-19 20:58:47 +01:00
|
|
|
@response[:sharing] = sharing_hash(answer) if current_user.sharing_enabled
|
2023-02-05 19:17:01 +01:00
|
|
|
|
2023-02-05 20:18:51 +01:00
|
|
|
return if inbox
|
|
|
|
|
|
|
|
@question = 1
|
2023-12-09 18:05:50 +01:00
|
|
|
@response[:render] = render_to_string(partial: "answerbox", locals: { a: answer, show_question: false })
|
2015-01-03 18:40:39 +01:00
|
|
|
end
|
|
|
|
|
2014-11-26 17:05:46 +01:00
|
|
|
def destroy
|
|
|
|
params.require :answer
|
|
|
|
|
|
|
|
answer = Answer.find(params[:answer])
|
2014-12-28 21:14:01 +01:00
|
|
|
|
2023-02-05 20:18:51 +01:00
|
|
|
unless (current_user == answer.user) || (privileged? answer.user)
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:status] = :nopriv
|
2022-07-06 12:09:58 +02:00
|
|
|
@response[:message] = t(".nopriv")
|
2014-11-26 17:05:46 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-01-27 13:02:58 +01:00
|
|
|
InboxEntry.create!(user: answer.user, question: answer.question, new: true, returning: true) if answer.user == current_user
|
2014-12-28 21:34:42 +01:00
|
|
|
answer.destroy
|
2014-11-26 17:05:46 +01:00
|
|
|
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:status] = :okay
|
2022-07-06 12:09:58 +02:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 20:27:59 +02:00
|
|
|
@response[:success] = true
|
2014-11-26 17:05:46 +01:00
|
|
|
end
|
2023-02-19 20:58:47 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sharing_hash(answer) = {
|
2023-10-18 20:01:59 +02:00
|
|
|
url: answer_share_url(answer),
|
|
|
|
text: prepare_tweet(answer, nil, true),
|
2023-02-19 20:58:47 +01:00
|
|
|
twitter: twitter_share_url(answer),
|
2024-03-01 20:48:51 +01:00
|
|
|
bluesky: bluesky_share_url(answer),
|
2023-02-19 20:58:47 +01:00
|
|
|
tumblr: tumblr_share_url(answer),
|
|
|
|
telegram: telegram_share_url(answer),
|
|
|
|
custom: CGI.escape(prepare_tweet(answer)),
|
|
|
|
}
|
2014-11-26 17:05:46 +01:00
|
|
|
end
|