tweet_base.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. return 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. return 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] && !user[target].empty?
  39. new_value = tweet_user.send(source).to_s
  40. next if !new_value || new_value.empty?
  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. if tweet.user_mentions
  147. tweet.user_mentions.each do |local_user|
  148. if !to
  149. to = ''
  150. else
  151. to += ', '
  152. end
  153. to += "@#{local_user.screen_name}"
  154. mention_ids.push local_user.id
  155. end
  156. end
  157. in_reply_to = tweet.in_reply_to_status_id
  158. twitter_preferences = {
  159. mention_ids: mention_ids,
  160. geo: tweet.geo,
  161. retweeted: tweet.retweeted?,
  162. possibly_sensitive: tweet.possibly_sensitive?,
  163. in_reply_to_user_id: tweet.in_reply_to_user_id,
  164. place: tweet.place,
  165. retweet_count: tweet.retweet_count,
  166. source: tweet.source,
  167. favorited: tweet.favorited?,
  168. truncated: tweet.truncated?,
  169. }
  170. else
  171. raise "Unknown tweet type '#{tweet.class}'"
  172. end
  173. UserInfo.current_user_id = user.id
  174. # set ticket state to open if not new
  175. ticket_state = get_state(channel, tweet, ticket)
  176. if ticket_state.name != ticket.state.name
  177. ticket.state = ticket_state
  178. ticket.save!
  179. end
  180. article_preferences = {
  181. twitter: twitter_preferences,
  182. links: [
  183. {
  184. url: "https://twitter.com/statuses/#{tweet.id}",
  185. target: '_blank',
  186. name: 'on Twitter',
  187. },
  188. ],
  189. }
  190. Ticket::Article.create!(
  191. from: from,
  192. to: to,
  193. body: tweet.text,
  194. message_id: tweet.id,
  195. ticket_id: ticket.id,
  196. in_reply_to: in_reply_to,
  197. type_id: Ticket::Article::Type.find_by(name: article_type).id,
  198. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  199. internal: false,
  200. preferences: preferences_cleanup(article_preferences),
  201. )
  202. end
  203. def to_group(tweet, group_id, channel)
  204. Rails.logger.debug 'import tweet'
  205. # use transaction
  206. if @connection_type == 'stream'
  207. ActiveRecord::Base.connection.reconnect!
  208. end
  209. ticket = nil
  210. Transaction.execute(reset_user_id: true) do
  211. # check if parent exists
  212. user = to_user(tweet)
  213. if tweet.class == Twitter::DirectMessage
  214. ticket = to_ticket(tweet, user, group_id, channel)
  215. to_article(tweet, user, ticket, channel)
  216. elsif tweet.class == Twitter::Tweet
  217. if tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
  218. existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
  219. if existing_article
  220. ticket = existing_article.ticket
  221. else
  222. begin
  223. # in case of streaming mode, get parent tweet via REST client
  224. if @connection_type == 'stream'
  225. client = TweetRest.new(@auth)
  226. parent_tweet = client.status(tweet.in_reply_to_status_id)
  227. else
  228. parent_tweet = @client.status(tweet.in_reply_to_status_id)
  229. end
  230. ticket = to_group(parent_tweet, group_id, channel)
  231. rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
  232. # just ignore if tweet has already gone
  233. Rails.logger.info "Can't import tweet (#{tweet.in_reply_to_status_id}), #{e.message}"
  234. end
  235. end
  236. end
  237. if !ticket
  238. ticket = to_ticket(tweet, user, group_id, channel)
  239. end
  240. to_article(tweet, user, ticket, channel)
  241. else
  242. raise "Unknown tweet type '#{tweet.class}'"
  243. end
  244. end
  245. if @connection_type == 'stream'
  246. ActiveRecord::Base.connection.close
  247. end
  248. ticket
  249. end
  250. def from_article(article)
  251. tweet = nil
  252. if article[:type] == 'twitter direct-message'
  253. Rails.logger.debug "Create twitter direct message from article to '#{article[:to]}'..."
  254. tweet = @client.create_direct_message(
  255. article[:to],
  256. article[:body],
  257. {}
  258. )
  259. elsif article[:type] == 'twitter status'
  260. Rails.logger.debug 'Create tweet from article...'
  261. tweet = @client.update(
  262. article[:body],
  263. {
  264. in_reply_to_status_id: article[:in_reply_to]
  265. }
  266. )
  267. else
  268. raise "Can't handle unknown twitter article type '#{article[:type]}'."
  269. end
  270. Rails.logger.debug tweet.inspect
  271. tweet
  272. end
  273. def get_state(channel, tweet, ticket = nil)
  274. tweet_user = user(tweet)
  275. # no changes in post is from page user it self
  276. if channel.options[:user][:id].to_s == tweet_user.id.to_s
  277. if !ticket
  278. return Ticket::State.find_by(name: 'closed') if !ticket
  279. end
  280. return ticket.state
  281. end
  282. state = Ticket::State.find_by(default_create: true)
  283. return state if !ticket
  284. return ticket.state if ticket.state_id == state.id
  285. Ticket::State.find_by(default_follow_up: true)
  286. end
  287. def tweet_limit_reached(tweet, factor = 1)
  288. max_count = 120
  289. if @connection_type == 'stream'
  290. max_count = 30
  291. end
  292. max_count = max_count * factor
  293. type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
  294. created_at = Time.zone.now - 15.minutes
  295. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  296. if created_count > max_count
  297. Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
  298. return true
  299. end
  300. false
  301. end
  302. def direct_message_limit_reached(tweet, factor = 1)
  303. max_count = 100
  304. if @connection_type == 'stream'
  305. max_count = 40
  306. end
  307. max_count = max_count * factor
  308. type_id = Ticket::Article::Type.lookup(name: 'twitter direct-message').id
  309. created_at = Time.zone.now - 15.minutes
  310. created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
  311. if created_count > max_count
  312. Rails.logger.info "Tweet direct message limit reached #{created_count}/#{max_count}, ignored tweed id (#{tweet.id})"
  313. return true
  314. end
  315. false
  316. end
  317. def preferences_cleanup(preferences)
  318. # replace Twitter::NullObject with nill to prevent elasticsearch index issue
  319. preferences.each do |_key, value|
  320. next if !value.is_a?(Hash)
  321. value.each do |sub_key, sub_level|
  322. if sub_level.class == NilClass
  323. value[sub_key] = nil
  324. next
  325. end
  326. if sub_level.class == Twitter::Place
  327. value[sub_key] = sub_level.attrs
  328. next
  329. end
  330. next if sub_level.class != Twitter::NullObject
  331. value[sub_key] = nil
  332. end
  333. end
  334. preferences
  335. end
  336. def locale_sender?(tweet)
  337. tweet_user = user(tweet)
  338. Channel.where(area: 'Twitter::Account').each do |local_channel|
  339. next if !local_channel.options
  340. next if !local_channel.options[:user]
  341. next if !local_channel.options[:user][:id]
  342. next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
  343. Rails.logger.debug "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}"
  344. return true
  345. end
  346. false
  347. end
  348. end