20180410000001_cleanup_user_preferences_notification_sound2.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class CleanupUserPreferencesNotificationSound2 < ActiveRecord::Migration[5.1]
  3. def local_to_h!(value)
  4. if value.instance_of?(ActionController::Parameters)
  5. value = value.permit!.to_h
  6. end
  7. if value.instance_of?(Hash) || value.instance_of?(ActiveSupport::HashWithIndifferentAccess)
  8. value.each_key do |local_key|
  9. value[local_key] = local_to_h!(value[local_key])
  10. end
  11. end
  12. value
  13. end
  14. def local_clear_preferences(user)
  15. return false if !user
  16. return false if !user.preferences
  17. return false if !user.preferences[:notification_sound]
  18. return false if !user.preferences[:notification_sound][:enabled]
  19. if user.preferences[:notification_sound][:enabled] == 'true'
  20. user.preferences[:notification_sound][:enabled] = true
  21. user.save!
  22. return true
  23. end
  24. return false if user.preferences[:notification_sound][:enabled] != 'false'
  25. user.preferences[:notification_sound][:enabled] = false
  26. user.save!
  27. true
  28. end
  29. def up
  30. User.with_permissions('ticket.agent').each do |user|
  31. local_to_h!(user.preferences)
  32. user.save!
  33. end
  34. items = SearchIndexBackend.search('preferences.notification_sound.enabled:*', 'User', limit: 3000) || []
  35. items.each do |item|
  36. next if !item[:id]
  37. user = User.find_by(id: item[:id])
  38. local_to_h!(user.preferences)
  39. local_clear_preferences(user)
  40. end
  41. Organization.all.each do |organization|
  42. organization.members.each do |user|
  43. local_to_h!(user.preferences)
  44. local_clear_preferences(user)
  45. end
  46. end
  47. Delayed::Job.limit(2_000).each do |job|
  48. Delayed::Worker.new.run(job)
  49. end
  50. end
  51. end