2015-07-09 14:56:01 +00:00
|
|
|
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
require 'koala'
|
|
|
|
|
|
|
|
class Facebook
|
|
|
|
|
|
|
|
attr_accessor :client, :account
|
|
|
|
|
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
|
|
|
client = Facebook.new('user_or_page_access_token')
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def initialize(access_token)
|
|
|
|
connect(access_token)
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
reconnect with other access_token
|
|
|
|
|
|
|
|
client.connect('user_or_page_access_token')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
def connect(access_token)
|
2016-01-16 00:16:31 +00:00
|
|
|
@client = Koala::Facebook::API.new(access_token)
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
disconnect client
|
|
|
|
|
|
|
|
client.disconnect
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=end
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
return if !@client
|
2015-07-09 14:56:01 +00:00
|
|
|
@client = nil
|
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get pages of user
|
|
|
|
|
|
|
|
pages = client.pages
|
|
|
|
|
|
|
|
result
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '12345',
|
|
|
|
name: 'Some Page Name',
|
|
|
|
access_token, 'some_access_token_for_page',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
def pages
|
|
|
|
pages = []
|
|
|
|
@client.get_connections('me', 'accounts').each { |page|
|
2016-01-16 00:16:31 +00:00
|
|
|
pages.push(
|
|
|
|
id: page['id'],
|
|
|
|
name: page['name'],
|
|
|
|
access_token: page['access_token'],
|
|
|
|
)
|
2015-07-09 14:56:01 +00:00
|
|
|
}
|
|
|
|
pages
|
|
|
|
end
|
|
|
|
|
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
|
|
|
get current user
|
|
|
|
|
|
|
|
pages = current_user
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
result
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
{
|
|
|
|
'id' => '1234567890123456',
|
|
|
|
'name' => 'Page/User Name',
|
|
|
|
'access_token' => 'some_acces_token'
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
@client.get_object('me')
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get user of comment/post
|
|
|
|
|
|
|
|
pages = user(comment_or_post)
|
|
|
|
|
|
|
|
result
|
|
|
|
|
|
|
|
???
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def user(item)
|
|
|
|
return if !item['from']
|
|
|
|
return if !item['from']['id']
|
|
|
|
cache_key = "FB:User:Lookup:#{item['from']['id']}"
|
|
|
|
cache = Cache.get(cache_key)
|
|
|
|
return cache if cache
|
|
|
|
begin
|
|
|
|
result = @client.get_object(item['from']['id'], fields: 'first_name,last_name,email')
|
|
|
|
rescue
|
|
|
|
result = @client.get_object(item['from']['id'], fields: 'name')
|
|
|
|
end
|
|
|
|
if result
|
|
|
|
Cache.write(cache_key, result, { expires_in: 15.minutes })
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def to_user(item)
|
2015-07-21 09:53:23 +00:00
|
|
|
Rails.logger.debug 'Create user from item...'
|
|
|
|
Rails.logger.debug item.inspect
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2015-07-21 09:53:23 +00:00
|
|
|
# do item_user lookup
|
|
|
|
item_user = user(item)
|
|
|
|
return if !item_user
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
auth = Authorization.find_by(uid: item_user['id'], provider: 'facebook')
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
# create or update user
|
|
|
|
user_data = {
|
2016-01-16 00:16:31 +00:00
|
|
|
image_source: "https://graph.facebook.com/#{item_user['id']}/picture?type=large",
|
2015-07-09 14:56:01 +00:00
|
|
|
}
|
|
|
|
if auth
|
2016-01-16 00:16:31 +00:00
|
|
|
user = User.find(auth.user_id)
|
|
|
|
map = {
|
|
|
|
#note: 'description',
|
|
|
|
}
|
|
|
|
|
|
|
|
# ignore if value is already set
|
2016-06-30 20:04:48 +00:00
|
|
|
map.each { |target, source|
|
2016-01-16 00:16:31 +00:00
|
|
|
next if user[target] && !user[target].empty?
|
|
|
|
new_value = tweet_user.send(source).to_s
|
|
|
|
next if !new_value || new_value.empty?
|
|
|
|
user_data[target] = new_value
|
|
|
|
}
|
|
|
|
user.update_attributes(user_data)
|
2015-07-09 14:56:01 +00:00
|
|
|
else
|
2016-01-16 00:16:31 +00:00
|
|
|
user_data[:login] = item_user['id']
|
|
|
|
if item_user['first_name'] && item_user['last_name']
|
|
|
|
user_data[:firstname] = item_user['first_name']
|
|
|
|
user_data[:lastname] = item_user['last_name']
|
|
|
|
else
|
|
|
|
user_data[:firstname] = item_user['name']
|
|
|
|
end
|
|
|
|
user_data[:active] = true
|
|
|
|
user_data[:roles] = Role.where(name: 'Customer')
|
|
|
|
|
|
|
|
user = User.create(user_data)
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
if user_data[:image_source]
|
|
|
|
avatar = Avatar.add(
|
|
|
|
object: 'User',
|
|
|
|
o_id: user.id,
|
|
|
|
url: user_data[:image_source],
|
|
|
|
source: 'facebook',
|
|
|
|
deletable: true,
|
|
|
|
updated_by_id: user.id,
|
|
|
|
created_by_id: user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# update user link
|
|
|
|
if avatar && user.image != avatar.store_hash
|
|
|
|
user.image = avatar.store_hash
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
# create authorization
|
|
|
|
if !auth
|
|
|
|
auth_data = {
|
|
|
|
uid: item_user['id'],
|
|
|
|
username: item_user['id'],
|
|
|
|
user_id: user.id,
|
|
|
|
provider: 'facebook'
|
|
|
|
}
|
|
|
|
Authorization.create(auth_data)
|
|
|
|
end
|
|
|
|
UserInfo.current_user_id = user.id
|
2015-07-09 14:56:01 +00:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def to_ticket(post, group_id, channel, page)
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
Rails.logger.debug 'Create ticket from post...'
|
|
|
|
Rails.logger.debug post.inspect
|
|
|
|
Rails.logger.debug group_id.inspect
|
|
|
|
|
|
|
|
user = to_user(post)
|
|
|
|
return if !user
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
# prepare title
|
|
|
|
|
|
|
|
title = post['message']
|
|
|
|
if title.length > 80
|
|
|
|
title = "#{title[0, 80]}..."
|
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
Ticket.create(
|
|
|
|
customer_id: user.id,
|
2016-01-16 00:16:31 +00:00
|
|
|
title: title,
|
2015-07-09 14:56:01 +00:00
|
|
|
group_id: group_id,
|
2016-01-16 00:16:31 +00:00
|
|
|
state: Ticket::State.find_by(name: 'new'),
|
|
|
|
priority: Ticket::Priority.find_by(name: '2 normal'),
|
|
|
|
preferences: {
|
|
|
|
channel_id: channel.id,
|
|
|
|
channel_fb_object_id: page['id'],
|
|
|
|
},
|
2015-07-09 14:56:01 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def to_article(post, ticket, _page)
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
Rails.logger.debug 'Create article from post...'
|
|
|
|
Rails.logger.debug post.inspect
|
|
|
|
Rails.logger.debug ticket.inspect
|
|
|
|
|
|
|
|
# set ticket state to open if not new
|
|
|
|
if ticket.state.name != 'new'
|
2016-01-16 00:16:31 +00:00
|
|
|
ticket.state = Ticket::State.find_by(name: 'open')
|
2015-07-09 14:56:01 +00:00
|
|
|
ticket.save
|
|
|
|
end
|
|
|
|
|
2015-07-17 14:57:10 +00:00
|
|
|
user = to_user(post)
|
2015-07-09 14:56:01 +00:00
|
|
|
return if !user
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
to = nil
|
|
|
|
if post['to'] && post['to']['data']
|
2016-06-30 20:04:48 +00:00
|
|
|
post['to']['data'].each { |to_entry|
|
2016-01-16 00:16:31 +00:00
|
|
|
if !to
|
|
|
|
to = ''
|
|
|
|
else
|
|
|
|
to += ', '
|
|
|
|
end
|
|
|
|
to += to_entry['name']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
feed_post = {
|
2016-01-16 00:16:31 +00:00
|
|
|
from: post['from']['name'],
|
|
|
|
to: to,
|
2015-07-09 14:56:01 +00:00
|
|
|
body: post['message'],
|
|
|
|
message_id: post['id'],
|
2016-01-16 00:16:31 +00:00
|
|
|
type_id: Ticket::Article::Type.find_by(name: 'facebook feed post').id,
|
2015-07-09 14:56:01 +00:00
|
|
|
}
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
articles = []
|
2016-01-16 00:16:31 +00:00
|
|
|
articles.push(feed_post)
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2015-07-21 09:53:23 +00:00
|
|
|
if post['comments'] && post['comments']['data']
|
2016-01-16 00:16:31 +00:00
|
|
|
articles += nested_comments(post['comments']['data'], post['id'])
|
2015-07-17 14:57:10 +00:00
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2015-07-21 09:53:23 +00:00
|
|
|
articles.each { |article|
|
2016-01-16 00:16:31 +00:00
|
|
|
next if Ticket::Article.find_by(message_id: article[:message_id])
|
2015-07-09 14:56:01 +00:00
|
|
|
article = {
|
2016-01-16 00:16:31 +00:00
|
|
|
#to: @account['name'],
|
2015-07-09 14:56:01 +00:00
|
|
|
ticket_id: ticket.id,
|
|
|
|
internal: false,
|
2016-01-16 00:16:31 +00:00
|
|
|
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
|
2015-07-17 14:57:10 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2016-01-16 00:16:31 +00:00
|
|
|
}.merge(article)
|
|
|
|
Ticket::Article.create(article)
|
2015-07-09 14:56:01 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
def to_group(post, group_id, channel, page)
|
2015-07-09 14:56:01 +00:00
|
|
|
Rails.logger.debug 'import post'
|
2016-01-16 00:16:31 +00:00
|
|
|
return if !post['message']
|
2015-07-09 14:56:01 +00:00
|
|
|
ticket = nil
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
# use transaction
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
|
|
|
UserInfo.current_user_id = 1
|
2016-01-16 00:16:31 +00:00
|
|
|
existing_article = Ticket::Article.find_by(message_id: post['id'])
|
|
|
|
ticket = if existing_article
|
|
|
|
existing_article.ticket
|
|
|
|
else
|
|
|
|
to_ticket(post, group_id, channel, page)
|
|
|
|
end
|
|
|
|
to_article(post, ticket, page)
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2016-04-14 07:17:13 +00:00
|
|
|
# execute object transaction
|
|
|
|
Observer::Transaction.commit
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ticket
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_article(article)
|
|
|
|
post = nil
|
|
|
|
if article[:type] == 'facebook feed comment'
|
|
|
|
Rails.logger.debug 'Create feed comment from article...'
|
2015-07-17 14:57:10 +00:00
|
|
|
post = @client.put_comment(article[:in_reply_to], article[:body])
|
2015-07-09 14:56:01 +00:00
|
|
|
else
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "Can't handle unknown facebook article type '#{article[:type]}'."
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
Rails.logger.debug post.inspect
|
2016-01-16 00:16:31 +00:00
|
|
|
@client.get_object(post['id'])
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def access_token_for_page(lookup)
|
|
|
|
access_token = nil
|
|
|
|
pages.each { |page|
|
|
|
|
next if !lookup[:page_id] && !lookup[:page]
|
|
|
|
next if lookup[:page_id] && lookup[:page_id].to_s != page[:id]
|
|
|
|
next if lookup[:page] && lookup[:page] != page[:name]
|
|
|
|
access_token = page[:access_token]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
access_token
|
|
|
|
end
|
2015-07-21 09:53:23 +00:00
|
|
|
|
|
|
|
def nested_comments(comments, in_reply_to)
|
|
|
|
|
|
|
|
Rails.logger.debug 'Fetching nested comments...'
|
|
|
|
Rails.logger.debug comments.inspect
|
|
|
|
|
|
|
|
result = []
|
|
|
|
return result if comments.empty?
|
|
|
|
|
|
|
|
comments.each { |comment|
|
|
|
|
user = to_user(comment)
|
|
|
|
next if !user
|
|
|
|
article_data = {
|
|
|
|
from: "#{user.firstname} #{user.lastname}",
|
|
|
|
body: comment['message'],
|
|
|
|
message_id: comment['id'],
|
|
|
|
type_id: Ticket::Article::Type.find_by( name: 'facebook feed comment' ).id,
|
|
|
|
in_reply_to: in_reply_to
|
|
|
|
}
|
2016-01-16 00:16:31 +00:00
|
|
|
result.push(article_data)
|
2015-07-21 09:53:23 +00:00
|
|
|
sub_comments = @client.get_object( "#{comment['id']}/comments" )
|
|
|
|
sub_articles = nested_comments(sub_comments, comment['id'])
|
|
|
|
result += sub_articles
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|