From 6e926250a51be6904709a5c431fb7fe3518ee49c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 14 Dec 2015 23:46:02 +0100 Subject: [PATCH] Some twitter to/from fixes. --- .../ticket_zoom/article_new.coffee | 3 +- .../ticket/article/communicate_twitter.rb | 37 +++++++++----- lib/tweet.rb | 32 +++++------- test/integration/twitter_test.rb | 49 ++++++++++--------- 4 files changed, 64 insertions(+), 57 deletions(-) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee index 48b9d3b82..02254e44b 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee @@ -312,7 +312,8 @@ class App.TicketZoomArticleNew extends App.Controller @textarea.focus() openTextarea: (event, withoutAnimation) => - event.stopPropagation() + if event + event.stopPropagation() if @articleNewEdit.hasClass('is-open') return diff --git a/app/models/observer/ticket/article/communicate_twitter.rb b/app/models/observer/ticket/article/communicate_twitter.rb index 1c2e7b29a..4171344d6 100644 --- a/app/models/observer/ticket/article/communicate_twitter.rb +++ b/app/models/observer/ticket/article/communicate_twitter.rb @@ -30,19 +30,30 @@ class Observer::Ticket::Article::CommunicateTwitter < ActiveRecord::Observer ) # fill article with tweet info - record.from = tweet.user.screen_name - if tweet.user_mentions - to = '' - twitter_mention_ids = [] - tweet.user_mentions.each {|user| - if to != '' - to += ' ' - end - to += "@#{user.screen_name}" - twitter_mention_ids.push user.id - } - record.to = to - record.preferences[:twitter_mention_ids] = twitter_mention_ids + + # direct message + if tweet.class == Twitter::DirectMessage + record.from = "@#{tweet.sender.screen_name}" + record.to = "@#{tweet.recipient.screen_name}" + + # regular tweet + elsif tweet.class == Twitter::Tweet + record.from = "@#{tweet.user.screen_name}" + if tweet.user_mentions + to = '' + twitter_mention_ids = [] + tweet.user_mentions.each {|user| + if to != '' + to += ' ' + end + to += "@#{user.screen_name}" + twitter_mention_ids.push user.id + } + record.to = to + record.preferences[:twitter_mention_ids] = twitter_mention_ids + end + else + fail "Unknown tweet type '#{tweet.class}'" end record.message_id = tweet.id diff --git a/lib/tweet.rb b/lib/tweet.rb index 8b8c91293..55ad83cf2 100644 --- a/lib/tweet.rb +++ b/lib/tweet.rb @@ -110,22 +110,16 @@ class Tweet Rails.logger.debug group_id.inspect if tweet.class == Twitter::DirectMessage - article = Ticket::Article.find_by( - from: tweet.sender.screen_name, - type_id: Ticket::Article::Type.find_by(name: 'twitter direct-message').id, - ) - if article - ticket = Ticket.find_by( - id: article.ticket_id, - customer_id: user.id, - state: Ticket::State.where.not( - state_type_id: Ticket::StateType.where( - name: %w(closed merged removed), - ) + ticket = Ticket.find_by( + create_article_type: Ticket::Article::Type.lookup(name: 'twitter direct-message'), + customer_id: user.id, + state: Ticket::State.where.not( + state_type_id: Ticket::StateType.where( + name: %w(closed merged removed), ) ) - return ticket if ticket - end + ) + return ticket if ticket end UserInfo.current_user_id = user.id @@ -134,8 +128,8 @@ class Tweet customer_id: user.id, title: "#{tweet.text[0, 37]}...", group_id: group_id, - state_id: Ticket::State.find_by(name: 'new').id, - priority_id: Ticket::Priority.find_by(name: '2 normal').id, + state: Ticket::State.find_by(name: 'new'), + priority: Ticket::Priority.find_by(name: '2 normal'), preferences: { channel_id: channel.id }, @@ -162,11 +156,11 @@ class Tweet in_reply_to = nil if tweet.class == Twitter::DirectMessage article_type = 'twitter direct-message' - to = tweet.recipient.screen_name - from = tweet.sender.screen_name + to = "@#{tweet.recipient.screen_name}" + from = "@#{tweet.sender.screen_name}" elsif tweet.class == Twitter::Tweet article_type = 'twitter status' - from = tweet.user.screen_name + from = "@#{tweet.user.screen_name}" in_reply_to = tweet.in_reply_to_status_id else fail "Unknown tweet type '#{tweet.class}'" diff --git a/test/integration/twitter_test.rb b/test/integration/twitter_test.rb index fa90d5e8c..a911b70c5 100644 --- a/test/integration/twitter_test.rb +++ b/test/integration/twitter_test.rb @@ -100,7 +100,6 @@ class TwitterTest < ActiveSupport::TestCase created_by_id: 1, ) assert(ticket, "outbound ticket created, text: #{text}") - article = Ticket::Article.create( ticket_id: ticket.id, body: text, @@ -111,6 +110,8 @@ class TwitterTest < ActiveSupport::TestCase created_by_id: 1, ) assert(article, "outbound article created, text: #{text}") + assert_equal('@armin_theo', article.from, 'ticket article from') + assert_equal('', article.to, 'ticket article to') # reply by me_bauer client = Twitter::REST::Client.new do |config| @@ -139,23 +140,20 @@ class TwitterTest < ActiveSupport::TestCase # fetch check system account sleep 10 - - # fetch check system account article = nil (1..2).each { Channel.fetch # check if follow up article has been created article = Ticket::Article.find_by(message_id: tweet.id) - break if article - sleep 10 } assert(article, "article tweet '#{tweet.id}' imported") - assert_equal('armin_theo', article.from, 'ticket article inbound from') - assert_equal(nil, article.to, 'ticket article inbound to') + assert_equal('@me_bauer', article.from, 'ticket article from') + #assert_equal('@armin_theo', article.to, 'ticket article to') + assert_equal(nil, article.to, 'ticket article to') assert_equal(tweet.id.to_s, article.message_id, 'ticket article inbound message_id') assert_equal(2, article.ticket.articles.count, 'ticket article inbound count') assert_equal(reply_text.utf8_to_3bytesutf8, ticket.articles.last.body, 'ticket article inbound body') @@ -176,25 +174,25 @@ class TwitterTest < ActiveSupport::TestCase tweet = client.update( text, ) - sleep 15 # fetch check system account + sleep 15 article = nil (1..2).each { Channel.fetch # check if ticket and article has been created article = Ticket::Article.find_by(message_id: tweet.id) - break if article - sleep 10 } assert(article) + assert_equal('@me_bauer', article.from, 'ticket article from') + assert_equal(nil, article.to, 'ticket article to') ticket = article.ticket # send reply - reply_text = '@armin_theo on my side #weather' + rand(9999).to_s + reply_text = '@me_bauer on my side #weather' + rand(9999).to_s article = Ticket::Article.create( ticket_id: ticket.id, body: reply_text, @@ -205,10 +203,12 @@ class TwitterTest < ActiveSupport::TestCase created_by_id: 1, ) assert(article, "outbound article created, text: #{reply_text}") - assert_equal(nil, article.to, 'ticket article outbound to') + assert_equal('@armin_theo', article.from, 'ticket article from') + assert_equal('@me_bauer', article.to, 'ticket article to') sleep 5 tweet_found = false client.user_timeline('armin_theo').each { |local_tweet| + sleep 10 next if local_tweet.id.to_s != article.message_id.to_s tweet_found = true break @@ -246,22 +246,22 @@ class TwitterTest < ActiveSupport::TestCase text, ) assert(dm, "dm with ##{hash} created") - sleep 15 # fetch check system account + sleep 15 article = nil (1..2).each { Channel.fetch # check if ticket and article has been created article = Ticket::Article.find_by(message_id: dm.id) - break if article - sleep 10 } assert(article, "inbound article '#{text}' created") + assert_equal('@me_bauer', article.from, 'ticket article from') + assert_equal('@armin_theo', article.to, 'ticket article to') ticket = article.ticket assert(ticket, 'ticket of inbound article exists') assert(ticket.articles, 'ticket.articles exists') @@ -279,11 +279,12 @@ class TwitterTest < ActiveSupport::TestCase updated_by_id: 1, created_by_id: 1, ) - ticket.state = Ticket::State.find_by(name: 'pending reminder') - ticket.save - assert(outbound_article, 'outbound article created') assert_equal(2, outbound_article.ticket.articles.count, 'ticket article outbound count') + assert_equal('@armin_theo', outbound_article.from, 'ticket article from') + assert_equal('@me_bauer', outbound_article.to, 'ticket article to') + ticket.state = Ticket::State.find_by(name: 'pending reminder') + ticket.save text = 'Ok. ' + hash dm = client.create_direct_message( @@ -291,22 +292,22 @@ class TwitterTest < ActiveSupport::TestCase text, ) assert(dm, "second dm with ##{hash} created") - sleep 15 # fetch check system account + sleep 15 article = nil (1..2).each { Channel.fetch # check if ticket and article has been created article = Ticket::Article.find_by(message_id: dm.id) - break if article - sleep 10 } assert(article, "inbound article '#{text}' created") + assert_equal('@me_bauer', article.from, 'ticket article inbound from') + assert_equal('@armin_theo', article.to, 'ticket article inbound to') assert_equal(article.ticket.id, ticket.id, 'still the same ticket') ticket = article.ticket assert(ticket, 'ticket of inbound article exists') @@ -324,22 +325,22 @@ class TwitterTest < ActiveSupport::TestCase text, ) assert(dm, "third dm with ##{hash} created") - sleep 15 # fetch check system account + sleep 15 article = nil (1..2).each { Channel.fetch # check if ticket and article has been created article = Ticket::Article.find_by(message_id: dm.id) - break if article - sleep 10 } assert(article, "inbound article '#{text}' created") + assert_equal('@me_bauer', article.from, 'ticket article inbound from') + assert_equal('@armin_theo', article.to, 'ticket article inbound to') ticket = article.ticket assert(ticket, 'ticket of inbound article exists') assert(ticket.articles, 'ticket.articles exists')