docbrown/app/controllers/feedback_messages_controller.rb
Ben Halpern 2add3ae300
Replace dev.to with ApplicationConfig in a few locations (#6008) [deploy]
* Replace dev.to with ApplicationConfig in a few locations

* Update app/labor/badge_rewarder.rb
2020-02-11 14:48:27 -05:00

69 lines
2 KiB
Ruby

class FeedbackMessagesController < ApplicationController
# No authorization required for entirely public controller
skip_before_action :verify_authenticity_token
def create
flash.clear
@feedback_message = FeedbackMessage.new(
feedback_message_params.merge(reporter_id: current_user&.id),
)
if recaptcha_verified? && @feedback_message.save
send_slack_message
redirect_to "/feedback_messages"
else
flash[:notice] = "Make sure the forms are filled 🤖 "
@previous_message = feedback_message_params[:message]
render "pages/report-abuse.html.erb"
end
end
private
def recaptcha_verified?
params["g-recaptcha-response"] &&
verify_recaptcha(secret_key: ApplicationConfig["RECAPTCHA_SECRET"])
end
def send_slack_message
SlackBot.ping(
generate_message,
channel: feedback_message_params[:feedback_type],
username: "#{feedback_message_params[:feedback_type]}_bot",
icon_emoji: ":#{emoji_for_feedback(feedback_message_params[:feedback_type])}:",
)
end
def generate_message
<<~HEREDOC
#{generate_user_detail}
Category: #{feedback_message_params[:category]}
Internal Report: https://#{ApplicationConfig['APP_DOMAIN']}/internal/reports
*_ Reported URL: #{feedback_message_params[:reported_url]} _*
-----
*Message:* #{feedback_message_params[:message]}
HEREDOC
end
def generate_user_detail
return "*Anonymous report:*" unless current_user
<<~HEREDOC
*Logged in user:*
reporter: #{current_user.username} - https://#{ApplicationConfig['APP_DOMAIN']}/#{current_user.username}
email: <mailto:#{current_user.email}|#{current_user.email}>
HEREDOC
end
def emoji_for_feedback(feedback_type)
case feedback_type
when "abuse-reports"
"cry"
else
"robot_face"
end
end
def feedback_message_params
params[:feedback_message].permit(:message, :feedback_type, :category, :reported_url)
end
end