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