From fa8c8ae8ab2a1d69fc8ee1df114c27f59b8545ba Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 25 Oct 2016 23:19:02 +0200 Subject: [PATCH] Fixed reopen of tickets all time. Create new page posts as already closed ticket if post creator was own page account (issue#251). --- app/models/channel/driver/facebook.rb | 9 ++++- lib/facebook.rb | 48 +++++++++++++++++++-------- test/integration/facebook_test.rb | 9 +++++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/app/models/channel/driver/facebook.rb b/app/models/channel/driver/facebook.rb index c947714a4..555f34f4a 100644 --- a/app/models/channel/driver/facebook.rb +++ b/app/models/channel/driver/facebook.rb @@ -4,7 +4,14 @@ require '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 @sync = options['sync'] @pages = options['pages'] diff --git a/lib/facebook.rb b/lib/facebook.rb index 13c67a54c..52a852b3a 100644 --- a/lib/facebook.rb +++ b/lib/facebook.rb @@ -204,17 +204,18 @@ result return if !user # prepare title - title = post['message'] if title.length > 80 title = "#{title[0, 80]}..." end + state = get_state(page, post) + Ticket.create( customer_id: user.id, title: title, group_id: group_id, - state: Ticket::State.find_by(name: 'new'), + state: state, priority: Ticket::Priority.find_by(name: '2 normal'), preferences: { channel_id: channel.id, @@ -223,18 +224,12 @@ result ) end - def to_article(post, ticket, _page) + def to_article(post, ticket, page) 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' - ticket.state = Ticket::State.find_by(name: 'open') - ticket.save - end - user = to_user(post) return if !user @@ -267,6 +262,14 @@ result articles.each { |article| 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 = { #to: @account['name'], ticket_id: ticket.id, @@ -312,6 +315,23 @@ result 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) access_token = nil pages.each { |page| @@ -336,14 +356,14 @@ result 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, + 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 } 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']) result += sub_articles } diff --git a/test/integration/facebook_test.rb b/test/integration/facebook_test.rb index bc7ffe12a..8ea2ade71 100644 --- a/test/integration/facebook_test.rb +++ b/test/integration/facebook_test.rb @@ -127,6 +127,7 @@ class FacebookTest < ActiveSupport::TestCase # check if first article has been created article = Ticket::Article.find_by(message_id: post['id']) + assert_equal('new', article.ticket.state.name) assert(article, "article post '#{post['id']}' imported") 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 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_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']) ticket = article.ticket + assert_equal('new', ticket.state.name) assert(article, "article post '#{post['id']}' imported") # check customer @@ -191,12 +194,17 @@ class FacebookTest < ActiveSupport::TestCase ) Scheduler.worker(true) + assert_equal('open', ticket.reload.state.name) outbound_article = Ticket::Article.find(outbound_article.id) assert(outbound_article, 'outbound article created') assert_equal(outbound_article.from, page_name, '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.' 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']) assert(article, "article comment '#{comment['id']}' imported") + assert_equal('open', ticket.reload.state.name) # reply via ticket reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."