tweet_base.rb 11 KB

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