From fa9242dea1e14faf9f4cd8cd7029f8ef5f69bc94 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 21 Aug 2020 18:12:17 +0800 Subject: [PATCH] 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 --- lib/twitter_sync.rb | 1 + spec/models/channel/driver/twitter_spec.rb | 29 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/twitter_sync.rb b/lib/twitter_sync.rb index e99bccc43..d0850fc16 100644 --- a/lib/twitter_sync.rb +++ b/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 diff --git a/spec/models/channel/driver/twitter_spec.rb b/spec/models/channel/driver/twitter_spec.rb index b7e4e0054..56ff4ff5a 100644 --- a/spec/models/channel/driver/twitter_spec.rb +++ b/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