twitter.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
  2. require_dependency 'external_credential/twitter'
  3. class Channel::Driver::Twitter
  4. =begin
  5. fetch tweets from twitter account
  6. options = {
  7. adapter: 'twitter',
  8. auth: {
  9. consumer_key: consumer_key,
  10. consumer_secret: consumer_secret,
  11. oauth_token: armin_theo_token,
  12. oauth_token_secret: armin_theo_token_secret,
  13. },
  14. sync: {
  15. search: [
  16. {
  17. term: '#citheo42',
  18. group_id: 2,
  19. },
  20. {
  21. term: '#citheo24',
  22. group_id: 1,
  23. },
  24. ],
  25. mentions: {
  26. group_id: 2,
  27. },
  28. direct_messages: {
  29. group_id: 2,
  30. }
  31. }
  32. }
  33. instance = Channel::Driver::Twitter.new
  34. result = instance.fetch(options, channel)
  35. returns
  36. {
  37. result: 'ok',
  38. }
  39. =end
  40. def fetch(options, channel)
  41. options = self.class.check_external_credential(options)
  42. @client = TwitterSync.new(options[:auth])
  43. @sync = options[:sync]
  44. @channel = channel
  45. Rails.logger.debug { 'twitter fetch started' }
  46. fetch_search
  47. disconnect
  48. Rails.logger.debug { 'twitter fetch completed' }
  49. {
  50. result: 'ok',
  51. notice: '',
  52. }
  53. end
  54. =begin
  55. instance = Channel::Driver::Twitter.new
  56. instance.fetchable?(channel)
  57. =end
  58. def fetchable?(channel)
  59. return true if Rails.env.test?
  60. # only fetch once in 30 minutes
  61. return true if !channel.preferences
  62. return true if !channel.preferences[:last_fetch]
  63. return false if channel.preferences[:last_fetch] > Time.zone.now - 20.minutes
  64. true
  65. end
  66. =begin
  67. instance = Channel::Driver::Twitter.new
  68. instance.send(
  69. {
  70. adapter: 'twitter',
  71. auth: {
  72. consumer_key: consumer_key,
  73. consumer_secret: consumer_secret,
  74. oauth_token: armin_theo_token,
  75. oauth_token_secret: armin_theo_token_secret,
  76. },
  77. },
  78. twitter_attributes,
  79. notification
  80. )
  81. =end
  82. def send(options, article, _notification = false)
  83. # return if we run import mode
  84. return if Setting.get('import_mode')
  85. options = self.class.check_external_credential(options)
  86. @client = TwitterSync.new(options[:auth])
  87. tweet = @client.from_article(article)
  88. disconnect
  89. tweet
  90. end
  91. def disconnect
  92. @client&.disconnect
  93. end
  94. =begin
  95. Channel::Driver::Twitter.streamable?
  96. returns
  97. true|false
  98. =end
  99. def self.streamable?
  100. false
  101. end
  102. =begin
  103. Channel::Driver::Twitter.process(payload, channel)
  104. =end
  105. def process(_adapter_options, payload, channel)
  106. @client = TwitterSync.new(channel.options[:auth], payload)
  107. @client.process_webhook(channel)
  108. end
  109. def self.check_external_credential(options)
  110. if options[:auth] && options[:auth][:external_credential_id]
  111. external_credential = ExternalCredential.find_by(id: options[:auth][:external_credential_id])
  112. raise "No such ExternalCredential.find(#{options[:auth][:external_credential_id]})" if !external_credential
  113. options[:auth][:consumer_key] = external_credential.credentials['consumer_key']
  114. options[:auth][:consumer_secret] = external_credential.credentials['consumer_secret']
  115. end
  116. options
  117. end
  118. private
  119. def fetch_search
  120. return if @sync[:search].blank?
  121. @sync[:search].each do |search|
  122. next if search[:term].blank?
  123. next if search[:term] == '#'
  124. next if search[:group_id].blank?
  125. result_type = search[:type] || 'mixed'
  126. Rails.logger.debug { " - searching for '#{search[:term]}'" }
  127. older_import = 0
  128. older_import_max = 20
  129. @client.client.search(search[:term], result_type: result_type).collect do |tweet|
  130. next if !track_retweets? && tweet.retweet?
  131. # ignore older messages
  132. if @sync[:import_older_tweets] != true
  133. if (@channel.created_at - 15.days) > tweet.created_at.dup.utc || older_import >= older_import_max
  134. older_import += 1
  135. Rails.logger.debug { "tweet to old: #{tweet.id}/#{tweet.created_at}" }
  136. next
  137. end
  138. end
  139. next if @client.locale_sender?(tweet) && own_tweet_already_imported?(tweet)
  140. next if Ticket::Article.exists?(message_id: tweet.id)
  141. break if @client.tweet_limit_reached(tweet)
  142. @client.to_group(tweet, search[:group_id], @channel)
  143. end
  144. end
  145. end
  146. def track_retweets?
  147. @channel.options && @channel.options['sync'] && @channel.options['sync']['track_retweets']
  148. end
  149. def own_tweet_already_imported?(tweet)
  150. event_time = Time.zone.now
  151. sleep 4
  152. 12.times do |loop_count|
  153. if Ticket::Article.exists?(message_id: tweet.id)
  154. Rails.logger.debug { "Own tweet already imported, skipping tweet #{tweet.id}" }
  155. return true
  156. end
  157. count = Delayed::Job.where('created_at < ?', event_time).count
  158. break if count.zero?
  159. sleep_time = 2 * count
  160. sleep_time = 5 if sleep_time > 5
  161. Rails.logger.debug { "Delay importing own tweets - sleep #{sleep_time} (loop #{loop_count})" }
  162. sleep sleep_time
  163. end
  164. if Ticket::Article.exists?(message_id: tweet.id)
  165. Rails.logger.debug { "Own tweet already imported, skipping tweet #{tweet.id}" }
  166. return true
  167. end
  168. false
  169. end
  170. end