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.

92 lines
2.4 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class CustomEmojisController < BaseController
  4. before_action :set_custom_emoji, except: [:index, :new, :create]
  5. def index
  6. authorize :custom_emoji, :index?
  7. @custom_emojis = filtered_custom_emojis.eager_load(:local_counterpart).page(params[:page])
  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. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.created_msg')
  18. else
  19. render :new
  20. end
  21. end
  22. def update
  23. authorize @custom_emoji, :update?
  24. if @custom_emoji.update(resource_params)
  25. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.updated_msg')
  26. else
  27. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.update_failed_msg')
  28. end
  29. end
  30. def destroy
  31. authorize @custom_emoji, :destroy?
  32. @custom_emoji.destroy
  33. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.destroyed_msg')
  34. end
  35. def copy
  36. authorize @custom_emoji, :copy?
  37. emoji = CustomEmoji.find_or_create_by(domain: nil, shortcode: @custom_emoji.shortcode)
  38. if emoji.update(image: @custom_emoji.image)
  39. flash[:notice] = I18n.t('admin.custom_emojis.copied_msg')
  40. else
  41. flash[:alert] = I18n.t('admin.custom_emojis.copy_failed_msg')
  42. end
  43. redirect_to admin_custom_emojis_path(page: params[:page])
  44. end
  45. def enable
  46. authorize @custom_emoji, :enable?
  47. @custom_emoji.update!(disabled: false)
  48. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.enabled_msg')
  49. end
  50. def disable
  51. authorize @custom_emoji, :disable?
  52. @custom_emoji.update!(disabled: true)
  53. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.disabled_msg')
  54. end
  55. private
  56. def set_custom_emoji
  57. @custom_emoji = CustomEmoji.find(params[:id])
  58. end
  59. def resource_params
  60. params.require(:custom_emoji).permit(:shortcode, :image, :visible_in_picker)
  61. end
  62. def filtered_custom_emojis
  63. CustomEmojiFilter.new(filter_params).results
  64. end
  65. def filter_params
  66. params.permit(
  67. :local,
  68. :remote
  69. )
  70. end
  71. end
  72. end