From 9c72ff18f9a52851e2776e62cd437c01fb8ae039 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 14 Mar 2017 06:21:35 +0100 Subject: [PATCH] Find article preferences with Twitter::NullObject and replace it with nill to prevent elasticsearch index issue. --- ...ixed_twitter_ticket_article_preferences.rb | 25 +++++++++++++++++++ lib/tweet_base.rb | 15 ++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20170314000002_fixed_twitter_ticket_article_preferences.rb diff --git a/db/migrate/20170314000002_fixed_twitter_ticket_article_preferences.rb b/db/migrate/20170314000002_fixed_twitter_ticket_article_preferences.rb new file mode 100644 index 000000000..ce2cff6d0 --- /dev/null +++ b/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 diff --git a/lib/tweet_base.rb b/lib/tweet_base.rb index 74a257b9c..a5608a37d 100644 --- a/lib/tweet_base.rb +++ b/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