diff --git a/lib/facebook.rb b/lib/facebook.rb index a07842d3d..545727467 100644 --- a/lib/facebook.rb +++ b/lib/facebook.rb @@ -42,34 +42,34 @@ class Facebook pages end - def user(post) + def user(item) - return if !post['from'] - return if !post['from']['id'] - return if !post['from']['name'] + return if !item['from'] + return if !item['from']['id'] + 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 - def to_user(post) + def to_user(item) - Rails.logger.debug 'Create user from post...' - Rails.logger.debug post.inspect + Rails.logger.debug 'Create user from item...' + Rails.logger.debug item.inspect - # do post_user lookup - post_user = user(post) + # do item_user lookup + 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 user_data = { - login: post_user['id'], # TODO - firstname: post_user['first_name'] || post_user['name'], - lastname: post_user['last_name'] || '', + login: item_user['id'], # TODO + firstname: item_user['first_name'] || item_user['name'], + lastname: item_user['last_name'] || '', email: '', password: '', # TODO: image_source: '', @@ -84,8 +84,8 @@ class Facebook # create or update authorization auth_data = { - uid: post_user['id'], - username: post_user['id'], # TODO + uid: item_user['id'], + username: item_user['id'], # TODO user_id: user.id, provider: 'facebook' } @@ -142,31 +142,13 @@ class Facebook articles = [] articles.push( feed_post ) - if post['comments'] - post['comments']['data'].each { |comment| - - 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'] ) - } + if post['comments'] && post['comments']['data'] + articles += nested_comments( post['comments']['data'], post['id'] ) end - inverted_articles = articles.reverse + articles.each { |article| - inverted_articles.each { |article| - - break if Ticket::Article.find_by( message_id: article[:message_id] ) + next if Ticket::Article.find_by( message_id: article[:message_id] ) article = { to: @account['name'], @@ -242,4 +224,37 @@ class Facebook 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.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 diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index deeee2604..4da033f90 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -1,3 +1,5 @@ +require 'base64' + module Import end module Import::OTRS @@ -382,9 +384,8 @@ module Import::OTRS Thread.current[:thread_no] = thread sleep thread * 3 log "Started import thread# #{thread} ..." - run = true steps = 20 - while run + loop do count += steps log "loading... thread# #{thread} ..." offset = count - steps @@ -394,8 +395,7 @@ module Import::OTRS records = load( 'Ticket', steps, count - steps) if !records || !records[0] log "... thread# #{thread}, no more work." - run = false - next + break end _ticket_result(records, locks, thread) end @@ -636,18 +636,49 @@ module Import::OTRS article_new[:type_id] = 9 end article_new.delete( :type ) - article_old = Ticket::Article.where( id: article_new[:id] ).first + article_object = Ticket::Article.find_by( id: article_new[:id] ) # set state types - if article_old + if article_object log "update Ticket::Article.find(#{article_new[:id]})" - article_old.update_attributes(article_new) + article_object.update_attributes(article_new) else log "add Ticket::Article.find(#{article_new[:id]})" - article = Ticket::Article.new(article_new) - article.id = article_new[:id] - article.save + article_object = Ticket::Article.new(article_new) + article_object.id = article_new[:id] + article_object.save end + + next if !article['Attachments'] + next if article['Attachments'].empty? + + # TODO: refactor + # check if there are attachments present + if !article_object.attachments.empty? + + # skip attachments if count is equal + next if article_object.attachments.count == article['Attachments'].count + + # if the count differs delete all so we + # can have a fresh start + article_object.attachments.each(&:delete) + end + + # import article attachments + article['Attachments'].each { |attachment| + Store.add( + object: 'Ticket::Article', + o_id: article_object.id, + filename: Base64.decode64(attachment['Filename']), + data: Base64.decode64(attachment['Content']), + preferences: { + 'Mime-Type' => attachment['ContentType'], + 'Content-ID' => attachment['ContentID'], + 'content-alternative' => attachment['ContentAlternative'], + }, + created_by_id: 1, + ) + } } end diff --git a/test/integration/otrs_import_test.rb b/test/integration/otrs_import_test.rb index fd51fa511..ace09a5b8 100644 --- a/test/integration/otrs_import_test.rb +++ b/test/integration/otrs_import_test.rb @@ -37,6 +37,7 @@ class OtrsImportTest < ActiveSupport::TestCase test 'check counts' do assert_equal( 603, Ticket.count, 'tickets' ) assert_equal( 3182, Ticket::Article.count, 'ticket articles' ) + assert_equal( 274, Store.count, 'ticket article attachments' ) assert_equal( 10, Ticket::State.count, 'ticket states' ) assert_equal( 24, Group.count, 'groups' ) end @@ -208,4 +209,22 @@ class OtrsImportTest < ActiveSupport::TestCase # - create entry # - state change entry end + + test 'check article attachments' do + + article = Ticket::Article.find(149) + assert_equal( 5, article.attachments.count ) + + attachment = article.attachments.first + assert_equal( 'image/jpeg', attachment[:preferences]['Mime-Type'] ) + assert_equal( 'Cursor_und_Banners_and_Alerts_und_Paket-Verwaltung_-_Admin_-_otrs336_und_otrs336.jpg', attachment.filename ) + + article = Ticket::Article.find(156) + assert_equal( 2, article.attachments.count ) + + attachment = article.attachments.second + assert_equal( 'application/pdf; name="=?UTF-8?B?5ZSQ6K+X5LiJ55m+6aaWLnBkZg==?="', attachment[:preferences]['Mime-Type'] ) + assert_equal( '唐诗三百首.pdf', attachment.filename ) + end + end