diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9763b87b..beeaade9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -46,6 +46,7 @@ def current_user @current_user end + #TODO remove this def viewing_user @viewing_user ||= current_user || begin if cookies[:identity] diff --git a/app/controllers/protips_controller.rb b/app/controllers/protips_controller.rb index 11044e7d..b2102f59 100644 --- a/app/controllers/protips_controller.rb +++ b/app/controllers/protips_controller.rb @@ -257,25 +257,16 @@ def unsubscribe end def report_inappropriate - - report_inappropriate_params = params.permit(:id) - - - protip_public_id = params.permit(:id) - - logger.info "Report Inappropriate: protip_public_id => '#{protip_public_id}', reporting_user => '#{viewing_user.inspect}', ip_address => '#{request.remote_ip}'" - + protip_public_id = params[:id] if cookies["report_inappropriate-#{protip_public_id}"].nil? - opts = { reporting_user: viewing_user, ip_address: request.remote_ip, protip_public_id: protip_public_id } - report_inappropriate_mailer = ::AbuseMailer.report_inappropriate(opts) - report_inappropriate_mailer.deliver + opts = { user_id: current_user, + ip: request.remote_ip} + ::AbuseMailer.report_inappropriate(protip_public_id,opts).deliver cookies["report_inappropriate-#{protip_public_id}"] = true end - respond_to do |format| - format.json { head :ok } - end + render nothing: true end def flag diff --git a/app/mailers/abuse_mailer.rb b/app/mailers/abuse_mailer.rb index 5dbd6cf4..3440aa73 100644 --- a/app/mailers/abuse_mailer.rb +++ b/app/mailers/abuse_mailer.rb @@ -1,21 +1,21 @@ class AbuseMailer < ActionMailer::Base - include ActionView::Helpers::TextHelper - include ActiveSupport::Benchmarkable - default_url_options[:host] = 'coderwall.com' default_url_options[:only_path] = false - default to: Proc.new { User.admins.map(&:email) }, + default to: -> { User.admins.pluck(:email) }, from: '"Coderwall" ' - def report_inappropriate(opts) + def report_inappropriate(public_id, opts={}) headers['X-Mailgun-Campaign-Id'] = 'coderwall-abuse-report_inappropriate' - - @ip_address = opts[:ip_address] - @reporting_user = opts[:reporting_user] - protip_public_id = (opts[:protip_public_id] || opts['protip_public_id']) - @protip = Protip.find_by_public_id(protip_public_id) + begin + @protip = Protip.find_by_public_id!(public_id) + @reporting_user = opts[:user_id] + @ip_address = opts[:ip] mail subject: "Spam Report for Protip: \"#{@protip.title}\" (#{@protip.id})" + rescue ActiveRecord::RecordNotFound + #Protip was probably deleted + true + end end end diff --git a/spec/mailers/abuse_mailer_spec.rb b/spec/mailers/abuse_mailer_spec.rb index d6e4bbc0..a15d44e8 100644 --- a/spec/mailers/abuse_mailer_spec.rb +++ b/spec/mailers/abuse_mailer_spec.rb @@ -1,9 +1,9 @@ RSpec.describe AbuseMailer, :type => :mailer do describe 'report_inappropriate' do - let(:mail) { AbuseMailer.report_inappropriate({ protip_public_id: protip.to_param }) } + let(:mail) { AbuseMailer.report_inappropriate(protip.to_param) } - let(:current_user) { Fabricate(:user, admin: true) } + let!(:current_user) { Fabricate(:user, admin: true) } let(:protip) { Protip.create!(