statuses_controller.rb 1.8 KB

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