From f5bc73817008ddfeb39da0dd2eea01cd07d47dd7 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 11:53:23 +0200 Subject: [PATCH 1/7] Added functionality to import nested comments. --- lib/facebook.rb | 95 ++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 40 deletions(-) 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 From 0f95d68cb56fade31708ff46c14756ead23ce717 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 13:14:44 +0200 Subject: [PATCH 2/7] Proper infinite loop. --- lib/import/otrs.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index deeee2604..9102b5dd0 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -382,9 +382,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 +393,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 From 4f3abbdf0e72c14c4dbf87863f561daff70c172f Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 13:15:56 +0200 Subject: [PATCH 3/7] Proper lookup. --- lib/import/otrs.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 9102b5dd0..d9e3df78e 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -634,7 +634,7 @@ module Import::OTRS article_new[:type_id] = 9 end article_new.delete( :type ) - article_old = Ticket::Article.where( id: article_new[:id] ).first + article_old = Ticket::Article.find_by( id: article_new[:id] ) # set state types if article_old From 8de0f7a061e6da5e118baf6d616b9e2907aa910a Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 15:46:01 +0200 Subject: [PATCH 4/7] Added functionality to import article attachments as well. --- lib/import/otrs.rb | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index d9e3df78e..984349637 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -1,3 +1,5 @@ +require 'base64' + module Import end module Import::OTRS @@ -634,18 +636,49 @@ module Import::OTRS article_new[:type_id] = 9 end article_new.delete( :type ) - article_old = Ticket::Article.find_by( id: article_new[:id] ) + 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: { + content_type: attachment['ContentType'], + content_id: attachment['ContentID'], + :'content-alternative' => attachment['ContentAlternative'], + }, + created_by_id: 1, + ) + } } end From 70e5d349bd88f63438e68693beaca4d6a00971fb Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 19:21:21 +0200 Subject: [PATCH 5/7] Added article attachments initegration tests. --- test/integration/otrs_import_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/integration/otrs_import_test.rb b/test/integration/otrs_import_test.rb index fd51fa511..2df97fc2b 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]['content_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]['content_type'] ) + assert_equal( '唐诗三百首.pdf', attachment.filename ) + end + end From 280f1402cdb87c4540b866dd867f8c38b389ef57 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Jul 2015 22:04:57 +0200 Subject: [PATCH 6/7] Changed attachment preference keys according to models/channel/email_parser.rb. --- lib/import/otrs.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 984349637..4da033f90 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -672,9 +672,9 @@ module Import::OTRS filename: Base64.decode64(attachment['Filename']), data: Base64.decode64(attachment['Content']), preferences: { - content_type: attachment['ContentType'], - content_id: attachment['ContentID'], - :'content-alternative' => attachment['ContentAlternative'], + 'Mime-Type' => attachment['ContentType'], + 'Content-ID' => attachment['ContentID'], + 'content-alternative' => attachment['ContentAlternative'], }, created_by_id: 1, ) From f395b997e927da24fb6dfc8fce1bad8d6a32b736 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 22 Jul 2015 10:33:29 +0200 Subject: [PATCH 7/7] Follow up: Changed attachment preference keys according to models/channel/email_parser.rb. --- test/integration/otrs_import_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/otrs_import_test.rb b/test/integration/otrs_import_test.rb index 2df97fc2b..ace09a5b8 100644 --- a/test/integration/otrs_import_test.rb +++ b/test/integration/otrs_import_test.rb @@ -216,14 +216,14 @@ class OtrsImportTest < ActiveSupport::TestCase assert_equal( 5, article.attachments.count ) attachment = article.attachments.first - assert_equal( 'image/jpeg', attachment[:preferences]['content_type'] ) + 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]['content_type'] ) + assert_equal( 'application/pdf; name="=?UTF-8?B?5ZSQ6K+X5LiJ55m+6aaWLnBkZg==?="', attachment[:preferences]['Mime-Type'] ) assert_equal( '唐诗三百首.pdf', attachment.filename ) end