Retrospring/app/controllers/ajax/answer_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.0 KiB
Ruby
Raw Normal View History

2023-02-05 11:18:51 -08:00
# frozen_string_literal: true
require "cgi"
class Ajax::AnswerController < AjaxController
include SocialHelper
def create
params.require :id
params.require :answer
params.require :inbox
2023-02-05 11:18:51 -08:00
inbox = (params[:inbox] == "true")
if inbox
inbox_entry = Inbox.find(params[:id])
unless current_user == inbox_entry.user
@response[:status] = :fail
@response[:message] = t(".error")
return
end
else
question = Question.find(params[:id])
unless question.user.privacy_allow_stranger_answers
@response[:status] = :privacy_stronk
@response[:message] = t(".privacy")
return
end
end
answer = if inbox
inbox_entry.answer params[:answer], current_user
else
current_user.answer question, params[:answer]
end
@response[:status] = :okay
@response[:message] = t(".success")
@response[:success] = true
2023-02-19 11:58:47 -08:00
@response[:sharing] = sharing_hash(answer) if current_user.sharing_enabled
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 })
end
2014-11-26 08:05:46 -08:00
def destroy
params.require :answer
answer = Answer.find(params[:answer])
2023-02-05 11:18:51 -08:00
unless (current_user == answer.user) || (privileged? answer.user)
@response[:status] = :nopriv
@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
@response[:status] = :okay
@response[:message] = t(".success")
@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) = {
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