The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

115 lines
3.1 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class CustomEmojisController < BaseController
  4. before_action :set_custom_emoji, except: [:index, :new, :create]
  5. before_action :set_filter_params
  6. include ObfuscateFilename
  7. obfuscate_filename [:custom_emoji, :image]
  8. def index
  9. authorize :custom_emoji, :index?
  10. @custom_emojis = filtered_custom_emojis.eager_load(:local_counterpart).page(params[:page])
  11. end
  12. def new
  13. authorize :custom_emoji, :create?
  14. @custom_emoji = CustomEmoji.new
  15. end
  16. def create
  17. authorize :custom_emoji, :create?
  18. @custom_emoji = CustomEmoji.new(resource_params)
  19. if @custom_emoji.save
  20. log_action :create, @custom_emoji
  21. redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.created_msg')
  22. else
  23. render :new
  24. end
  25. end
  26. def update
  27. authorize @custom_emoji, :update?
  28. if @custom_emoji.update(resource_params)
  29. log_action :update, @custom_emoji
  30. flash[:notice] = I18n.t('admin.custom_emojis.updated_msg')
  31. else
  32. flash[:alert] = I18n.t('admin.custom_emojis.update_failed_msg')
  33. end
  34. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  35. end
  36. def destroy
  37. authorize @custom_emoji, :destroy?
  38. @custom_emoji.destroy!
  39. log_action :destroy, @custom_emoji
  40. flash[:notice] = I18n.t('admin.custom_emojis.destroyed_msg')
  41. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  42. end
  43. def copy
  44. authorize @custom_emoji, :copy?
  45. emoji = CustomEmoji.find_or_initialize_by(domain: nil,
  46. shortcode: @custom_emoji.shortcode)
  47. emoji.image = @custom_emoji.image
  48. if emoji.save
  49. log_action :create, emoji
  50. flash[:notice] = I18n.t('admin.custom_emojis.copied_msg')
  51. else
  52. flash[:alert] = I18n.t('admin.custom_emojis.copy_failed_msg')
  53. end
  54. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  55. end
  56. def enable
  57. authorize @custom_emoji, :enable?
  58. @custom_emoji.update!(disabled: false)
  59. log_action :enable, @custom_emoji
  60. flash[:notice] = I18n.t('admin.custom_emojis.enabled_msg')
  61. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  62. end
  63. def disable
  64. authorize @custom_emoji, :disable?
  65. @custom_emoji.update!(disabled: true)
  66. log_action :disable, @custom_emoji
  67. flash[:notice] = I18n.t('admin.custom_emojis.disabled_msg')
  68. redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
  69. end
  70. private
  71. def set_custom_emoji
  72. @custom_emoji = CustomEmoji.find(params[:id])
  73. end
  74. def set_filter_params
  75. @filter_params = filter_params.to_hash.symbolize_keys
  76. end
  77. def resource_params
  78. params.require(:custom_emoji).permit(:shortcode, :image, :visible_in_picker)
  79. end
  80. def filtered_custom_emojis
  81. CustomEmojiFilter.new(filter_params).results
  82. end
  83. def filter_params
  84. params.permit(
  85. :local,
  86. :remote,
  87. :by_domain,
  88. :shortcode
  89. )
  90. end
  91. end
  92. end