2021-06-01 12:20:20 +00:00
|
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
require 'http/uri'
|
|
|
|
|
|
|
|
|
|
class TwitterSync
|
|
|
|
|
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
STATUS_URL_TEMPLATE = 'https://twitter.com/_/status/%s'.freeze
|
2019-12-27 04:17:41 +00:00
|
|
|
|
DM_URL_TEMPLATE = 'https://twitter.com/messages/%s'.freeze
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
attr_accessor :client
|
|
|
|
|
|
|
|
|
|
def initialize(auth, payload = nil)
|
2020-02-21 04:43:51 +00:00
|
|
|
|
@client = Twitter::REST::Client.new(
|
|
|
|
|
consumer_key: auth[:consumer_key],
|
|
|
|
|
consumer_secret: auth[:consumer_secret],
|
|
|
|
|
access_token: auth[:oauth_token] || auth[:access_token],
|
|
|
|
|
access_token_secret: auth[:oauth_token_secret] || auth[:access_token_secret],
|
|
|
|
|
)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
@payload = payload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
|
return if !@client
|
|
|
|
|
|
|
|
|
|
@client = nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user(tweet)
|
|
|
|
|
raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { "Twitter sender for tweet (#{tweet.id}): found" }
|
|
|
|
|
Rails.logger.debug { tweet.user.inspect }
|
|
|
|
|
tweet.user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_user(tweet)
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'Create user from tweet...' }
|
|
|
|
|
Rails.logger.debug { tweet.inspect }
|
|
|
|
|
|
|
|
|
|
# do tweet_user lookup
|
|
|
|
|
tweet_user = user(tweet)
|
|
|
|
|
|
|
|
|
|
auth = Authorization.find_by(uid: tweet_user.id, provider: 'twitter')
|
|
|
|
|
|
|
|
|
|
# create or update user
|
|
|
|
|
user_data = {
|
|
|
|
|
image_source: tweet_user.profile_image_url.to_s,
|
|
|
|
|
}
|
|
|
|
|
if auth
|
|
|
|
|
user = User.find(auth.user_id)
|
|
|
|
|
map = {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
note: 'description',
|
|
|
|
|
web: 'website',
|
2018-12-03 14:10:36 +00:00
|
|
|
|
address: 'location',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# ignore if value is already set
|
|
|
|
|
map.each do |target, source|
|
|
|
|
|
next if user[target].present?
|
|
|
|
|
|
|
|
|
|
new_value = tweet_user.send(source).to_s
|
|
|
|
|
next if new_value.blank?
|
|
|
|
|
|
|
|
|
|
user_data[target] = new_value
|
|
|
|
|
end
|
|
|
|
|
user.update!(user_data)
|
|
|
|
|
else
|
|
|
|
|
user_data[:login] = tweet_user.screen_name
|
|
|
|
|
user_data[:firstname] = tweet_user.name
|
|
|
|
|
user_data[:web] = tweet_user.website.to_s
|
|
|
|
|
user_data[:note] = tweet_user.description
|
|
|
|
|
user_data[:address] = tweet_user.location
|
|
|
|
|
user_data[:active] = true
|
|
|
|
|
user_data[:role_ids] = Role.signup_role_ids
|
|
|
|
|
|
|
|
|
|
user = User.create!(user_data)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if user_data[:image_source]
|
|
|
|
|
avatar = Avatar.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: user.id,
|
|
|
|
|
url: user_data[:image_source],
|
|
|
|
|
source: 'twitter',
|
|
|
|
|
deletable: true,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
updated_by_id: user.id,
|
|
|
|
|
created_by_id: user.id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# update user link
|
|
|
|
|
if avatar && user.image != avatar.store_hash
|
|
|
|
|
user.image = avatar.store_hash
|
|
|
|
|
user.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# create or update authorization
|
|
|
|
|
auth_data = {
|
|
|
|
|
uid: tweet_user.id,
|
|
|
|
|
username: tweet_user.screen_name,
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
provider: 'twitter'
|
|
|
|
|
}
|
|
|
|
|
if auth
|
|
|
|
|
auth.update!(auth_data)
|
|
|
|
|
else
|
|
|
|
|
Authorization.create!(auth_data)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_ticket(tweet, user, group_id, channel)
|
|
|
|
|
UserInfo.current_user_id = user.id
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'Create ticket from tweet...' }
|
|
|
|
|
Rails.logger.debug { tweet.inspect }
|
|
|
|
|
Rails.logger.debug { user.inspect }
|
|
|
|
|
Rails.logger.debug { group_id.inspect }
|
|
|
|
|
|
|
|
|
|
# normalize message
|
|
|
|
|
message = {}
|
|
|
|
|
|
2020-10-22 13:57:01 +00:00
|
|
|
|
if tweet.instance_of?(Twitter::Tweet)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
message = {
|
|
|
|
|
type: 'tweet',
|
|
|
|
|
text: tweet.text,
|
|
|
|
|
}
|
|
|
|
|
state = get_state(channel, tweet)
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-01 11:23:52 +00:00
|
|
|
|
if tweet.is_a?(Twitter::DirectMessage)
|
|
|
|
|
message = {
|
|
|
|
|
type: 'direct_message',
|
|
|
|
|
text: tweet.text,
|
|
|
|
|
}
|
|
|
|
|
state = get_state(channel, tweet)
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
if tweet.is_a?(Hash) && tweet['type'] == 'message_create'
|
|
|
|
|
message = {
|
|
|
|
|
type: 'direct_message',
|
|
|
|
|
text: tweet['message_create']['message_data']['text'],
|
|
|
|
|
}
|
|
|
|
|
state = get_state(channel, tweet)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if tweet.is_a?(Hash) && tweet['text'].present?
|
|
|
|
|
message = {
|
|
|
|
|
type: 'tweet',
|
|
|
|
|
text: tweet['text'],
|
|
|
|
|
}
|
|
|
|
|
state = get_state(channel, tweet)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# process message
|
|
|
|
|
if message[:type] == 'direct_message'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# prepare title
|
|
|
|
|
title = message[:text]
|
|
|
|
|
if title.length > 80
|
|
|
|
|
title = "#{title[0, 80]}..."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Ticket.create!(
|
|
|
|
|
customer_id: user.id,
|
|
|
|
|
title: title,
|
|
|
|
|
group_id: group_id || Group.first.id,
|
|
|
|
|
state: state,
|
|
|
|
|
priority: Ticket::Priority.find_by(default_create: true),
|
|
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
channel_id: channel.id,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
channel_screen_name: channel.options['user']['screen_name'],
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_article_webhook(item, user, ticket, channel)
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'Create article from tweet...' }
|
|
|
|
|
Rails.logger.debug { item.inspect }
|
|
|
|
|
Rails.logger.debug { user.inspect }
|
|
|
|
|
Rails.logger.debug { ticket.inspect }
|
|
|
|
|
|
|
|
|
|
# import tweet
|
|
|
|
|
to = nil
|
|
|
|
|
from = nil
|
|
|
|
|
text = nil
|
|
|
|
|
message_id = nil
|
|
|
|
|
article_type = nil
|
|
|
|
|
in_reply_to = nil
|
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
|
|
if item['type'] == 'message_create'
|
|
|
|
|
message_id = item['id']
|
|
|
|
|
text = item['message_create']['message_data']['text']
|
|
|
|
|
if item['message_create']['message_data']['entities'] && item['message_create']['message_data']['entities']['urls'].present?
|
|
|
|
|
item['message_create']['message_data']['entities']['urls'].each do |local_url|
|
|
|
|
|
next if local_url['url'].blank?
|
|
|
|
|
|
|
|
|
|
if local_url['expanded_url'].present?
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_url['url'])}}, local_url['expanded_url'])
|
2018-12-03 14:10:36 +00:00
|
|
|
|
elsif local_url['display_url']
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_url['url'])}}, local_url['display_url'])
|
2018-12-03 14:10:36 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-12-27 04:17:41 +00:00
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
app = get_app_webhook(item['message_create']['source_app_id'])
|
|
|
|
|
article_type = 'twitter direct-message'
|
2019-12-27 04:17:41 +00:00
|
|
|
|
recipient_id = item['message_create']['target']['recipient_id']
|
2018-12-03 14:10:36 +00:00
|
|
|
|
recipient_screen_name = to_user_webhook_data(item['message_create']['target']['recipient_id'])['screen_name']
|
2019-12-27 04:17:41 +00:00
|
|
|
|
sender_id = item['message_create']['sender_id']
|
2018-12-03 14:10:36 +00:00
|
|
|
|
sender_screen_name = to_user_webhook_data(item['message_create']['sender_id'])['screen_name']
|
|
|
|
|
to = "@#{recipient_screen_name}"
|
|
|
|
|
from = "@#{sender_screen_name}"
|
2019-11-07 10:01:58 +00:00
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
twitter_preferences = {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
created_at: item['created_timestamp'],
|
2019-12-27 04:17:41 +00:00
|
|
|
|
recipient_id: recipient_id,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
recipient_screen_name: recipient_screen_name,
|
2019-12-27 04:17:41 +00:00
|
|
|
|
sender_id: sender_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
|
sender_screen_name: sender_screen_name,
|
|
|
|
|
app_id: app['app_id'],
|
|
|
|
|
app_name: app['app_name'],
|
2018-12-03 14:10:36 +00:00
|
|
|
|
}
|
2019-11-07 10:01:58 +00:00
|
|
|
|
|
|
|
|
|
article_preferences = {
|
|
|
|
|
twitter: self.class.preferences_cleanup(twitter_preferences),
|
|
|
|
|
links: [
|
|
|
|
|
{
|
2019-12-27 04:17:41 +00:00
|
|
|
|
url: DM_URL_TEMPLATE % [recipient_id, sender_id].map(&:to_i).sort.join('-'),
|
2019-11-07 10:01:58 +00:00
|
|
|
|
target: '_blank',
|
|
|
|
|
name: 'on Twitter',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
elsif item['text'].present?
|
|
|
|
|
message_id = item['id']
|
|
|
|
|
text = item['text']
|
|
|
|
|
if item['extended_tweet'] && item['extended_tweet']['full_text'].present?
|
|
|
|
|
text = item['extended_tweet']['full_text']
|
|
|
|
|
end
|
|
|
|
|
article_type = 'twitter status'
|
|
|
|
|
sender_screen_name = item['user']['screen_name']
|
|
|
|
|
from = "@#{sender_screen_name}"
|
|
|
|
|
mention_ids = []
|
|
|
|
|
if item['entities']
|
|
|
|
|
|
|
|
|
|
item['entities']['user_mentions']&.each do |local_user|
|
2020-11-05 16:31:00 +00:00
|
|
|
|
if to
|
2018-12-03 14:10:36 +00:00
|
|
|
|
to += ', '
|
2020-11-05 16:31:00 +00:00
|
|
|
|
else
|
|
|
|
|
to = ''
|
2018-12-03 14:10:36 +00:00
|
|
|
|
end
|
|
|
|
|
to += "@#{local_user['screen_name']}"
|
|
|
|
|
mention_ids.push local_user['id']
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-27 04:17:41 +00:00
|
|
|
|
item['entities']['urls']&.each do |local_media|
|
|
|
|
|
|
|
|
|
|
if local_media['url'].present?
|
|
|
|
|
if local_media['expanded_url'].present?
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_media['url'])}}, local_media['expanded_url'])
|
2019-12-27 04:17:41 +00:00
|
|
|
|
elsif local_media['display_url']
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_media['url'])}}, local_media['display_url'])
|
2019-12-27 04:17:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
item['entities']['media']&.each do |local_media|
|
|
|
|
|
|
|
|
|
|
if local_media['url'].present?
|
|
|
|
|
if local_media['expanded_url'].present?
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_media['url'])}}, local_media['expanded_url'])
|
2018-12-03 14:10:36 +00:00
|
|
|
|
elsif local_media['display_url']
|
2021-05-12 11:37:44 +00:00
|
|
|
|
text.gsub!(%r{#{Regexp.quote(local_media['url'])}}, local_media['display_url'])
|
2018-12-03 14:10:36 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
url = local_media['media_url_https'] || local_media['media_url']
|
|
|
|
|
next if url.blank?
|
|
|
|
|
|
|
|
|
|
result = download_file(url)
|
|
|
|
|
if !result.success? || !result.body
|
|
|
|
|
Rails.logger.error "Unable for download image from twitter (#{url}): #{result.code}"
|
|
|
|
|
next
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
attachment = {
|
|
|
|
|
filename: url.sub(%r{^.*/(.+?)$}, '\1'),
|
2018-12-19 17:31:51 +00:00
|
|
|
|
content: result.body,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
attachments.push attachment
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
in_reply_to = item['in_reply_to_status_id']
|
|
|
|
|
|
|
|
|
|
twitter_preferences = {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
mention_ids: mention_ids,
|
|
|
|
|
geo: item['geo'],
|
|
|
|
|
retweeted: item['retweeted'],
|
|
|
|
|
possibly_sensitive: item['possibly_sensitive'],
|
2018-12-03 14:10:36 +00:00
|
|
|
|
in_reply_to_user_id: item['in_reply_to_user_id'],
|
2018-12-19 17:31:51 +00:00
|
|
|
|
place: item['place'],
|
|
|
|
|
retweet_count: item['retweet_count'],
|
|
|
|
|
source: item['source'],
|
|
|
|
|
favorited: item['favorited'],
|
|
|
|
|
truncated: item['truncated'],
|
2018-12-03 14:10:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 10:01:58 +00:00
|
|
|
|
article_preferences = {
|
|
|
|
|
twitter: self.class.preferences_cleanup(twitter_preferences),
|
|
|
|
|
links: [
|
|
|
|
|
{
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
url: STATUS_URL_TEMPLATE % item['id'],
|
2019-11-07 10:01:58 +00:00
|
|
|
|
target: '_blank',
|
|
|
|
|
name: 'on Twitter',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
else
|
|
|
|
|
raise "Unknown tweet type '#{item.class}'"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
UserInfo.current_user_id = user.id
|
|
|
|
|
|
|
|
|
|
# set ticket state to open if not new
|
|
|
|
|
ticket_state = get_state(channel, item, ticket)
|
|
|
|
|
if ticket_state.name != ticket.state.name
|
|
|
|
|
ticket.state = ticket_state
|
|
|
|
|
ticket.save!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
article = Ticket::Article.create!(
|
|
|
|
|
from: from,
|
|
|
|
|
to: to,
|
|
|
|
|
body: text,
|
|
|
|
|
message_id: message_id,
|
|
|
|
|
ticket_id: ticket.id,
|
|
|
|
|
in_reply_to: in_reply_to,
|
|
|
|
|
type_id: Ticket::Article::Type.find_by(name: article_type).id,
|
|
|
|
|
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
|
|
|
|
internal: false,
|
|
|
|
|
preferences: self.class.preferences_cleanup(article_preferences),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
attachments.each do |attachment|
|
|
|
|
|
Store.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
|
object: 'Ticket::Article',
|
|
|
|
|
o_id: article.id,
|
|
|
|
|
data: attachment[:content],
|
|
|
|
|
filename: attachment[:filename],
|
2018-12-03 14:10:36 +00:00
|
|
|
|
preferences: {},
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_article(tweet, user, ticket, channel)
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'Create article from tweet...' }
|
|
|
|
|
Rails.logger.debug { tweet.inspect }
|
|
|
|
|
Rails.logger.debug { user.inspect }
|
|
|
|
|
Rails.logger.debug { ticket.inspect }
|
|
|
|
|
|
|
|
|
|
# import tweet
|
|
|
|
|
to = nil
|
|
|
|
|
raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
|
|
|
|
|
|
|
|
|
|
article_type = 'twitter status'
|
|
|
|
|
from = "@#{tweet.user.screen_name}"
|
|
|
|
|
mention_ids = []
|
|
|
|
|
tweet.user_mentions&.each do |local_user|
|
2020-11-05 16:31:00 +00:00
|
|
|
|
if to
|
2018-12-03 14:10:36 +00:00
|
|
|
|
to += ', '
|
2020-11-05 16:31:00 +00:00
|
|
|
|
else
|
|
|
|
|
to = ''
|
2018-12-03 14:10:36 +00:00
|
|
|
|
end
|
|
|
|
|
to += "@#{local_user.screen_name}"
|
|
|
|
|
mention_ids.push local_user.id
|
|
|
|
|
end
|
|
|
|
|
in_reply_to = tweet.in_reply_to_status_id
|
|
|
|
|
|
|
|
|
|
twitter_preferences = {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
mention_ids: mention_ids,
|
|
|
|
|
geo: tweet.geo,
|
|
|
|
|
retweeted: tweet.retweeted?,
|
|
|
|
|
possibly_sensitive: tweet.possibly_sensitive?,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
in_reply_to_user_id: tweet.in_reply_to_user_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
|
place: tweet.place,
|
|
|
|
|
retweet_count: tweet.retweet_count,
|
|
|
|
|
source: tweet.source,
|
|
|
|
|
favorited: tweet.favorited?,
|
|
|
|
|
truncated: tweet.truncated?,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserInfo.current_user_id = user.id
|
|
|
|
|
|
|
|
|
|
# set ticket state to open if not new
|
|
|
|
|
ticket_state = get_state(channel, tweet, ticket)
|
|
|
|
|
if ticket_state.name != ticket.state.name
|
|
|
|
|
ticket.state = ticket_state
|
|
|
|
|
ticket.save!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
article_preferences = {
|
|
|
|
|
twitter: self.class.preferences_cleanup(twitter_preferences),
|
2018-12-19 17:31:51 +00:00
|
|
|
|
links: [
|
2018-12-03 14:10:36 +00:00
|
|
|
|
{
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
url: STATUS_URL_TEMPLATE % tweet.id,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
target: '_blank',
|
2018-12-19 17:31:51 +00:00
|
|
|
|
name: 'on Twitter',
|
2018-12-03 14:10:36 +00:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ticket::Article.create!(
|
|
|
|
|
from: from,
|
|
|
|
|
to: to,
|
|
|
|
|
body: tweet.text,
|
|
|
|
|
message_id: tweet.id,
|
|
|
|
|
ticket_id: ticket.id,
|
|
|
|
|
in_reply_to: in_reply_to,
|
|
|
|
|
type_id: Ticket::Article::Type.find_by(name: article_type).id,
|
|
|
|
|
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
|
|
|
|
internal: false,
|
|
|
|
|
preferences: self.class.preferences_cleanup(article_preferences),
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_group(tweet, group_id, channel)
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'import tweet' }
|
|
|
|
|
|
|
|
|
|
ticket = nil
|
2021-08-20 03:36:30 +00:00
|
|
|
|
Transaction.execute(reset_user_id: true, context: 'twitter') do
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
# check if parent exists
|
|
|
|
|
user = to_user(tweet)
|
|
|
|
|
raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
|
|
|
|
|
|
|
|
|
|
if 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)
|
|
|
|
|
if existing_article
|
|
|
|
|
ticket = existing_article.ticket
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
parent_tweet = @client.status(tweet.in_reply_to_status_id)
|
|
|
|
|
ticket = to_group(parent_tweet, group_id, channel)
|
|
|
|
|
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
|
|
|
|
|
# just ignore if tweet has already gone
|
|
|
|
|
Rails.logger.info "Can't import tweet (#{tweet.in_reply_to_status_id}), #{e.message}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if !ticket
|
|
|
|
|
ticket = to_ticket(tweet, user, group_id, channel)
|
|
|
|
|
end
|
|
|
|
|
to_article(tweet, user, ticket, channel)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
ticket
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
2019-12-23 07:26:43 +00:00
|
|
|
|
create a tweet or direct message from an article
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def from_article(article)
|
|
|
|
|
tweet = nil
|
2020-07-13 12:46:08 +00:00
|
|
|
|
case article[:type]
|
|
|
|
|
when 'twitter direct-message'
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
Rails.logger.debug { "Create twitter direct message from article to '#{article[:to]}'..." }
|
|
|
|
|
|
|
|
|
|
article[:to].delete!('@')
|
|
|
|
|
authorization = Authorization.find_by(provider: 'twitter', username: article[:to])
|
|
|
|
|
raise "Unable to lookup user_id for @#{article[:to]}" if !authorization
|
|
|
|
|
|
2021-07-01 11:23:52 +00:00
|
|
|
|
tweet = @client.create_direct_message(authorization.uid.to_i, article[:body])
|
2020-07-13 12:46:08 +00:00
|
|
|
|
when 'twitter status'
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
Rails.logger.debug { 'Create tweet from article...' }
|
|
|
|
|
|
2019-12-23 07:26:43 +00:00
|
|
|
|
# workaround for https://github.com/sferik/twitter/issues/677
|
|
|
|
|
# https://github.com/zammad/zammad/issues/2873 - unable to post
|
|
|
|
|
# tweets with * - replace `*` with the wide-asterisk `*`.
|
|
|
|
|
article[:body].tr!('*', '*') if article[:body].present?
|
2018-12-03 14:10:36 +00:00
|
|
|
|
tweet = @client.update(
|
|
|
|
|
article[:body],
|
|
|
|
|
{
|
|
|
|
|
in_reply_to_status_id: article[:in_reply_to]
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
raise "Can't handle unknown twitter article type '#{article[:type]}'."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { tweet.inspect }
|
|
|
|
|
tweet
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_state(channel, tweet, ticket = nil)
|
|
|
|
|
|
|
|
|
|
user_id = if tweet.is_a?(Hash)
|
|
|
|
|
if tweet['user'] && tweet['user']['id']
|
|
|
|
|
tweet['user']['id']
|
|
|
|
|
else
|
|
|
|
|
tweet['message_create']['sender_id']
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
user(tweet).id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# no changes in post is from page user it self
|
|
|
|
|
if channel.options[:user][:id].to_s == user_id.to_s
|
|
|
|
|
if !ticket
|
2020-09-30 09:07:01 +00:00
|
|
|
|
return Ticket::State.find_by(name: 'closed')
|
2018-12-03 14:10:36 +00:00
|
|
|
|
end
|
2020-09-30 09:07:01 +00:00
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
return ticket.state
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
state = Ticket::State.find_by(default_create: true)
|
|
|
|
|
return state if !ticket
|
|
|
|
|
return ticket.state if ticket.state_id == state.id
|
|
|
|
|
|
|
|
|
|
Ticket::State.find_by(default_follow_up: true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tweet_limit_reached(tweet, factor = 1)
|
|
|
|
|
max_count = 120
|
2021-07-16 14:10:31 +00:00
|
|
|
|
max_count *= factor
|
2018-12-03 14:10:36 +00:00
|
|
|
|
type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
|
|
|
|
|
created_at = Time.zone.now - 15.minutes
|
|
|
|
|
created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
|
|
|
|
|
if created_count > max_count
|
|
|
|
|
Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
replace Twitter::Place and Twitter::Geo as hash and replace Twitter::NullObject with nil
|
|
|
|
|
|
|
|
|
|
preferences = TwitterSync.preferences_cleanup(
|
|
|
|
|
twitter: twitter_preferences,
|
|
|
|
|
links: [
|
|
|
|
|
{
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
url: 'https://twitter.com/_/status/123',
|
2018-12-03 14:10:36 +00:00
|
|
|
|
target: '_blank',
|
|
|
|
|
name: 'on Twitter',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
preferences = {
|
|
|
|
|
twitter: TwitterSync.preferences_cleanup(twitter_preferences),
|
|
|
|
|
links: [
|
|
|
|
|
{
|
Fixes #2715 - Twitter status URLs are broken
This commit is a complement of its parent:
the parent fixes broken links to Twitter DMs,
while this one fixes broken links to Twitter statuses.
When importing tweets (articles of type "twitter status"),
Zammad generates a source link URL using the following format:
https://twitter.com/statuses/:id
and then stores that URL under `article.preferences[:links]`.
This URL template worked until as recently as 2017[0],
but currently fails for users actively logged in to Twitter.
Now, the correct URL template appears to be
https://twitter.com/:user_id/status/:id
where `:user_id` is not strict, and may be any word (\w+) <= 20 chars.
Try it yourself:
$ curl https://twitter.com/elonmusk/status/1069382411899817990
<html><body>You are being <a href="https://twitter.com/medenhofer/status/1069382411899817990">redirected</a>.</body></html>
In this commit, we replace `:user_id` with a single underscore (`_`).
This behavior is not officially documented anywhere (as far as I know),
but it works (for now).
This commit also extends the previous commit's DB migration/bg job
to rectify existing, broken tweet URLs stored in the database.
For performance purposes, this migration is performed in the background
and limited to the latest 10,000 Twitter articles.
[0]: https://stackoverflow.com/questions/41786123
GitHub: https://github.com/zammad/zammad/issues/2715
2019-11-14 06:03:34 +00:00
|
|
|
|
url: 'https://twitter.com/_/status/123',
|
2018-12-03 14:10:36 +00:00
|
|
|
|
target: '_blank',
|
|
|
|
|
name: 'on Twitter',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def self.preferences_cleanup(preferences)
|
|
|
|
|
|
|
|
|
|
# replace Twitter::NullObject with nill to prevent elasticsearch index issue
|
|
|
|
|
preferences.each do |key, value|
|
|
|
|
|
|
2020-10-22 13:57:01 +00:00
|
|
|
|
if value.instance_of?(Twitter::Place) || value.instance_of?(Twitter::Geo)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
preferences[key] = value.to_h
|
|
|
|
|
next
|
|
|
|
|
end
|
2020-10-22 13:57:01 +00:00
|
|
|
|
if value.instance_of?(Twitter::NullObject)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
preferences[key] = nil
|
|
|
|
|
next
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
next if !value.is_a?(Hash)
|
|
|
|
|
|
|
|
|
|
value.each do |sub_key, sub_level|
|
2020-10-22 13:57:01 +00:00
|
|
|
|
if sub_level.instance_of?(NilClass)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
value[sub_key] = nil
|
|
|
|
|
next
|
|
|
|
|
end
|
2020-10-22 13:57:01 +00:00
|
|
|
|
if sub_level.instance_of?(Twitter::Place) || sub_level.instance_of?(Twitter::Geo)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
value[sub_key] = sub_level.to_h
|
|
|
|
|
next
|
|
|
|
|
end
|
|
|
|
|
next if sub_level.class != Twitter::NullObject
|
|
|
|
|
|
|
|
|
|
value[sub_key] = nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if preferences[:twitter]
|
|
|
|
|
if preferences[:twitter][:geo].blank?
|
|
|
|
|
preferences[:twitter][:geo] = {}
|
|
|
|
|
end
|
|
|
|
|
if preferences[:twitter][:place].blank?
|
|
|
|
|
preferences[:twitter][:place] = {}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if preferences[:geo].blank?
|
|
|
|
|
preferences[:geo] = {}
|
|
|
|
|
end
|
|
|
|
|
if preferences[:place].blank?
|
|
|
|
|
preferences[:place] = {}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
preferences
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
check if tweet is from local sender
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
client.locale_sender?(tweet)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def locale_sender?(tweet)
|
|
|
|
|
tweet_user = user(tweet)
|
|
|
|
|
Channel.where(area: 'Twitter::Account').each do |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
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug { "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}" }
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
process webhook messages from twitter
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
client.process_webhook(channel)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def process_webhook(channel)
|
|
|
|
|
Rails.logger.debug { 'import tweet' }
|
|
|
|
|
ticket = nil
|
|
|
|
|
if @payload['direct_message_events'].present? && channel.options[:sync][:direct_messages][:group_id].present?
|
|
|
|
|
@payload['direct_message_events'].each do |item|
|
|
|
|
|
next if item['type'] != 'message_create'
|
|
|
|
|
|
2020-08-03 08:35:43 +00:00
|
|
|
|
next if Ticket::Article.exists?(message_id: item['id'])
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
user = to_user_webhook(item['message_create']['sender_id'])
|
|
|
|
|
ticket = to_ticket(item, user, channel.options[:sync][:direct_messages][:group_id], channel)
|
|
|
|
|
to_article_webhook(item, user, ticket, channel)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if @payload['tweet_create_events'].present?
|
|
|
|
|
@payload['tweet_create_events'].each do |item|
|
2020-08-03 08:35:43 +00:00
|
|
|
|
next if Ticket::Article.exists?(message_id: item['id'])
|
2020-08-21 10:12:17 +00:00
|
|
|
|
next if item.key?('retweeted_status') && !channel.options.dig('sync', 'track_retweets')
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
# check if it's mention
|
|
|
|
|
group_id = nil
|
|
|
|
|
if channel.options[:sync][:mentions][:group_id].present? && item['entities']['user_mentions']
|
|
|
|
|
item['entities']['user_mentions'].each do |local_user|
|
|
|
|
|
next if channel.options[:user][:id].to_s != local_user['id'].to_s
|
|
|
|
|
|
|
|
|
|
group_id = channel.options[:sync][:mentions][:group_id]
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# check if it's search term
|
|
|
|
|
if !group_id && channel.options[:sync][:search].present?
|
|
|
|
|
channel.options[:sync][:search].each do |local_search|
|
|
|
|
|
next if local_search[:term].blank?
|
|
|
|
|
next if local_search[:group_id].blank?
|
2021-05-12 11:37:44 +00:00
|
|
|
|
next if !item['text'].match?(%r{#{Regexp.quote(local_search[:term])}}i)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
group_id = local_search[:group_id]
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
next if !group_id
|
|
|
|
|
|
|
|
|
|
user = to_user_webhook(item['user']['id'], item['user'])
|
|
|
|
|
if item['in_reply_to_status_id'].present?
|
|
|
|
|
existing_article = Ticket::Article.find_by(message_id: item['in_reply_to_status_id'])
|
|
|
|
|
if existing_article
|
|
|
|
|
ticket = existing_article.ticket
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
parent_tweet = @client.status(item['in_reply_to_status_id'])
|
|
|
|
|
ticket = to_group(parent_tweet, group_id, channel)
|
|
|
|
|
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
|
|
|
|
|
# just ignore if tweet has already gone
|
|
|
|
|
Rails.logger.info "Can't import tweet (#{item['in_reply_to_status_id']}), #{e.message}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if !ticket
|
|
|
|
|
ticket = to_ticket(item, user, group_id, channel)
|
|
|
|
|
end
|
|
|
|
|
to_article_webhook(item, user, ticket, channel)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
ticket
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_app_webhook(app_id)
|
|
|
|
|
return {} if !@payload['apps']
|
|
|
|
|
return {} if !@payload['apps'][app_id]
|
|
|
|
|
|
|
|
|
|
@payload['apps'][app_id]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_user_webhook_data(user_id)
|
|
|
|
|
if @payload['user'] && @payload['user']['id'].to_s == user_id.to_s
|
|
|
|
|
return @payload['user']
|
|
|
|
|
end
|
|
|
|
|
raise 'no users in payload' if !@payload['users']
|
|
|
|
|
raise 'no users in payload' if !@payload['users'][user_id]
|
|
|
|
|
|
|
|
|
|
@payload['users'][user_id]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
download public media file from twitter
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
result = client.download_file(url)
|
|
|
|
|
|
|
|
|
|
result.body
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def download_file(url)
|
|
|
|
|
UserAgent.get(
|
|
|
|
|
url,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
open_timeout: 20,
|
|
|
|
|
read_timeout: 40,
|
2021-07-20 13:31:46 +00:00
|
|
|
|
verify_ssl: true,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_user_webhook(user_id, payload_user = nil)
|
|
|
|
|
user_payload = if payload_user && payload_user['id'].to_s == user_id.to_s
|
|
|
|
|
payload_user
|
|
|
|
|
else
|
|
|
|
|
to_user_webhook_data(user_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
auth = Authorization.find_by(uid: user_payload['id'], provider: 'twitter')
|
|
|
|
|
|
|
|
|
|
# create or update user
|
|
|
|
|
user_data = {
|
|
|
|
|
image_source: user_payload['profile_image_url'],
|
|
|
|
|
}
|
|
|
|
|
if auth
|
|
|
|
|
user = User.find(auth.user_id)
|
|
|
|
|
map = {
|
2018-12-19 17:31:51 +00:00
|
|
|
|
note: 'description',
|
|
|
|
|
web: 'url',
|
2018-12-03 14:10:36 +00:00
|
|
|
|
address: 'location',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# ignore if value is already set
|
2019-12-27 04:17:41 +00:00
|
|
|
|
map.each do |target, source|
|
2018-12-03 14:10:36 +00:00
|
|
|
|
next if user[target].present?
|
|
|
|
|
|
2019-12-27 04:17:41 +00:00
|
|
|
|
new_value = user_payload[source].to_s
|
2018-12-03 14:10:36 +00:00
|
|
|
|
next if new_value.blank?
|
|
|
|
|
|
|
|
|
|
user_data[target] = new_value
|
|
|
|
|
end
|
|
|
|
|
user.update!(user_data)
|
|
|
|
|
else
|
|
|
|
|
user_data[:login] = user_payload['screen_name']
|
|
|
|
|
user_data[:firstname] = user_payload['name']
|
|
|
|
|
user_data[:web] = user_payload['url']
|
|
|
|
|
user_data[:note] = user_payload['description']
|
|
|
|
|
user_data[:address] = user_payload['location']
|
|
|
|
|
user_data[:active] = true
|
|
|
|
|
user_data[:role_ids] = Role.signup_role_ids
|
|
|
|
|
|
|
|
|
|
user = User.create!(user_data)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if user_data[:image_source].present?
|
|
|
|
|
avatar = Avatar.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: user.id,
|
|
|
|
|
url: user_data[:image_source],
|
|
|
|
|
source: 'twitter',
|
|
|
|
|
deletable: true,
|
2018-12-03 14:10:36 +00:00
|
|
|
|
updated_by_id: user.id,
|
|
|
|
|
created_by_id: user.id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# update user link
|
|
|
|
|
if avatar && user.image != avatar.store_hash
|
|
|
|
|
user.image = avatar.store_hash
|
|
|
|
|
user.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# create or update authorization
|
|
|
|
|
auth_data = {
|
|
|
|
|
uid: user_payload['id'],
|
|
|
|
|
username: user_payload['screen_name'],
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
provider: 'twitter'
|
|
|
|
|
}
|
|
|
|
|
if auth
|
|
|
|
|
auth.update!(auth_data)
|
|
|
|
|
else
|
|
|
|
|
Authorization.create!(auth_data)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
get the user of current twitter client
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
user_hash = client.who_am_i
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def who_am_i
|
|
|
|
|
@client.user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
request a new webhook verification request from twitter
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
webhook_request_verification(webhook_id, env_name, webhook_url)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def webhook_request_verification(webhook_id, env_name, webhook_url)
|
|
|
|
|
|
|
|
|
|
Twitter::REST::Request.new(@client, :put, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
|
|
|
|
|
rescue => e
|
|
|
|
|
raise "Webhook registered but not valid (#{webhook_url}). Unable to set webhook to valid: #{e.message}"
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
get webhooks by env_name
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
webhooks = webhooks_by_env_name(env_name)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def webhooks_by_env_name(env_name)
|
|
|
|
|
Twitter::REST::Request.new(@client, :get, "/1.1/account_activity/all/#{env_name}/webhooks.json", {}).perform
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
get all webhooks
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
webhooks = webhooks(env_name)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def webhooks
|
|
|
|
|
Twitter::REST::Request.new(@client, :get, '/1.1/account_activity/all/webhooks.json', {}).perform
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
delete a webhooks
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
2019-09-03 18:23:29 +00:00
|
|
|
|
webhook_delete(webhook_id, env_name)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
2019-09-03 18:23:29 +00:00
|
|
|
|
def webhook_delete(webhook_id, env_name)
|
2018-12-03 14:10:36 +00:00
|
|
|
|
Twitter::REST::Request.new(@client, :delete, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
register a new webhooks at twitter
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
webhook_register(env_name, webhook_url)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def webhook_register(env_name, webhook_url)
|
|
|
|
|
options = {
|
|
|
|
|
url: webhook_url,
|
|
|
|
|
}
|
|
|
|
|
begin
|
|
|
|
|
response = Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/webhooks.json", options).perform
|
|
|
|
|
rescue => e
|
|
|
|
|
message = "Unable to register webhook: #{e.message}"
|
|
|
|
|
if %r{http://}.match?(webhook_url)
|
|
|
|
|
message += ' Only https webhooks possible to register.'
|
|
|
|
|
elsif webhooks.count.positive?
|
|
|
|
|
message += " Already #{webhooks.count} webhooks registered. Maybe you need to delete one first."
|
|
|
|
|
end
|
|
|
|
|
raise message
|
|
|
|
|
end
|
|
|
|
|
response
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
subscribe a user to a webhooks at twitter
|
|
|
|
|
|
|
|
|
|
client = TwitterSync.new
|
|
|
|
|
webhook_subscribe(env_name)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def webhook_subscribe(env_name)
|
|
|
|
|
|
|
|
|
|
Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/subscriptions.json", {}).perform
|
|
|
|
|
rescue => e
|
|
|
|
|
raise "Unable to subscriptions with via webhook: #{e.message}"
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|