Set target user in report action

This commit is contained in:
Andreas Nedbal 2024-01-23 20:32:48 +01:00 committed by Andreas Nedbal
parent ab442db40b
commit 27a57e304d
2 changed files with 10 additions and 4 deletions

View File

@ -36,7 +36,13 @@ class Ajax::ReportController < AjaxController
return return
end end
current_user.report object, params[:reason] target_user = if object.class.to_s == "User"
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,10 +148,10 @@ 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, reason = nil) def report(object, target_user = nil, reason = nil)
existing = Report.find_by(type: "Reports::#{object.class}", target_id: object.id, user_id: 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, reason:) Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: id, target_user_id: target_user&.id, reason:)
elsif !reason.nil? && reason.length.positive? elsif !reason.nil? && reason.length.positive?
existing.append_reason(reason) existing.append_reason(reason)
end end