tweet.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
  2. require 'twitter'
  3. class Tweet
  4. attr_accessor :client
  5. def initialize(auth)
  6. @client = Twitter::REST::Client.new do |config|
  7. config.consumer_key = auth[:consumer_key]
  8. config.consumer_secret = auth[:consumer_secret]
  9. config.access_token = auth[:oauth_token]
  10. config.access_token_secret = auth[:oauth_token_secret]
  11. end
  12. end
  13. def disconnect
  14. return if !@client
  15. @client = nil
  16. end
  17. def user(tweet)
  18. # status (full user data is included)
  19. return tweet.user if tweet.respond_to?('user')
  20. # direct message (full user data is included)
  21. return tweet.sender if tweet.respond_to?('sender')
  22. # search (no user data is included, do extra lookup)
  23. begin
  24. return @client.user(tweet.from_user_id) if tweet.respond_to?('from_user_id')
  25. rescue => e
  26. Rails.logger.error "Twitter (#{tweet.id}): 'from_user_id' lookup error '#{e.inspect}'"
  27. end
  28. Rails.logger.error "Twitter (#{tweet.id}): unknown user source"
  29. end
  30. def to_user(tweet)
  31. Rails.logger.debug 'Create user from tweet...'
  32. Rails.logger.debug tweet.inspect
  33. # do tweet_user lookup
  34. tweet_user = user(tweet)
  35. return if !tweet_user
  36. auth = Authorization.find_by( uid: tweet_user.id, provider: 'twitter' )
  37. # create or update user
  38. user_data = {
  39. login: tweet_user.screen_name,
  40. firstname: tweet_user.name,
  41. lastname: '',
  42. email: '',
  43. password: '',
  44. image_source: tweet_user.profile_image_url.to_s,
  45. note: tweet_user.description,
  46. active: true,
  47. roles: Role.where( name: 'Customer' ),
  48. }
  49. if auth
  50. user_data[:id] = auth.user_id
  51. end
  52. user = User.create_or_update( user_data )
  53. # create or update authorization
  54. auth_data = {
  55. uid: tweet_user.id,
  56. username: tweet_user.screen_name,
  57. user_id: user.id,
  58. provider: 'twitter'
  59. }
  60. if auth
  61. auth.update_attributes( auth_data )
  62. else
  63. Authorization.new( auth_data )
  64. end
  65. UserInfo.current_user_id = user.id
  66. user
  67. end
  68. def to_ticket(tweet, user, group_id)
  69. Rails.logger.debug 'Create ticket from tweet...'
  70. Rails.logger.debug tweet.inspect
  71. Rails.logger.debug user.inspect
  72. Rails.logger.debug group_id.inspect
  73. if tweet.class.to_s == 'Twitter::DirectMessage'
  74. article = Ticket::Article.find_by(
  75. from: 'me_bauer',
  76. type_id: Ticket::Article::Type.find_by( name: 'twitter direct-message' ).id,
  77. )
  78. if article
  79. ticket = Ticket.find_by(
  80. id: article.ticket_id,
  81. customer_id: user.id,
  82. state: Ticket::State.where.not(
  83. state_type_id: Ticket::StateType.where(
  84. name: 'closed',
  85. )
  86. )
  87. )
  88. return ticket if ticket
  89. end
  90. end
  91. Ticket.create(
  92. customer_id: user.id,
  93. title: "#{tweet.text[0, 37]}...",
  94. group_id: group_id,
  95. state_id: Ticket::State.find_by( name: 'new' ).id,
  96. priority_id: Ticket::Priority.find_by( name: '2 normal' ).id,
  97. )
  98. end
  99. def to_article(tweet, user, ticket)
  100. Rails.logger.debug 'Create article from tweet...'
  101. Rails.logger.debug tweet.inspect
  102. Rails.logger.debug user.inspect
  103. Rails.logger.debug ticket.inspect
  104. # set ticket state to open if not new
  105. if ticket.state.name != 'new'
  106. ticket.state = Ticket::State.find_by( name: 'open' )
  107. ticket.save
  108. end
  109. # import tweet
  110. to = nil
  111. if tweet.respond_to?('recipient')
  112. to = tweet.recipient.name
  113. end
  114. article_type = 'twitter status'
  115. if tweet.class.to_s == 'Twitter::DirectMessage'
  116. article_type = 'twitter direct-message'
  117. end
  118. in_reply_to = nil
  119. if tweet.respond_to?('in_reply_to_status_id') && tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
  120. in_reply_to = tweet.in_reply_to_status_id
  121. end
  122. Ticket::Article.create(
  123. from: user.login,
  124. to: to,
  125. body: tweet.text,
  126. message_id: tweet.id,
  127. ticket_id: ticket.id,
  128. in_reply_to: in_reply_to,
  129. type_id: Ticket::Article::Type.find_by( name: article_type ).id,
  130. sender_id: Ticket::Article::Sender.find_by( name: 'Customer' ).id,
  131. internal: false,
  132. )
  133. end
  134. def to_group(tweet, group_id)
  135. Rails.logger.debug 'import tweet'
  136. ticket = nil
  137. # use transaction
  138. ActiveRecord::Base.transaction do
  139. UserInfo.current_user_id = 1
  140. # check if parent exists
  141. user = to_user(tweet)
  142. return if !user
  143. if tweet.respond_to?('in_reply_to_status_id') && tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
  144. existing_article = Ticket::Article.find_by( message_id: tweet.in_reply_to_status_id.to_s )
  145. if existing_article
  146. ticket = existing_article.ticket
  147. else
  148. Rails.logger.debug 'import in_reply_tweet ' + tweet.in_reply_to_status_id.to_s
  149. parent_tweet = @client.status( tweet.in_reply_to_status_id )
  150. ticket = to_group( parent_tweet, group_id )
  151. end
  152. else
  153. ticket = to_ticket(tweet, user, group_id)
  154. end
  155. to_article(tweet, user, ticket)
  156. # execute ticket events
  157. Observer::Ticket::Notification.transaction
  158. end
  159. ticket
  160. end
  161. def from_article(article)
  162. tweet = nil
  163. if article[:type] == 'twitter direct-message'
  164. Rails.logger.debug "Create twitter direct message from article to '#{article[:to]}'..."
  165. tweet = @client.create_direct_message(
  166. article[:to],
  167. article[:body],
  168. {}
  169. )
  170. elsif article[:type] == 'twitter status'
  171. Rails.logger.debug 'Create tweet from article...'
  172. tweet = @client.update(
  173. article[:body],
  174. {
  175. in_reply_to_status_id: article[:in_reply_to]
  176. }
  177. )
  178. else
  179. fail "Can't handle unknown twitter article type '#{article[:type]}'."
  180. end
  181. Rails.logger.debug tweet.inspect
  182. tweet
  183. end
  184. end