20180806000001_fixed_twitter_ticket_article_preferences7.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class FixedTwitterTicketArticlePreferences7 < ActiveRecord::Migration[5.0]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. # find article preferences with Twitter::NullObject and replace it with nill to prevent elasticsearch index issue
  7. article_type_ids = Ticket::Article::Type.where(name: ['twitter status', 'twitter direct-message']).pluck(:id)
  8. article_ids = Ticket::Article.where(type_id: article_type_ids).pluck(:id)
  9. article_ids.each do |article_id|
  10. article = Ticket::Article.find(article_id)
  11. next if !article.preferences
  12. changed = false
  13. article.preferences.each_value do |value|
  14. next if value.class != ActiveSupport::HashWithIndifferentAccess
  15. value.each do |sub_key, sub_level|
  16. if sub_level.instance_of?(NilClass)
  17. value[sub_key] = nil
  18. next
  19. end
  20. if sub_level.instance_of?(Twitter::Place) || sub_level.instance_of?(Twitter::Geo)
  21. value[sub_key] = sub_level.to_h
  22. changed = true
  23. next
  24. end
  25. next if sub_level.class != Twitter::NullObject
  26. value[sub_key] = nil
  27. changed = true
  28. end
  29. end
  30. if article.preferences[:twitter]&.key?(:geo) && article.preferences[:twitter][:geo].nil?
  31. article.preferences[:twitter][:geo] = {}
  32. changed = true
  33. end
  34. if article.preferences[:twitter]&.key?(:place) && article.preferences[:twitter][:place].nil?
  35. article.preferences[:twitter][:place] = {}
  36. changed = true
  37. end
  38. next if !changed
  39. article.save!
  40. end
  41. end
  42. end