Merge pull request #15 from skiprope/report-reason

Add support for report reasons (fixes Retrospring/bugs#7)
This commit is contained in:
Georg G. 2015-04-22 18:25:29 +02:00
commit e306d36b10
10 changed files with 61 additions and 73 deletions

View File

@ -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"
reportDialog "comment", cid, -> btn.button "reset"

View File

@ -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"
reportDialog "answer", aid, -> btn.button "reset"

View File

@ -21,6 +21,7 @@
#= require question
#= require settings
#= require user
#= require report
# not required:
# _tree ./moderation

View File

@ -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

View File

@ -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"
reportDialog "user", target, -> btn.button "reset"

View File

@ -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]}."

View File

@ -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]

View File

@ -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
%div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report

View File

@ -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
= render 'moderation/moderationbox', report: r

View File

@ -0,0 +1,5 @@
class AddReasonToReport < ActiveRecord::Migration
def change
add_column :reports, :reason, :string, default: nil
end
end