custom_emoji_batch.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }
  32. category = begin
  33. if category_id.present?
  34. CustomEmojiCategory.find(category_id)
  35. elsif category_name.present?
  36. CustomEmojiCategory.find_or_create_by!(name: category_name)
  37. end
  38. end
  39. custom_emojis.each do |custom_emoji|
  40. custom_emoji.update(category_id: category&.id)
  41. log_action :update, custom_emoji
  42. end
  43. end
  44. def list!
  45. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }
  46. custom_emojis.each do |custom_emoji|
  47. custom_emoji.update(visible_in_picker: true)
  48. log_action :update, custom_emoji
  49. end
  50. end
  51. def unlist!
  52. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }
  53. custom_emojis.each do |custom_emoji|
  54. custom_emoji.update(visible_in_picker: false)
  55. log_action :update, custom_emoji
  56. end
  57. end
  58. def enable!
  59. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :enable?) }
  60. custom_emojis.each do |custom_emoji|
  61. custom_emoji.update(disabled: false)
  62. log_action :enable, custom_emoji
  63. end
  64. end
  65. def disable!
  66. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :disable?) }
  67. custom_emojis.each do |custom_emoji|
  68. custom_emoji.update(disabled: true)
  69. log_action :disable, custom_emoji
  70. end
  71. end
  72. def copy!
  73. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :copy?) }
  74. custom_emojis.each do |custom_emoji|
  75. copied_custom_emoji = custom_emoji.copy!
  76. log_action :create, copied_custom_emoji
  77. end
  78. end
  79. def delete!
  80. custom_emojis.each { |custom_emoji| authorize(custom_emoji, :destroy?) }
  81. custom_emojis.each do |custom_emoji|
  82. custom_emoji.destroy
  83. log_action :destroy, custom_emoji
  84. end
  85. end
  86. end