diff --git a/app/assets/stylesheets/report.scss b/app/assets/stylesheets/report.scss index 30e6f32eb..4282ad637 100644 --- a/app/assets/stylesheets/report.scss +++ b/app/assets/stylesheets/report.scss @@ -1,5 +1,4 @@ #reports { - padding-top: 2em; .reason { padding-bottom: 20px; } diff --git a/app/helpers/report_helper.rb b/app/helpers/report_helper.rb index 99533489e..5a6f1e0e3 100644 --- a/app/helpers/report_helper.rb +++ b/app/helpers/report_helper.rb @@ -3,22 +3,15 @@ # the COPYRIGHT file. module ReportHelper - def get_reported_guid(id, type) - if type == "post" - Post.where(id: id).first.author.guid - elsif type == "comment" - Comment.where(id: id).first.author.guid - end - end - - def report_content(id, type) - if type == "post" && !(post = Post.find_by_id(id)).nil? - raw t("report.post_label", title: link_to(post_page_title(post), post_path(id))) - elsif type == "comment" && !(comment = Comment.find_by_id(id)).nil? - # comment_message is not html_safe. To prevent - # cross-site-scripting we have to escape html + def report_content(report) + case (item = report.item) + when Post + raw t("report.post_label", title: link_to(post_page_title(item), post_path(item.id))) + when Comment raw t("report.comment_label", data: link_to( - h(comment_message(comment)), post_path(comment.post.id, anchor: comment.guid))) + h(comment_message(item)), + post_path(item.post.id, anchor: item.guid) + )) else raw t("report.not_found") end diff --git a/app/models/report.rb b/app/models/report.rb index 36356c48f..5e056cf02 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,4 +1,6 @@ class Report < ActiveRecord::Base + POST, COMMENT = %w(post comment).map(&:freeze) + validates :user_id, presence: true validates :item_id, presence: true validates :item_type, presence: true, :inclusion => { :in => %w(post comment), @@ -14,6 +16,18 @@ class Report < ActiveRecord::Base after_commit :send_report_notification, :on => :create + def item + if item_type == POST + Post.find_by(id: item_id) + elsif item_type == COMMENT + Comment.find_by(id: item_id) + end + end + + def reported_author + item.author unless item.nil? + end + def entry_does_not_exist if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id) errors[:base] << 'You cannot report the same post twice.' @@ -27,35 +41,24 @@ class Report < ActiveRecord::Base end def destroy_reported_item - if item_type == 'post' - delete_post - elsif item_type == 'comment' - delete_comment + case item + when Post + if item.author.local? + item.author.owner.retract(item) + else + item.destroy + end + when Comment + if item.author.local? + item.author.owner.retract(comment) + elsif item.parent.author.local? + item.parent.author.owner.retract(comment) + else + item.destroy + end end mark_as_reviewed end - - def delete_post - if post = Post.where(id: item_id).first - if post.author.local? - post.author.owner.retract(post) - else - post.destroy - end - end - end - - def delete_comment - if comment = Comment.where(id: item_id).first - if comment.author.local? - comment.author.owner.retract(comment) - elsif comment.parent.author.local? - comment.parent.author.owner.retract(comment) - else - comment.destroy - end - end - end def mark_as_reviewed Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true) diff --git a/app/views/report/index.html.haml b/app/views/report/index.html.haml index c8745fcec..6a285874e 100644 --- a/app/views/report/index.html.haml +++ b/app/views/report/index.html.haml @@ -10,27 +10,24 @@ #reports %h1 = t("report.title") - - @reports.each do |r| + - @reports.each do |report| .panel.panel-default - - username = User.find_by_id(r.user_id).username + - username = report.user.username .panel-heading .reporter.pull-right = raw t("report.reported_label", person: link_to(username, user_profile_path(username))) .title - = report_content(r.item_id, r.item_type) + = report_content(report) .panel-body .reason - = t("report.reason_label", text: r.text) + = t("report.reason_label", text: report.text) = button_to t("report.reported_user_details"), - user_search_path(admins_controller_user_search: {guid: get_reported_guid(r.item_id, - r.item_type)}), class: "btn pull-left btn-info btn-small", - method: :post - = button_to t("report.review_link"), report_path(r.id, type: r.item_type), - class: "btn pull-left btn-info btn-small", - method: :put - = button_to t("report.delete_link"), report_path(r.id, type: r.item_type), + user_search_path(admins_controller_user_search: {guid: report.reported_author.guid}), + class: "btn pull-left btn-info btn-small", method: :post + = button_to t("report.review_link"), report_path(report.id, type: report.item_type), + class: "btn pull-left btn-info btn-small", method: :put + = button_to t("report.delete_link"), report_path(report.id, type: report.item_type), data: {confirm: t("report.confirm_deletion")}, - class: "btn pull-right btn-danger btn-small", - method: :delete + class: "btn pull-right btn-danger btn-small", method: :delete diff --git a/spec/helpers/report_helper_spec.rb b/spec/helpers/report_helper_spec.rb index 8cb003c42..6a6b95958 100644 --- a/spec/helpers/report_helper_spec.rb +++ b/spec/helpers/report_helper_spec.rb @@ -1,17 +1,27 @@ -require 'spec_helper' +require "spec_helper" -describe ReportHelper, :type => :helper do +describe ReportHelper, type: :helper do before do - @comment = FactoryGirl.create(:comment) - @post = @comment.post + @user = bob + @post = @user.post(:status_message, text: "hello", to: @user.aspects.first.id) + @comment = @user.comment!(@post, "welcome") + end + + describe "#get_reported_guid" do + it "returns user guid from post" do + expect(helper.get_reported_guid(@post, "post")) == @user.guid + end + it "returns user guid from comment" do + expect(helper.get_reported_guid(@comment, "comment")) == @user.guid + end end describe "#report_content" do it "contains a link to the post" do - expect(helper.report_content(@post, 'post')).to include %Q(href="#{post_path(@post)}") + expect(helper.report_content(@post, "post")).to include %(href="#{post_path(@post)}") end - it "contains an anchor to the comment" do - expect(helper.report_content(@comment, 'comment')).to include %Q(href="#{post_path(@post, anchor: @comment.guid)}") + it "contains an anchor to the comment" do + expect(helper.report_content(@comment, "comment")).to include %(href="#{post_path(@post, anchor: @comment.guid)}") end end end