2015-07-09 14:56:01 +00:00
|
|
|
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
require 'facebook'
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
class Channel::Driver::Facebook
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
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
|
|
|
|
|
|
|
Rails.logger.debug 'facebook fetch started'
|
|
|
|
|
|
|
|
fetch_feed
|
|
|
|
disconnect
|
|
|
|
|
|
|
|
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
|
|
|
|
options['pages'].each {|page|
|
|
|
|
next if page['id'].to_s != fb_object_id.to_s
|
|
|
|
access_token = page['access_token']
|
|
|
|
}
|
|
|
|
if !access_token
|
|
|
|
fail "No access_token found for fb_object_id: #{fb_object_id}"
|
|
|
|
end
|
|
|
|
client = Facebook.new(access_token)
|
|
|
|
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-01-16 00:16:31 +00:00
|
|
|
def fetchable?(_channel)
|
|
|
|
true
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
def disconnect
|
2014-06-22 07:00:09 +00:00
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
private
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def get_page(page_id)
|
|
|
|
@pages.each {|page|
|
|
|
|
return page if page['id'].to_s == page_id.to_s
|
|
|
|
}
|
|
|
|
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']
|
|
|
|
|
|
|
|
@sync['pages'].each {|page_to_sync_id, page_to_sync_params|
|
|
|
|
page = get_page(page_to_sync_id)
|
|
|
|
next if !page
|
|
|
|
next if !page_to_sync_params['group_id']
|
|
|
|
next if page_to_sync_params['group_id'].empty?
|
|
|
|
page_client = Facebook.new(page['access_token'])
|
|
|
|
|
|
|
|
posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,comments')
|
|
|
|
posts.each {|post|
|
|
|
|
page_client.to_group(post, page_to_sync_params['group_id'], @channel, page)
|
|
|
|
}
|
2015-07-09 14:56:01 +00:00
|
|
|
}
|
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
|