twitter.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module RSpecTwitter
  3. class Helper
  4. attr_accessor :twitter_client, :user_screen_name
  5. def initialize(auth)
  6. @twitter_client = TwitterSync.new(
  7. consumer_key: auth[:consumer_key],
  8. consumer_secret: auth[:consumer_secret],
  9. oauth_token: auth[:oauth_token],
  10. oauth_token_secret: auth[:oauth_token_secret],
  11. )
  12. @user_screen_name = 'APITesting00x'
  13. return if !live_mode?
  14. @user_screen_name = @twitter_client.client.user.screen_name
  15. end
  16. def delete_old_tweets
  17. log('I\'m deleting tweets older than one hour...')
  18. tweets = current_tweets.select { |tweet| tweet.created_at < 1.hour.ago }
  19. while tweets.size.positive?
  20. perform_delete(tweets, nil, [])
  21. tweets = current_tweets.select { |tweet| tweet.created_at < 1.hour.ago }
  22. end
  23. end
  24. def delete_all_tweets(identifier = nil)
  25. log('I\'m deleting all tweets...') if identifier.nil?
  26. log("I'm deleting all tweets matching identifier '#{identifier}...'") if identifier.present?
  27. tweets = current_tweets
  28. tweets_to_ignore = []
  29. while tweets.size.positive?
  30. perform_delete(tweets, identifier, tweets_to_ignore)
  31. tweets = current_tweets.reject { |tweet| tweets_to_ignore.include?(tweet.id) }
  32. end
  33. end
  34. def perform_delete(tweets, identifier, ignore_list)
  35. tweets.each do |tweet|
  36. next if !tweet_exists?(tweet)
  37. next if ignore_list.include?(tweet.id)
  38. if tweet_match?(tweet, identifier)
  39. delete_tweet(tweet)
  40. next
  41. end
  42. ignore_list << tweet.id
  43. end
  44. nil
  45. end
  46. def current_tweets
  47. twitter_client.client.user_timeline({ count: 200 })
  48. end
  49. def tweet_match?(tweet, identifier)
  50. return true if identifier.nil?
  51. return true if identifier.present? && tweet.text.include?(identifier)
  52. false
  53. end
  54. def tweet_exists?(tweet)
  55. twitter_client.client.status(tweet)&.present?
  56. rescue
  57. false
  58. end
  59. def create_tweet(status, options = {})
  60. log("Creating tweet '#{status}'...")
  61. twitter_client.client.update(status, options)
  62. end
  63. def delete_tweet(tweet)
  64. log("Deleting tweet with id #{tweet.id}...")
  65. twitter_client.client.destroy_status(tweet)
  66. rescue
  67. nil
  68. end
  69. def create_retweet(id)
  70. log("Creating retweet for tweet '#{id}'...")
  71. twitter_client.client.retweet(id)
  72. end
  73. def ensure_tweet_availability(identifier, amount)
  74. log("Ensuring availability of #{amount} tweets with identifier '#{identifier}'...")
  75. time_now = Time.zone.now
  76. while Time.zone.now < time_now + 120.seconds
  77. if twitter_client.client.search(identifier, result_type: 'mixed').attrs[:statuses].count.eql?(amount)
  78. log("Found #{amount} tweets for '#{identifier}'. Amazing!")
  79. return true
  80. end
  81. # Only fall asleep if we're not using cassettes.
  82. if live_mode?
  83. log("Waiting 30 seconds for tweets to show up in search results (#{identifier})...")
  84. sleep 30
  85. end
  86. end
  87. log("Could not find #{amount} tweets for '#{identifier}' within 120 seconds. Giving up.")
  88. false
  89. end
  90. def live_mode?
  91. %w[1 true].include?(ENV['CI_IGNORE_CASSETTES'])
  92. end
  93. def log(msg)
  94. Rails.logger.debug { "[TWITTER > #{user_screen_name}] #{msg}" }
  95. end
  96. end
  97. end
  98. RSpec.configure do |config|
  99. config.include RSpecTwitter
  100. end