2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
require 'channel/driver/facebook'
|
2015-07-09 14:56:01 +00:00
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
class Observer::Ticket::Article::CommunicateFacebook < ActiveRecord::Observer
|
|
|
|
observe 'ticket::_article'
|
|
|
|
|
|
|
|
def after_create(record)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
# if sender is customer, do not communication
|
2016-01-16 00:16:31 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
2015-05-07 10:11:45 +00:00
|
|
|
return 1 if sender.nil?
|
2012-12-24 13:55:43 +00:00
|
|
|
return 1 if sender['name'] == 'Customer'
|
|
|
|
|
2015-07-09 14:56:01 +00:00
|
|
|
# only apply for facebook
|
2016-01-16 00:16:31 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: record.type_id)
|
2015-07-09 14:56:01 +00:00
|
|
|
return if type['name'] !~ /\Afacebook/
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2016-01-16 00:16:31 +00:00
|
|
|
ticket = Ticket.lookup(id: record.ticket_id)
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "Can't find ticket.preferences for Ticket.find(#{record.ticket_id})" if !ticket.preferences
|
|
|
|
raise "Can't find ticket.preferences['channel_id'] for Ticket.find(#{record.ticket_id})" if !ticket.preferences['channel_id']
|
2016-01-16 00:16:31 +00:00
|
|
|
channel = Channel.lookup(id: ticket.preferences['channel_id'])
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "Channel.find(#{channel.id}) isn't a twitter channel!" if channel.options[:adapter] !~ /\Afacebook/i
|
2016-01-16 00:16:31 +00:00
|
|
|
|
|
|
|
# check source object id
|
|
|
|
ticket = record.ticket
|
|
|
|
if !ticket.preferences['channel_fb_object_id']
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "fb object id is missing in ticket.preferences['channel_fb_object_id'] for Ticket.find(#{ticket.id})"
|
2016-01-16 00:16:31 +00:00
|
|
|
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
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
facebook = Channel::Driver::Facebook.new
|
2016-01-16 00:16:31 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
record.from = post['from']['name']
|
2015-07-09 14:56:01 +00:00
|
|
|
record.message_id = post['id']
|
|
|
|
record.save
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|