Refactor mbox import script

This commit is contained in:
Gerhard Schlager 2017-12-13 21:58:13 +01:00
parent f525d83b53
commit cafe69caac
1 changed files with 10 additions and 32 deletions

View File

@ -99,13 +99,12 @@ module ImportScripts::Mbox
def map_post(row)
user_id = user_id_from_imported_user_id(row['from_email']) || Discourse::SYSTEM_USER_ID
attachment_html = map_attachments(row['raw_message'], user_id) if row['attachment_count'].positive?
{
id: row['msg_id'],
user_id: user_id,
created_at: to_time(row['email_date']),
raw: format_raw(row['body'], attachment_html, row['elided']),
raw: format_raw(row, user_id),
raw_email: row['raw_message'],
via_email: true,
post_create_action: proc do |post|
@ -114,18 +113,19 @@ module ImportScripts::Mbox
}
end
def format_raw(email_body, attachment_html, elided)
body = email_body || ''
body << attachment_html if attachment_html.present?
def format_raw(row, user_id)
body = row['body'] || ''
elided = row['elided']
if row['attachment_count'].positive?
receiver = Email::Receiver.new(row['raw_message'])
body = receiver.add_attachments(body, user_id)
end
body << Email::Receiver.elided_html(elided) if elided.present?
body
end
def escape_tags(text)
text.gsub!(/^(\[\/?(?:plaintext|attachments|elided)\])$/, ' \1')
text
end
def map_first_post(row)
mapped = map_post(row)
mapped[:category] = category_id_from_imported_category_id(row['category'])
@ -146,28 +146,6 @@ module ImportScripts::Mbox
mapped
end
def map_attachments(raw_message, user_id)
receiver = Email::Receiver.new(raw_message)
attachment_markdown = ''
receiver.attachments.each do |attachment|
tmp = Tempfile.new(['discourse-email-attachment', File.extname(attachment.filename)])
begin
File.open(tmp.path, 'w+b') { |f| f.write attachment.body.decoded }
upload = UploadCreator.new(tmp, attachment.filename).create_for(user_id)
if upload && upload.errors.empty?
attachment_markdown << "\n\n#{receiver.attachment_markdown(upload)}\n\n"
end
ensure
tmp.try(:close!)
end
end
attachment_markdown
end
def create_incoming_email(post, row)
IncomingEmail.create(
message_id: row['msg_id'],