Code cleanup. Reworked for current twitter result structure. Improved timing.
This commit is contained in:
parent
4adf2e3750
commit
5852dc6d8c
5 changed files with 160 additions and 150 deletions
2
Gemfile
2
Gemfile
|
@ -14,7 +14,6 @@ gem 'json'
|
|||
# Gems used only for assets and not required
|
||||
# in production environments by default.
|
||||
group :assets do
|
||||
gem 'sqlite3'
|
||||
gem 'sass-rails' #, github: 'rails/sass-rails'
|
||||
gem 'coffee-rails'
|
||||
gem 'coffee-script-source'
|
||||
|
@ -53,7 +52,6 @@ gem 'therubyracer'
|
|||
|
||||
# e. g. for mysql you need to load mysql
|
||||
gem 'mysql2', '~> 0.3.20'
|
||||
#gem 'sqlite3'
|
||||
|
||||
gem 'net-ldap'
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ class Channel::Driver::Twitter
|
|||
@tweet.client.search(search[:term], result_type: result_type).collect { |tweet|
|
||||
|
||||
break if search[:limit] && search[:limit] <= counter
|
||||
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
|
||||
break if Ticket::Article.find_by(message_id: tweet.id)
|
||||
|
||||
@tweet.to_group(tweet, search[:group_id])
|
||||
|
||||
|
@ -72,7 +72,7 @@ class Channel::Driver::Twitter
|
|||
@tweet.client.mentions_timeline.each { |tweet|
|
||||
|
||||
break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
|
||||
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
|
||||
break if Ticket::Article.find_by(message_id: tweet.id)
|
||||
|
||||
@tweet.to_group(tweet, @sync[:mentions][:group_id])
|
||||
|
||||
|
@ -91,7 +91,7 @@ class Channel::Driver::Twitter
|
|||
@tweet.client.direct_messages.each { |tweet|
|
||||
|
||||
break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
|
||||
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
|
||||
break if Ticket::Article.find_by(message_id: tweet.id)
|
||||
|
||||
@tweet.to_group(tweet, @sync[:direct_messages][:group_id])
|
||||
|
||||
|
|
97
lib/tweet.rb
97
lib/tweet.rb
|
@ -14,32 +14,29 @@ class Tweet
|
|||
config.access_token = auth[:oauth_token]
|
||||
config.access_token_secret = auth[:oauth_token_secret]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def disconnect
|
||||
|
||||
return if !@client
|
||||
|
||||
@client = nil
|
||||
end
|
||||
|
||||
def user(tweet)
|
||||
|
||||
# status (full user data is included)
|
||||
return tweet.user if tweet.respond_to?('user')
|
||||
|
||||
# direct message (full user data is included)
|
||||
return tweet.sender if tweet.respond_to?('sender')
|
||||
|
||||
# search (no user data is included, do extra lookup)
|
||||
begin
|
||||
return @client.user(tweet.from_user_id) if tweet.respond_to?('from_user_id')
|
||||
rescue => e
|
||||
Rails.logger.error "Twitter (#{tweet.id}): 'from_user_id' lookup error '#{e.inspect}'"
|
||||
if tweet.class == Twitter::DirectMessage
|
||||
Rails.logger.error "Twitter sender for dm (#{tweet.id}): found"
|
||||
Rails.logger.debug tweet.sender.inspect
|
||||
return tweet.sender
|
||||
elsif tweet.class == Twitter::Tweet
|
||||
Rails.logger.error "Twitter sender for tweet (#{tweet.id}): found"
|
||||
Rails.logger.debug tweet.user.inspect
|
||||
return tweet.user
|
||||
else
|
||||
fail "Unknown tweet type '#{tweet.class}'"
|
||||
end
|
||||
|
||||
Rails.logger.error "Twitter (#{tweet.id}): unknown user source"
|
||||
|
||||
end
|
||||
|
||||
def to_user(tweet)
|
||||
|
@ -50,26 +47,26 @@ class Tweet
|
|||
# do tweet_user lookup
|
||||
tweet_user = user(tweet)
|
||||
|
||||
return if !tweet_user
|
||||
|
||||
auth = Authorization.find_by(uid: tweet_user.id, provider: 'twitter')
|
||||
|
||||
# create or update user
|
||||
user_data = {
|
||||
login: tweet_user.screen_name,
|
||||
firstname: tweet_user.name,
|
||||
lastname: '',
|
||||
email: '',
|
||||
password: '',
|
||||
image_source: tweet_user.profile_image_url.to_s,
|
||||
note: tweet_user.description,
|
||||
active: true,
|
||||
roles: Role.where( name: 'Customer' ),
|
||||
}
|
||||
if auth
|
||||
user_data[:id] = auth.user_id
|
||||
user = User.find(auth.user_id)
|
||||
if (!user_data[:note] || user_data[:note].empty?) && tweet_user.description
|
||||
user_data[:note] = tweet_user.description
|
||||
end
|
||||
user.update_attributes(user_data)
|
||||
else
|
||||
user_data[:firstname] = tweet_user.name
|
||||
user_data[:note] = tweet_user.description
|
||||
user_data[:active] = true
|
||||
user_data[:roles] = Role.where(name: 'Customer')
|
||||
user = User.create(user_data)
|
||||
end
|
||||
user = User.create_or_update( user_data )
|
||||
|
||||
# create or update authorization
|
||||
auth_data = {
|
||||
|
@ -81,7 +78,7 @@ class Tweet
|
|||
if auth
|
||||
auth.update_attributes(auth_data)
|
||||
else
|
||||
Authorization.new( auth_data )
|
||||
Authorization.create(auth_data)
|
||||
end
|
||||
|
||||
UserInfo.current_user_id = user.id
|
||||
|
@ -96,19 +93,18 @@ class Tweet
|
|||
Rails.logger.debug user.inspect
|
||||
Rails.logger.debug group_id.inspect
|
||||
|
||||
if tweet.class.to_s == 'Twitter::DirectMessage'
|
||||
if tweet.class == Twitter::DirectMessage
|
||||
article = Ticket::Article.find_by(
|
||||
from: tweet.in_reply_to_screen_name,
|
||||
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: 'closed',
|
||||
name: %w(closed merged removed),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -140,22 +136,23 @@ class Tweet
|
|||
|
||||
# import tweet
|
||||
to = nil
|
||||
if tweet.respond_to?('recipient')
|
||||
to = tweet.recipient.name
|
||||
end
|
||||
|
||||
article_type = 'twitter status'
|
||||
if tweet.class.to_s == 'Twitter::DirectMessage'
|
||||
article_type = 'twitter direct-message'
|
||||
end
|
||||
|
||||
from = nil
|
||||
article_type = nil
|
||||
in_reply_to = nil
|
||||
if tweet.respond_to?('in_reply_to_status_id') && tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
|
||||
if tweet.class == Twitter::DirectMessage
|
||||
article_type = 'twitter direct-message'
|
||||
to = tweet.recipient.screen_name
|
||||
from = tweet.sender.screen_name
|
||||
elsif tweet.class == Twitter::Tweet
|
||||
article_type = 'twitter status'
|
||||
from = tweet.in_reply_to_screen_name
|
||||
in_reply_to = tweet.in_reply_to_status_id
|
||||
else
|
||||
fail "Unknown tweet type '#{tweet.class}'"
|
||||
end
|
||||
|
||||
Ticket::Article.create(
|
||||
from: tweet.in_reply_to_screen_name,
|
||||
from: from,
|
||||
to: to,
|
||||
body: tweet.text,
|
||||
message_id: tweet.id,
|
||||
|
@ -179,25 +176,25 @@ class Tweet
|
|||
|
||||
# check if parent exists
|
||||
user = to_user(tweet)
|
||||
|
||||
return if !user
|
||||
|
||||
if tweet.respond_to?('in_reply_to_status_id') && tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
|
||||
|
||||
existing_article = Ticket::Article.find_by( message_id: tweet.in_reply_to_status_id.to_s )
|
||||
if tweet.class == Twitter::DirectMessage
|
||||
ticket = to_ticket(tweet, user, group_id)
|
||||
to_article(tweet, user, ticket)
|
||||
elsif tweet.class == Twitter::Tweet
|
||||
if tweet.in_reply_to_status_id
|
||||
existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
|
||||
if existing_article
|
||||
ticket = existing_article.ticket
|
||||
else
|
||||
Rails.logger.debug 'import in_reply_tweet ' + tweet.in_reply_to_status_id.to_s
|
||||
|
||||
parent_tweet = @client.status(tweet.in_reply_to_status_id)
|
||||
ticket = to_group(parent_tweet, group_id)
|
||||
end
|
||||
else
|
||||
ticket = to_ticket(tweet, user, group_id)
|
||||
end
|
||||
|
||||
to_article(tweet, user, ticket)
|
||||
else
|
||||
fail "Unknown tweet type '#{tweet.class}'"
|
||||
end
|
||||
|
||||
# execute ticket events
|
||||
Observer::Ticket::Notification.transaction
|
||||
|
|
|
@ -135,13 +135,25 @@ 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(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')
|
||||
end
|
||||
|
@ -161,11 +173,11 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
tweet = client.update(
|
||||
text,
|
||||
)
|
||||
sleep 15
|
||||
sleep 10
|
||||
|
||||
# fetch check system account
|
||||
article = nil
|
||||
(1..3).each {
|
||||
(1..2).each {
|
||||
Channel.fetch
|
||||
|
||||
# check if ticket and article has been created
|
||||
|
@ -190,7 +202,8 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
created_by_id: 1,
|
||||
)
|
||||
assert(article, "outbound article created, text: #{reply_text}")
|
||||
sleep 2
|
||||
assert_equal(nil, article.to, 'ticket article outbound to')
|
||||
sleep 5
|
||||
tweet_found = false
|
||||
client.user_timeline('armin_theo').each { |local_tweet|
|
||||
next if local_tweet.id.to_s != article.message_id.to_s
|
||||
|
@ -209,7 +222,7 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
config.access_token = armin_theo_token
|
||||
config.access_token_secret = armin_theo_token_secret
|
||||
end
|
||||
dms = client.direct_messages(count: 200)
|
||||
dms = client.direct_messages(count: 40)
|
||||
dms.each {|dm|
|
||||
client.destroy_direct_message(dm.id)
|
||||
}
|
||||
|
@ -219,11 +232,10 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
access_token: me_bauer_token,
|
||||
access_token_secret: me_bauer_token_secret
|
||||
)
|
||||
dms = client.direct_messages(count: 200)
|
||||
dms = client.direct_messages(count: 40)
|
||||
dms.each {|dm|
|
||||
client.destroy_direct_message(dm.id)
|
||||
}
|
||||
|
||||
hash = '#citheo44' + rand(9999).to_s
|
||||
text = 'How about the details? ' + hash
|
||||
dm = client.create_direct_message(
|
||||
|
@ -231,6 +243,7 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
text,
|
||||
)
|
||||
assert(dm, "dm with ##{hash} created")
|
||||
sleep 10
|
||||
|
||||
# fetch check system account
|
||||
article = nil
|
||||
|
@ -242,7 +255,7 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
|
||||
break if article
|
||||
|
||||
sleep 15
|
||||
sleep 10
|
||||
}
|
||||
|
||||
assert(article, "inbound article '#{text}' created")
|
||||
|
@ -275,6 +288,7 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
text,
|
||||
)
|
||||
assert(dm, "second dm with ##{hash} created")
|
||||
sleep 10
|
||||
|
||||
# fetch check system account
|
||||
article = nil
|
||||
|
@ -290,6 +304,7 @@ class TwitterTest < ActiveSupport::TestCase
|
|||
}
|
||||
|
||||
assert(article, "inbound article '#{text}' created")
|
||||
assert_equal(article.ticket.id, ticket.id, 'still the same ticket')
|
||||
ticket = article.ticket
|
||||
assert(ticket, 'ticket of inbound article exists')
|
||||
assert(ticket.articles, 'ticket.articles exists')
|
||||
|
|
Loading…
Reference in a new issue