Skip to content

Commit

Permalink
Merged pull request zammad#630 - add sync setting 'track_retweets' to…
Browse files Browse the repository at this point in the history
… twitter channel - Big thanks to @schurig.
  • Loading branch information
thorsteneckel committed Jan 20, 2017
1 parent 417b84c commit 31eac0c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions app/assets/javascripts/app/views/twitter/account_edit.jst.eco
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 10 additions & 0 deletions app/assets/javascripts/app/views/twitter/list.jst.eco
Original file line number Diff line number Diff line change
Expand Up @@ -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: %>
Expand Down
5 changes: 5 additions & 0 deletions app/models/channel/driver/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def fetch_search
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
Expand All @@ -299,6 +300,7 @@ def fetch_mentions
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
Expand Down Expand Up @@ -342,4 +344,7 @@ def check_external_credential(options)
options
end

def track_retweets?
@channel.options && @channel.options['sync'] && @channel.options['sync']['track_retweets']
end
end
3 changes: 2 additions & 1 deletion lib/external_credential/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def self.link_account(request_token, params)
limit: 20,
search: [],
mentions: {},
direct_messages: {}
direct_messages: {},
track_retweets: false
}
},
active: true,
Expand Down
85 changes: 80 additions & 5 deletions test/integration/twitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class TwitterTest < ActiveSupport::TestCase
id: system_id,
},
sync: {
track_retweets: true,
search: [
{
term: hash_tag2,
Expand Down Expand Up @@ -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(
Expand All @@ -555,15 +554,92 @@ 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
rand(999).to_s + (0...10).map { ('a'..'z').to_a[rand(26)] }.join
end

def rand_word
words = [
[
'dog',
'cat',
'house',
Expand All @@ -580,8 +656,7 @@ def rand_word
'stay tuned',
'be a good boy',
'invent new things',
]
words[rand(words.length)]
].sample
end

end

0 comments on commit 31eac0c

Please sign in to comment.