2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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 = []
|
2017-10-01 12:25:52 +00:00
|
|
|
@client.get_connections('me', 'accounts').each do |page|
|
2016-01-16 00:16:31 +00:00
|
|
|
pages.push(
|
|
|
|
id: page['id'],
|
|
|
|
name: page['name'],
|
|
|
|
access_token: page['access_token'],
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
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']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
cache_key = "FB:User:Lookup:#{item['from']['id']}"
|
2021-05-31 13:05:54 +00:00
|
|
|
cache = Cache.read(cache_key)
|
2016-01-16 00:16:31 +00:00
|
|
|
return cache if cache
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
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)
|
2018-03-20 17:47:49 +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)
|
2017-09-11 11:16:08 +00:00
|
|
|
user.update!(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
|
2016-08-12 16:39:09 +00:00
|
|
|
user_data[:active] = true
|
|
|
|
user_data[:role_ids] = Role.signup_role_ids
|
2016-01-16 00:16:31 +00:00
|
|
|
|
|
|
|
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(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'User',
|
|
|
|
o_id: user.id,
|
|
|
|
url: user_data[:image_source],
|
|
|
|
source: 'facebook',
|
|
|
|
deletable: true,
|
2016-01-16 00:16:31 +00:00
|
|
|
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
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'Create ticket from post...' }
|
|
|
|
Rails.logger.debug { post.inspect }
|
|
|
|
Rails.logger.debug { group_id.inspect }
|
2015-07-09 14:56:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-10-25 21:19:02 +00:00
|
|
|
state = get_state(page, post)
|
2016-10-28 14:59:15 +00:00
|
|
|
Ticket.create!(
|
2015-07-09 14:56:01 +00:00
|
|
|
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-10-25 21:19:02 +00:00
|
|
|
state: state,
|
2016-01-16 00:16:31 +00:00
|
|
|
priority: Ticket::Priority.find_by(name: '2 normal'),
|
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
channel_id: channel.id,
|
2016-01-16 00:16:31 +00:00
|
|
|
channel_fb_object_id: page['id'],
|
2018-12-19 17:31:51 +00:00
|
|
|
facebook: {
|
2016-10-28 14:59:15 +00:00
|
|
|
permalink_url: post['permalink_url'],
|
|
|
|
}
|
2016-01-16 00:16:31 +00:00
|
|
|
},
|
2015-07-09 14:56:01 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-10-25 21:19:02 +00:00
|
|
|
def to_article(post, ticket, page)
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'Create article from post...' }
|
|
|
|
Rails.logger.debug { post.inspect }
|
|
|
|
Rails.logger.debug { ticket.inspect }
|
2015-07-09 14:56:01 +00:00
|
|
|
|
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']
|
2017-10-01 12:25:52 +00:00
|
|
|
post['to']['data'].each do |to_entry|
|
2020-11-05 16:31:00 +00:00
|
|
|
if to
|
2016-01-16 00:16:31 +00:00
|
|
|
to += ', '
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
to = ''
|
2016-01-16 00:16:31 +00:00
|
|
|
end
|
|
|
|
to += to_entry['name']
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
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
|
|
|
|
2016-10-28 14:59:15 +00:00
|
|
|
base_url = nil
|
|
|
|
if ticket.preferences['facebook'] && ticket.preferences['facebook']['permalink_url']
|
|
|
|
base_url = ticket.preferences['facebook']['permalink_url']
|
|
|
|
end
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
articles.each do |article|
|
2020-08-03 08:35:43 +00:00
|
|
|
next if Ticket::Article.exists?(message_id: article[:message_id])
|
2016-10-25 21:19:02 +00:00
|
|
|
|
|
|
|
# set ticket state to open if not new
|
|
|
|
ticket_state = get_state(page, post, ticket)
|
|
|
|
if ticket_state.name != ticket.state.name
|
|
|
|
ticket.state = ticket_state
|
2016-10-28 13:06:00 +00:00
|
|
|
ticket.save!
|
2016-10-25 21:19:02 +00:00
|
|
|
end
|
|
|
|
|
2016-10-28 14:59:15 +00:00
|
|
|
links = []
|
|
|
|
if base_url
|
|
|
|
url = base_url
|
|
|
|
realtive_id = article[:message_id].split('_')[1]
|
|
|
|
if realtive_id
|
|
|
|
url += "?comment_id=#{realtive_id}"
|
|
|
|
end
|
|
|
|
links = [
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
url: url,
|
2016-10-28 14:59:15 +00:00
|
|
|
target: '_blank',
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'on Facebook',
|
2016-10-28 14:59:15 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
article = {
|
2021-07-16 13:44:10 +00:00
|
|
|
# to: @account['name'],
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket.id,
|
|
|
|
internal: false,
|
|
|
|
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,
|
2018-12-19 17:31:51 +00:00
|
|
|
preferences: {
|
2016-10-28 14:59:15 +00:00
|
|
|
links: links,
|
|
|
|
},
|
2016-01-16 00:16:31 +00:00
|
|
|
}.merge(article)
|
|
|
|
Ticket::Article.create(article)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
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)
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'import post' }
|
2016-01-16 00:16:31 +00:00
|
|
|
return if !post['message']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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
|
2021-08-20 03:36:30 +00:00
|
|
|
Transaction.execute(reset_user_id: true, context: 'facebook') do
|
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
|
|
|
end
|
|
|
|
|
|
|
|
ticket
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_article(article)
|
|
|
|
post = nil
|
2017-11-23 08:09:44 +00:00
|
|
|
if article[:type] != 'facebook feed comment'
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'Create feed comment from article...' }
|
2017-11-23 08:09:44 +00:00
|
|
|
post = @client.put_comment(article[:in_reply_to], article[:body])
|
2018-03-20 17:47:49 +00:00
|
|
|
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
|
|
|
|
|
2016-10-25 21:19:02 +00:00
|
|
|
def get_state(page, post, ticket = nil)
|
|
|
|
|
|
|
|
# no changes in post is from page user it self
|
|
|
|
if post['from'] && post['from']['id'].to_s == page['id'].to_s
|
|
|
|
if !ticket
|
2020-09-30 09:07:01 +00:00
|
|
|
return Ticket::State.find_by(name: 'closed')
|
2016-10-25 21:19:02 +00:00
|
|
|
end
|
2020-09-30 09:07:01 +00:00
|
|
|
|
2016-10-25 21:19:02 +00:00
|
|
|
return ticket.state
|
|
|
|
end
|
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
state = Ticket::State.find_by(default_create: true)
|
2016-10-25 21:19:02 +00:00
|
|
|
return state if !ticket
|
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
return ticket.state if ticket.state_id == state.id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
Ticket::State.find_by(default_follow_up: true)
|
2016-10-25 21:19:02 +00:00
|
|
|
end
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
def access_token_for_page(lookup)
|
|
|
|
access_token = nil
|
2017-10-01 12:25:52 +00:00
|
|
|
pages.each do |page|
|
2015-07-09 14:56:01 +00:00
|
|
|
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]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
access_token = page[:access_token]
|
|
|
|
break
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-09 14:56:01 +00:00
|
|
|
access_token
|
|
|
|
end
|
2015-07-21 09:53:23 +00:00
|
|
|
|
|
|
|
def nested_comments(comments, in_reply_to)
|
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { 'Fetching nested comments...' }
|
|
|
|
Rails.logger.debug { comments.inspect }
|
2015-07-21 09:53:23 +00:00
|
|
|
|
|
|
|
result = []
|
2017-11-23 08:09:44 +00:00
|
|
|
return result if comments.blank?
|
2015-07-21 09:53:23 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
comments.each do |comment|
|
2015-07-21 09:53:23 +00:00
|
|
|
user = to_user(comment)
|
|
|
|
next if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-21 09:53:23 +00:00
|
|
|
article_data = {
|
2016-10-25 21:19:02 +00:00
|
|
|
from: "#{user.firstname} #{user.lastname}",
|
|
|
|
body: comment['message'],
|
|
|
|
message_id: comment['id'],
|
|
|
|
type_id: Ticket::Article::Type.find_by(name: 'facebook feed comment').id,
|
2015-07-21 09:53:23 +00:00
|
|
|
in_reply_to: in_reply_to
|
|
|
|
}
|
2016-01-16 00:16:31 +00:00
|
|
|
result.push(article_data)
|
2016-10-28 14:59:15 +00:00
|
|
|
sub_comments = @client.get_object("#{comment['id']}/comments", fields: 'id,from,to,message,created_time')
|
2015-07-21 09:53:23 +00:00
|
|
|
sub_articles = nested_comments(sub_comments, comment['id'])
|
|
|
|
result += sub_articles
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-21 09:53:23 +00:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2016-01-16 00:16:31 +00:00
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
end
|