Browse Source

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

Martin Edenhofer 7 years ago
parent
commit
4094bd1d51

+ 2 - 2
db/migrate/20170725000001_fixed_twitter_ticket_article_preferences3.rb → db/migrate/20170908000001_fixed_twitter_ticket_article_preferences4.rb

@@ -1,11 +1,11 @@
-class FixedTwitterTicketArticlePreferences3 < ActiveRecord::Migration[4.2]
+class FixedTwitterTicketArticlePreferences4 < 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')
+    article_type = Ticket::Article::Type.find_by(name: ['twitter status', 'twitter direct-message'])
     article_ids = Ticket::Article.where(type_id: article_type.id).pluck(:id)
     article_ids.each { |article_id|
       article = Ticket::Article.find(article_id)

+ 16 - 14
lib/tweet_base.rb

@@ -153,12 +153,12 @@ class TweetBase
     from = nil
     article_type = nil
     in_reply_to = nil
-    preferences = {}
+    twitter_preferences = {}
     if tweet.class == Twitter::DirectMessage
       article_type = 'twitter direct-message'
       to = "@#{tweet.recipient.screen_name}"
       from = "@#{tweet.sender.screen_name}"
-      preferences = {
+      twitter_preferences = {
         created_at: tweet.created_at,
         recipient_id: tweet.recipient.id,
         recipient_screen_name: tweet.recipient.screen_name,
@@ -182,7 +182,7 @@ class TweetBase
       end
       in_reply_to = tweet.in_reply_to_status_id
 
-      preferences = {
+      twitter_preferences = {
         mention_ids: mention_ids,
         geo: tweet.geo,
         retweeted: tweet.retweeted?,
@@ -208,6 +208,17 @@ class TweetBase
       ticket.save!
     end
 
+    article_preferences = {
+      twitter: twitter_preferences,
+      links: [
+        {
+          url: "https://twitter.com/statuses/#{tweet.id}",
+          target: '_blank',
+          name: 'on Twitter',
+        },
+      ],
+    }
+
     Ticket::Article.create!(
       from:        from,
       to:          to,
@@ -218,16 +229,7 @@ class TweetBase
       type_id:     Ticket::Article::Type.find_by(name: article_type).id,
       sender_id:   Ticket::Article::Sender.find_by(name: 'Customer').id,
       internal:    false,
-      preferences: {
-        twitter: preferences_cleanup(preferences),
-        links: [
-          {
-            url: "https://twitter.com/statuses/#{tweet.id}",
-            target: '_blank',
-            name: 'on Twitter',
-          },
-        ],
-      }
+      preferences: preferences_cleanup(article_preferences),
     )
   end
 
@@ -369,7 +371,7 @@ class TweetBase
 
     # replace Twitter::NullObject with nill to prevent elasticsearch index issue
     preferences.each { |_key, value|
-      next if value.class != ActiveSupport::HashWithIndifferentAccess && value.class != Hash
+      next if !value.is_a?(Hash)
       value.each { |sub_key, sub_level|
         if sub_level.class == NilClass
           value[sub_key] = nil

+ 69 - 0
test/unit/ticket_article_twitter_test.rb

@@ -0,0 +1,69 @@
+# encoding: utf-8
+require 'test_helper'
+
+class TicketArticleTwitter < ActiveSupport::TestCase
+
+  test 'preferences cleanup' do
+
+    org_community = Organization.create_if_not_exists(
+      name: 'Zammad Foundation',
+    )
+    user_community = User.create_or_update(
+      login: 'article.twitter@example.org',
+      firstname: 'Article',
+      lastname: 'Twitter',
+      email: 'article.twitter@example.org',
+      password: '',
+      active: true,
+      roles: [ Role.find_by(name: 'Customer') ],
+      organization_id: org_community.id,
+      updated_by_id: 1,
+      created_by_id: 1,
+    )
+
+    ticket1 = Ticket.create!(
+      group_id: Group.first.id,
+      customer_id: user_community.id,
+      title: 'Tweet 1!',
+      updated_by_id: 1,
+      created_by_id: 1,
+    )
+    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(ActiveSupport::HashWithIndifferentAccess.new(
+                                                       twitter: {
+                                                         '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
+                                                       },
+                                                       links: [
+                                                         {
+                                                           url: 'https://twitter.com/statuses/123',
+                                                           target: '_blank',
+                                                           name: 'on Twitter',
+                                                         },
+                                                       ],
+      )),
+      updated_by_id: 1,
+      created_by_id: 1,
+    )
+    assert(article1.preferences[:twitter])
+    assert_equal(1_234_567_890, article1.preferences[:twitter][:mention_ids][0])
+    assert_nil(article1.preferences[:twitter][:geo])
+    assert_equal(NilClass, article1.preferences[:twitter][:geo].class)
+
+  end
+
+end