tweet_base.rb 11 KB

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