Browse Source

Fixes #2736 - Retweet conversion setting not working

=== Background

Zammad's Twitter integration creates new articles
whenever certain kinds of tweets are created
(normally, whenever someone @mentions you
or a tweet includes a word you're watching for).

But if someone @mentions you and then a million people RT that,
you might not want a separate article for every single one of those RTs.
That's why the Twitter integration config has a "track retweets" setting
which is enabled by default.

=== Problem

In #2736[0], @GreXXL points out that
Zammad does not honor the "track retweets" setting.
Actually, it does--but only in Channel::Driver::Twitter#fetch,
which is not where Zammad gets new tweets from.

Rather, new tweets come from Twitter's webhook API,
which hits Zammad's callback URL (POST /api/v1/channels_twitter_webhook)
whenever the relevant tweets are created on Twitter's servers.
These webhook events are turned into Zammad articles
in the TwitterSync#process_webhook method,
which previously had no logic for checking the "track retweets" setting.

=== Fix

This commit adds the appropriate logic
to Zammad's Twitter webhook handler.

[0]: https://github.com/zammad/zammad/issues/2736
Ryan Lue 4 years ago
parent
commit
fa9242dea1
2 changed files with 30 additions and 0 deletions
  1. 1 0
      lib/twitter_sync.rb
  2. 29 0
      spec/models/channel/driver/twitter_spec.rb

+ 1 - 0
lib/twitter_sync.rb

@@ -704,6 +704,7 @@ process webhook messages from twitter
     if @payload['tweet_create_events'].present?
       @payload['tweet_create_events'].each do |item|
         next if Ticket::Article.exists?(message_id: item['id'])
+        next if item.key?('retweeted_status') && !channel.options.dig('sync', 'track_retweets')
 
         # check if it's mention
         group_id = nil

+ 29 - 0
spec/models/channel/driver/twitter_spec.rb

@@ -568,6 +568,35 @@ RSpec.describe Channel::Driver::Twitter do
             expect(Ticket::Article.last.attachments).to be_one
           end
         end
+
+        context 'when message is a retweet' do
+          let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-retweet.yml') }
+
+          context 'and "conversion of retweets" is enabled' do
+            before do
+              channel.options['sync']['track_retweets'] = true
+              channel.save
+            end
+
+            it 'creates a new article' do
+              expect { channel.process(payload) }
+                .to change(Ticket::Article, :count).by(1)
+                .and change { Ticket::Article.exists?(article_attributes) }.to(true)
+            end
+          end
+
+          context 'and "conversion of retweets" is disabled' do
+            before do
+              channel.options['sync']['track_retweets'] = false
+              channel.save
+            end
+
+            it 'does not create a new article' do
+              expect { channel.process(payload) }
+                .not_to change(Ticket::Article, :count)
+            end
+          end
+        end
       end
     end