twitter_sync.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
  2. require 'http/uri'
  3. class TwitterSync
  4. STATUS_URL_TEMPLATE = 'https://twitter.com/_/status/%s'.freeze
  5. DM_URL_TEMPLATE = 'https://twitter.com/messages/%s'.freeze
  6. attr_accessor :client
  7. def initialize(auth, payload = nil)
  8. @client = Twitter::REST::Client.new(
  9. consumer_key: auth[:consumer_key],
  10. consumer_secret: auth[:consumer_secret],
  11. access_token: auth[:oauth_token] || auth[:access_token],
  12. access_token_secret: auth[:oauth_token_secret] || auth[:access_token_secret],
  13. )
  14. @payload = payload
  15. end
  16. def disconnect
  17. return if !@client
  18. @client = nil
  19. end
  20. def user(tweet)
  21. raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
  22. Rails.logger.debug { "Twitter sender for tweet (#{tweet.id}): found" }
  23. Rails.logger.debug { tweet.user.inspect }
  24. tweet.user
  25. end
  26. def to_user(tweet)
  27. Rails.logger.debug { 'Create user from tweet...' }
  28. Rails.logger.debug { tweet.inspect }
  29. # do tweet_user lookup
  30. tweet_user = user(tweet)
  31. auth = Authorization.find_by(uid: tweet_user.id, provider: 'twitter')
  32. # create or update user
  33. user_data = {
  34. image_source: tweet_user.profile_image_url.to_s,
  35. }
  36. if auth
  37. user = User.find(auth.user_id)
  38. map = {
  39. note: 'description',
  40. web: 'website',
  41. address: 'location',
  42. }
  43. # ignore if value is already set
  44. map.each do |target, source|
  45. next if user[target].present?
  46. new_value = tweet_user.send(source).to_s
  47. next if new_value.blank?
  48. user_data[target] = new_value
  49. end
  50. user.update!(user_data)
  51. else
  52. user_data[:login] = tweet_user.screen_name
  53. user_data[:firstname] = tweet_user.name
  54. user_data[:web] = tweet_user.website.to_s
  55. user_data[:note] = tweet_user.description
  56. user_data[:address] = tweet_user.location
  57. user_data[:active] = true
  58. user_data[:role_ids] = Role.signup_role_ids
  59. user = User.create!(user_data)
  60. end
  61. if user_data[:image_source]
  62. avatar = Avatar.add(
  63. object: 'User',
  64. o_id: user.id,
  65. url: user_data[:image_source],
  66. source: 'twitter',
  67. deletable: true,
  68. updated_by_id: user.id,
  69. created_by_id: user.id,
  70. )
  71. # update user link
  72. if avatar && user.image != avatar.store_hash
  73. user.image = avatar.store_hash
  74. user.save
  75. end
  76. end
  77. # create or update authorization
  78. auth_data = {
  79. uid: tweet_user.id,
  80. username: tweet_user.screen_name,
  81. user_id: user.id,
  82. provider: 'twitter'
  83. }
  84. if auth
  85. auth.update!(auth_data)
  86. else
  87. Authorization.create!(auth_data)
  88. end
  89. user
  90. end
  91. def to_ticket(tweet, user, group_id, channel)
  92. UserInfo.current_user_id = user.id
  93. Rails.logger.debug { 'Create ticket from tweet...' }
  94. Rails.logger.debug { tweet.inspect }
  95. Rails.logger.debug { user.inspect }
  96. Rails.logger.debug { group_id.inspect }
  97. # normalize message
  98. message = {}
  99. if tweet.class == Twitter::Tweet
  100. message = {
  101. type: 'tweet',
  102. text: tweet.text,
  103. }
  104. state = get_state(channel, tweet)
  105. end
  106. if tweet.is_a?(Hash) && tweet['type'] == 'message_create'
  107. message = {
  108. type: 'direct_message',
  109. text: tweet['message_create']['message_data']['text'],
  110. }
  111. state = get_state(channel, tweet)
  112. end
  113. if tweet.is_a?(Hash) && tweet['text'].present?
  114. message = {
  115. type: 'tweet',
  116. text: tweet['text'],
  117. }
  118. state = get_state(channel, tweet)
  119. end
  120. # process message
  121. if message[:type] == 'direct_message'
  122. ticket = Ticket.find_by(
  123. create_article_type: Ticket::Article::Type.lookup(name: 'twitter direct-message'),
  124. customer_id: user.id,
  125. state: Ticket::State.where.not(
  126. state_type_id: Ticket::StateType.where(
  127. name: %w[closed merged removed],
  128. )
  129. )
  130. )
  131. return ticket if ticket
  132. end
  133. # prepare title
  134. title = message[:text]
  135. if title.length > 80
  136. title = "#{title[0, 80]}..."
  137. end
  138. Ticket.create!(
  139. customer_id: user.id,
  140. title: title,
  141. group_id: group_id || Group.first.id,
  142. state: state,
  143. priority: Ticket::Priority.find_by(default_create: true),
  144. preferences: {
  145. channel_id: channel.id,
  146. channel_screen_name: channel.options['user']['screen_name'],
  147. },
  148. )
  149. end
  150. def to_article_webhook(item, user, ticket, channel)
  151. Rails.logger.debug { 'Create article from tweet...' }
  152. Rails.logger.debug { item.inspect }
  153. Rails.logger.debug { user.inspect }
  154. Rails.logger.debug { ticket.inspect }
  155. # import tweet
  156. to = nil
  157. from = nil
  158. text = nil
  159. message_id = nil
  160. article_type = nil
  161. in_reply_to = nil
  162. attachments = []
  163. if item['type'] == 'message_create'
  164. message_id = item['id']
  165. text = item['message_create']['message_data']['text']
  166. if item['message_create']['message_data']['entities'] && item['message_create']['message_data']['entities']['urls'].present?
  167. item['message_create']['message_data']['entities']['urls'].each do |local_url|
  168. next if local_url['url'].blank?
  169. if local_url['expanded_url'].present?
  170. text.gsub!(/#{Regexp.quote(local_url['url'])}/, local_url['expanded_url'])
  171. elsif local_url['display_url']
  172. text.gsub!(/#{Regexp.quote(local_url['url'])}/, local_url['display_url'])
  173. end
  174. end
  175. end
  176. app = get_app_webhook(item['message_create']['source_app_id'])
  177. article_type = 'twitter direct-message'
  178. recipient_id = item['message_create']['target']['recipient_id']
  179. recipient_screen_name = to_user_webhook_data(item['message_create']['target']['recipient_id'])['screen_name']
  180. sender_id = item['message_create']['sender_id']
  181. sender_screen_name = to_user_webhook_data(item['message_create']['sender_id'])['screen_name']
  182. to = "@#{recipient_screen_name}"
  183. from = "@#{sender_screen_name}"
  184. twitter_preferences = {
  185. created_at: item['created_timestamp'],
  186. recipient_id: recipient_id,
  187. recipient_screen_name: recipient_screen_name,
  188. sender_id: sender_id,
  189. sender_screen_name: sender_screen_name,
  190. app_id: app['app_id'],
  191. app_name: app['app_name'],
  192. }
  193. article_preferences = {
  194. twitter: self.class.preferences_cleanup(twitter_preferences),
  195. links: [
  196. {
  197. url: DM_URL_TEMPLATE % [recipient_id, sender_id].map(&:to_i).sort.join('-'),
  198. target: '_blank',
  199. name: 'on Twitter',
  200. },
  201. ],
  202. }
  203. elsif item['text'].present?
  204. message_id = item['id']
  205. text = item['text']
  206. if item['extended_tweet'] && item['extended_tweet']['full_text'].present?
  207. text = item['extended_tweet']['full_text']
  208. end
  209. article_type = 'twitter status'
  210. sender_screen_name = item['user']['screen_name']
  211. from = "@#{sender_screen_name}"
  212. mention_ids = []
  213. if item['entities']
  214. item['entities']['user_mentions']&.each do |local_user|
  215. if !to
  216. to = ''
  217. else
  218. to += ', '
  219. end
  220. to += "@#{local_user['screen_name']}"
  221. mention_ids.push local_user['id']
  222. end
  223. item['entities']['urls']&.each do |local_media|
  224. if local_media['url'].present?
  225. if local_media['expanded_url'].present?
  226. text.gsub!(/#{Regexp.quote(local_media['url'])}/, local_media['expanded_url'])
  227. elsif local_media['display_url']
  228. text.gsub!(/#{Regexp.quote(local_media['url'])}/, local_media['display_url'])
  229. end
  230. end
  231. end
  232. item['entities']['media']&.each do |local_media|
  233. if local_media['url'].present?
  234. if local_media['expanded_url'].present?
  235. text.gsub!(/#{Regexp.quote(local_media['url'])}/, local_media['expanded_url'])
  236. elsif local_media['display_url']
  237. text.gsub!(/#{Regexp.quote(local_media['url'])}/, local_media['display_url'])
  238. end
  239. end
  240. url = local_media['media_url_https'] || local_media['media_url']
  241. next if url.blank?
  242. result = download_file(url)
  243. if !result.success? || !result.body
  244. Rails.logger.error "Unable for download image from twitter (#{url}): #{result.code}"
  245. next
  246. end
  247. attachment = {
  248. filename: url.sub(%r{^.*/(.+?)$}, '\1'),
  249. content: result.body,
  250. }
  251. attachments.push attachment
  252. end
  253. end
  254. in_reply_to = item['in_reply_to_status_id']
  255. twitter_preferences = {
  256. mention_ids: mention_ids,
  257. geo: item['geo'],
  258. retweeted: item['retweeted'],
  259. possibly_sensitive: item['possibly_sensitive'],
  260. in_reply_to_user_id: item['in_reply_to_user_id'],
  261. place: item['place'],
  262. retweet_count: item['retweet_count'],
  263. source: item['source'],
  264. favorited: item['favorited'],
  265. truncated: item['truncated'],
  266. }
  267. article_preferences = {
  268. twitter: self.class.preferences_cleanup(twitter_preferences),
  269. links: [
  270. {
  271. url: STATUS_URL_TEMPLATE % item['id'],
  272. target: '_blank',
  273. name: 'on Twitter',
  274. },
  275. ],
  276. }
  277. else
  278. raise "Unknown tweet type '#{item.class}'"
  279. end
  280. UserInfo.current_user_id = user.id
  281. # set ticket state to open if not new
  282. ticket_state = get_state(channel, item, ticket)
  283. if ticket_state.name != ticket.state.name
  284. ticket.state = ticket_state
  285. ticket.save!
  286. end
  287. article = Ticket::Article.create!(
  288. from: from,
  289. to: to,
  290. body: text,
  291. message_id: message_id,
  292. ticket_id: ticket.id,
  293. in_reply_to: in_reply_to,
  294. type_id: Ticket::Article::Type.find_by(name: article_type).id,
  295. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  296. internal: false,
  297. preferences: self.class.preferences_cleanup(article_preferences),
  298. )
  299. attachments.each do |attachment|
  300. Store.add(
  301. object: 'Ticket::Article',
  302. o_id: article.id,
  303. data: attachment[:content],
  304. filename: attachment[:filename],
  305. preferences: {},
  306. )
  307. end
  308. end
  309. def to_article(tweet, user, ticket, channel)
  310. Rails.logger.debug { 'Create article from tweet...' }
  311. Rails.logger.debug { tweet.inspect }
  312. Rails.logger.debug { user.inspect }
  313. Rails.logger.debug { ticket.inspect }
  314. # import tweet
  315. to = nil
  316. raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
  317. article_type = 'twitter status'
  318. from = "@#{tweet.user.screen_name}"
  319. mention_ids = []
  320. tweet.user_mentions&.each do |local_user|
  321. if !to
  322. to = ''
  323. else
  324. to += ', '
  325. end
  326. to += "@#{local_user.screen_name}"
  327. mention_ids.push local_user.id
  328. end
  329. in_reply_to = tweet.in_reply_to_status_id
  330. twitter_preferences = {
  331. mention_ids: mention_ids,
  332. geo: tweet.geo,
  333. retweeted: tweet.retweeted?,
  334. possibly_sensitive: tweet.possibly_sensitive?,
  335. in_reply_to_user_id: tweet.in_reply_to_user_id,
  336. place: tweet.place,
  337. retweet_count: tweet.retweet_count,
  338. source: tweet.source,
  339. favorited: tweet.favorited?,
  340. truncated: tweet.truncated?,
  341. }
  342. UserInfo.current_user_id = user.id
  343. # set ticket state to open if not new
  344. ticket_state = get_state(channel, tweet, ticket)
  345. if ticket_state.name != ticket.state.name
  346. ticket.state = ticket_state
  347. ticket.save!
  348. end
  349. article_preferences = {
  350. twitter: self.class.preferences_cleanup(twitter_preferences),
  351. links: [
  352. {
  353. url: STATUS_URL_TEMPLATE % tweet.id,
  354. target: '_blank',
  355. name: 'on Twitter',
  356. },
  357. ],
  358. }
  359. Ticket::Article.create!(
  360. from: from,
  361. to: to,
  362. body: tweet.text,
  363. message_id: tweet.id,
  364. ticket_id: ticket.id,
  365. in_reply_to: in_reply_to,
  366. type_id: Ticket::Article::Type.find_by(name: article_type).id,
  367. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  368. internal: false,
  369. preferences: self.class.preferences_cleanup(article_preferences),
  370. )
  371. end
  372. def to_group(tweet, group_id, channel)
  373. Rails.logger.debug { 'import tweet' }
  374. ticket = nil
  375. Transaction.execute(reset_user_id: true) do
  376. # check if parent exists
  377. user = to_user(tweet)
  378. raise "Unknown tweet type '#{tweet.class}'" if tweet.class != Twitter::Tweet
  379. if tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
  380. existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
  381. if existing_article
  382. ticket = existing_article.ticket
  383. else
  384. begin
  385. parent_tweet = @client.status(tweet.in_reply_to_status_id)
  386. ticket = to_group(parent_tweet, group_id, channel)
  387. rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
  388. # just ignore if tweet has already gone
  389. Rails.logger.info "Can't import tweet (#{tweet.in_reply_to_status_id}), #{e.message}"
  390. end
  391. end
  392. end
  393. if !ticket
  394. ticket = to_ticket(tweet, user, group_id, channel)
  395. end
  396. to_article(tweet, user, ticket, channel)
  397. end
  398. ticket
  399. end
  400. =begin
  401. create a tweet or direct message from an article
  402. =end
  403. def from_article(article)
  404. tweet = nil
  405. case article[:type]
  406. when 'twitter direct-message'
  407. Rails.logger.debug { "Create twitter direct message from article to '#{article[:to]}'..." }
  408. # tweet = @client.create_direct_message(
  409. # article[:to],
  410. # article[:body],
  411. # {}
  412. # )
  413. article[:to].delete!('@')
  414. authorization = Authorization.find_by(provider: 'twitter', username: article[:to])
  415. raise "Unable to lookup user_id for @#{article[:to]}" if !authorization
  416. data = {
  417. event: {
  418. type: 'message_create',
  419. message_create: {
  420. target: {
  421. recipient_id: authorization.uid,
  422. },
  423. message_data: {
  424. text: article[:body],
  425. }
  426. }
  427. }
  428. }
  429. tweet = Twitter::REST::Request.new(@client, :json_post, '/1.1/direct_messages/events/new.json', data).perform
  430. when 'twitter status'
  431. Rails.logger.debug { 'Create tweet from article...' }
  432. # rubocop:disable Style/AsciiComments
  433. # workaround for https://github.com/sferik/twitter/issues/677
  434. # https://github.com/zammad/zammad/issues/2873 - unable to post
  435. # tweets with * - replace `*` with the wide-asterisk `*`.
  436. # rubocop:enable Style/AsciiComments
  437. article[:body].tr!('*', '*') if article[:body].present?
  438. tweet = @client.update(
  439. article[:body],
  440. {
  441. in_reply_to_status_id: article[:in_reply_to]
  442. }
  443. )
  444. else
  445. raise "Can't handle unknown twitter article type '#{article[:type]}'."
  446. end
  447. Rails.logger.debug { tweet.inspect }
  448. tweet
  449. end
  450. def get_state(channel, tweet, ticket = nil)
  451. user_id = if tweet.is_a?(Hash)
  452. if tweet['user'] && tweet['user']['id']
  453. tweet['user']['id']
  454. else
  455. tweet['message_create']['sender_id']
  456. end
  457. else
  458. user(tweet).id
  459. end
  460. # no changes in post is from page user it self
  461. if channel.options[:user][:id].to_s == user_id.to_s
  462. if !ticket
  463. return Ticket::State.find_by(name: 'closed') if !ticket
  464. end
  465. return ticket.state
  466. end
  467. state = Ticket::State.find_by(default_create: true)
  468. return state if !ticket
  469. return ticket.state if ticket.state_id == state.id
  470. Ticket::State.find_by(default_follow_up: true)
  471. end
  472. def tweet_limit_reached(tweet, factor = 1)
  473. max_count = 120
  474. max_count = max_count * factor
  475. type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
  476. created_at = Time.zone.now - 15.minutes
  477. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  478. if created_count > max_count
  479. Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
  480. return true
  481. end
  482. false
  483. end
  484. =begin
  485. replace Twitter::Place and Twitter::Geo as hash and replace Twitter::NullObject with nil
  486. preferences = TwitterSync.preferences_cleanup(
  487. twitter: twitter_preferences,
  488. links: [
  489. {
  490. url: 'https://twitter.com/_/status/123',
  491. target: '_blank',
  492. name: 'on Twitter',
  493. },
  494. ],
  495. )
  496. or
  497. preferences = {
  498. twitter: TwitterSync.preferences_cleanup(twitter_preferences),
  499. links: [
  500. {
  501. url: 'https://twitter.com/_/status/123',
  502. target: '_blank',
  503. name: 'on Twitter',
  504. },
  505. ],
  506. }
  507. =end
  508. def self.preferences_cleanup(preferences)
  509. # replace Twitter::NullObject with nill to prevent elasticsearch index issue
  510. preferences.each do |key, value|
  511. if value.class == Twitter::Place || value.class == Twitter::Geo
  512. preferences[key] = value.to_h
  513. next
  514. end
  515. if value.class == Twitter::NullObject
  516. preferences[key] = nil
  517. next
  518. end
  519. next if !value.is_a?(Hash)
  520. value.each do |sub_key, sub_level|
  521. if sub_level.class == NilClass
  522. value[sub_key] = nil
  523. next
  524. end
  525. if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
  526. value[sub_key] = sub_level.to_h
  527. next
  528. end
  529. next if sub_level.class != Twitter::NullObject
  530. value[sub_key] = nil
  531. end
  532. end
  533. if preferences[:twitter]
  534. if preferences[:twitter][:geo].blank?
  535. preferences[:twitter][:geo] = {}
  536. end
  537. if preferences[:twitter][:place].blank?
  538. preferences[:twitter][:place] = {}
  539. end
  540. else
  541. if preferences[:geo].blank?
  542. preferences[:geo] = {}
  543. end
  544. if preferences[:place].blank?
  545. preferences[:place] = {}
  546. end
  547. end
  548. preferences
  549. end
  550. =begin
  551. check if tweet is from local sender
  552. client = TwitterSync.new
  553. client.locale_sender?(tweet)
  554. =end
  555. def locale_sender?(tweet)
  556. tweet_user = user(tweet)
  557. Channel.where(area: 'Twitter::Account').each do |local_channel|
  558. next if !local_channel.options
  559. next if !local_channel.options[:user]
  560. next if !local_channel.options[:user][:id]
  561. next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
  562. Rails.logger.debug { "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}" }
  563. return true
  564. end
  565. false
  566. end
  567. =begin
  568. process webhook messages from twitter
  569. client = TwitterSync.new
  570. client.process_webhook(channel)
  571. =end
  572. def process_webhook(channel)
  573. Rails.logger.debug { 'import tweet' }
  574. ticket = nil
  575. if @payload['direct_message_events'].present? && channel.options[:sync][:direct_messages][:group_id].present?
  576. @payload['direct_message_events'].each do |item|
  577. next if item['type'] != 'message_create'
  578. next if Ticket::Article.exists?(message_id: item['id'])
  579. user = to_user_webhook(item['message_create']['sender_id'])
  580. ticket = to_ticket(item, user, channel.options[:sync][:direct_messages][:group_id], channel)
  581. to_article_webhook(item, user, ticket, channel)
  582. end
  583. end
  584. if @payload['tweet_create_events'].present?
  585. @payload['tweet_create_events'].each do |item|
  586. next if Ticket::Article.exists?(message_id: item['id'])
  587. next if item.key?('retweeted_status') && !channel.options.dig('sync', 'track_retweets')
  588. # check if it's mention
  589. group_id = nil
  590. if channel.options[:sync][:mentions][:group_id].present? && item['entities']['user_mentions']
  591. item['entities']['user_mentions'].each do |local_user|
  592. next if channel.options[:user][:id].to_s != local_user['id'].to_s
  593. group_id = channel.options[:sync][:mentions][:group_id]
  594. break
  595. end
  596. end
  597. # check if it's search term
  598. if !group_id && channel.options[:sync][:search].present?
  599. channel.options[:sync][:search].each do |local_search|
  600. next if local_search[:term].blank?
  601. next if local_search[:group_id].blank?
  602. next if !item['text'].match?(/#{Regexp.quote(local_search[:term])}/i)
  603. group_id = local_search[:group_id]
  604. break
  605. end
  606. end
  607. next if !group_id
  608. user = to_user_webhook(item['user']['id'], item['user'])
  609. if item['in_reply_to_status_id'].present?
  610. existing_article = Ticket::Article.find_by(message_id: item['in_reply_to_status_id'])
  611. if existing_article
  612. ticket = existing_article.ticket
  613. else
  614. begin
  615. parent_tweet = @client.status(item['in_reply_to_status_id'])
  616. ticket = to_group(parent_tweet, group_id, channel)
  617. rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
  618. # just ignore if tweet has already gone
  619. Rails.logger.info "Can't import tweet (#{item['in_reply_to_status_id']}), #{e.message}"
  620. end
  621. end
  622. end
  623. if !ticket
  624. ticket = to_ticket(item, user, group_id, channel)
  625. end
  626. to_article_webhook(item, user, ticket, channel)
  627. end
  628. end
  629. ticket
  630. end
  631. def get_app_webhook(app_id)
  632. return {} if !@payload['apps']
  633. return {} if !@payload['apps'][app_id]
  634. @payload['apps'][app_id]
  635. end
  636. def to_user_webhook_data(user_id)
  637. if @payload['user'] && @payload['user']['id'].to_s == user_id.to_s
  638. return @payload['user']
  639. end
  640. raise 'no users in payload' if !@payload['users']
  641. raise 'no users in payload' if !@payload['users'][user_id]
  642. @payload['users'][user_id]
  643. end
  644. =begin
  645. download public media file from twitter
  646. client = TwitterSync.new
  647. result = client.download_file(url)
  648. result.body
  649. =end
  650. def download_file(url)
  651. UserAgent.get(
  652. url,
  653. {},
  654. {
  655. open_timeout: 20,
  656. read_timeout: 40,
  657. },
  658. )
  659. end
  660. def to_user_webhook(user_id, payload_user = nil)
  661. user_payload = if payload_user && payload_user['id'].to_s == user_id.to_s
  662. payload_user
  663. else
  664. to_user_webhook_data(user_id)
  665. end
  666. auth = Authorization.find_by(uid: user_payload['id'], provider: 'twitter')
  667. # create or update user
  668. user_data = {
  669. image_source: user_payload['profile_image_url'],
  670. }
  671. if auth
  672. user = User.find(auth.user_id)
  673. map = {
  674. note: 'description',
  675. web: 'url',
  676. address: 'location',
  677. }
  678. # ignore if value is already set
  679. map.each do |target, source|
  680. next if user[target].present?
  681. new_value = user_payload[source].to_s
  682. next if new_value.blank?
  683. user_data[target] = new_value
  684. end
  685. user.update!(user_data)
  686. else
  687. user_data[:login] = user_payload['screen_name']
  688. user_data[:firstname] = user_payload['name']
  689. user_data[:web] = user_payload['url']
  690. user_data[:note] = user_payload['description']
  691. user_data[:address] = user_payload['location']
  692. user_data[:active] = true
  693. user_data[:role_ids] = Role.signup_role_ids
  694. user = User.create!(user_data)
  695. end
  696. if user_data[:image_source].present?
  697. avatar = Avatar.add(
  698. object: 'User',
  699. o_id: user.id,
  700. url: user_data[:image_source],
  701. source: 'twitter',
  702. deletable: true,
  703. updated_by_id: user.id,
  704. created_by_id: user.id,
  705. )
  706. # update user link
  707. if avatar && user.image != avatar.store_hash
  708. user.image = avatar.store_hash
  709. user.save
  710. end
  711. end
  712. # create or update authorization
  713. auth_data = {
  714. uid: user_payload['id'],
  715. username: user_payload['screen_name'],
  716. user_id: user.id,
  717. provider: 'twitter'
  718. }
  719. if auth
  720. auth.update!(auth_data)
  721. else
  722. Authorization.create!(auth_data)
  723. end
  724. user
  725. end
  726. =begin
  727. get the user of current twitter client
  728. client = TwitterSync.new
  729. user_hash = client.who_am_i
  730. =end
  731. def who_am_i
  732. @client.user
  733. end
  734. =begin
  735. request a new webhook verification request from twitter
  736. client = TwitterSync.new
  737. webhook_request_verification(webhook_id, env_name, webhook_url)
  738. =end
  739. def webhook_request_verification(webhook_id, env_name, webhook_url)
  740. Twitter::REST::Request.new(@client, :put, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
  741. rescue => e
  742. raise "Webhook registered but not valid (#{webhook_url}). Unable to set webhook to valid: #{e.message}"
  743. end
  744. =begin
  745. get webhooks by env_name
  746. client = TwitterSync.new
  747. webhooks = webhooks_by_env_name(env_name)
  748. =end
  749. def webhooks_by_env_name(env_name)
  750. Twitter::REST::Request.new(@client, :get, "/1.1/account_activity/all/#{env_name}/webhooks.json", {}).perform
  751. end
  752. =begin
  753. get all webhooks
  754. client = TwitterSync.new
  755. webhooks = webhooks(env_name)
  756. =end
  757. def webhooks
  758. Twitter::REST::Request.new(@client, :get, '/1.1/account_activity/all/webhooks.json', {}).perform
  759. end
  760. =begin
  761. delete a webhooks
  762. client = TwitterSync.new
  763. webhook_delete(webhook_id, env_name)
  764. =end
  765. def webhook_delete(webhook_id, env_name)
  766. Twitter::REST::Request.new(@client, :delete, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
  767. end
  768. =begin
  769. register a new webhooks at twitter
  770. client = TwitterSync.new
  771. webhook_register(env_name, webhook_url)
  772. =end
  773. def webhook_register(env_name, webhook_url)
  774. options = {
  775. url: webhook_url,
  776. }
  777. begin
  778. response = Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/webhooks.json", options).perform
  779. rescue => e
  780. message = "Unable to register webhook: #{e.message}"
  781. if %r{http://}.match?(webhook_url)
  782. message += ' Only https webhooks possible to register.'
  783. elsif webhooks.count.positive?
  784. message += " Already #{webhooks.count} webhooks registered. Maybe you need to delete one first."
  785. end
  786. raise message
  787. end
  788. response
  789. end
  790. =begin
  791. subscribe a user to a webhooks at twitter
  792. client = TwitterSync.new
  793. webhook_subscribe(env_name)
  794. =end
  795. def webhook_subscribe(env_name)
  796. Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/subscriptions.json", {}).perform
  797. rescue => e
  798. raise "Unable to subscriptions with via webhook: #{e.message}"
  799. end
  800. end