From 31eac0c1fa415eca845b66bd36ed4306a3ccb961 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 19 Jan 2017 16:23:41 +0100 Subject: [PATCH] Merged pull request #630 - add sync setting 'track_retweets' to twitter channel - Big thanks to @schurig. --- .../app/controllers/_channel/twitter.coffee | 4 + .../app/views/twitter/account_edit.jst.eco | 10 ++- .../app/views/twitter/list.jst.eco | 10 +++ app/models/channel/driver/twitter.rb | 5 ++ lib/external_credential/twitter.rb | 3 +- test/integration/twitter_test.rb | 85 +++++++++++++++++-- 6 files changed, 108 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_channel/twitter.coffee b/app/assets/javascripts/app/controllers/_channel/twitter.coffee index 448ba774f..77243b001 100644 --- a/app/assets/javascripts/app/controllers/_channel/twitter.coffee +++ b/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( diff --git a/app/assets/javascripts/app/views/twitter/account_edit.jst.eco b/app/assets/javascripts/app/views/twitter/account_edit.jst.eco index 4c310fd79..20729cdfe 100644 --- a/app/assets/javascripts/app/views/twitter/account_edit.jst.eco +++ b/app/assets/javascripts/app/views/twitter/account_edit.jst.eco @@ -22,13 +22,17 @@ <%- @Icon('plus-small') %> - +

<%- @T('Mentions Group') %>

-

<%- @T('Choose which group %s will get added to.', 'mentions') %>

+

<%- @T('Choose which group mentions will get added to.') %>

<%- @T('Direct Messages Group') %>

-

<%- @T('Choose which group %s will get added to.', 'direct messages') %>

+

<%- @T('Choose which group direct messages will get added to.') %>

+

<%- @T('Retweets') %>

+

<%- @T('Choose if retweets should also be converted to tickets.') %>

+ checked<% end %>> <%- @T('Track retweets') %> + \ No newline at end of file diff --git a/app/assets/javascripts/app/views/twitter/list.jst.eco b/app/assets/javascripts/app/views/twitter/list.jst.eco index 6eaeca7c6..7e16c75b2 100644 --- a/app/assets/javascripts/app/views/twitter/list.jst.eco +++ b/app/assets/javascripts/app/views/twitter/list.jst.eco @@ -60,6 +60,16 @@ <% end %> +
+
+

<%- @T('Retweets') %>

+ <% if channel.options.sync.track_retweets: %> + <%- @T('Retweets are converted to Tickets') %>. + <% else: %> + <%- @T('Conversion of retweets to tickets is turned off') %>. + <% end %> +
+
<%- @T('Delete') %>
<% if channel.active is true: %> diff --git a/app/models/channel/driver/twitter.rb b/app/models/channel/driver/twitter.rb index cce2b4cff..a78009395 100644 --- a/app/models/channel/driver/twitter.rb +++ b/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 diff --git a/lib/external_credential/twitter.rb b/lib/external_credential/twitter.rb index 940b66cd8..7368a42f6 100644 --- a/lib/external_credential/twitter.rb +++ b/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, diff --git a/test/integration/twitter_test.rb b/test/integration/twitter_test.rb index d700caa46..f5320a237 100644 --- a/test/integration/twitter_test.rb +++ b/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