Follow up for issue #1394 - added Twitter::Geo as object to convert - Reindex elastic search not possible because of <null>/Twitter::NullObject.

This commit is contained in:
Martin Edenhofer 2018-06-28 17:17:27 +02:00
parent 5c4c41db6f
commit 39d57f3428
3 changed files with 103 additions and 22 deletions

View file

@ -0,0 +1,36 @@
class FixedTwitterTicketArticlePreferences5 < ActiveRecord::Migration[5.0]
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_ids = Ticket::Article::Type.where(name: ['twitter status', 'twitter direct-message']).pluck(:id)
article_ids = Ticket::Article.where(type_id: article_type_ids).pluck(:id)
article_ids.each do |article_id|
article = Ticket::Article.find(article_id)
next if !article.preferences
changed = false
article.preferences.each_value do |value|
next if value.class != ActiveSupport::HashWithIndifferentAccess
value.each do |sub_key, sub_level|
if sub_level.class == NilClass
value[sub_key] = nil
next
end
if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
value[sub_key] = sub_level.attrs
changed = true
next
end
next if sub_level.class != Twitter::NullObject
value[sub_key] = nil
changed = true
end
end
next if !changed
article.save!
end
end
end

View file

@ -373,7 +373,7 @@ class TweetBase
value[sub_key] = nil value[sub_key] = nil
next next
end end
if sub_level.class == Twitter::Place if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
value[sub_key] = sub_level.attrs value[sub_key] = sub_level.attrs
next next
end end

View file

@ -28,26 +28,20 @@ class TicketArticleTwitter < ActiveSupport::TestCase
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
article1 = Ticket::Article.create!( twitter_preferences = {
ticket_id: ticket1.id, mention_ids: [1_234_567_890],
type_id: Ticket::Article::Type.find_by(name: 'twitter status').id, geo: Twitter::NullObject.new,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id, retweeted: false,
from: '@example', possibly_sensitive: false,
body: 'some tweet', in_reply_to_user_id: 1_234_567_890,
internal: false, place: Twitter::NullObject.new,
preferences: TweetBase.new.preferences_cleanup(ActiveSupport::HashWithIndifferentAccess.new( retweet_count: 0,
twitter: { source: '<a href="http://example.com/software/tweetbot/mac" rel="nofollow">Tweetbot for Mac</a>',
'mention_ids' => [1_234_567_890], favorited: false,
'geo' => Twitter::NullObject.new, truncated: false
'retweeted' => false, }
'possibly_sensitive' => false, preferences = {
'in_reply_to_user_id' => 1_234_567_890, twitter: twitter_preferences,
'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
},
links: [ links: [
{ {
url: 'https://twitter.com/statuses/123', url: 'https://twitter.com/statuses/123',
@ -55,7 +49,15 @@ class TicketArticleTwitter < ActiveSupport::TestCase
name: 'on Twitter', name: 'on Twitter',
}, },
], ],
)), }
article1 = 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: TweetBase.new.preferences_cleanup(preferences),
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
@ -63,6 +65,49 @@ class TicketArticleTwitter < ActiveSupport::TestCase
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_nil(article1.preferences[:twitter][:geo])
assert_equal(NilClass, article1.preferences[:twitter][:geo].class) assert_equal(NilClass, article1.preferences[:twitter][:geo].class)
assert_nil(article1.preferences[:twitter][:place])
assert_equal(NilClass, article1.preferences[:twitter][:place].class)
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 = {
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: TweetBase.new.preferences_cleanup(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_equal({ 'coordinates' => [1, 1] }, article2.preferences[:twitter][:geo])
assert_equal(ActiveSupport::HashWithIndifferentAccess, article2.preferences[:twitter][:place].class)
assert_equal({ 'country' => 'da', 'name' => 'do', 'woeid' => 1, 'id' => 1 }, article2.preferences[:twitter][:place])
end end