Move target user resolving code into `report` method

This commit is contained in:
Andreas Nedbal 2024-01-23 20:42:16 +01:00 committed by Andreas Nedbal
parent 27a57e304d
commit 05634ae69d
2 changed files with 8 additions and 8 deletions

View File

@ -36,13 +36,7 @@ class Ajax::ReportController < AjaxController
return return
end end
target_user = if object.class.to_s == "User" current_user.report object, params[:reason]
object
elsif object.respond_to? :user
object.user
end
current_user.report object, target_user, params[:reason]
@response[:status] = :okay @response[:status] = :okay
@response[:message] = t(".success", parameter: params[:type].titleize) @response[:message] = t(".success", parameter: params[:type].titleize)

View File

@ -148,7 +148,13 @@ class User < ApplicationRecord
def admin? = has_cached_role?(:administrator) def admin? = has_cached_role?(:administrator)
# region stuff used for reporting/moderation # region stuff used for reporting/moderation
def report(object, target_user = nil, reason = nil) def report(object, reason = nil)
target_user = if object.class.to_s == "User"
object
elsif object.respond_to? :user
object.user
end
existing = Report.find_by(type: "Reports::#{object.class}", target_id: object.id, user_id: id, target_user_id: target_user&.id, deleted: false) existing = Report.find_by(type: "Reports::#{object.class}", target_id: object.id, user_id: id, target_user_id: target_user&.id, deleted: false)
if existing.nil? if existing.nil?
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: id, target_user_id: target_user&.id, reason:) Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: id, target_user_id: target_user&.id, reason:)