forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_base.rb
463 lines (392 loc) · 12.5 KB
/
tweet_base.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
require 'http/uri'
class TweetBase
attr_accessor :client
def user(tweet)
if tweet.class == Twitter::DirectMessage
Rails.logger.debug { "Twitter sender for dm (#{tweet.id}): found" }
Rails.logger.debug { tweet.sender.inspect }
tweet.sender
elsif tweet.class == Twitter::Tweet
Rails.logger.debug { "Twitter sender for tweet (#{tweet.id}): found" }
Rails.logger.debug { tweet.user.inspect }
tweet.user
else
raise "Unknown tweet type '#{tweet.class}'"
end
end
def to_user(tweet)
Rails.logger.debug { 'Create user from tweet...' }
Rails.logger.debug { tweet.inspect }
# do tweet_user lookup
tweet_user = user(tweet)
auth = Authorization.find_by(uid: tweet_user.id, provider: 'twitter')
# create or update user
user_data = {
image_source: tweet_user.profile_image_url.to_s,
}
if auth
user = User.find(auth.user_id)
map = {
note: 'description',
web: 'website',
address: 'location',
}
# ignore if value is already set
map.each do |target, source|
next if user[target].present?
new_value = tweet_user.send(source).to_s
next if new_value.blank?
user_data[target] = new_value
end
user.update!(user_data)
else
user_data[:login] = tweet_user.screen_name
user_data[:firstname] = tweet_user.name
user_data[:web] = tweet_user.website.to_s
user_data[:note] = tweet_user.description
user_data[:address] = tweet_user.location
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: 'twitter',
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 or update authorization
auth_data = {
uid: tweet_user.id,
username: tweet_user.screen_name,
user_id: user.id,
provider: 'twitter'
}
if auth
auth.update!(auth_data)
else
Authorization.create!(auth_data)
end
user
end
def to_ticket(tweet, user, group_id, channel)
UserInfo.current_user_id = user.id
Rails.logger.debug { 'Create ticket from tweet...' }
Rails.logger.debug { tweet.inspect }
Rails.logger.debug { user.inspect }
Rails.logger.debug { group_id.inspect }
if tweet.class == Twitter::DirectMessage
ticket = Ticket.find_by(
create_article_type: Ticket::Article::Type.lookup(name: 'twitter direct-message'),
customer_id: user.id,
state: Ticket::State.where.not(
state_type_id: Ticket::StateType.where(
name: %w[closed merged removed],
)
)
)
return ticket if ticket
end
# prepare title
title = tweet.text
if title.length > 80
title = "#{title[0, 80]}..."
end
state = get_state(channel, tweet)
Ticket.create!(
customer_id: user.id,
title: title,
group_id: group_id || Group.first.id,
state: state,
priority: Ticket::Priority.find_by(name: '2 normal'),
preferences: {
channel_id: channel.id,
channel_screen_name: channel.options['user']['screen_name'],
},
)
end
def to_article(tweet, user, ticket, channel)
Rails.logger.debug { 'Create article from tweet...' }
Rails.logger.debug { tweet.inspect }
Rails.logger.debug { user.inspect }
Rails.logger.debug { ticket.inspect }
# import tweet
to = nil
from = nil
article_type = nil
in_reply_to = nil
twitter_preferences = {}
if tweet.class == Twitter::DirectMessage
article_type = 'twitter direct-message'
to = "@#{tweet.recipient.screen_name}"
from = "@#{tweet.sender.screen_name}"
twitter_preferences = {
created_at: tweet.created_at,
recipient_id: tweet.recipient.id,
recipient_screen_name: tweet.recipient.screen_name,
sender_id: tweet.sender.id,
sender_screen_name: tweet.sender.screen_name,
}
elsif tweet.class == Twitter::Tweet
article_type = 'twitter status'
from = "@#{tweet.user.screen_name}"
mention_ids = []
tweet.user_mentions&.each do |local_user|
if !to
to = ''
else
to += ', '
end
to += "@#{local_user.screen_name}"
mention_ids.push local_user.id
end
in_reply_to = tweet.in_reply_to_status_id
twitter_preferences = {
mention_ids: mention_ids,
geo: tweet.geo,
retweeted: tweet.retweeted?,
possibly_sensitive: tweet.possibly_sensitive?,
in_reply_to_user_id: tweet.in_reply_to_user_id,
place: tweet.place,
retweet_count: tweet.retweet_count,
source: tweet.source,
favorited: tweet.favorited?,
truncated: tweet.truncated?,
}
else
raise "Unknown tweet type '#{tweet.class}'"
end
UserInfo.current_user_id = user.id
# set ticket state to open if not new
ticket_state = get_state(channel, tweet, ticket)
if ticket_state.name != ticket.state.name
ticket.state = ticket_state
ticket.save!
end
article_preferences = {
twitter: self.class.preferences_cleanup(twitter_preferences),
links: [
{
url: "https://twitter.com/statuses/#{tweet.id}",
target: '_blank',
name: 'on Twitter',
},
],
}
Ticket::Article.create!(
from: from,
to: to,
body: tweet.text,
message_id: tweet.id,
ticket_id: ticket.id,
in_reply_to: in_reply_to,
type_id: Ticket::Article::Type.find_by(name: article_type).id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
internal: false,
preferences: article_preferences,
)
end
def to_group(tweet, group_id, channel)
Rails.logger.debug { 'import tweet' }
# use transaction
if @connection_type == 'stream'
ActiveRecord::Base.connection.reconnect!
end
ticket = nil
Transaction.execute(reset_user_id: true) do
# check if parent exists
user = to_user(tweet)
if tweet.class == Twitter::DirectMessage
ticket = to_ticket(tweet, user, group_id, channel)
to_article(tweet, user, ticket, channel)
elsif tweet.class == Twitter::Tweet
if tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
if existing_article
ticket = existing_article.ticket
else
begin
# in case of streaming mode, get parent tweet via REST client
if @connection_type == 'stream'
client = TweetRest.new(@auth)
parent_tweet = client.status(tweet.in_reply_to_status_id)
else
parent_tweet = @client.status(tweet.in_reply_to_status_id)
end
ticket = to_group(parent_tweet, group_id, channel)
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
# just ignore if tweet has already gone
Rails.logger.info "Can't import tweet (#{tweet.in_reply_to_status_id}), #{e.message}"
end
end
end
if !ticket
ticket = to_ticket(tweet, user, group_id, channel)
end
to_article(tweet, user, ticket, channel)
else
raise "Unknown tweet type '#{tweet.class}'"
end
end
if @connection_type == 'stream'
ActiveRecord::Base.connection.close
end
ticket
end
def from_article(article)
tweet = nil
if article[:type] == 'twitter direct-message'
Rails.logger.debug { "Create twitter direct message from article to '#{article[:to]}'..." }
tweet = @client.create_direct_message(
article[:to],
article[:body],
{}
)
elsif article[:type] == 'twitter status'
Rails.logger.debug { 'Create tweet from article...' }
tweet = @client.update(
article[:body],
{
in_reply_to_status_id: article[:in_reply_to]
}
)
else
raise "Can't handle unknown twitter article type '#{article[:type]}'."
end
Rails.logger.debug { tweet.inspect }
tweet
end
def get_state(channel, tweet, ticket = nil)
tweet_user = user(tweet)
# no changes in post is from page user it self
if channel.options[:user][:id].to_s == tweet_user.id.to_s
if !ticket
return Ticket::State.find_by(name: 'closed') if !ticket
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 tweet_limit_reached(tweet, factor = 1)
max_count = 120
if @connection_type == 'stream'
max_count = 30
end
max_count = max_count * factor
type_id = Ticket::Article::Type.lookup(name: 'twitter status').id
created_at = Time.zone.now - 15.minutes
created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
if created_count > max_count
Rails.logger.info "Tweet limit of #{created_count}/#{max_count} reached, ignored tweed id (#{tweet.id})"
return true
end
false
end
def direct_message_limit_reached(tweet, factor = 1)
max_count = 100
if @connection_type == 'stream'
max_count = 40
end
max_count = max_count * factor
type_id = Ticket::Article::Type.lookup(name: 'twitter direct-message').id
created_at = Time.zone.now - 15.minutes
created_count = Ticket::Article.where('created_at > ? AND type_id = ?', created_at, type_id).count
if created_count > max_count
Rails.logger.info "Tweet direct message limit reached #{created_count}/#{max_count}, ignored tweed id (#{tweet.id})"
return true
end
false
end
=begin
replace Twitter::Place and Twitter::Geo as hash and replace Twitter::NullObject with nil
preferences = TweetBase.preferences_cleanup(
twitter: twitter_preferences,
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
)
or
preferences = {
twitter: TweetBase.preferences_cleanup(twitter_preferences),
links: [
{
url: 'https://twitter.com/statuses/123',
target: '_blank',
name: 'on Twitter',
},
],
}
=end
def self.preferences_cleanup(preferences)
# replace Twitter::NullObject with nill to prevent elasticsearch index issue
preferences.each do |key, value|
if value.class == Twitter::Place || value.class == Twitter::Geo
preferences[key] = value.to_h
next
end
if value.class == Twitter::NullObject
preferences[key] = nil
next
end
next if !value.is_a?(Hash)
value.each do |sub_key, sub_level|
if sub_level.class == NilClass
value[sub_key] = nil
next
end
if sub_level.class == Twitter::Place || sub_level.class == Twitter::Geo
value[sub_key] = sub_level.to_h
next
end
next if sub_level.class != Twitter::NullObject
value[sub_key] = nil
end
end
if preferences[:twitter]
if preferences[:twitter][:geo].blank?
preferences[:twitter][:geo] = {}
end
if preferences[:twitter][:place].blank?
preferences[:twitter][:place] = {}
end
else
if preferences[:geo].blank?
preferences[:geo] = {}
end
if preferences[:place].blank?
preferences[:place] = {}
end
end
preferences
end
def locale_sender?(tweet)
tweet_user = user(tweet)
Channel.where(area: 'Twitter::Account').each do |local_channel|
next if !local_channel.options
next if !local_channel.options[:user]
next if !local_channel.options[:user][:id]
next if local_channel.options[:user][:id].to_s != tweet_user.id.to_s
Rails.logger.debug { "Tweet is sent by local account with user id #{tweet_user.id} and tweet.id #{tweet.id}" }
return true
end
false
end
end