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