twitter_sync.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. if article[:type] == 'twitter direct-message'
  406. Rails.logger.debug { "Create twitter direct message from article to '#{article[:to]}'..." }
  407. # tweet = @client.create_direct_message(
  408. # article[:to],
  409. # article[:body],
  410. # {}
  411. # )
  412. article[:to].delete!('@')
  413. authorization = Authorization.find_by(provider: 'twitter', username: article[:to])
  414. raise "Unable to lookup user_id for @#{article[:to]}" if !authorization
  415. data = {
  416. event: {
  417. type: 'message_create',
  418. message_create: {
  419. target: {
  420. recipient_id: authorization.uid,
  421. },
  422. message_data: {
  423. text: article[:body],
  424. }
  425. }
  426. }
  427. }
  428. tweet = Twitter::REST::Request.new(@client, :json_post, '/1.1/direct_messages/events/new.json', data).perform
  429. elsif article[:type] == 'twitter status'
  430. Rails.logger.debug { 'Create tweet from article...' }
  431. # rubocop:disable Style/AsciiComments
  432. # workaround for https://github.com/sferik/twitter/issues/677
  433. # https://github.com/zammad/zammad/issues/2873 - unable to post
  434. # tweets with * - replace `*` with the wide-asterisk `*`.
  435. # rubocop:enable Style/AsciiComments
  436. article[:body].tr!('*', '*') if article[:body].present?
  437. tweet = @client.update(
  438. article[:body],
  439. {
  440. in_reply_to_status_id: article[:in_reply_to]
  441. }
  442. )
  443. else
  444. raise "Can't handle unknown twitter article type '#{article[:type]}'."
  445. end
  446. Rails.logger.debug { tweet.inspect }
  447. tweet
  448. end
  449. def get_state(channel, tweet, ticket = nil)
  450. user_id = if tweet.is_a?(Hash)
  451. if tweet['user'] && tweet['user']['id']
  452. tweet['user']['id']
  453. else
  454. tweet['message_create']['sender_id']
  455. end
  456. else
  457. user(tweet).id
  458. end
  459. # no changes in post is from page user it self
  460. if channel.options[:user][:id].to_s == user_id.to_s
  461. if !ticket
  462. return Ticket::State.find_by(name: 'closed') if !ticket
  463. end
  464. return ticket.state
  465. end
  466. state = Ticket::State.find_by(default_create: true)
  467. return state if !ticket
  468. return ticket.state if ticket.state_id == state.id
  469. Ticket::State.find_by(default_follow_up: true)
  470. end
  471. def tweet_limit_reached(tweet, factor = 1)
  472. max_count = 120
  473. max_count = max_count * factor
  474. type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
  475. created_at = Time.zone.now - 15.minutes
  476. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  477. if created_count > max_count
  478. Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
  479. return true
  480. end
  481. false
  482. end
  483. =begin
  484. replace Twitter::Place and Twitter::Geo as hash and replace Twitter::NullObject with nil
  485. preferences = TwitterSync.preferences_cleanup(
  486. twitter: twitter_preferences,
  487. links: [
  488. {
  489. url: 'https://twitter.com/_/status/123',
  490. target: '_blank',
  491. name: 'on Twitter',
  492. },
  493. ],
  494. )
  495. or
  496. preferences = {
  497. twitter: TwitterSync.preferences_cleanup(twitter_preferences),
  498. links: [
  499. {
  500. url: 'https://twitter.com/_/status/123',
  501. target: '_blank',
  502. name: 'on Twitter',
  503. },
  504. ],
  505. }
  506. =end
  507. def self.preferences_cleanup(preferences)
  508. # replace Twitter::NullObject with nill to prevent elasticsearch index issue
  509. preferences.each do |key, value|
  510. if value.class == Twitter::Place || value.class == Twitter::Geo
  511. preferences[key] = value.to_h
  512. next
  513. end
  514. if value.class == Twitter::NullObject
  515. preferences[key] = nil
  516. next
  517. end
  518. next if !value.is_a?(Hash)
  519. value.each do |sub_key, sub_level|
  520. if sub_level.class == NilClass
  521. value[sub_key] = nil
  522. next
  523. end
  524. if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
  525. value[sub_key] = sub_level.to_h
  526. next
  527. end
  528. next if sub_level.class != Twitter::NullObject
  529. value[sub_key] = nil
  530. end
  531. end
  532. if preferences[:twitter]
  533. if preferences[:twitter][:geo].blank?
  534. preferences[:twitter][:geo] = {}
  535. end
  536. if preferences[:twitter][:place].blank?
  537. preferences[:twitter][:place] = {}
  538. end
  539. else
  540. if preferences[:geo].blank?
  541. preferences[:geo] = {}
  542. end
  543. if preferences[:place].blank?
  544. preferences[:place] = {}
  545. end
  546. end
  547. preferences
  548. end
  549. =begin
  550. check if tweet is from local sender
  551. client = TwitterSync.new
  552. client.locale_sender?(tweet)
  553. =end
  554. def locale_sender?(tweet)
  555. tweet_user = user(tweet)
  556. Channel.where(area: 'Twitter::Account').each do |local_channel|
  557. next if !local_channel.options
  558. next if !local_channel.options[:user]
  559. next if !local_channel.options[:user][:id]
  560. next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
  561. Rails.logger.debug { "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}" }
  562. return true
  563. end
  564. false
  565. end
  566. =begin
  567. process webhook messages from twitter
  568. client = TwitterSync.new
  569. client.process_webhook(channel)
  570. =end
  571. def process_webhook(channel)
  572. Rails.logger.debug { 'import tweet' }
  573. ticket = nil
  574. if @payload['direct_message_events'].present? && channel.options[:sync][:direct_messages][:group_id].present?
  575. @payload['direct_message_events'].each do |item|
  576. next if item['type'] != 'message_create'
  577. next if Ticket::Article.find_by(message_id: item['id'])
  578. user = to_user_webhook(item['message_create']['sender_id'])
  579. ticket = to_ticket(item, user, channel.options[:sync][:direct_messages][:group_id], channel)
  580. to_article_webhook(item, user, ticket, channel)
  581. end
  582. end
  583. if @payload['tweet_create_events'].present?
  584. @payload['tweet_create_events'].each do |item|
  585. next if Ticket::Article.find_by(message_id: item['id'])
  586. # check if it's mention
  587. group_id = nil
  588. if channel.options[:sync][:mentions][:group_id].present? && item['entities']['user_mentions']
  589. item['entities']['user_mentions'].each do |local_user|
  590. next if channel.options[:user][:id].to_s != local_user['id'].to_s
  591. group_id = channel.options[:sync][:mentions][:group_id]
  592. break
  593. end
  594. end
  595. # check if it's search term
  596. if !group_id && channel.options[:sync][:search].present?
  597. channel.options[:sync][:search].each do |local_search|
  598. next if local_search[:term].blank?
  599. next if local_search[:group_id].blank?
  600. next if !item['text'].match?(/#{Regexp.quote(local_search[:term])}/i)
  601. group_id = local_search[:group_id]
  602. break
  603. end
  604. end
  605. next if !group_id
  606. user = to_user_webhook(item['user']['id'], item['user'])
  607. if item['in_reply_to_status_id'].present?
  608. existing_article = Ticket::Article.find_by(message_id: item['in_reply_to_status_id'])
  609. if existing_article
  610. ticket = existing_article.ticket
  611. else
  612. begin
  613. parent_tweet = @client.status(item['in_reply_to_status_id'])
  614. ticket = to_group(parent_tweet, group_id, channel)
  615. rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
  616. # just ignore if tweet has already gone
  617. Rails.logger.info "Can't import tweet (#{item['in_reply_to_status_id']}), #{e.message}"
  618. end
  619. end
  620. end
  621. if !ticket
  622. ticket = to_ticket(item, user, group_id, channel)
  623. end
  624. to_article_webhook(item, user, ticket, channel)
  625. end
  626. end
  627. ticket
  628. end
  629. def get_app_webhook(app_id)
  630. return {} if !@payload['apps']
  631. return {} if !@payload['apps'][app_id]
  632. @payload['apps'][app_id]
  633. end
  634. def to_user_webhook_data(user_id)
  635. if @payload['user'] && @payload['user']['id'].to_s == user_id.to_s
  636. return @payload['user']
  637. end
  638. raise 'no users in payload' if !@payload['users']
  639. raise 'no users in payload' if !@payload['users'][user_id]
  640. @payload['users'][user_id]
  641. end
  642. =begin
  643. download public media file from twitter
  644. client = TwitterSync.new
  645. result = client.download_file(url)
  646. result.body
  647. =end
  648. def download_file(url)
  649. UserAgent.get(
  650. url,
  651. {},
  652. {
  653. open_timeout: 20,
  654. read_timeout: 40,
  655. },
  656. )
  657. end
  658. def to_user_webhook(user_id, payload_user = nil)
  659. user_payload = if payload_user && payload_user['id'].to_s == user_id.to_s
  660. payload_user
  661. else
  662. to_user_webhook_data(user_id)
  663. end
  664. auth = Authorization.find_by(uid: user_payload['id'], provider: 'twitter')
  665. # create or update user
  666. user_data = {
  667. image_source: user_payload['profile_image_url'],
  668. }
  669. if auth
  670. user = User.find(auth.user_id)
  671. map = {
  672. note: 'description',
  673. web: 'url',
  674. address: 'location',
  675. }
  676. # ignore if value is already set
  677. map.each do |target, source|
  678. next if user[target].present?
  679. new_value = user_payload[source].to_s
  680. next if new_value.blank?
  681. user_data[target] = new_value
  682. end
  683. user.update!(user_data)
  684. else
  685. user_data[:login] = user_payload['screen_name']
  686. user_data[:firstname] = user_payload['name']
  687. user_data[:web] = user_payload['url']
  688. user_data[:note] = user_payload['description']
  689. user_data[:address] = user_payload['location']
  690. user_data[:active] = true
  691. user_data[:role_ids] = Role.signup_role_ids
  692. user = User.create!(user_data)
  693. end
  694. if user_data[:image_source].present?
  695. avatar = Avatar.add(
  696. object: 'User',
  697. o_id: user.id,
  698. url: user_data[:image_source],
  699. source: 'twitter',
  700. deletable: true,
  701. updated_by_id: user.id,
  702. created_by_id: user.id,
  703. )
  704. # update user link
  705. if avatar && user.image != avatar.store_hash
  706. user.image = avatar.store_hash
  707. user.save
  708. end
  709. end
  710. # create or update authorization
  711. auth_data = {
  712. uid: user_payload['id'],
  713. username: user_payload['screen_name'],
  714. user_id: user.id,
  715. provider: 'twitter'
  716. }
  717. if auth
  718. auth.update!(auth_data)
  719. else
  720. Authorization.create!(auth_data)
  721. end
  722. user
  723. end
  724. =begin
  725. get the user of current twitter client
  726. client = TwitterSync.new
  727. user_hash = client.who_am_i
  728. =end
  729. def who_am_i
  730. @client.user
  731. end
  732. =begin
  733. request a new webhook verification request from twitter
  734. client = TwitterSync.new
  735. webhook_request_verification(webhook_id, env_name, webhook_url)
  736. =end
  737. def webhook_request_verification(webhook_id, env_name, webhook_url)
  738. Twitter::REST::Request.new(@client, :put, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
  739. rescue => e
  740. raise "Webhook registered but not valid (#{webhook_url}). Unable to set webhook to valid: #{e.message}"
  741. end
  742. =begin
  743. get webhooks by env_name
  744. client = TwitterSync.new
  745. webhooks = webhooks_by_env_name(env_name)
  746. =end
  747. def webhooks_by_env_name(env_name)
  748. Twitter::REST::Request.new(@client, :get, "/1.1/account_activity/all/#{env_name}/webhooks.json", {}).perform
  749. end
  750. =begin
  751. get all webhooks
  752. client = TwitterSync.new
  753. webhooks = webhooks(env_name)
  754. =end
  755. def webhooks
  756. Twitter::REST::Request.new(@client, :get, '/1.1/account_activity/all/webhooks.json', {}).perform
  757. end
  758. =begin
  759. delete a webhooks
  760. client = TwitterSync.new
  761. webhook_delete(webhook_id, env_name)
  762. =end
  763. def webhook_delete(webhook_id, env_name)
  764. Twitter::REST::Request.new(@client, :delete, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json", {}).perform
  765. end
  766. =begin
  767. register a new webhooks at twitter
  768. client = TwitterSync.new
  769. webhook_register(env_name, webhook_url)
  770. =end
  771. def webhook_register(env_name, webhook_url)
  772. options = {
  773. url: webhook_url,
  774. }
  775. begin
  776. response = Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/webhooks.json", options).perform
  777. rescue => e
  778. message = "Unable to register webhook: #{e.message}"
  779. if %r{http://}.match?(webhook_url)
  780. message += ' Only https webhooks possible to register.'
  781. elsif webhooks.count.positive?
  782. message += " Already #{webhooks.count} webhooks registered. Maybe you need to delete one first."
  783. end
  784. raise message
  785. end
  786. response
  787. end
  788. =begin
  789. subscribe a user to a webhooks at twitter
  790. client = TwitterSync.new
  791. webhook_subscribe(env_name)
  792. =end
  793. def webhook_subscribe(env_name)
  794. Twitter::REST::Request.new(@client, :post, "/1.1/account_activity/all/#{env_name}/subscriptions.json", {}).perform
  795. rescue => e
  796. raise "Unable to subscriptions with via webhook: #{e.message}"
  797. end
  798. end