Merge branch 'develop' of github.com:martini/zammad into develop

This commit is contained in:
Felix Niklas 2015-07-23 15:41:09 +02:00
commit 9f511273b1
3 changed files with 115 additions and 50 deletions

View file

@ -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

View file

@ -1,3 +1,5 @@
require 'base64'
module Import module Import
end end
module Import::OTRS module Import::OTRS
@ -382,9 +384,8 @@ module Import::OTRS
Thread.current[:thread_no] = thread Thread.current[:thread_no] = thread
sleep thread * 3 sleep thread * 3
log "Started import thread# #{thread} ..." log "Started import thread# #{thread} ..."
run = true
steps = 20 steps = 20
while run loop do
count += steps count += steps
log "loading... thread# #{thread} ..." log "loading... thread# #{thread} ..."
offset = count - steps offset = count - steps
@ -394,8 +395,7 @@ module Import::OTRS
records = load( 'Ticket', steps, count - steps) records = load( 'Ticket', steps, count - steps)
if !records || !records[0] if !records || !records[0]
log "... thread# #{thread}, no more work." log "... thread# #{thread}, no more work."
run = false break
next
end end
_ticket_result(records, locks, thread) _ticket_result(records, locks, thread)
end end
@ -636,18 +636,49 @@ module Import::OTRS
article_new[:type_id] = 9 article_new[:type_id] = 9
end end
article_new.delete( :type ) 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 # set state types
if article_old if article_object
log "update Ticket::Article.find(#{article_new[:id]})" log "update Ticket::Article.find(#{article_new[:id]})"
article_old.update_attributes(article_new) article_object.update_attributes(article_new)
else else
log "add Ticket::Article.find(#{article_new[:id]})" log "add Ticket::Article.find(#{article_new[:id]})"
article = Ticket::Article.new(article_new) article_object = Ticket::Article.new(article_new)
article.id = article_new[:id] article_object.id = article_new[:id]
article.save article_object.save
end 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 end

View file

@ -37,6 +37,7 @@ class OtrsImportTest < ActiveSupport::TestCase
test 'check counts' do test 'check counts' do
assert_equal( 603, Ticket.count, 'tickets' ) assert_equal( 603, Ticket.count, 'tickets' )
assert_equal( 3182, Ticket::Article.count, 'ticket articles' ) 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( 10, Ticket::State.count, 'ticket states' )
assert_equal( 24, Group.count, 'groups' ) assert_equal( 24, Group.count, 'groups' )
end end
@ -208,4 +209,22 @@ class OtrsImportTest < ActiveSupport::TestCase
# - create entry # - create entry
# - state change entry # - state change entry
end 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 end