Followup for issue #1394 - Reindex elastic search not possible because of <null>/Twitter::NullObject.

This commit is contained in:
Martin Edenhofer 2018-10-25 01:46:43 +02:00
parent e0fd1e52ec
commit f074c0c734
3 changed files with 153 additions and 23 deletions

View file

@ -1,4 +1,4 @@
class FixedTwitterTicketArticlePreferences6 < ActiveRecord::Migration[5.0] class FixedTwitterTicketArticlePreferences7 < ActiveRecord::Migration[5.0]
def up def up
# return if it's a new setup # return if it's a new setup

View file

@ -366,10 +366,50 @@ class TweetBase
false false
end end
=begin
replace Twitter::Place and Twitter::Geo as hash and replace Twitter::NullObject with nil
preferences = TweetBase.preferences_cleanup(
twitter: twitter_preferences,
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
)
or
preferences = {
twitter: TweetBase.preferences_cleanup(twitter_preferences),
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
}
=end
def self.preferences_cleanup(preferences) def self.preferences_cleanup(preferences)
# replace Twitter::NullObject with nill to prevent elasticsearch index issue # replace Twitter::NullObject with nill to prevent elasticsearch index issue
preferences.each_value do |value| preferences.each do |key, value|
if value.class == Twitter::Place || value.class == Twitter::Geo
preferences[key] = value.to_h
next
end
if value.class == Twitter::NullObject
preferences[key] = nil
next
end
next if !value.is_a?(Hash) next if !value.is_a?(Hash)
value.each do |sub_key, sub_level| value.each do |sub_key, sub_level|
@ -387,12 +427,21 @@ class TweetBase
end end
end end
if preferences[:twitter]
if preferences[:twitter][:geo].blank?
preferences[:twitter][:geo] = {}
end
if preferences[:twitter][:place].blank?
preferences[:twitter][:place] = {}
end
else
if preferences[:geo].blank? if preferences[:geo].blank?
preferences[:geo] = {} preferences[:geo] = {}
end end
if preferences[:place].blank? if preferences[:place].blank?
preferences[:place] = {} preferences[:place] = {}
end end
end
preferences preferences
end end

View file

