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

This commit is contained in:
Thorsten Eckel 2017-01-19 16:23:41 +01:00
parent 417b84ca67
commit 31eac0c1fa
6 changed files with 108 additions and 9 deletions

View file

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

View file

@ -22,13 +22,17 @@
<%- @Icon('plus-small') %> <%- @Icon('plus-small') %>
</tfoot> </tfoot>
</table> </table>
<h3><%- @T('Mentions Group') %></h3> <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> <div class="js-mentionsGroup"></div>
<h3><%- @T('Direct Messages Group') %></h3> <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> <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> </fieldset>

View file

@ -60,6 +60,16 @@
<% end %> <% end %>
</div> </div>
</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="action-controls">
<div class="btn btn--danger btn--secondary js-delete"><%- @T('Delete') %></div> <div class="btn btn--danger btn--secondary js-delete"><%- @T('Delete') %></div>
<% if channel.active is true: %> <% if channel.active is true: %>

View file

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

View file

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

View file

@ -78,6 +78,7 @@ class TwitterTest < ActiveSupport::TestCase
id: system_id, id: system_id,
}, },
sync: { sync: {
track_retweets: true,
search: [ search: [
{ {
term: hash_tag2, term: hash_tag2,
@ -527,8 +528,6 @@ class TwitterTest < ActiveSupport::TestCase
channel = Channel.find(channel_id) channel = Channel.find(channel_id)
assert_equal('', channel.last_log_out) assert_equal('', channel.last_log_out)
assert_equal('ok', channel.status_out) assert_equal('ok', channel.status_out)
#assert_equal('', channel.last_log_in)
#assert_equal('ok', channel.status_in)
# get dm via stream # get dm via stream
client = Twitter::REST::Client.new( client = Twitter::REST::Client.new(
@ -555,7 +554,84 @@ class TwitterTest < ActiveSupport::TestCase
assert(article, "inbound article '#{text}' created") assert(article, "inbound article '#{text}' created")
assert_equal(customer_login, article.from, 'ticket article from') assert_equal(customer_login, article.from, 'ticket article from')
assert_equal(system_login, article.to, 'ticket article to') 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 end
def hash_gen def hash_gen
@ -563,7 +639,7 @@ class TwitterTest < ActiveSupport::TestCase
end end
def rand_word def rand_word
words = [ [
'dog', 'dog',
'cat', 'cat',
'house', 'house',
@ -580,8 +656,7 @@ class TwitterTest < ActiveSupport::TestCase
'stay tuned', 'stay tuned',
'be a good boy', 'be a good boy',
'invent new things', 'invent new things',
] ].sample
words[rand(words.length)]
end end
end end