tweet_base.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
  2. require 'http/uri'
  3. class TweetBase
  4. attr_accessor :client
  5. def user(tweet)
  6. if tweet.class == Twitter::DirectMessage
  7. Rails.logger.debug { "Twitter sender for dm (#{tweet.id}): found" }
  8. Rails.logger.debug { tweet.sender.inspect }
  9. tweet.sender
  10. elsif tweet.class == Twitter::Tweet
  11. Rails.logger.debug { "Twitter sender for tweet (#{tweet.id}): found" }
  12. Rails.logger.debug { tweet.user.inspect }
  13. tweet.user
  14. else
  15. raise "Unknown tweet type '#{tweet.class}'"
  16. end
  17. end
  18. def to_user(tweet)
  19. Rails.logger.debug { 'Create user from tweet...' }
  20. Rails.logger.debug { tweet.inspect }
  21. # do tweet_user lookup
  22. tweet_user = user(tweet)
  23. auth = Authorization.find_by(uid: tweet_user.id, provider: 'twitter')
  24. # create or update user
  25. user_data = {
  26. image_source: tweet_user.profile_image_url.to_s,
  27. }
  28. if auth
  29. user = User.find(auth.user_id)
  30. map = {
  31. note: 'description',
  32. web: 'website',
  33. address: 'location',
  34. }
  35. # ignore if value is already set
  36. map.each do |target, source|
  37. next if user[target].present?
  38. new_value = tweet_user.send(source).to_s
  39. next if new_value.blank?
  40. user_data[target] = new_value
  41. end
  42. user.update!(user_data)
  43. else
  44. user_data[:login] = tweet_user.screen_name
  45. user_data[:firstname] = tweet_user.name
  46. user_data[:web] = tweet_user.website.to_s
  47. user_data[:note] = tweet_user.description
  48. user_data[:address] = tweet_user.location
  49. user_data[:active] = true
  50. user_data[:role_ids] = Role.signup_role_ids
  51. user = User.create!(user_data)
  52. end
  53. if user_data[:image_source]
  54. avatar = Avatar.add(
  55. object: 'User',
  56. o_id: user.id,
  57. url: user_data[:image_source],
  58. source: 'twitter',
  59. deletable: true,
  60. updated_by_id: user.id,
  61. created_by_id: user.id,
  62. )
  63. # update user link
  64. if avatar && user.image != avatar.store_hash
  65. user.image = avatar.store_hash
  66. user.save
  67. end
  68. end
  69. # create or update authorization
  70. auth_data = {
  71. uid: tweet_user.id,
  72. username: tweet_user.screen_name,
  73. user_id: user.id,
  74. provider: 'twitter'
  75. }
  76. if auth
  77. auth.update!(auth_data)
  78. else
  79. Authorization.create!(auth_data)
  80. end
  81. user
  82. end
  83. def to_ticket(tweet, user, group_id, channel)
  84. UserInfo.current_user_id = user.id
  85. Rails.logger.debug { 'Create ticket from tweet...' }
  86. Rails.logger.debug { tweet.inspect }
  87. Rails.logger.debug { user.inspect }
  88. Rails.logger.debug { group_id.inspect }
  89. if tweet.class == Twitter::DirectMessage
  90. ticket = Ticket.find_by(
  91. create_article_type: Ticket::Article::Type.lookup(name: 'twitter direct-message'),
  92. customer_id: user.id,
  93. state: Ticket::State.where.not(
  94. state_type_id: Ticket::StateType.where(
  95. name: %w[closed merged removed],
  96. )
  97. )
  98. )
  99. return ticket if ticket
  100. end
  101. # prepare title
  102. title = tweet.text
  103. if title.length > 80
  104. title = "#{title[0, 80]}..."
  105. end
  106. state = get_state(channel, tweet)
  107. Ticket.create!(
  108. customer_id: user.id,
  109. title: title,
  110. group_id: group_id || Group.first.id,
  111. state: state,
  112. priority: Ticket::Priority.find_by(name: '2 normal'),
  113. preferences: {
  114. channel_id: channel.id,
  115. channel_screen_name: channel.options['user']['screen_name'],
  116. },
  117. )
  118. end
  119. def to_article(tweet, user, ticket, channel)
  120. Rails.logger.debug { 'Create article from tweet...' }
  121. Rails.logger.debug { tweet.inspect }
  122. Rails.logger.debug { user.inspect }
  123. Rails.logger.debug { ticket.inspect }
  124. # import tweet
  125. to = nil
  126. from = nil
  127. article_type = nil
  128. in_reply_to = nil
  129. twitter_preferences = {}
  130. if tweet.class == Twitter::DirectMessage
  131. article_type = 'twitter direct-message'
  132. to = "@#{tweet.recipient.screen_name}"
  133. from = "@#{tweet.sender.screen_name}"
  134. twitter_preferences = {
  135. created_at: tweet.created_at,
  136. recipient_id: tweet.recipient.id,
  137. recipient_screen_name: tweet.recipient.screen_name,
  138. sender_id: tweet.sender.id,
  139. sender_screen_name: tweet.sender.screen_name,
  140. }
  141. elsif tweet.class == Twitter::Tweet
  142. article_type = 'twitter status'
  143. from = "@#{tweet.user.screen_name}"
  144. mention_ids = []
  145. tweet.user_mentions&.each do |local_user|
  146. if !to
  147. to = ''
  148. else
  149. to += ', '
  150. end
  151. to += "@#{local_user.screen_name}"
  152. mention_ids.push local_user.id
  153. end
  154. in_reply_to = tweet.in_reply_to_status_id
  155. twitter_preferences = {
  156. mention_ids: mention_ids,
  157. geo: tweet.geo,
  158. retweeted: tweet.retweeted?,
  159. possibly_sensitive: tweet.possibly_sensitive?,
  160. in_reply_to_user_id: tweet.in_reply_to_user_id,
  161. place: tweet.place,
  162. retweet_count: tweet.retweet_count,
  163. source: tweet.source,
  164. favorited: tweet.favorited?,
  165. truncated: tweet.truncated?,
  166. }
  167. else
  168. raise "Unknown tweet type '#{tweet.class}'"
  169. end
  170. UserInfo.current_user_id = user.id
  171. # set ticket state to open if not new
  172. ticket_state = get_state(channel, tweet, ticket)
  173. if ticket_state.name != ticket.state.name
  174. ticket.state = ticket_state
  175. ticket.save!
  176. end
  177. article_preferences = {
  178. twitter: twitter_preferences,
  179. links: [
  180. {
  181. url: "https://twitter.com/statuses/#{tweet.id}",
  182. target: '_blank',
  183. name: 'on Twitter',
  184. },
  185. ],
  186. }
  187. Ticket::Article.create!(
  188. from: from,
  189. to: to,
  190. body: tweet.text,
  191. message_id: tweet.id,
  192. ticket_id: ticket.id,
  193. in_reply_to: in_reply_to,
  194. type_id: Ticket::Article::Type.find_by(name: article_type).id,
  195. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  196. internal: false,
  197. preferences: preferences_cleanup(article_preferences),
  198. )
  199. end
  200. def to_group(tweet, group_id, channel)
  201. Rails.logger.debug { 'import tweet' }
  202. # use transaction
  203. if @connection_type == 'stream'
  204. ActiveRecord::Base.connection.reconnect!
  205. end
  206. ticket = nil
  207. Transaction.execute(reset_user_id: true) do
  208. # check if parent exists
  209. user = to_user(tweet)
  210. if tweet.class == Twitter::DirectMessage
  211. ticket = to_ticket(tweet, user, group_id, channel)
  212. to_article(tweet, user, ticket, channel)
  213. elsif tweet.class == Twitter::Tweet
  214. if tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
  215. existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
  216. if existing_article
  217. ticket = existing_article.ticket
  218. else
  219. begin
  220. # in case of streaming mode, get parent tweet via REST client
  221. if @connection_type == 'stream'
  222. client = TweetRest.new(@auth)
  223. parent_tweet = client.status(tweet.in_reply_to_status_id)
  224. else
  225. parent_tweet = @client.status(tweet.in_reply_to_status_id)
  226. end
  227. ticket = to_group(parent_tweet, group_id, channel)
  228. rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
  229. # just ignore if tweet has already gone
  230. Rails.logger.info "Can't import tweet (#{tweet.in_reply_to_status_id}), #{e.message}"
  231. end
  232. end
  233. end
  234. if !ticket
  235. ticket = to_ticket(tweet, user, group_id, channel)
  236. end
  237. to_article(tweet, user, ticket, channel)
  238. else
  239. raise "Unknown tweet type '#{tweet.class}'"
  240. end
  241. end
  242. if @connection_type == 'stream'
  243. ActiveRecord::Base.connection.close
  244. end
  245. ticket
  246. end
  247. def from_article(article)
  248. tweet = nil
  249. if article[:type] == 'twitter direct-message'
  250. Rails.logger.debug { "Create twitter direct message from article to '#{article[:to]}'..." }
  251. tweet = @client.create_direct_message(
  252. article[:to],
  253. article[:body],
  254. {}
  255. )
  256. elsif article[:type] == 'twitter status'
  257. Rails.logger.debug { 'Create tweet from article...' }
  258. tweet = @client.update(
  259. article[:body],
  260. {
  261. in_reply_to_status_id: article[:in_reply_to]
  262. }
  263. )
  264. else
  265. raise "Can't handle unknown twitter article type '#{article[:type]}'."
  266. end
  267. Rails.logger.debug { tweet.inspect }
  268. tweet
  269. end
  270. def get_state(channel, tweet, ticket = nil)
  271. tweet_user = user(tweet)
  272. # no changes in post is from page user it self
  273. if channel.options[:user][:id].to_s == tweet_user.id.to_s
  274. if !ticket
  275. return Ticket::State.find_by(name: 'closed') if !ticket
  276. end
  277. return ticket.state
  278. end
  279. state = Ticket::State.find_by(default_create: true)
  280. return state if !ticket
  281. return ticket.state if ticket.state_id == state.id
  282. Ticket::State.find_by(default_follow_up: true)
  283. end
  284. def tweet_limit_reached(tweet, factor = 1)
  285. max_count = 120
  286. if @connection_type == 'stream'
  287. max_count = 30
  288. end
  289. max_count = max_count * factor
  290. type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
  291. created_at = Time.zone.now - 15.minutes
  292. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  293. if created_count > max_count
  294. Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
  295. return true
  296. end
  297. false
  298. end
  299. def direct_message_limit_reached(tweet, factor = 1)
  300. max_count = 100
  301. if @connection_type == 'stream'
  302. max_count = 40
  303. end
  304. max_count = max_count * factor
  305. type_id = Ticket::Article::Type.lookup(name: 'twitter direct-message').id
  306. created_at = Time.zone.now - 15.minutes
  307. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  308. if created_count > max_count
  309. Rails.logger.info "Tweet direct message limit reached #{created_count}/#{max_count}, ignored tweed id (#{tweet.id})"
  310. return true
  311. end
  312. false
  313. end
  314. def preferences_cleanup(preferences)
  315. # replace Twitter::NullObject with nill to prevent elasticsearch index issue
  316. preferences.each_value do |value|
  317. next if !value.is_a?(Hash)
  318. value.each do |sub_key, sub_level|
  319. if sub_level.class == NilClass
  320. value[sub_key] = nil
  321. next
  322. end
  323. if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
  324. value[sub_key] = sub_level.attrs
  325. next
  326. end
  327. next if sub_level.class != Twitter::NullObject
  328. value[sub_key] = nil
  329. end
  330. end
  331. preferences
  332. end
  333. def locale_sender?(tweet)
  334. tweet_user = user(tweet)
  335. Channel.where(area: 'Twitter::Account').each do |local_channel|
  336. next if !local_channel.options
  337. next if !local_channel.options[:user]
  338. next if !local_channel.options[:user][:id]
  339. next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
  340. Rails.logger.debug { "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}" }
  341. return true
  342. end
  343. false
  344. end
  345. end