facebook.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Channel::Driver::Facebook
  3. =begin
  4. instance = Channel::Driver::Facebook.new
  5. instance.fetch(channel.options, channel)
  6. =end
  7. def fetch(options, channel)
  8. @channel = channel
  9. @sync = options['sync']
  10. @pages = options['pages']
  11. Rails.logger.debug { 'facebook fetch started' }
  12. fetch_feed
  13. disconnect
  14. Rails.logger.debug { 'facebook fetch completed' }
  15. notice = ''
  16. {
  17. result: 'ok',
  18. notice: notice,
  19. }
  20. end
  21. def deliver(options, fb_object_id, article, _notification = false)
  22. access_token = nil
  23. options['pages'].each do |page|
  24. next if page['id'].to_s != fb_object_id.to_s
  25. access_token = page['access_token']
  26. end
  27. if !access_token
  28. raise "No access_token found for fb_object_id: #{fb_object_id}"
  29. end
  30. client = ::Facebook.new(access_token)
  31. client.from_article(article)
  32. end
  33. =begin
  34. instance = Channel::Driver::Facebook.new
  35. instance.fetchable?(channel)
  36. =end
  37. def fetchable?(channel)
  38. return true if Rails.env.test?
  39. # because of new page rate limit - https://developers.facebook.com/blog/post/2016/06/16/page-level-rate-limits/
  40. # only fetch once in 5 minutes
  41. return true if !channel.preferences
  42. return true if !channel.preferences[:last_fetch]
  43. return false if channel.preferences[:last_fetch] > 5.minutes.ago
  44. true
  45. end
  46. def disconnect; end
  47. =begin
  48. Channel::Driver::Facebook.streamable?
  49. returns
  50. true|false
  51. =end
  52. def self.streamable?
  53. false
  54. end
  55. private
  56. def get_page(page_id)
  57. @pages.each do |page|
  58. return page if page['id'].to_s == page_id.to_s
  59. end
  60. nil
  61. end
  62. def fetch_feed
  63. return if !@sync
  64. return if !@sync['pages']
  65. @sync['pages'].each do |page_to_sync_id, page_to_sync_params|
  66. older_import = 0
  67. older_import_max = 12
  68. page = get_page(page_to_sync_id)
  69. next if !page
  70. next if page_to_sync_params['group_id'].blank?
  71. page_client = ::Facebook.new(page['access_token'])
  72. posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}')
  73. posts.each do |post|
  74. # ignore older messages
  75. if (@channel.created_at - 15.days) > Time.zone.parse(post['created_time']) || older_import >= older_import_max
  76. older_import += 1
  77. Rails.logger.debug { "post to old: #{post['id']}/#{post['created_time']}" }
  78. next
  79. end
  80. page_client.to_group(post, page_to_sync_params['group_id'], @channel, page)
  81. end
  82. end
  83. true
  84. end
  85. end