custom_emoji_batch.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # frozen_string_literal: true
  2. class Form::CustomEmojiBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. include AccountableConcern
  6. attr_accessor :custom_emoji_ids, :action, :current_account,
  7. :category_id, :category_name, :visible_in_picker
  8. def save
  9. case action
  10. when 'update'
  11. update!
  12. when 'list'
  13. list!
  14. when 'unlist'
  15. unlist!
  16. when 'enable'
  17. enable!
  18. when 'disable'
  19. disable!
  20. when 'copy'
  21. copy!
  22. when 'delete'
  23. delete!
  24. end
  25. end
  26. private
  27. def custom_emojis
  28. @custom_emojis ||= CustomEmoji.where(id: custom_emoji_ids)
  29. end
  30. def update!
  31. verify_authorization(:update?)
  32. category = if category_id.present?
  33. CustomEmojiCategory.find(category_id)
  34. elsif category_name.present?
  35. CustomEmojiCategory.find_or_create_by!(name: category_name)
  36. end
  37. custom_emojis.each do |custom_emoji|
  38. custom_emoji.update(category_id: category&.id)
  39. log_action :update, custom_emoji
  40. end
  41. end
  42. def list!
  43. verify_authorization(:update?)
  44. custom_emojis.each do |custom_emoji|
  45. custom_emoji.update(visible_in_picker: true)
  46. log_action :update, custom_emoji
  47. end
  48. end
  49. def unlist!
  50. verify_authorization(:update?)
  51. custom_emojis.each do |custom_emoji|
  52. custom_emoji.update(visible_in_picker: false)
  53. log_action :update, custom_emoji
  54. end
  55. end
  56. def enable!
  57. verify_authorization(:enable?)
  58. custom_emojis.each do |custom_emoji|
  59. custom_emoji.update(disabled: false)
  60. log_action :enable, custom_emoji
  61. end
  62. end
  63. def disable!
  64. verify_authorization(:disable?)
  65. custom_emojis.each do |custom_emoji|
  66. custom_emoji.update(disabled: true)
  67. log_action :disable, custom_emoji
  68. end
  69. end
  70. def copy!
  71. verify_authorization(:copy?)
  72. custom_emojis.each do |custom_emoji|
  73. copied_custom_emoji = custom_emoji.copy!
  74. log_action :create, copied_custom_emoji
  75. end
  76. end
  77. def delete!
  78. verify_authorization(:destroy?)
  79. custom_emojis.each do |custom_emoji|
  80. custom_emoji.destroy
  81. log_action :destroy, custom_emoji
  82. end
  83. end
  84. def verify_authorization(permission)
  85. custom_emojis.each { |custom_emoji| authorize(custom_emoji, permission) }
  86. end
  87. end