facebook.rb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Facebook
  3. attr_accessor :client, :account
  4. =begin
  5. client = Facebook.new('user_or_page_access_token')
  6. =end
  7. def initialize(access_token)
  8. connect(access_token)
  9. end
  10. =begin
  11. reconnect with other access_token
  12. client.connect('user_or_page_access_token')
  13. =end
  14. def connect(access_token)
  15. @client = Koala::Facebook::API.new(access_token)
  16. end
  17. =begin
  18. disconnect client
  19. client.disconnect
  20. =end
  21. def disconnect
  22. return if !@client
  23. @client = nil
  24. end
  25. =begin
  26. get pages of user
  27. pages = client.pages
  28. result
  29. [
  30. {
  31. id: '12345',
  32. name: 'Some Page Name',
  33. access_token, 'some_access_token_for_page',
  34. },
  35. ]
  36. =end
  37. def pages
  38. @client.get_connections('me', 'accounts').map do |page|
  39. {
  40. id: page['id'],
  41. name: page['name'],
  42. access_token: page['access_token']
  43. }
  44. end
  45. end
  46. =begin
  47. get current user
  48. pages = current_user
  49. result
  50. {
  51. 'id' => '1234567890123456',
  52. 'name' => 'Page/User Name',
  53. 'access_token' => 'some_acces_token'
  54. }
  55. =end
  56. def current_user
  57. @client.get_object('me')
  58. end
  59. =begin
  60. get user of comment/post
  61. pages = user(comment_or_post)
  62. result
  63. ???
  64. =end
  65. def user(item)
  66. return if !item['from']
  67. return if !item['from']['id']
  68. cache_key = "FB:User:Lookup:#{item['from']['id']}"
  69. cache = Rails.cache.read(cache_key)
  70. return cache if cache
  71. begin
  72. result = @client.get_object(item['from']['id'], fields: 'first_name,last_name,email')
  73. rescue
  74. result = @client.get_object(item['from']['id'], fields: 'name')
  75. end
  76. if result
  77. Rails.cache.write(cache_key, result, { expires_in: 15.minutes })
  78. end
  79. result
  80. end
  81. def to_user(item)
  82. Rails.logger.debug { 'Create user from item...' }
  83. Rails.logger.debug { item.inspect }
  84. # do item_user lookup
  85. item_user = user(item)
  86. return if !item_user
  87. auth = Authorization.find_by(uid: item_user['id'], provider: 'facebook')
  88. # create or update user
  89. user_data = {
  90. image_source: "https://graph.facebook.com/#{item_user['id']}/picture?type=large",
  91. }
  92. if auth
  93. user = User.find(auth.user_id)
  94. user.update!(user_data)
  95. else
  96. user_data[:login] = item_user['id']
  97. if item_user['first_name'] && item_user['last_name']
  98. user_data[:firstname] = item_user['first_name']
  99. user_data[:lastname] = item_user['last_name']
  100. else
  101. user_data[:firstname] = item_user['name']
  102. end
  103. user_data[:active] = true
  104. user_data[:role_ids] = Role.signup_role_ids
  105. user = User.create(user_data)
  106. end
  107. if user_data[:image_source]
  108. avatar = Avatar.add(
  109. object: 'User',
  110. o_id: user.id,
  111. url: user_data[:image_source],
  112. source: 'facebook',
  113. deletable: true,
  114. updated_by_id: user.id,
  115. created_by_id: user.id,
  116. )
  117. # update user link
  118. if avatar && user.image != avatar.store_hash
  119. user.image = avatar.store_hash
  120. user.save
  121. end
  122. end
  123. # create authorization
  124. if !auth
  125. auth_data = {
  126. uid: item_user['id'],
  127. username: item_user['id'],
  128. user_id: user.id,
  129. provider: 'facebook'
  130. }
  131. Authorization.create(auth_data)
  132. end
  133. UserInfo.current_user_id = user.id
  134. user
  135. end
  136. def to_ticket(post, group_id, channel, page)
  137. Rails.logger.debug { 'Create ticket from post...' }
  138. Rails.logger.debug { post.inspect }
  139. Rails.logger.debug { group_id.inspect }
  140. user = to_user(post)
  141. return if !user
  142. # prepare title
  143. title = post['message']
  144. if title.length > 80
  145. title = "#{title[0, 80]}..."
  146. end
  147. state = get_state(page, post)
  148. Ticket.create!(
  149. customer_id: user.id,
  150. title: title,
  151. group_id: group_id,
  152. state: state,
  153. priority: Ticket::Priority.find_by(name: '2 normal'),
  154. preferences: {
  155. channel_id: channel.id,
  156. channel_fb_object_id: page['id'],
  157. facebook: {
  158. permalink_url: post['permalink_url'],
  159. }
  160. },
  161. )
  162. end
  163. def to_article(post, ticket, page)
  164. Rails.logger.debug { 'Create article from post...' }
  165. Rails.logger.debug { post.inspect }
  166. Rails.logger.debug { ticket.inspect }
  167. user = to_user(post)
  168. return if !user
  169. to = nil
  170. if post['to'] && post['to']['data']
  171. post['to']['data'].each do |to_entry|
  172. if to
  173. to += ', '
  174. else
  175. to = ''
  176. end
  177. to += to_entry['name']
  178. end
  179. end
  180. feed_post = {
  181. from: post['from']['name'],
  182. to: to,
  183. body: post['message'],
  184. message_id: post['id'],
  185. type_id: Ticket::Article::Type.find_by(name: 'facebook feed post').id,
  186. }
  187. articles = []
  188. articles.push(feed_post)
  189. if post['comments'] && post['comments']['data']
  190. articles += nested_comments(post['comments']['data'], post['id'])
  191. end
  192. base_url = nil
  193. if ticket.preferences['facebook'] && ticket.preferences['facebook']['permalink_url']
  194. base_url = ticket.preferences['facebook']['permalink_url']
  195. end
  196. articles.each do |article|
  197. next if Ticket::Article.exists?(message_id: article[:message_id])
  198. # set ticket state to open if not new
  199. ticket_state = get_state(page, post, ticket)
  200. if ticket_state.name != ticket.state.name
  201. ticket.state = ticket_state
  202. ticket.save!
  203. end
  204. links = []
  205. if base_url
  206. url = base_url
  207. realtive_id = article[:message_id].split('_')[1]
  208. if realtive_id
  209. url += "?comment_id=#{realtive_id}"
  210. end
  211. links = [
  212. {
  213. url: url,
  214. target: '_blank',
  215. name: 'on Facebook',
  216. },
  217. ]
  218. end
  219. article = {
  220. # to: @account['name'],
  221. ticket_id: ticket.id,
  222. internal: false,
  223. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  224. created_by_id: 1,
  225. updated_by_id: 1,
  226. preferences: {
  227. links: links,
  228. },
  229. }.merge(article)
  230. Ticket::Article.create(article)
  231. end
  232. end
  233. def to_group(post, group_id, channel, page)
  234. Rails.logger.debug { 'import post' }
  235. return if !post['message']
  236. ticket = nil
  237. # use transaction
  238. Transaction.execute(reset_user_id: true, context: 'facebook') do
  239. existing_article = Ticket::Article.find_by(message_id: post['id'])
  240. ticket = if existing_article
  241. existing_article.ticket
  242. else
  243. to_ticket(post, group_id, channel, page)
  244. end
  245. to_article(post, ticket, page)
  246. end
  247. ticket
  248. end
  249. def from_article(article)
  250. post = nil
  251. if article[:type] != 'facebook feed comment'
  252. raise "Can't handle unknown facebook article type '#{article[:type]}'."
  253. end
  254. Rails.logger.debug { 'Create feed comment from article...' }
  255. post = @client.put_comment(article[:in_reply_to], article[:body])
  256. Rails.logger.debug { post.inspect }
  257. @client.get_object(post['id'])
  258. end
  259. private
  260. def get_state(page, post, ticket = nil)
  261. # no changes in post is from page user itself
  262. if post['from'] && post['from']['id'].to_s == page['id'].to_s
  263. if !ticket
  264. return Ticket::State.find_by(name: 'closed')
  265. end
  266. return ticket.state
  267. end
  268. state = Ticket::State.find_by(default_create: true)
  269. return state if !ticket
  270. return ticket.state if ticket.state_id == state.id
  271. Ticket::State.find_by(default_follow_up: true)
  272. end
  273. def access_token_for_page(lookup)
  274. access_token = nil
  275. pages.each do |page|
  276. next if !lookup[:page_id] && !lookup[:page]
  277. next if lookup[:page_id] && lookup[:page_id].to_s != page[:id]
  278. next if lookup[:page] && lookup[:page] != page[:name]
  279. access_token = page[:access_token]
  280. break
  281. end
  282. access_token
  283. end
  284. def nested_comments(comments, in_reply_to)
  285. Rails.logger.debug { 'Fetching nested comments...' }
  286. Rails.logger.debug { comments.inspect }
  287. result = []
  288. return result if comments.blank?
  289. comments.each do |comment|
  290. user = to_user(comment)
  291. next if !user
  292. article_data = {
  293. from: "#{user.firstname} #{user.lastname}",
  294. body: comment['message'],
  295. message_id: comment['id'],
  296. type_id: Ticket::Article::Type.find_by(name: 'facebook feed comment').id,
  297. in_reply_to: in_reply_to
  298. }
  299. result.push(article_data)
  300. sub_comments = @client.get_object("#{comment['id']}/comments", fields: 'id,from,to,message,created_time')
  301. sub_articles = nested_comments(sub_comments, comment['id'])
  302. result += sub_articles
  303. end
  304. result
  305. end
  306. end