statuses_controller.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. module Admin
  3. class StatusesController < BaseController
  4. include Authorization
  5. helper_method :current_params
  6. before_action :set_account
  7. before_action :set_status, only: [:update, :destroy]
  8. PER_PAGE = 20
  9. def index
  10. @statuses = @account.statuses
  11. if params[:media]
  12. account_media_status_ids = @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  13. @statuses.merge!(Status.where(id: account_media_status_ids))
  14. end
  15. @statuses = @statuses.preload(:media_attachments, :mentions).page(params[:page]).per(PER_PAGE)
  16. @form = Form::StatusBatch.new
  17. end
  18. def create
  19. @form = Form::StatusBatch.new(form_status_batch_params)
  20. flash[:alert] = t('admin.statuses.failed_to_execute') unless @form.save
  21. redirect_to admin_account_statuses_path(@account.id, current_params)
  22. end
  23. def update
  24. @status.update(status_params)
  25. redirect_to admin_account_statuses_path(@account.id, current_params)
  26. end
  27. def destroy
  28. authorize @status, :destroy?
  29. RemovalWorker.perform_async(@status.id)
  30. render json: @status
  31. end
  32. private
  33. def status_params
  34. params.require(:status).permit(:sensitive)
  35. end
  36. def form_status_batch_params
  37. params.require(:form_status_batch).permit(:action, status_ids: [])
  38. end
  39. def set_status
  40. @status = @account.statuses.find(params[:id])
  41. end
  42. def set_account
  43. @account = Account.find(params[:account_id])
  44. end
  45. def current_params
  46. page = (params[:page] || 1).to_i
  47. {
  48. media: params[:media],
  49. page: page > 1 && page,
  50. }.select { |_, value| value.present? }
  51. end
  52. end
  53. end