Fixed some Facebook issues. Improved tests.
This commit is contained in:
parent
ec69425f2c
commit
a397132931
7 changed files with 422 additions and 201 deletions
|
@ -57,6 +57,18 @@ job_integration_twitter:
|
|||
- rake db:seed
|
||||
- ruby -I test/ test/integration/twitter_test.rb
|
||||
|
||||
job_integration_facebook:
|
||||
stage: test
|
||||
tags:
|
||||
- core
|
||||
script:
|
||||
- export RAILS_ENV=test
|
||||
- rake db:drop;
|
||||
- rake db:create
|
||||
- rake db:migrate
|
||||
- rake db:seed
|
||||
- ruby -I test/ test/integration/facebook_test.rb
|
||||
|
||||
job_integration_geo_ip:
|
||||
stage: test
|
||||
tags:
|
||||
|
|
|
@ -4,53 +4,78 @@ require 'facebook'
|
|||
|
||||
class Channel::Driver::Facebook
|
||||
|
||||
def fetch (_adapter_options, channel)
|
||||
|
||||
def fetch (options, channel)
|
||||
@channel = channel
|
||||
@facebook = Facebook.new( @channel[:options] )
|
||||
@sync = @channel[:options][:sync]
|
||||
@sync = options['sync']
|
||||
@pages = options['pages']
|
||||
|
||||
Rails.logger.debug 'facebook fetch started'
|
||||
|
||||
fetch_feed
|
||||
|
||||
disconnect
|
||||
|
||||
Rails.logger.debug 'facebook fetch completed'
|
||||
notice = ''
|
||||
{
|
||||
result: 'ok',
|
||||
notice: notice,
|
||||
}
|
||||
end
|
||||
|
||||
def send(article, _notification = false)
|
||||
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
|
||||
|
||||
@channel = Channel.find_by( area: 'Facebook::Inbound', active: true )
|
||||
@facebook = Facebook.new( @channel[:options] )
|
||||
=begin
|
||||
|
||||
post = @facebook.from_article(article)
|
||||
disconnect
|
||||
instance = Channel::Driver::Facebook.new
|
||||
instance.fetchable?(channel)
|
||||
|
||||
post
|
||||
=end
|
||||
|
||||
def fetchable?(_channel)
|
||||
true
|
||||
end
|
||||
|
||||
def disconnect
|
||||
@facebook.disconnect
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_feed
|
||||
|
||||
return if !@sync[:group_id]
|
||||
|
||||
counter = 0
|
||||
feed = @facebook.client.get_connections('me', 'feed')
|
||||
feed.each { |feed_item|
|
||||
|
||||
break if @sync[:limit] && @sync[:limit] <= counter
|
||||
|
||||
post = @facebook.client.get_object( feed_item['id'] )
|
||||
|
||||
@facebook.to_group( post, @sync[:group_id] )
|
||||
|
||||
counter += 1
|
||||
def get_page(page_id)
|
||||
@pages.each {|page|
|
||||
return page if page['id'].to_s == page_id.to_s
|
||||
}
|
||||
nil
|
||||
end
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -11,21 +11,43 @@ class Observer::Ticket::Article::CommunicateFacebook < ActiveRecord::Observer
|
|||
return if Setting.get('import_mode')
|
||||
|
||||
# if sender is customer, do not communication
|
||||
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
|
||||
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
||||
return 1 if sender.nil?
|
||||
return 1 if sender['name'] == 'Customer'
|
||||
|
||||
# only apply for facebook
|
||||
type = Ticket::Article::Type.lookup( id: record.type_id )
|
||||
type = Ticket::Article::Type.lookup(id: record.type_id)
|
||||
return if type['name'] !~ /\Afacebook/
|
||||
|
||||
ticket = Ticket.lookup(id: record.ticket_id)
|
||||
fail "Can't find ticket.preferences for Ticket.find(#{record.ticket_id})" if !ticket.preferences
|
||||
fail "Can't find ticket.preferences['channel_id'] for Ticket.find(#{record.ticket_id})" if !ticket.preferences['channel_id']
|
||||
channel = Channel.lookup(id: ticket.preferences['channel_id'])
|
||||
fail "Channel.find(#{channel.id}) isn't a twitter channel!" if channel.options[:adapter] !~ /\Afacebook/i
|
||||
|
||||
# check source object id
|
||||
ticket = record.ticket
|
||||
if !ticket.preferences['channel_fb_object_id']
|
||||
fail "fb object id is missing in ticket.preferences['channel_fb_object_id'] for Ticket.find(#{ticket.id})"
|
||||
end
|
||||
|
||||
# fill in_reply_to
|
||||
if !record.in_reply_to || record.in_reply_to.empty?
|
||||
record.in_reply_to = ticket.articles.first.message_id
|
||||
end
|
||||
|
||||
facebook = Channel::Driver::Facebook.new
|
||||
post = facebook.send({
|
||||
post = facebook.send(
|
||||
channel.options,
|
||||
ticket.preferences[:channel_fb_object_id],
|
||||
{
|
||||
type: type['name'],
|
||||
to: record.to,
|
||||
body: record.body,
|
||||
in_reply_to: record.in_reply_to
|
||||
})
|
||||
in_reply_to: record.in_reply_to,
|
||||
}
|
||||
)
|
||||
record.from = post['from']['name']
|
||||
record.message_id = post['id']
|
||||
record.save
|
||||
end
|
||||
|
|
|
@ -9,14 +9,14 @@ class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
|
|||
return if Setting.get('import_mode')
|
||||
|
||||
# if sender is customer, do not change anything
|
||||
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
|
||||
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
||||
return if sender.nil?
|
||||
return if sender['name'] == 'Customer'
|
||||
|
||||
# set from if not given
|
||||
return if record.from
|
||||
|
||||
user = User.find( record.created_by_id )
|
||||
user = User.find(record.created_by_id)
|
||||
record.from = "#{user.firstname} #{user.lastname}"
|
||||
end
|
||||
end
|
||||
|
|
273
lib/facebook.rb
273
lib/facebook.rb
|
@ -6,101 +6,195 @@ class Facebook
|
|||
|
||||
attr_accessor :client, :account
|
||||
|
||||
def initialize(options)
|
||||
=begin
|
||||
|
||||
connect( options[:auth][:access_token] )
|
||||
client = Facebook.new('user_or_page_access_token')
|
||||
|
||||
page_access_token = access_token_for_page( options[:sync] )
|
||||
=end
|
||||
|
||||
if page_access_token
|
||||
connect( page_access_token )
|
||||
def initialize(access_token)
|
||||
connect(access_token)
|
||||
end
|
||||
|
||||
@account = client.get_object('me')
|
||||
end
|
||||
=begin
|
||||
|
||||
reconnect with other access_token
|
||||
|
||||
client.connect('user_or_page_access_token')
|
||||
|
||||
=end
|
||||
|
||||
def connect(access_token)
|
||||
@client = Koala::Facebook::API.new( access_token )
|
||||
@client = Koala::Facebook::API.new(access_token)
|
||||
end
|
||||
|
||||
=begin
|
||||
|
||||
disconnect client
|
||||
|
||||
client.disconnect
|
||||
|
||||
=end
|
||||
|
||||
def disconnect
|
||||
|
||||
return if !@client
|
||||
|
||||
@client = nil
|
||||
end
|
||||
|
||||
=begin
|
||||
|
||||
get pages of user
|
||||
|
||||
pages = client.pages
|
||||
|
||||
result
|
||||
|
||||
[
|
||||
{
|
||||
id: '12345',
|
||||
name: 'Some Page Name',
|
||||
access_token, 'some_access_token_for_page',
|
||||
},
|
||||
]
|
||||
|
||||
=end
|
||||
|
||||
def pages
|
||||
pages = []
|
||||
@client.get_connections('me', 'accounts').each { |page|
|
||||
pages.push({
|
||||
pages.push(
|
||||
id: page['id'],
|
||||
name: page['name'],
|
||||
access_token: page['access_token'],
|
||||
})
|
||||
)
|
||||
}
|
||||
pages
|
||||
end
|
||||
|
||||
def user(item)
|
||||
=begin
|
||||
|
||||
get current user
|
||||
|
||||
pages = current_user
|
||||
|
||||
result
|
||||
|
||||
{
|
||||
'id' => '1234567890123456',
|
||||
'name' => 'Page/User Name',
|
||||
'access_token' => 'some_acces_token'
|
||||
}
|
||||
|
||||
=end
|
||||
|
||||
def current_user
|
||||
@client.get_object('me')
|
||||
end
|
||||
|
||||
=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']
|
||||
return if !item['from']['name']
|
||||
|
||||
return if item['from']['id'] == @account['id']
|
||||
|
||||
@client.get_object( 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
|
||||
|
||||
def to_user(item)
|
||||
|
||||
Rails.logger.debug 'Create user from item...'
|
||||
Rails.logger.debug item.inspect
|
||||
|
||||
# do item_user lookup
|
||||
item_user = user(item)
|
||||
|
||||
return if !item_user
|
||||
|
||||
auth = Authorization.find_by( uid: item_user['id'], provider: 'facebook' )
|
||||
auth = Authorization.find_by(uid: item_user['id'], provider: 'facebook')
|
||||
|
||||
# create or update user
|
||||
user_data = {
|
||||
login: item_user['id'], # TODO
|
||||
firstname: item_user['first_name'] || item_user['name'],
|
||||
lastname: item_user['last_name'] || '',
|
||||
email: '',
|
||||
password: '',
|
||||
# TODO: image_source: '',
|
||||
# TODO: note: '',
|
||||
active: true,
|
||||
roles: Role.where( name: 'Customer' ),
|
||||
image_source: "https://graph.facebook.com/#{item_user['id']}/picture?type=large",
|
||||
}
|
||||
if auth
|
||||
user_data[:id] = auth.user_id
|
||||
end
|
||||
user = User.create_or_update( user_data )
|
||||
user = User.find(auth.user_id)
|
||||
map = {
|
||||
#note: 'description',
|
||||
}
|
||||
|
||||
# create or update authorization
|
||||
# ignore if value is already set
|
||||
map.each {|target, source|
|
||||
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)
|
||||
else
|
||||
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)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
# create authorization
|
||||
if !auth
|
||||
auth_data = {
|
||||
uid: item_user['id'],
|
||||
username: item_user['id'], # TODO
|
||||
username: item_user['id'],
|
||||
user_id: user.id,
|
||||
provider: 'facebook'
|
||||
}
|
||||
if auth
|
||||
auth.update_attributes( auth_data )
|
||||
else
|
||||
Authorization.new( auth_data )
|
||||
Authorization.create(auth_data)
|
||||
end
|
||||
|
||||
UserInfo.current_user_id = user.id
|
||||
|
||||
user
|
||||
end
|
||||
|
||||
def to_ticket(post, group_id)
|
||||
def to_ticket(post, group_id, channel, page)
|
||||
|
||||
Rails.logger.debug 'Create ticket from post...'
|
||||
Rails.logger.debug post.inspect
|
||||
|
@ -109,16 +203,27 @@ class Facebook
|
|||
user = to_user(post)
|
||||
return if !user
|
||||
|
||||
# prepare title
|
||||
|
||||
title = post['message']
|
||||
if title.length > 80
|
||||
title = "#{title[0, 80]}..."
|
||||
end
|
||||
|
||||
Ticket.create(
|
||||
customer_id: user.id,
|
||||
title: "#{post['message'][0, 37]}...",
|
||||
title: title,
|
||||
group_id: group_id,
|
||||
state: Ticket::State.find_by( name: 'new' ),
|
||||
priority: Ticket::Priority.find_by( name: '2 normal' ),
|
||||
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'],
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
def to_article(post, ticket)
|
||||
def to_article(post, ticket, _page)
|
||||
|
||||
Rails.logger.debug 'Create article from post...'
|
||||
Rails.logger.debug post.inspect
|
||||
|
@ -126,62 +231,70 @@ class Facebook
|
|||
|
||||
# set ticket state to open if not new
|
||||
if ticket.state.name != 'new'
|
||||
ticket.state = Ticket::State.find_by( name: 'open' )
|
||||
ticket.state = Ticket::State.find_by(name: 'open')
|
||||
ticket.save
|
||||
end
|
||||
|
||||
user = to_user(post)
|
||||
return if !user
|
||||
|
||||
to = nil
|
||||
if post['to'] && post['to']['data']
|
||||
post['to']['data'].each {|to_entry|
|
||||
if !to
|
||||
to = ''
|
||||
else
|
||||
to += ', '
|
||||
end
|
||||
to += to_entry['name']
|
||||
}
|
||||
end
|
||||
|
||||
feed_post = {
|
||||
from: "#{user.firstname} #{user.lastname}",
|
||||
from: post['from']['name'],
|
||||
to: to,
|
||||
body: post['message'],
|
||||
message_id: post['id'],
|
||||
type_id: Ticket::Article::Type.find_by( name: 'facebook feed post' ).id,
|
||||
type_id: Ticket::Article::Type.find_by(name: 'facebook feed post').id,
|
||||
}
|
||||
|
||||
articles = []
|
||||
articles.push( feed_post )
|
||||
articles.push(feed_post)
|
||||
|
||||
if post['comments'] && post['comments']['data']
|
||||
articles += nested_comments( post['comments']['data'], post['id'] )
|
||||
articles += nested_comments(post['comments']['data'], post['id'])
|
||||
end
|
||||
|
||||
articles.each { |article|
|
||||
|
||||
next if Ticket::Article.find_by( message_id: article[:message_id] )
|
||||
|
||||
next if Ticket::Article.find_by(message_id: article[:message_id])
|
||||
article = {
|
||||
to: @account['name'],
|
||||
#to: @account['name'],
|
||||
ticket_id: ticket.id,
|
||||
internal: false,
|
||||
sender_id: Ticket::Article::Sender.lookup( name: 'Customer' ).id,
|
||||
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
|
||||
created_by_id: 1,
|
||||
updated_by_id: 1,
|
||||
}.merge( article )
|
||||
|
||||
Ticket::Article.create( article )
|
||||
}.merge(article)
|
||||
Ticket::Article.create(article)
|
||||
}
|
||||
end
|
||||
|
||||
def to_group(post, group_id)
|
||||
|
||||
def to_group(post, group_id, channel, page)
|
||||
Rails.logger.debug 'import post'
|
||||
|
||||
return if !post['message']
|
||||
ticket = nil
|
||||
|
||||
# use transaction
|
||||
ActiveRecord::Base.transaction do
|
||||
|
||||
UserInfo.current_user_id = 1
|
||||
|
||||
existing_article = Ticket::Article.find_by( message_id: post['id'] )
|
||||
if existing_article
|
||||
ticket = existing_article.ticket
|
||||
existing_article = Ticket::Article.find_by(message_id: post['id'])
|
||||
ticket = if existing_article
|
||||
existing_article.ticket
|
||||
else
|
||||
ticket = to_ticket(post, group_id)
|
||||
return if !ticket
|
||||
to_ticket(post, group_id, channel, page)
|
||||
end
|
||||
|
||||
to_article(post, ticket)
|
||||
to_article(post, ticket, page)
|
||||
|
||||
# execute ticket events
|
||||
Observer::Ticket::Notification.transaction
|
||||
|
@ -191,37 +304,28 @@ class Facebook
|
|||
end
|
||||
|
||||
def from_article(article)
|
||||
|
||||
post = nil
|
||||
if article[:type] == 'facebook feed comment'
|
||||
|
||||
Rails.logger.debug 'Create feed comment from article...'
|
||||
|
||||
post = @client.put_comment(article[:in_reply_to], article[:body])
|
||||
else
|
||||
fail "Can't handle unknown facebook article type '#{article[:type]}'."
|
||||
end
|
||||
|
||||
Rails.logger.debug post.inspect
|
||||
@client.get_object( post['id'] )
|
||||
@client.get_object(post['id'])
|
||||
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
|
||||
|
||||
|
@ -234,11 +338,8 @@ class Facebook
|
|||
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'],
|
||||
|
@ -246,15 +347,13 @@ class Facebook
|
|||
type_id: Ticket::Article::Type.find_by( name: 'facebook feed comment' ).id,
|
||||
in_reply_to: in_reply_to
|
||||
}
|
||||
result.push( article_data )
|
||||
|
||||
result.push(article_data)
|
||||
sub_comments = @client.get_object( "#{comment['id']}/comments" )
|
||||
|
||||
sub_articles = nested_comments(sub_comments, comment['id'])
|
||||
|
||||
result += sub_articles
|
||||
}
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,27 +7,55 @@ class FacebookTest < ActiveSupport::TestCase
|
|||
Setting.set('system_init_done', true)
|
||||
|
||||
# needed to check correct behavior
|
||||
Group.create_if_not_exists(
|
||||
id: 2,
|
||||
group = Group.create_if_not_exists(
|
||||
name: 'Facebook',
|
||||
note: 'All Facebook feed posts.',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1
|
||||
)
|
||||
|
||||
provider_key = 'CAACEdEose0cBAC56WJvrGb5avKbTlH0c7P4xZCZBfT8zG4nkgEWeFKGnnpNZC8xeedXzmqZCxEUrAumX245T4MborvAmRW52PSpuDiXwXXSMjYaZCJOih5v6CsP3xrZAfGxhPWBbI8dSoquBv8eRbUAMSir9SDSoDeKJSdSfhuytqx5wfveE8YibzT2ZAwYz0d7d2QZAN4b10d9j9UpBhXCCCahj4hyk9JQZD'
|
||||
consumer_key = 'CAACEdEose0cBAHZCXAQ68snZBf2C7jT6G7pVXaWajbZCZAZAFWRZAVUb9FAMXHZBQECZBX0iL5qOeTsZA0mnR0586XTq9vYiWP8Y3qCzftrd9hnsP7J9VB6APnR67NEdY8SozxIFtctQA9Xp4Lb8lbxBmig2v5oXRIH513kImPYXJoCFUlQs0aJeZBCtRG6BekfPs5GPZB8tieQE3yGgtZBTZA3HI2TtQLZBNXyLAZD'
|
||||
# account config
|
||||
if !ENV['FACEBOOK_USER']
|
||||
fail "ERROR: Need FACEBOOK_USER - hint FACEBOOK_USER='name:1234:access_token'"
|
||||
end
|
||||
user_name = ENV['FACEBOOK_USER'].split(':')[0]
|
||||
user_id = ENV['FACEBOOK_USER'].split(':')[1]
|
||||
user_access_token = ENV['FACEBOOK_USER'].split(':')[2]
|
||||
|
||||
if !ENV['FACEBOOK_PAGE']
|
||||
fail "ERROR: Need FACEBOOK_PAGE - hint FACEBOOK_PAGE='name:1234:access_token'"
|
||||
end
|
||||
page_name = ENV['FACEBOOK_PAGE'].split(':')[0]
|
||||
page_id = ENV['FACEBOOK_PAGE'].split(':')[1]
|
||||
page_access_token = ENV['FACEBOOK_PAGE'].split(':')[2]
|
||||
|
||||
if !ENV['FACEBOOK_CUSTOMER']
|
||||
fail "ERROR: Need FACEBOOK_CUSTOMER - hint FACEBOOK_CUSTOMER='name:1234:access_token'"
|
||||
end
|
||||
customer_name = ENV['FACEBOOK_CUSTOMER'].split(':')[0]
|
||||
customer_id = ENV['FACEBOOK_CUSTOMER'].split(':')[1]
|
||||
customer_access_token = ENV['FACEBOOK_CUSTOMER'].split(':')[2]
|
||||
|
||||
provider_page_name = 'Hansi Merkurs Hutfabrik'
|
||||
provider_options = {
|
||||
adapter: 'facebook',
|
||||
auth: {
|
||||
access_token: provider_key
|
||||
access_token: user_access_token
|
||||
},
|
||||
user: {
|
||||
name: user_name,
|
||||
id: user_id,
|
||||
},
|
||||
pages: [
|
||||
{
|
||||
'id' => page_id,
|
||||
'name' => page_name,
|
||||
'access_token' => page_access_token,
|
||||
}
|
||||
],
|
||||
sync: {
|
||||
page: provider_page_name,
|
||||
group_id: 2,
|
||||
limit: 1,
|
||||
pages: {
|
||||
page_id => { 'group_id' => group.id.to_s },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,113 +70,148 @@ class FacebookTest < ActiveSupport::TestCase
|
|||
updated_by_id: 1,
|
||||
)
|
||||
|
||||
test 'pages' do
|
||||
|
||||
provider_options_clone = provider_options
|
||||
|
||||
provider_options_clone[:sync].delete(:page)
|
||||
|
||||
facebook = Facebook.new( provider_options_clone )
|
||||
|
||||
pages = facebook.pages
|
||||
|
||||
page_found = false
|
||||
pages.each { |page|
|
||||
|
||||
next if page[:name] != provider_page_name
|
||||
page_found = true
|
||||
}
|
||||
|
||||
assert( page_found, "Page lookup for '#{provider_page_name}'" )
|
||||
# check users account
|
||||
test 'a - user account' do
|
||||
client = Facebook.new(user_access_token)
|
||||
current_user = client.current_user
|
||||
assert_equal(user_id, current_user['id'])
|
||||
assert_equal(user_name, current_user['name'])
|
||||
end
|
||||
|
||||
test 'feed post to ticket' do
|
||||
# check available pages
|
||||
test 'b - available pages' do
|
||||
client = Facebook.new(user_access_token)
|
||||
page_found = false
|
||||
client.pages.each {|page|
|
||||
next if page[:name] != page_name
|
||||
page_found = true
|
||||
assert_equal(page_id, page[:id])
|
||||
assert_equal(page_name, page[:name])
|
||||
}
|
||||
assert(page_found, "Page lookup for '#{page_name}'")
|
||||
end
|
||||
|
||||
consumer_client = Koala::Facebook::API.new( consumer_key )
|
||||
feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
|
||||
# check access to pages
|
||||
test 'c - page access' do
|
||||
page_found = false
|
||||
provider_options[:pages].each {|page|
|
||||
client = Facebook.new(page['access_token'])
|
||||
current_user = client.current_user
|
||||
next if page['name'] != page_name
|
||||
page_found = true
|
||||
assert_equal(page_id, current_user['id'])
|
||||
assert_equal(page_name, current_user['name'])
|
||||
}
|
||||
|
||||
facebook = Facebook.new( provider_options )
|
||||
assert(page_found, "Page lookup for '#{page_name}'")
|
||||
end
|
||||
|
||||
post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
|
||||
# check page account
|
||||
test 'd - page account' do
|
||||
client = Facebook.new(page_access_token)
|
||||
current_user = client.current_user
|
||||
assert_equal(page_id, current_user['id'])
|
||||
assert_equal(page_name, current_user['name'])
|
||||
end
|
||||
|
||||
test 'e - feed post to ticket' do
|
||||
|
||||
customer_client = Koala::Facebook::API.new(customer_access_token)
|
||||
message = "I've got an issue with my hat, serial number ##{rand(99_999)}"
|
||||
post = customer_client.put_wall_post(message, {}, page_id)
|
||||
|
||||
# fetch check system account
|
||||
Channel.fetch
|
||||
|
||||
# check if first article has been created
|
||||
article = Ticket::Article.find_by( message_id: post['id'] )
|
||||
article = Ticket::Article.find_by(message_id: post['id'])
|
||||
|
||||
assert( article, "article post '#{post['id']}' imported" )
|
||||
assert_equal( article.body, feed_post, 'ticket article inbound body' )
|
||||
assert_equal( 1, article.ticket.articles.count, 'ticket article inbound count' )
|
||||
assert_equal( feed_post, article.ticket.articles.last.body, 'ticket article inbound body' )
|
||||
assert(article, "article post '#{post['id']}' imported")
|
||||
assert_equal(article.from, customer_name, 'ticket article inbound body')
|
||||
assert_equal(article.to, page_name, 'ticket article inbound body')
|
||||
assert_equal(article.body, message, 'ticket article inbound body')
|
||||
assert_equal(1, article.ticket.articles.count, 'ticket article inbound count')
|
||||
assert_equal(message, article.ticket.articles.last.body, 'ticket article inbound body')
|
||||
|
||||
# check customer
|
||||
customer = article.ticket.customer
|
||||
assert_equal('Bernd', customer.firstname)
|
||||
assert_equal('Hofbecker', customer.lastname)
|
||||
|
||||
post_comment = "Any updates yet? It's urgent. I love my hat."
|
||||
comment = consumer_client.put_comment(post['id'], post_comment)
|
||||
comment = customer_client.put_comment(post['id'], post_comment)
|
||||
|
||||
# fetch check system account
|
||||
Channel.fetch
|
||||
|
||||
# check if second article has been created
|
||||
article = Ticket::Article.find_by( message_id: comment['id'] )
|
||||
article = Ticket::Article.find_by(message_id: comment['id'])
|
||||
|
||||
assert( article, "article comment '#{comment['id']}' imported" )
|
||||
assert_equal( article.body, post_comment, 'ticket article inbound body' )
|
||||
assert_equal( 2, article.ticket.articles.count, 'ticket article inbound count' )
|
||||
assert_equal( post_comment, article.ticket.articles.last.body, 'ticket article inbound body' )
|
||||
assert(article, "article comment '#{comment['id']}' imported")
|
||||
assert_equal(article.from, customer_name, 'ticket article inbound body')
|
||||
assert_equal(article.body, post_comment, 'ticket article inbound body')
|
||||
assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
|
||||
assert_equal(post_comment, article.ticket.articles.last.body, 'ticket article inbound body')
|
||||
end
|
||||
|
||||
test 'feed post and comment reply' do
|
||||
test 'f - feed post and comment reply' do
|
||||
|
||||
consumer_client = Koala::Facebook::API.new( consumer_key )
|
||||
feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
|
||||
|
||||
facebook = Facebook.new( provider_options )
|
||||
|
||||
post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
|
||||
customer_client = Koala::Facebook::API.new(customer_access_token)
|
||||
feed_post = "I've got an issue with my hat, serial number ##{rand(99_999)}"
|
||||
post = customer_client.put_wall_post(feed_post, {}, page_id)
|
||||
|
||||
# fetch check system account
|
||||
Channel.fetch
|
||||
|
||||
# check if first article has been created
|
||||
article = Ticket::Article.find_by( message_id: post['id'] )
|
||||
article = Ticket::Article.find_by(message_id: post['id'])
|
||||
ticket = article.ticket
|
||||
assert(article, "article post '#{post['id']}' imported")
|
||||
|
||||
reply_text = "What's your issue Bernd?"
|
||||
# check customer
|
||||
customer = ticket.customer
|
||||
assert_equal('Bernd', customer.firstname)
|
||||
assert_equal('Hofbecker', customer.lastname)
|
||||
|
||||
# reply via ticket
|
||||
reply_text = "What's your issue Bernd?"
|
||||
outbound_article = Ticket::Article.create(
|
||||
ticket_id: article.ticket.id,
|
||||
ticket_id: ticket.id,
|
||||
body: reply_text,
|
||||
in_reply_to: post['id'],
|
||||
type: Ticket::Article::Type.find_by( name: 'facebook feed comment' ),
|
||||
sender: Ticket::Article::Sender.find_by( name: 'Agent' ),
|
||||
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
||||
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
|
||||
internal: false,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
assert( outbound_article, 'outbound article created' )
|
||||
assert_equal( outbound_article.ticket.articles.count, 2, 'ticket article outbound count' )
|
||||
assert(outbound_article, 'outbound article created')
|
||||
assert_equal(outbound_article.from, 'Hansi Merkurs Hutfabrik', 'ticket article outbound count')
|
||||
assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
|
||||
|
||||
post_comment = 'The peacock feather is fallen off.'
|
||||
comment = consumer_client.put_comment(post['id'], post_comment)
|
||||
comment = customer_client.put_comment(post['id'], post_comment)
|
||||
|
||||
# fetch check system account
|
||||
Channel.fetch
|
||||
|
||||
reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
|
||||
article = Ticket::Article.find_by(message_id: comment['id'])
|
||||
assert(article, "article comment '#{comment['id']}' imported")
|
||||
|
||||
# reply via ticket
|
||||
reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
|
||||
outbound_article = Ticket::Article.create(
|
||||
ticket_id: article.ticket.id,
|
||||
ticket_id: ticket.id,
|
||||
body: reply_text,
|
||||
in_reply_to: comment['id'],
|
||||
type: Ticket::Article::Type.find_by( name: 'facebook feed comment' ),
|
||||
sender: Ticket::Article::Sender.find_by( name: 'Agent' ),
|
||||
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
||||
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
|
||||
internal: false,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
|
||||
assert( outbound_article, 'outbound article created' )
|
||||
assert_equal( outbound_article.ticket.articles.count, 4, 'ticket article outbound count' )
|
||||
assert(outbound_article, 'outbound article created')
|
||||
assert_equal(outbound_article.from, 'Hansi Merkurs Hutfabrik', 'ticket article outbound count')
|
||||
assert_equal(outbound_article.ticket.articles.count, 4, 'ticket article outbound count')
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -187,7 +187,7 @@ class TwitterBrowserTest < TestCase
|
|||
)
|
||||
|
||||
# wait till new streaming of channel is active
|
||||
sleep 35
|
||||
sleep 40
|
||||
|
||||
# start tweet from customer
|
||||
client = Twitter::REST::Client.new do |config|
|
||||
|
|
Loading…
Reference in a new issue