Browse Source

Find article preferences with Twitter::NullObject and replace it with nill to prevent elasticsearch index issue.

Martin Edenhofer 8 years ago
parent
commit
9c72ff18f9

+ 25 - 0
db/migrate/20170314000002_fixed_twitter_ticket_article_preferences.rb

@@ -0,0 +1,25 @@
+class FixedTwitterTicketArticlePreferences < ActiveRecord::Migration
+  def up
+
+    # return if it's a new setup
+    return if !Setting.find_by(name: 'system_init_done')
+
+    # find article preferences with Twitter::NullObject and replace it with nill to prevent elasticsearch index issue
+    article_type = Ticket::Article::Type.find_by(name: 'twitter status')
+    Ticket::Article.where(type_id: article_type.id).each { |article|
+      next if !article.preferences
+      changed = false
+      article.preferences.each { |_key, value|
+        next if value.class != ActiveSupport::HashWithIndifferentAccess
+        value.each { |sub_key, sub_level|
+          next if sub_level.class != Twitter::NullObject
+          value[sub_key] = nil
+          changed = true
+        }
+      }
+      next if !changed
+      article.save!
+    }
+
+  end
+end

+ 14 - 1
lib/tweet_base.rb

@@ -219,7 +219,7 @@ class TweetBase
       sender_id:   Ticket::Article::Sender.find_by(name: 'Customer').id,
       internal:    false,
       preferences: {
-        twitter: preferences,
+        twitter: preferences_cleanup(preferences),
         links: [
           {
             url: "https://twitter.com/statuses/#{tweet.id}",
@@ -373,4 +373,17 @@ class TweetBase
     false
   end
 
+  def preferences_cleanup(preferences)
+
+    # replace Twitter::NullObject with nill to prevent elasticsearch index issue
+    preferences.each { |_key, value|
+      next if value.class != ActiveSupport::HashWithIndifferentAccess
+      value.each { |sub_key, sub_level|
+        next if sub_level.class != Twitter::NullObject
+        value[sub_key] = nil
+      }
+    }
+    preferences
+  end
+
 end