From b9bf0b04b67e15acffdeb07a85c5c36c660e245f Mon Sep 17 00:00:00 2001 From: Yuki Date: Wed, 22 Apr 2015 08:29:10 +0530 Subject: [PATCH] Add support for report reasons --- .../answerbox/comment/report.coffee | 23 +-------------- .../javascripts/answerbox/report.coffee | 23 +-------------- .../javascripts/application.js.erb.coffee | 1 + app/assets/javascripts/report.coffee | 28 +++++++++++++++++++ app/assets/javascripts/user.coffee | 24 +--------------- app/controllers/ajax/report_controller.rb | 3 +- app/models/user.rb | 15 ++++++++-- app/views/moderation/_moderationbox.html.haml | 10 ++++++- app/views/moderation/index.html.haml | 2 +- .../20150422024104_add_reason_to_report.rb | 5 ++++ 10 files changed, 61 insertions(+), 73 deletions(-) create mode 100644 app/assets/javascripts/report.coffee create mode 100644 db/migrate/20150422024104_add_reason_to_report.rb diff --git a/app/assets/javascripts/answerbox/comment/report.coffee b/app/assets/javascripts/answerbox/comment/report.coffee index 559552fa..672f3d98 100644 --- a/app/assets/javascripts/answerbox/comment/report.coffee +++ b/app/assets/javascripts/answerbox/comment/report.coffee @@ -2,25 +2,4 @@ $(document).on "click", "a[data-action=ab-comment-report]", (ev) -> ev.preventDefault() btn = $(this) cid = btn[0].dataset.cId - swal - title: "Really report?" - text: "A moderator will review this comment and decide what happens." - type: "warning" - showCancelButton: true - confirmButtonColor: "#DD6B55" - confirmButtonText: "Report" - closeOnConfirm: true - , -> - $.ajax - url: '/ajax/report' - type: 'POST' - data: - id: cid - type: 'comment' - success: (data, status, jqxhr) -> - showNotification data.message, data.success - error: (jqxhr, status, error) -> - console.log jqxhr, status, error - showNotification "An error occurred, a developer should check the console for details", false - complete: (jqxhr, status) -> - btn.button "reset" \ No newline at end of file + reportDialog "comment", cid, -> btn.button "reset" diff --git a/app/assets/javascripts/answerbox/report.coffee b/app/assets/javascripts/answerbox/report.coffee index 266755b5..f9f64e65 100644 --- a/app/assets/javascripts/answerbox/report.coffee +++ b/app/assets/javascripts/answerbox/report.coffee @@ -2,25 +2,4 @@ $(document).on "click", "a[data-action=ab-report]", (ev) -> ev.preventDefault() btn = $(this) aid = btn[0].dataset.aId - swal - title: "Really report?" - text: "A moderator will review this answer and decide what happens." - type: "warning" - showCancelButton: true - confirmButtonColor: "#DD6B55" - confirmButtonText: "Report" - closeOnConfirm: true - , -> - $.ajax - url: '/ajax/report' # TODO: find a way to use rake routes instead of hardcoding them here - type: 'POST' - data: - id: aid - type: 'answer' - success: (data, status, jqxhr) -> - showNotification data.message, data.success - error: (jqxhr, status, error) -> - console.log jqxhr, status, error - showNotification "An error occurred, a developer should check the console for details", false - complete: (jqxhr, status) -> - btn.button "reset" \ No newline at end of file + reportDialog "answer", aid, -> btn.button "reset" diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index f34b63c9..9d0839b2 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -21,6 +21,7 @@ #= require question #= require settings #= require user +#= require report # not required: # _tree ./moderation diff --git a/app/assets/javascripts/report.coffee b/app/assets/javascripts/report.coffee new file mode 100644 index 00000000..a2603e1c --- /dev/null +++ b/app/assets/javascripts/report.coffee @@ -0,0 +1,28 @@ +window.reportDialog = (type, target, callback) -> + swal + title: "Really report #{target}?" + text: "A moderator will review this #{type} and decide what happens.\nIf you'd like, you can also specify a reason." + type: "input" + showCancelButton: true + confirmButtonColor: "#DD6B55" + confirmButtonText: "Report" + closeOnConfirm: true + inputPlaceholder: "Specify a reason..." + , (value) -> + if typeof value == "boolean" && value == false + return false + + $.ajax + url: '/ajax/report' + type: 'POST' + data: + id: target + type: type + reason: value + success: (data, status, jqxhr) -> + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + callback type, target, jqxhr, status diff --git a/app/assets/javascripts/user.coffee b/app/assets/javascripts/user.coffee index 7ab8fd4d..e12e0407 100644 --- a/app/assets/javascripts/user.coffee +++ b/app/assets/javascripts/user.coffee @@ -50,26 +50,4 @@ $(document).on "click", "a[data-action=report-user]", (ev) -> ev.preventDefault() btn = $(this) target = btn[0].dataset.target - - swal - title: "Really report #{target}?" - text: "A moderator will review this user and decide what happens." - type: "warning" - showCancelButton: true - confirmButtonColor: "#DD6B55" - confirmButtonText: "Report" - closeOnConfirm: true - , -> - $.ajax - url: '/ajax/report' - type: 'POST' - data: - id: target - type: 'user' - success: (data, status, jqxhr) -> - showNotification data.message, data.success - error: (jqxhr, status, error) -> - console.log jqxhr, status, error - showNotification "An error occurred, a developer should check the console for details", false - complete: (jqxhr, status) -> - btn.button "reset" \ No newline at end of file + reportDialog "user", target, -> btn.button "reset" diff --git a/app/controllers/ajax/report_controller.rb b/app/controllers/ajax/report_controller.rb index bea0dcc5..42ab82da 100644 --- a/app/controllers/ajax/report_controller.rb +++ b/app/controllers/ajax/report_controller.rb @@ -22,13 +22,12 @@ class Ajax::ReportController < ApplicationController params[:type].strip.capitalize.constantize.find params[:id] end - if object.nil? @message = "Could not find #{params[:type]}" return end - current_user.report object + current_user.report object, params[:reason] @status = :okay @message = "#{params[:type].capitalize} reported. A moderator will decide what happens with the #{params[:type]}." diff --git a/app/models/user.rb b/app/models/user.rb index c92dabe4..d5b0cff1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -149,8 +149,19 @@ class User < ActiveRecord::Base end # region stuff used for reporting/moderation - def report(object) - Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id) + def report(object, reason = nil) + existing = Report.find_by(target_id: object.id, user_id: self.id, deleted: false) + if existing.nil? + Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id, reason: reason) + elsif not reason.nil? and reason.length > 0 + if existing.reason.nil? + existing.update(reason: reason) + else + existing.update(reason: [existing.reason || "", reason].join("\n")) + end + else + existing + end end # @param upvote [Boolean] diff --git a/app/views/moderation/_moderationbox.html.haml b/app/views/moderation/_moderationbox.html.haml index a671a220..439b4eed 100644 --- a/app/views/moderation/_moderationbox.html.haml +++ b/app/views/moderation/_moderationbox.html.haml @@ -13,6 +13,14 @@ = user_screen_name report.target - else = report.target.content + %p + %b + Reason: + %br + - (report.reason || "No reason provided.").lines.each do |reason| + - next unless reason.strip.length > 0 + = reason.strip + %br .row .col-md-6.col-sm-4.col-xs-6.text-left %a.btn.btn-primary{href: content_url(report)} @@ -32,4 +40,4 @@ %button.btn.btn-default.btn-sm{type: :button, name: "mod-delete-report", data: { id: report.id }} %i.fa.fa-trash-o .panel-footer{id: "mod-comments-section-#{report.id}", style: 'display: none'} - %div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report \ No newline at end of file + %div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report diff --git a/app/views/moderation/index.html.haml b/app/views/moderation/index.html.haml index 5fb59763..8a5c3e34 100644 --- a/app/views/moderation/index.html.haml +++ b/app/views/moderation/index.html.haml @@ -4,4 +4,4 @@ = render 'moderation/moderation_tabs' .col-md-9.col-sm-9.col-xs-12 - @reports.each do |r| - = render 'moderation/moderationbox', report: r \ No newline at end of file + = render 'moderation/moderationbox', report: r diff --git a/db/migrate/20150422024104_add_reason_to_report.rb b/db/migrate/20150422024104_add_reason_to_report.rb new file mode 100644 index 00000000..c3f31615 --- /dev/null +++ b/db/migrate/20150422024104_add_reason_to_report.rb @@ -0,0 +1,5 @@ +class AddReasonToReport < ActiveRecord::Migration + def change + add_column :reports, :reason, :string, default: nil + end +end