Browse Source

Merged pull request #630 - add sync setting 'track_retweets' to twitter channel - Big thanks to @schurig.

Thorsten Eckel 8 years ago
parent
commit
31eac0c1fa

+ 4 - 0
app/assets/javascripts/app/controllers/_channel/twitter.coffee

@@ -268,6 +268,10 @@ class AccountEdit extends App.ControllerModal
           position += 1
       else
         search.push params.search
+    if params.track_retweets
+      params.track_retweets = true
+    else
+      params.track_retweets = false
     params.search = search
     @channel.options.sync = params
     @ajax(

+ 7 - 3
app/assets/javascripts/app/views/twitter/account_edit.jst.eco

@@ -22,13 +22,17 @@
           <%- @Icon('plus-small') %>
     </tfoot>
   </table>
-  
+
   <h3><%- @T('Mentions Group') %></h3>
-  <p class="description"><%- @T('Choose which group %s will get added to.', 'mentions') %></p>
+  <p class="description"><%- @T('Choose which group mentions will get added to.') %></p>
   <div class="js-mentionsGroup"></div>
 
   <h3><%- @T('Direct Messages Group') %></h3>
-  <p class="description"><%- @T('Choose which group %s will get added to.', 'direct messages') %></p>
+  <p class="description"><%- @T('Choose which group direct messages will get added to.') %></p>
   <div class="js-directMessagesGroup"></div>
 
+  <h3><%- @T('Retweets') %></h3>
+  <p class="description"><%- @T('Choose if retweets should also be converted to tickets.') %></p>
+  <input name="track_retweets" type="checkbox" id="setting-chat" value="true" <% if @channel.options.sync.track_retweets: %>checked<% end %>> <%- @T('Track retweets') %>
+
 </fieldset>

+ 10 - 0
app/assets/javascripts/app/views/twitter/list.jst.eco

@@ -60,6 +60,16 @@
         <% end %>
       </div>
     </div>
+    <div class="action-flow action-flow--row">
+      <div class="action-block">
+        <h3><%- @T('Retweets') %></h3>
+        <% if channel.options.sync.track_retweets: %>
+          <%- @T('Retweets are converted to Tickets') %>.
+        <% else: %>
+          <%- @T('Conversion of retweets to tickets is turned off') %>.
+        <% end %>
+      </div>
+    </div>
     <div class="action-controls">
       <div class="btn btn--danger btn--secondary js-delete"><%- @T('Delete') %></div>
       <% if channel.active is true: %>

+ 5 - 0
app/models/channel/driver/twitter.rb

@@ -278,6 +278,7 @@ returns
       older_import = 0
       older_import_max = 20
       @rest_client.client.search(search[:term], result_type: result_type).collect { |tweet|
+        next if !track_retweets? && tweet.retweet?
 
         # ignore older messages
         if (@channel.created_at - 15.days) > tweet.created_at || older_import >= older_import_max
@@ -299,6 +300,7 @@ returns
     older_import = 0
     older_import_max = 20
     @rest_client.client.mentions_timeline.each { |tweet|
+      next if !track_retweets? && tweet.retweet?
 
       # ignore older messages
       if (@channel.created_at - 15.days) > tweet.created_at || older_import >= older_import_max
@@ -342,4 +344,7 @@ returns
     options
   end
 
+  def track_retweets?
+    @channel.options && @channel.options['sync'] && @channel.options['sync']['track_retweets']
+  end
 end

+ 2 - 1
lib/external_credential/twitter.rb

@@ -72,7 +72,8 @@ class ExternalCredential::Twitter
           limit: 20,
           search: [],
           mentions: {},
-          direct_messages: {}
+          direct_messages: {},
+          track_retweets: false
         }
       },
       active: true,

+ 80 - 5
test/integration/twitter_test.rb

@@ -78,6 +78,7 @@ class TwitterTest < ActiveSupport::TestCase
         id: system_id,
       },
       sync: {
+        track_retweets: true,
         search: [
           {
             term: hash_tag2,
@@ -527,8 +528,6 @@ class TwitterTest < ActiveSupport::TestCase
     channel = Channel.find(channel_id)
     assert_equal('', channel.last_log_out)
     assert_equal('ok', channel.status_out)
-    #assert_equal('', channel.last_log_in)
-    #assert_equal('ok', channel.status_in)
 
     # get dm via stream
     client = Twitter::REST::Client.new(
@@ -555,7 +554,84 @@ class TwitterTest < ActiveSupport::TestCase
     assert(article, "inbound article '#{text}' created")
     assert_equal(customer_login, article.from, 'ticket article from')
     assert_equal(system_login, article.to, 'ticket article to')
+  end
+
+  test 'e track_retweets enabled' do
+
+    client = Twitter::REST::Client.new do |config|
+      config.consumer_key        = consumer_key
+      config.consumer_secret     = consumer_secret
+      config.access_token        = system_token
+      config.access_token_secret = system_token_secret
+    end
+
+    hash  = "#{hash_tag1} ##{hash_gen}"
+    text  = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
+    tweet = client.update(text)
+
+    client = Twitter::REST::Client.new(
+      consumer_key:        consumer_key,
+      consumer_secret:     consumer_secret,
+      access_token:        customer_token,
+      access_token_secret: customer_token_secret
+    )
+
+    retweet = client.retweet(tweet).first
+
+    # fetch check system account
+    sleep 15
+    article = nil
+    1.times {
+      Channel.fetch
+
+      # check if ticket and article has been created
+      article = Ticket::Article.find_by(message_id: retweet.id)
+      break if article
+      sleep 10
+    }
+
+    assert(article, "retweet article '#{text}' created")
+  end
+
+  test 'f track_retweets disabled' do
+
+    # disable track_retweets
+    channel[:options]['sync']['track_retweets'] = false
+    channel.save!
+
+    client = Twitter::REST::Client.new do |config|
+      config.consumer_key        = consumer_key
+      config.consumer_secret     = consumer_secret
+      config.access_token        = system_token
+      config.access_token_secret = system_token_secret
+    end
+
+    hash  = "#{hash_tag1} ##{hash_gen}"
+    text  = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
+    tweet = client.update(text)
+
+    client = Twitter::REST::Client.new(
+      consumer_key:        consumer_key,
+      consumer_secret:     consumer_secret,
+      access_token:        customer_token,
+      access_token_secret: customer_token_secret
+    )
+
+    retweet = client.retweet(tweet).first
+
+    # fetch check system account
+    sleep 15
+    article = nil
+    1.times {
+      Channel.fetch
+
+      # check if ticket and article has been created
+      article = Ticket::Article.find_by(message_id: retweet.id)
+      break if article
+      sleep 10
+    }
 
+    assert_equal(nil, article, "retweet article '#{text}' not created")
   end
 
   def hash_gen
@@ -563,7 +639,7 @@ class TwitterTest < ActiveSupport::TestCase
   end
 
   def rand_word
-    words = [
+    [
       'dog',
       'cat',
       'house',
@@ -580,8 +656,7 @@ class TwitterTest < ActiveSupport::TestCase
       'stay tuned',
       'be a good boy',
       'invent new things',
-    ]
-    words[rand(words.length)]
+    ].sample
   end
 
 end