2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
class Channel::Driver::Facebook
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-10-25 21:19:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
instance = Channel::Driver::Facebook.new
|
|
|
|
instance.fetch(channel.options, channel)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def fetch(options, channel)
|
2015-07-09 14:56:01 +00:00
|
|
|
@channel = channel
|
2016-01-16 00:16:31 +00:00
|
|
|
@sync = options['sync']
|
|
|
|
@pages = options['pages']
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'facebook fetch started' }
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
fetch_feed
|
|
|
|
disconnect
|
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'facebook fetch completed' }
|
2016-01-16 00:16:31 +00:00
|
|
|
notice = ''
|
|
|
|
{
|
|
|
|
result: 'ok',
|
|
|
|
notice: notice,
|
|
|
|
}
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def send(options, fb_object_id, article, _notification = false)
|
|
|
|
access_token = nil
|
2017-10-01 12:25:52 +00:00
|
|
|
options['pages'].each do |page|
|
2016-01-16 00:16:31 +00:00
|
|
|
next if page['id'].to_s != fb_object_id.to_s
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
access_token = page['access_token']
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
if !access_token
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "No access_token found for fb_object_id: #{fb_object_id}"
|
2016-01-16 00:16:31 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
client = ::Facebook.new(access_token)
|
2016-01-16 00:16:31 +00:00
|
|
|
client.from_article(article)
|
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=begin
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
instance = Channel::Driver::Facebook.new
|
|
|
|
instance.fetchable?(channel)
|
|
|
|
|
|
|
|
=end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-07-18 13:49:31 +00:00
|
|
|
def fetchable?(channel)
|
2016-07-18 07:37:43 +00:00
|
|
|
return true if Rails.env.test?
|
|
|
|
|
|
|
|
# because of new page rate limit - https://developers.facebook.com/blog/post/2016/06/16/page-level-rate-limits/
|
|
|
|
# only fetch once in 5 minutes
|
|
|
|
return true if !channel.preferences
|
|
|
|
return true if !channel.preferences[:last_fetch]
|
2022-01-03 09:47:32 +00:00
|
|
|
return false if channel.preferences[:last_fetch] > 5.minutes.ago
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
true
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
def disconnect; end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
2017-10-02 11:23:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Channel::Driver::Facebook.streamable?
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.streamable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
private
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def get_page(page_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
@pages.each do |page|
|
2016-01-16 00:16:31 +00:00
|
|
|
return page if page['id'].to_s == page_id.to_s
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
nil
|
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def fetch_feed
|
|
|
|
return if !@sync
|
|
|
|
return if !@sync['pages']
|
|
|
|
|
2016-10-28 13:06:00 +00:00
|
|
|
older_import = 0
|
|
|
|
older_import_max = 12
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
@sync['pages'].each do |page_to_sync_id, page_to_sync_params|
|
2016-01-16 00:16:31 +00:00
|
|
|
page = get_page(page_to_sync_id)
|
|
|
|
next if !page
|
2016-12-02 11:24:00 +00:00
|
|
|
next if page_to_sync_params['group_id'].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
page_client = ::Facebook.new(page['access_token'])
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2016-10-28 14:59:15 +00:00
|
|
|
posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}')
|
2017-10-01 12:25:52 +00:00
|
|
|
posts.each do |post|
|
2016-10-28 13:06:00 +00:00
|
|
|
|
|
|
|
# ignore older messages
|
|
|
|
if (@channel.created_at - 15.days) > Time.zone.parse(post['created_time']) || older_import >= older_import_max
|
|
|
|
older_import += 1
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "post to old: #{post['id']}/#{post['created_time']}" }
|
2016-10-28 13:06:00 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
page_client.to_group(post, page_to_sync_params['group_id'], @channel, page)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
|
|
|
|
true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|