Refactor mbox import script
This commit is contained in:
parent
f525d83b53
commit
cafe69caac
|
@ -99,13 +99,12 @@ module ImportScripts::Mbox
|
||||||
|
|
||||||
def map_post(row)
|
def map_post(row)
|
||||||
user_id = user_id_from_imported_user_id(row['from_email']) || Discourse::SYSTEM_USER_ID
|
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'],
|
id: row['msg_id'],
|
||||||
user_id: user_id,
|
user_id: user_id,
|
||||||
created_at: to_time(row['email_date']),
|
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'],
|
raw_email: row['raw_message'],
|
||||||
via_email: true,
|
via_email: true,
|
||||||
post_create_action: proc do |post|
|
post_create_action: proc do |post|
|
||||||
|
@ -114,16 +113,17 @@ module ImportScripts::Mbox
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_raw(email_body, attachment_html, elided)
|
def format_raw(row, user_id)
|
||||||
body = email_body || ''
|
body = row['body'] || ''
|
||||||
body << attachment_html if attachment_html.present?
|
elided = row['elided']
|
||||||
body << Email::Receiver.elided_html(elided) if elided.present?
|
|
||||||
body
|
if row['attachment_count'].positive?
|
||||||
|
receiver = Email::Receiver.new(row['raw_message'])
|
||||||
|
body = receiver.add_attachments(body, user_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def escape_tags(text)
|
body << Email::Receiver.elided_html(elided) if elided.present?
|
||||||
text.gsub!(/^(\[\/?(?:plaintext|attachments|elided)\])$/, ' \1')
|
body
|
||||||
text
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def map_first_post(row)
|
def map_first_post(row)
|
||||||
|
@ -146,28 +146,6 @@ module ImportScripts::Mbox
|
||||||
mapped
|
mapped
|
||||||
end
|
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)
|
def create_incoming_email(post, row)
|
||||||
IncomingEmail.create(
|
IncomingEmail.create(
|
||||||
message_id: row['msg_id'],
|
message_id: row['msg_id'],
|
||||||
|
|
Loading…
Reference in New Issue