twitter.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
  2. class Channel::Driver::Twitter
  3. def fetch (_adapter_options, channel)
  4. @channel = channel
  5. @tweet = Tweet.new( @channel[:options][:auth] )
  6. @sync = @channel[:options][:sync]
  7. Rails.logger.debug 'twitter fetch started'
  8. fetch_search
  9. fetch_mentions
  10. fetch_direct_messages
  11. disconnect
  12. Rails.logger.debug 'twitter fetch completed'
  13. end
  14. def send(article, _notification = false)
  15. @channel = Channel.find_by( area: 'Twitter::Account', active: true )
  16. @tweet = Tweet.new( @channel[:options][:auth] )
  17. tweet = @tweet.from_article(article)
  18. disconnect
  19. tweet
  20. end
  21. def disconnect
  22. @tweet.disconnect
  23. end
  24. private
  25. def fetch_search
  26. return if !@sync[:search]
  27. return if @sync[:search].empty?
  28. # search results
  29. @sync[:search].each { |search|
  30. result_type = search[:type] || 'mixed'
  31. Rails.logger.debug " - searching for '#{search[:term]}'"
  32. counter = 0
  33. @tweet.client.search( search[:term], result_type: result_type ).collect { |tweet|
  34. break if search[:limit] && search[:limit] <= counter
  35. break if Ticket::Article.find_by( message_id: tweet.id.to_s )
  36. @tweet.to_group( tweet, search[:group_id] )
  37. counter += 1
  38. }
  39. }
  40. end
  41. def fetch_mentions
  42. return if !@sync[:mentions]
  43. return if @sync[:mentions].empty?
  44. Rails.logger.debug ' - searching for mentions'
  45. counter = 0
  46. @tweet.client.mentions_timeline.each { |tweet|
  47. break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
  48. break if Ticket::Article.find_by( message_id: tweet.id.to_s )
  49. @tweet.to_group( tweet, @sync[:mentions][:group_id] )
  50. counter += 1
  51. }
  52. end
  53. def fetch_direct_messages
  54. return if !@sync[:direct_messages]
  55. return if @sync[:direct_messages].empty?
  56. Rails.logger.debug ' - searching for direct_messages'
  57. counter = 0
  58. @tweet.client.direct_messages.each { |tweet|
  59. break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
  60. break if Ticket::Article.find_by( message_id: tweet.id.to_s )
  61. @tweet.to_group( tweet, @sync[:direct_messages][:group_id] )
  62. counter += 1
  63. }
  64. end
  65. end