trabajo-afectivo/app/models/channel/driver/twitter.rb

224 lines
5.1 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2015-07-02 15:13:04 +00:00
2016-01-10 13:24:54 +00:00
class Channel::Driver::Twitter
2015-12-14 09:23:14 +00:00
=begin
fetch tweets from twitter account
options = {
adapter: 'twitter',
auth: {
consumer_key: consumer_key,
consumer_secret: consumer_secret,
oauth_token: armin_theo_token,
oauth_token_secret: armin_theo_token_secret,
},
sync: {
search: [
{
term: '#citheo42',
group_id: 2,
},
{
term: '#citheo24',
group_id: 1,
},
],
mentions: {
group_id: 2,
},
direct_messages: {
group_id: 2,
}
}
}
instance = Channel::Driver::Twitter.new
result = instance.fetch(options, channel)
returns
{
result: 'ok',
}
=end
def fetch(options, channel)
2015-07-02 15:13:04 +00:00
options = self.class.check_external_credential(options)
2015-12-21 00:48:49 +00:00
@client = TwitterSync.new(options[:auth])
@sync = options[:sync]
@channel = channel
2015-07-02 15:13:04 +00:00
Rails.logger.debug { 'twitter fetch started' }
2015-07-02 15:13:04 +00:00
fetch_search
2015-07-02 15:13:04 +00:00
disconnect
Rails.logger.debug { 'twitter fetch completed' }
2015-12-14 09:23:14 +00:00
{
result: 'ok',
notice: '',
2015-12-14 09:23:14 +00:00
}
2015-07-02 15:13:04 +00:00
end
2016-01-10 13:24:54 +00:00
=begin
instance = Channel::Driver::Twitter.new
instance.fetchable?(channel)
=end
def fetchable?(channel)
return true if Rails.env.test?
# only fetch once in 30 minutes
2016-01-10 13:24:54 +00:00
return true if !channel.preferences
return true if !channel.preferences[:last_fetch]
return false if channel.preferences[:last_fetch] > Time.zone.now - 20.minutes
2016-01-10 13:24:54 +00:00
true
end
2015-12-14 09:23:14 +00:00
=begin
2015-07-02 15:13:04 +00:00
2015-12-14 09:23:14 +00:00
instance = Channel::Driver::Twitter.new
instance.send(
{
adapter: 'twitter',
auth: {
consumer_key: consumer_key,
consumer_secret: consumer_secret,
oauth_token: armin_theo_token,
oauth_token_secret: armin_theo_token_secret,
},
},
twitter_attributes,
notification
)
2015-07-02 15:13:04 +00:00
2015-12-14 09:23:14 +00:00
=end
2015-07-02 15:13:04 +00:00
2015-12-14 09:23:14 +00:00
def send(options, article, _notification = false)
# return if we run import mode
return if Setting.get('import_mode')
options = self.class.check_external_credential(options)
2015-12-21 00:48:49 +00:00
@client = TwitterSync.new(options[:auth])
tweet = @client.from_article(article)
2015-12-14 09:23:14 +00:00
disconnect
2015-07-02 15:13:04 +00:00
tweet
end
def disconnect
@client&.disconnect
end
=begin
Channel::Driver::Twitter.streamable?
returns
true|false
=end
def self.streamable?
false
end
2016-01-09 12:23:11 +00:00
=begin
Channel::Driver::Twitter.process(payload, channel)
2016-01-09 12:23:11 +00:00
=end
def process(_adapter_options, payload, channel)
@client = TwitterSync.new(channel.options[:auth], payload)
@client.process_webhook(channel)
end
def self.check_external_credential(options)
if options[:auth] && options[:auth][:external_credential_id]
external_credential = ExternalCredential.find_by(id: options[:auth][:external_credential_id])
raise "No such ExternalCredential.find(#{options[:auth][:external_credential_id]})" if !external_credential
options[:auth][:consumer_key] = external_credential.credentials['consumer_key']
options[:auth][:consumer_secret] = external_credential.credentials['consumer_secret']
end
options
2015-07-02 15:13:04 +00:00
end
private
def fetch_search
2016-12-02 11:24:00 +00:00
return if @sync[:search].blank?
@sync[:search].each do |search|
2016-12-02 11:24:00 +00:00
next if search[:term].blank?
next if search[:term] == '#'
2016-12-02 11:24:00 +00:00
next if search[:group_id].blank?
2015-07-02 15:13:04 +00:00
result_type = search[:type] || 'mixed'
Rails.logger.debug { " - searching for '#{search[:term]}'" }
older_import = 0
older_import_max = 20
@client.client.search(search[:term], result_type: result_type).collect do |tweet|
next if !track_retweets? && tweet.retweet?
# ignore older messages
2018-12-07 12:11:08 +00:00
if @sync[:import_older_tweets] != true
if (@channel.created_at - 15.days) > tweet.created_at.dup.utc || older_import >= older_import_max # rubocop:disable Style/SoleNestedConditional
2018-12-07 12:11:08 +00:00
older_import += 1
Rails.logger.debug { "tweet to old: #{tweet.id}/#{tweet.created_at}" }
next
end
end
next if @client.locale_sender?(tweet) && own_tweet_already_imported?(tweet)
next if Ticket::Article.exists?(message_id: tweet.id)
break if @client.tweet_limit_reached(tweet)
2015-07-02 15:13:04 +00:00
@client.to_group(tweet, search[:group_id], @channel)
end
end
2015-07-02 15:13:04 +00:00
end
def track_retweets?
@channel.options && @channel.options['sync'] && @channel.options['sync']['track_retweets']
end
def own_tweet_already_imported?(tweet)
event_time = Time.zone.now
sleep 4
12.times do |loop_count|
if Ticket::Article.exists?(message_id: tweet.id)
Rails.logger.debug { "Own tweet already imported, skipping tweet #{tweet.id}" }
return true
end
count = Delayed::Job.where('created_at < ?', event_time).count
break if count.zero?
sleep_time = 2 * count
sleep_time = 5 if sleep_time > 5
Rails.logger.debug { "Delay importing own tweets - sleep #{sleep_time} (loop #{loop_count})" }
sleep sleep_time
end
if Ticket::Article.exists?(message_id: tweet.id)
Rails.logger.debug { "Own tweet already imported, skipping tweet #{tweet.id}" }
return true
end
false
end
2015-07-02 15:13:04 +00:00
end