custom_emojis_controller.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. module Admin
  3. class CustomEmojisController < BaseController
  4. def index
  5. authorize :custom_emoji, :index?
  6. @custom_emojis = filtered_custom_emojis.eager_load(:local_counterpart).page(params[:page])
  7. @form = Form::CustomEmojiBatch.new
  8. end
  9. def new
  10. authorize :custom_emoji, :create?
  11. @custom_emoji = CustomEmoji.new
  12. end
  13. def create
  14. authorize :custom_emoji, :create?
  15. @custom_emoji = CustomEmoji.new(resource_params)
  16. if @custom_emoji.save
  17. log_action :create, @custom_emoji
  18. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.created_msg')
  19. else
  20. render :new
  21. end
  22. end
  23. def batch
  24. authorize :custom_emoji, :index?
  25. @form = Form::CustomEmojiBatch.new(form_custom_emoji_batch_params.merge(current_account: current_account, action: action_from_button))
  26. @form.save
  27. rescue ActionController::ParameterMissing
  28. flash[:alert] = I18n.t('admin.custom_emojis.no_emoji_selected')
  29. rescue Mastodon::NotPermittedError
  30. flash[:alert] = I18n.t('admin.custom_emojis.not_permitted')
  31. ensure
  32. redirect_to admin_custom_emojis_path(filter_params)
  33. end
  34. private
  35. def resource_params
  36. params.require(:custom_emoji).permit(:shortcode, :image, :visible_in_picker)
  37. end
  38. def filtered_custom_emojis
  39. CustomEmojiFilter.new(filter_params).results
  40. end
  41. def filter_params
  42. params.slice(:page, *CustomEmojiFilter::KEYS).permit(:page, *CustomEmojiFilter::KEYS)
  43. end
  44. def action_from_button
  45. if params[:update]
  46. 'update'
  47. elsif params[:list]
  48. 'list'
  49. elsif params[:unlist]
  50. 'unlist'
  51. elsif params[:enable]
  52. 'enable'
  53. elsif params[:disable]
  54. 'disable'
  55. elsif params[:copy]
  56. 'copy'
  57. elsif params[:delete]
  58. 'delete'
  59. end
  60. end
  61. def form_custom_emoji_batch_params
  62. params.require(:form_custom_emoji_batch).permit(:action, :category_id, :category_name, custom_emoji_ids: [])
  63. end
  64. end
  65. end