You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::ReportsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }, except: [:create]
  4. before_action -> { doorkeeper_authorize! :write }, only: [:create]
  5. before_action :require_user!
  6. respond_to :json
  7. def index
  8. @reports = current_account.reports
  9. render json: @reports, each_serializer: REST::ReportSerializer
  10. end
  11. def create
  12. @report = current_account.reports.create!(
  13. target_account: reported_account,
  14. status_ids: reported_status_ids,
  15. comment: report_params[:comment]
  16. )
  17. User.staff.includes(:account).each { |u| AdminMailer.new_report(u.account, @report).deliver_later }
  18. render json: @report, serializer: REST::ReportSerializer
  19. end
  20. private
  21. def reported_status_ids
  22. Status.find(status_ids).pluck(:id)
  23. end
  24. def status_ids
  25. Array(report_params[:status_ids])
  26. end
  27. def reported_account
  28. Account.find(report_params[:account_id])
  29. end
  30. def report_params
  31. params.permit(:account_id, :comment, status_ids: [])
  32. end
  33. end