@ -40,7 +40,7 @@ class TicketArticleTwitter < ActiveSupport::TestCase
truncated: false truncated: false
} }
preferences = { preferences = {
twitter: twitter_preferences, twitter: TweetBase.preferences_cleanup(twitter_preferences),
links: [ links: [
{ {
url: 'https://twitter.com/statuses/123', url: 'https://twitter.com/statuses/123',
@ -56,16 +56,56 @@ class TicketArticleTwitter < ActiveSupport::TestCase
from: '@example', from: '@example',
body: 'some tweet', body: 'some tweet',
internal: false, internal: false,
preferences: TweetBase.preferences_cleanup(preferences), preferences: preferences,
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
assert(article1.preferences[:twitter]) assert(article1.preferences[:twitter])
assert_equal(1_234_567_890, article1.preferences[:twitter][:mention_ids][0]) assert_equal(1_234_567_890, article1.preferences[:twitter][:mention_ids][0])
assert_nil(article1.preferences[:twitter][:geo]) assert_equal(ActiveSupport::HashWithIndifferentAccess, article1.preferences[:twitter][:geo].class)
assert_equal(NilClass, article1.preferences[:twitter][:geo].class) assert(article1.preferences[:twitter][:geo].blank?)
assert_nil(article1.preferences[:twitter][:place]) assert_equal(ActiveSupport::HashWithIndifferentAccess, article1.preferences[:twitter][:place].class)
assert_equal(NilClass, article1.preferences[:twitter][:place].class) assert(article1.preferences[:twitter][:place].blank?)
twitter_preferences = {
mention_ids: [1_234_567_890],
geo: Twitter::NullObject.new,
retweeted: false,
possibly_sensitive: false,
in_reply_to_user_id: 1_234_567_890,
place: Twitter::NullObject.new,
retweet_count: 0,
source: '<a href="http://example.com/software/tweetbot/mac" rel="nofollow">Tweetbot for Mac</a>',
favorited: false,
truncated: false
}
preferences = TweetBase.preferences_cleanup(
twitter: twitter_preferences,
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
)
article2 = Ticket::Article.create!(
ticket_id: ticket1.id,
type_id: Ticket::Article::Type.find_by(name: 'twitter status').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
from: '@example',
body: 'some tweet',
internal: false,
preferences: preferences,
updated_by_id: 1,
created_by_id: 1,
)
assert(article2.preferences[:twitter])
assert_equal(1_234_567_890, article2.preferences[:twitter][:mention_ids][0])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article2.preferences[:twitter][:geo].class)
assert(article2.preferences[:twitter][:geo].blank?)
assert_equal(ActiveSupport::HashWithIndifferentAccess, article2.preferences[:twitter][:place].class)
assert(article2.preferences[:twitter][:place].blank?)
twitter_preferences = { twitter_preferences = {
mention_ids: [1_234_567_890], mention_ids: [1_234_567_890],
@ -80,7 +120,7 @@ class TicketArticleTwitter < ActiveSupport::TestCase
truncated: false truncated: false
} }
preferences = { preferences = {
twitter: twitter_preferences, twitter: TweetBase.preferences_cleanup(twitter_preferences),
links: [ links: [
{ {
url: 'https://twitter.com/statuses/123', url: 'https://twitter.com/statuses/123',
@ -90,23 +130,64 @@ class TicketArticleTwitter < ActiveSupport::TestCase
], ],
} }
article2 = Ticket::Article.create!( article3 = Ticket::Article.create!(
ticket_id: ticket1.id, ticket_id: ticket1.id,
type_id: Ticket::Article::Type.find_by(name: 'twitter status').id, type_id: Ticket::Article::Type.find_by(name: 'twitter status').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id, sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
from: '@example', from: '@example',
body: 'some tweet', body: 'some tweet',
internal: false, internal: false,
preferences: TweetBase.preferences_cleanup(preferences), preferences: preferences,
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
assert(article2.preferences[:twitter]) assert(article3.preferences[:twitter])
assert_equal(1_234_567_890, article2.preferences[:twitter][:mention_ids][0]) assert_equal(1_234_567_890, article3.preferences[:twitter][:mention_ids][0])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article2.preferences[:twitter][:geo].class) assert_equal(ActiveSupport::HashWithIndifferentAccess, article3.preferences[:twitter][:geo].class)
assert_equal({ 'coordinates' => [1, 1] }, article2.preferences[:twitter][:geo]) assert_equal({ 'coordinates' => [1, 1] }, article3.preferences[:twitter][:geo])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article2.preferences[:twitter][:place].class) assert_equal(ActiveSupport::HashWithIndifferentAccess, article3.preferences[:twitter][:place].class)
assert_equal({ 'country' => 'da', 'name' => 'do', 'woeid' => 1, 'id' => 1 }, article2.preferences[:twitter][:place]) assert_equal({ 'country' => 'da', 'name' => 'do', 'woeid' => 1, 'id' => 1 }, article3.preferences[:twitter][:place])
twitter_preferences = {
mention_ids: [1_234_567_890],
geo: Twitter::Geo.new(coordinates: [1, 1]),
retweeted: false,
possibly_sensitive: false,
in_reply_to_user_id: 1_234_567_890,
place: Twitter::Place.new(country: 'da', name: 'do', woeid: 1, id: 1),
retweet_count: 0,
source: '<a href="http://example.com/software/tweetbot/mac" rel="nofollow">Tweetbot for Mac</a>',
favorited: false,
truncated: false
}
preferences = TweetBase.preferences_cleanup(
twitter: twitter_preferences,
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
)
article4 = Ticket::Article.create!(
ticket_id: ticket1.id,
type_id: Ticket::Article::Type.find_by(name: 'twitter status').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
from: '@example',
body: 'some tweet',
internal: false,
preferences: preferences,
updated_by_id: 1,
created_by_id: 1,
)
assert(article4.preferences[:twitter])
assert_equal(1_234_567_890, article4.preferences[:twitter][:mention_ids][0])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article4.preferences[:twitter][:geo].class)
assert_equal({ 'coordinates' => [1, 1] }, article4.preferences[:twitter][:geo])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article4.preferences[:twitter][:place].class)
assert_equal({ 'country' => 'da', 'name' => 'do', 'woeid' => 1, 'id' => 1 }, article4.preferences[:twitter][:place])
end end