Adds a rake task for refreshing posts received via email

This is useful when the email_reply_trimmer gem was updated and you want to apply those changes to existing posts.
This commit is contained in:
Gerhard Schlager
2017-10-06 14:28:26 +02:00
parent 19d04dd9eb
commit 1477a0e910
2 changed files with 39 additions and 9 deletions

View File

@ -620,6 +620,12 @@ module Email
def create_post_with_attachments(options = {})
# deal with attachments
options[:raw] = add_attachments(options[:raw], options[:user].id, options)
create_post(options)
end
def add_attachments(raw, user_id, options = {})
attachments.each do |attachment|
tmp = Tempfile.new(["discourse-email-attachment", File.extname(attachment.filename)])
begin
@ -627,19 +633,19 @@ module Email
File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded }
# create the upload for the user
opts = { for_group_message: options[:is_group_message] }
upload = UploadCreator.new(tmp, attachment.filename, opts).create_for(options[:user].id)
upload = UploadCreator.new(tmp, attachment.filename, opts).create_for(user_id)
if upload && upload.errors.empty?
# try to inline images
if attachment.content_type.start_with?("image/")
if options[:raw][attachment.url]
options[:raw].sub!(attachment.url, upload.url)
elsif options[:raw][/\[image:.*?\d+[^\]]*\]/i]
options[:raw].sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
if attachment.content_type&.start_with?("image/")
if raw[attachment.url]
raw.sub!(attachment.url, upload.url)
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
else
options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n"
raw << "\n\n#{attachment_markdown(upload)}\n\n"
end
else
options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n"
raw << "\n\n#{attachment_markdown(upload)}\n\n"
end
end
ensure
@ -647,7 +653,7 @@ module Email
end
end
create_post(options)
raw
end
def attachment_markdown(upload)