facebook.rb 8.8 KB

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