Added functionality to import nested comments.
This commit is contained in:
parent
db863f7b1b
commit
f5bc738170
1 changed files with 55 additions and 40 deletions
|
@ -42,34 +42,34 @@ class Facebook
|
||||||
pages
|
pages
|
||||||
end
|
end
|
||||||
|
|
||||||
def user(post)
|
def user(item)
|
||||||
|
|
||||||
return if !post['from']
|
return if !item['from']
|
||||||
return if !post['from']['id']
|
return if !item['from']['id']
|
||||||
return if !post['from']['name']
|
return if !item['from']['name']
|
||||||
|
|
||||||
return if !post['from']['id'] == @account['id']
|
return if item['from']['id'] == @account['id']
|
||||||
|
|
||||||
@client.get_object( post['from']['id'] )
|
@client.get_object( item['from']['id'] )
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_user(post)
|
def to_user(item)
|
||||||
|
|
||||||
Rails.logger.debug 'Create user from post...'
|
Rails.logger.debug 'Create user from item...'
|
||||||
Rails.logger.debug post.inspect
|
Rails.logger.debug item.inspect
|
||||||
|
|
||||||
# do post_user lookup
|
# do item_user lookup
|
||||||
post_user = user(post)
|
item_user = user(item)
|
||||||
|
|
||||||
return if !post_user
|
return if !item_user
|
||||||
|
|
||||||
auth = Authorization.find_by( uid: post_user['id'], provider: 'facebook' )
|
auth = Authorization.find_by( uid: item_user['id'], provider: 'facebook' )
|
||||||
|
|
||||||
# create or update user
|
# create or update user
|
||||||
user_data = {
|
user_data = {
|
||||||
login: post_user['id'], # TODO
|
login: item_user['id'], # TODO
|
||||||
firstname: post_user['first_name'] || post_user['name'],
|
firstname: item_user['first_name'] || item_user['name'],
|
||||||
lastname: post_user['last_name'] || '',
|
lastname: item_user['last_name'] || '',
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
# TODO: image_source: '',
|
# TODO: image_source: '',
|
||||||
|
@ -84,8 +84,8 @@ class Facebook
|
||||||
|
|
||||||
# create or update authorization
|
# create or update authorization
|
||||||
auth_data = {
|
auth_data = {
|
||||||
uid: post_user['id'],
|
uid: item_user['id'],
|
||||||
username: post_user['id'], # TODO
|
username: item_user['id'], # TODO
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
provider: 'facebook'
|
provider: 'facebook'
|
||||||
}
|
}
|
||||||
|
@ -142,31 +142,13 @@ class Facebook
|
||||||
articles = []
|
articles = []
|
||||||
articles.push( feed_post )
|
articles.push( feed_post )
|
||||||
|
|
||||||
if post['comments']
|
if post['comments'] && post['comments']['data']
|
||||||
post['comments']['data'].each { |comment|
|
articles += nested_comments( post['comments']['data'], post['id'] )
|
||||||
|
|
||||||
user = to_user(comment)
|
|
||||||
|
|
||||||
next if !user
|
|
||||||
|
|
||||||
post_comment = {
|
|
||||||
from: "#{user.firstname} #{user.lastname}",
|
|
||||||
body: comment['message'],
|
|
||||||
message_id: comment['id'],
|
|
||||||
type_id: Ticket::Article::Type.find_by( name: 'facebook feed comment' ).id,
|
|
||||||
}
|
|
||||||
articles.push( post_comment )
|
|
||||||
|
|
||||||
# TODO: sub-comments
|
|
||||||
# comment_data = @client.get_object( comment['id'] )
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
inverted_articles = articles.reverse
|
articles.each { |article|
|
||||||
|
|
||||||
inverted_articles.each { |article|
|
next if Ticket::Article.find_by( message_id: article[:message_id] )
|
||||||
|
|
||||||
break if Ticket::Article.find_by( message_id: article[:message_id] )
|
|
||||||
|
|
||||||
article = {
|
article = {
|
||||||
to: @account['name'],
|
to: @account['name'],
|
||||||
|
@ -242,4 +224,37 @@ class Facebook
|
||||||
|
|
||||||
access_token
|
access_token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def nested_comments(comments, in_reply_to)
|
||||||
|
|
||||||
|
Rails.logger.debug 'Fetching nested comments...'
|
||||||
|
Rails.logger.debug comments.inspect
|
||||||
|
|
||||||
|
result = []
|
||||||
|
return result if comments.empty?
|
||||||
|
|
||||||
|
comments.each { |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" )
|
||||||
|
|
||||||
|
sub_articles = nested_comments(sub_comments, comment['id'])
|
||||||
|
|
||||||
|
result += sub_articles
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue