docbrown/app/controllers/feedback_messages_controller.rb
Daniel Uber f2a8cbce7e
Remove "connect" feedback message special treatment (#16167)
* Remove special handling of "connect" feedback by name

The special casing was related to "connect" feedback having both a
reporter and an offender. Check for offender instead.

Additionally, there was special casing in the controller to rate-limit
connect feedback separately from other channels. Since connect doesn't
exist, we should not need this.

There's a small bit of functionality (when I post to feedback_messages, the
number of feedback messages increases) that was removed from the test
case, we can add that back (and "connect" type, and
offender_id attributes) since it looks like it might have been a
useful assertion.

* Add back feedback message controller creates feedback message case

This was removed in the last commit because it was in a "connect" chat
channel context, but the basic "should persist a record" test was
otherwise valid. Submit an abuse-report rather than a connect message
report.

* typo

feeedback, woops.
2022-01-19 08:32:10 -06:00

73 lines
2.5 KiB
Ruby

class FeedbackMessagesController < ApplicationController
# No authorization required for entirely public controller
skip_before_action :verify_authenticity_token
FLASH_MESSAGE = "Make sure the forms are filled. 🤖 Other possible errors: "\
"%<errors>s".freeze
FEEDBACK_ALLOWED_PARAMS = %i[message feedback_type category reported_url offender_id].freeze
def create
flash.clear
params = feedback_message_params.merge(reporter_id: current_user&.id)
@feedback_message = FeedbackMessage.new(params)
recaptcha_enabled = ReCaptcha::CheckEnabled.call(current_user)
if (!recaptcha_enabled || recaptcha_verified?) && !rate_limit? && @feedback_message.save
Slack::Messengers::Feedback.call(
user: current_user,
type: feedback_message_params[:feedback_type],
category: feedback_message_params[:category],
reported_url: feedback_message_params[:reported_url],
message: feedback_message_params[:message],
)
rate_limiter.track_limit_by_action(:feedback_message_creation)
if user_signed_in?
Rails.cache.fetch("user-#{current_user.id}-feedback-response-sent-at", expires_in: 24.hours) do
NotifyMailer.with(email_to: current_user.email).feedback_response_email.deliver_later
Time.current
end
end
respond_to do |format|
format.html { redirect_to feedback_messages_path }
format.json { render json: { success: true, message: "Your report is submitted" } }
end
else
@previous_message = feedback_message_params[:message]
flash[:notice] = format(FLASH_MESSAGE, errors: @feedback_message.errors_as_sentence.presence || "N/A")
respond_to do |format|
format.html { render "pages/report_abuse" }
format.json do
render json: {
success: false,
message: @feedback_message.errors_as_sentence,
status: :bad_request
}
end
end
end
end
private
def recaptcha_verified?
recaptcha_params = { secret_key: Settings::Authentication.recaptcha_secret_key }
params["g-recaptcha-response"] && verify_recaptcha(recaptcha_params)
end
def feedback_message_params
params.require(:feedback_message).permit(FEEDBACK_ALLOWED_PARAMS)
end
def rate_limit?
begin
rate_limit!(:feedback_message_creation)
rescue StandardError => e
@feedback_message.errors.add(:feedback_message_creation, e.message)
return true
end
false
end
end