Fixed reopen of tickets all time. Create new page posts as already closed ticket if post creator was own page account (issue#251).

This commit is contained in:
Martin Edenhofer 2016-10-25 23:19:02 +02:00
parent da2a11cb8c
commit fa8c8ae8ab
3 changed files with 51 additions and 15 deletions

View file

@ -4,7 +4,14 @@ require 'facebook'
class Channel::Driver::Facebook class Channel::Driver::Facebook
def fetch (options, channel) =begin
instance = Channel::Driver::Facebook.new
instance.fetch(channel.options, channel)
=end
def fetch(options, channel)
@channel = channel @channel = channel
@sync = options['sync'] @sync = options['sync']
@pages = options['pages'] @pages = options['pages']

View file

@ -204,17 +204,18 @@ result
return if !user return if !user
# prepare title # prepare title
title = post['message'] title = post['message']
if title.length > 80 if title.length > 80
title = "#{title[0, 80]}..." title = "#{title[0, 80]}..."
end end
state = get_state(page, post)
Ticket.create( Ticket.create(
customer_id: user.id, customer_id: user.id,
title: title, title: title,
group_id: group_id, group_id: group_id,
state: Ticket::State.find_by(name: 'new'), state: state,
priority: Ticket::Priority.find_by(name: '2 normal'), priority: Ticket::Priority.find_by(name: '2 normal'),
preferences: { preferences: {
channel_id: channel.id, channel_id: channel.id,
@ -223,18 +224,12 @@ result
) )
end end
def to_article(post, ticket, _page) def to_article(post, ticket, page)
Rails.logger.debug 'Create article from post...' Rails.logger.debug 'Create article from post...'
Rails.logger.debug post.inspect Rails.logger.debug post.inspect
Rails.logger.debug ticket.inspect Rails.logger.debug ticket.inspect
# set ticket state to open if not new
if ticket.state.name != 'new'
ticket.state = Ticket::State.find_by(name: 'open')
ticket.save
end
user = to_user(post) user = to_user(post)
return if !user return if !user
@ -267,6 +262,14 @@ result
articles.each { |article| 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])
# 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
ticket.save
end
article = { article = {
#to: @account['name'], #to: @account['name'],
ticket_id: ticket.id, ticket_id: ticket.id,
@ -312,6 +315,23 @@ result
private private
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
return Ticket::State.find_by(name: 'closed') if !ticket
end
return ticket.state
end
state = Ticket::State.find_by(name: 'new')
return state if !ticket
return ticket.state if ticket.state.name == 'new'
Ticket::State.find_by(name: 'open')
end
def access_token_for_page(lookup) def access_token_for_page(lookup)
access_token = nil access_token = nil
pages.each { |page| pages.each { |page|
@ -336,14 +356,14 @@ result
user = to_user(comment) user = to_user(comment)
next if !user next if !user
article_data = { article_data = {
from: "#{user.firstname} #{user.lastname}", from: "#{user.firstname} #{user.lastname}",
body: comment['message'], body: comment['message'],
message_id: comment['id'], message_id: comment['id'],
type_id: Ticket::Article::Type.find_by( name: 'facebook feed comment' ).id, type_id: Ticket::Article::Type.find_by(name: 'facebook feed comment').id,
in_reply_to: in_reply_to in_reply_to: in_reply_to
} }
result.push(article_data) result.push(article_data)
sub_comments = @client.get_object( "#{comment['id']}/comments" ) sub_comments = @client.get_object("#{comment['id']}/comments")
sub_articles = nested_comments(sub_comments, comment['id']) sub_articles = nested_comments(sub_comments, comment['id'])
result += sub_articles result += sub_articles
} }

View file

@ -127,6 +127,7 @@ class FacebookTest < ActiveSupport::TestCase
# check if first article has been created # 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_equal('new', article.ticket.state.name)
assert(article, "article post '#{post['id']}' imported") assert(article, "article post '#{post['id']}' imported")
assert_equal(article.from, customer_name, 'ticket article inbound body') assert_equal(article.from, customer_name, 'ticket article inbound body')
@ -150,6 +151,7 @@ class FacebookTest < ActiveSupport::TestCase
# check if second article has been created # 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_equal('new', article.ticket.reload.state.name)
assert(article, "article comment '#{comment['id']}' imported") assert(article, "article comment '#{comment['id']}' imported")
assert_equal(article.from, customer_name, 'ticket article inbound body') assert_equal(article.from, customer_name, 'ticket article inbound body')
@ -171,6 +173,7 @@ class FacebookTest < ActiveSupport::TestCase
article = Ticket::Article.find_by(message_id: post['id']) article = Ticket::Article.find_by(message_id: post['id'])
ticket = article.ticket ticket = article.ticket
assert_equal('new', ticket.state.name)
assert(article, "article post '#{post['id']}' imported") assert(article, "article post '#{post['id']}' imported")
# check customer # check customer
@ -191,12 +194,17 @@ class FacebookTest < ActiveSupport::TestCase
) )
Scheduler.worker(true) Scheduler.worker(true)
assert_equal('open', ticket.reload.state.name)
outbound_article = Ticket::Article.find(outbound_article.id) outbound_article = Ticket::Article.find(outbound_article.id)
assert(outbound_article, 'outbound article created') assert(outbound_article, 'outbound article created')
assert_equal(outbound_article.from, page_name, 'ticket article outbound count') assert_equal(outbound_article.from, page_name, 'ticket article outbound count')
assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count') assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
ticket = Ticket.find(outbound_article.ticket_id)
ticket.state = Ticket::State.find_by(name: 'closed')
ticket.save!
post_comment = 'The peacock feather is fallen off.' post_comment = 'The peacock feather is fallen off.'
comment = customer_client.put_comment(post['id'], post_comment) comment = customer_client.put_comment(post['id'], post_comment)
@ -207,6 +215,7 @@ class FacebookTest < ActiveSupport::TestCase
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(article, "article comment '#{comment['id']}' imported")
assert_equal('open', ticket.reload.state.name)
# reply via ticket # reply via ticket
reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}." reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."