Prevent import of own created tweets by streaming api.
This commit is contained in:
parent
b745bce6ac
commit
054ad6ced3
2 changed files with 63 additions and 7 deletions
|
@ -202,6 +202,23 @@ class TweetBase
|
||||||
# use transaction
|
# use transaction
|
||||||
if @connection_type == 'stream'
|
if @connection_type == 'stream'
|
||||||
ActiveRecord::Base.connection.reconnect!
|
ActiveRecord::Base.connection.reconnect!
|
||||||
|
|
||||||
|
# if sender is a system account, wait until twitter message id is stored
|
||||||
|
# on article to prevent two (own created & twitter created) articles
|
||||||
|
tweet_user = user(tweet)
|
||||||
|
Channel.where(area: 'Twitter::Account').each {|local_channel|
|
||||||
|
next if !local_channel.options
|
||||||
|
next if !local_channel.options[:user]
|
||||||
|
next if !local_channel.options[:user][:id]
|
||||||
|
next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
# return if tweet already exists (send via system)
|
||||||
|
if Ticket::Article.find_by(message_id: tweet.id)
|
||||||
|
Rails.logger.debug "Do not import tweet.id #{tweet.id}, article already exists"
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,7 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
Setting.set('system_init_done', true)
|
Setting.set('system_init_done', true)
|
||||||
|
|
||||||
# needed to check correct behavior
|
# needed to check correct behavior
|
||||||
Group.create_if_not_exists(
|
group = Group.create_if_not_exists(
|
||||||
id: 2,
|
|
||||||
name: 'Twitter',
|
name: 'Twitter',
|
||||||
note: 'All Tweets.',
|
note: 'All Tweets.',
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
|
@ -29,6 +28,9 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
if !ENV['TWITTER_SYSTEM_LOGIN']
|
if !ENV['TWITTER_SYSTEM_LOGIN']
|
||||||
raise "ERROR: Need TWITTER_SYSTEM_LOGIN - hint TWITTER_SYSTEM_LOGIN='@system'"
|
raise "ERROR: Need TWITTER_SYSTEM_LOGIN - hint TWITTER_SYSTEM_LOGIN='@system'"
|
||||||
end
|
end
|
||||||
|
if !ENV['TWITTER_SYSTEM_ID']
|
||||||
|
raise "ERROR: Need TWITTER_SYSTEM_ID - hint TWITTER_SYSTEM_ID='1405469528'"
|
||||||
|
end
|
||||||
if !ENV['TWITTER_SYSTEM_TOKEN']
|
if !ENV['TWITTER_SYSTEM_TOKEN']
|
||||||
raise "ERROR: Need TWITTER_SYSTEM_TOKEN - hint TWITTER_SYSTEM_TOKEN='1234'"
|
raise "ERROR: Need TWITTER_SYSTEM_TOKEN - hint TWITTER_SYSTEM_TOKEN='1234'"
|
||||||
end
|
end
|
||||||
|
@ -36,6 +38,7 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
raise "ERROR: Need TWITTER_SYSTEM_TOKEN_SECRET - hint TWITTER_SYSTEM_TOKEN_SECRET='1234'"
|
raise "ERROR: Need TWITTER_SYSTEM_TOKEN_SECRET - hint TWITTER_SYSTEM_TOKEN_SECRET='1234'"
|
||||||
end
|
end
|
||||||
system_login = ENV['TWITTER_SYSTEM_LOGIN']
|
system_login = ENV['TWITTER_SYSTEM_LOGIN']
|
||||||
|
system_id = ENV['TWITTER_SYSTEM_ID']
|
||||||
system_login_without_at = system_login[1, system_login.length]
|
system_login_without_at = system_login[1, system_login.length]
|
||||||
system_token = ENV['TWITTER_SYSTEM_TOKEN']
|
system_token = ENV['TWITTER_SYSTEM_TOKEN']
|
||||||
system_token_secret = ENV['TWITTER_SYSTEM_TOKEN_SECRET']
|
system_token_secret = ENV['TWITTER_SYSTEM_TOKEN_SECRET']
|
||||||
|
@ -69,13 +72,13 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
},
|
},
|
||||||
user: {
|
user: {
|
||||||
screen_name: system_login,
|
screen_name: system_login,
|
||||||
id: '1234',
|
id: system_id,
|
||||||
},
|
},
|
||||||
sync: {
|
sync: {
|
||||||
search: [
|
search: [
|
||||||
{
|
{
|
||||||
term: '#citheo42',
|
term: '#citheo42',
|
||||||
group_id: 2,
|
group_id: group.id,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
term: '#zarepl24',
|
term: '#zarepl24',
|
||||||
|
@ -83,10 +86,10 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
mentions: {
|
mentions: {
|
||||||
group_id: 2,
|
group_id: group.id,
|
||||||
},
|
},
|
||||||
direct_messages: {
|
direct_messages: {
|
||||||
group_id: 2,
|
group_id: group.id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -103,7 +106,7 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
ticket = Ticket.create(
|
ticket = Ticket.create(
|
||||||
title: text[0, 40],
|
title: text[0, 40],
|
||||||
customer_id: user.id,
|
customer_id: user.id,
|
||||||
group_id: 2,
|
group_id: group.id,
|
||||||
state: Ticket::State.find_by(name: 'new'),
|
state: Ticket::State.find_by(name: 'new'),
|
||||||
priority: Ticket::Priority.find_by(name: '2 normal'),
|
priority: Ticket::Priority.find_by(name: '2 normal'),
|
||||||
preferences: {
|
preferences: {
|
||||||
|
@ -431,6 +434,42 @@ class TwitterTest < ActiveSupport::TestCase
|
||||||
assert_equal(customer_login, article.from, 'ticket article from')
|
assert_equal(customer_login, article.from, 'ticket article from')
|
||||||
assert_equal(nil, article.to, 'ticket article to')
|
assert_equal(nil, article.to, 'ticket article to')
|
||||||
|
|
||||||
|
# send reply
|
||||||
|
reply_text = "RE #{text}"
|
||||||
|
article = Ticket::Article.create(
|
||||||
|
ticket_id: article.ticket_id,
|
||||||
|
body: reply_text,
|
||||||
|
type: Ticket::Article::Type.find_by(name: 'twitter status'),
|
||||||
|
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
|
||||||
|
internal: false,
|
||||||
|
updated_by_id: 1,
|
||||||
|
created_by_id: 1,
|
||||||
|
)
|
||||||
|
assert(article, "outbound article created, text: #{reply_text}")
|
||||||
|
assert_equal(system_login, article.from, 'ticket article from')
|
||||||
|
assert_equal('', article.to, 'ticket article to')
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
tweet_found = false
|
||||||
|
client.user_timeline(system_login_without_at).each { |local_tweet|
|
||||||
|
sleep 10
|
||||||
|
next if local_tweet.id.to_s != article.message_id.to_s
|
||||||
|
tweet_found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
|
||||||
|
|
||||||
|
count = Ticket::Article.where(message_id: article.message_id).count
|
||||||
|
assert_equal(1, count)
|
||||||
|
|
||||||
|
channel_id = article.ticket.preferences[:channel_id]
|
||||||
|
assert(channel_id)
|
||||||
|
channel = Channel.find(channel_id)
|
||||||
|
assert_equal('', channel.last_log_out)
|
||||||
|
assert_equal('ok', channel.status_out)
|
||||||
|
#assert_equal('', channel.last_log_in)
|
||||||
|
#assert_equal('ok', channel.status_in)
|
||||||
|
|
||||||
# get dm via stream
|
# get dm via stream
|
||||||
client = Twitter::REST::Client.new(
|
client = Twitter::REST::Client.new(
|
||||||
consumer_key: consumer_key,
|
consumer_key: consumer_key,
|
||||||
|
|
Loading…
Reference in a new issue