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:
parent
19d04dd9eb
commit
1477a0e910
|
@ -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)
|
||||
|
|
|
@ -238,3 +238,27 @@ task 'posts:defer_all_flags' => :environment do
|
|||
|
||||
puts "", "#{flags_deferred} flags deferred!", ""
|
||||
end
|
||||
|
||||
desc 'Refreshes each post that was received via email'
|
||||
task 'posts:refresh_emails', [:topic_id] => [:environment] do |_, args|
|
||||
posts = Post.where.not(raw_email: nil).where(via_email: true)
|
||||
posts = posts.where(topic_id: args[:topic_id]) if args[:topic_id]
|
||||
|
||||
updated = 0
|
||||
total = posts.count
|
||||
|
||||
posts.find_each do |post|
|
||||
receiver = Email::Receiver.new(post.raw_email)
|
||||
|
||||
body, elided = receiver.select_body
|
||||
body = receiver.add_attachments(body || '', post.user_id)
|
||||
body << Email::Receiver.elided_html(elided) if elided.present?
|
||||
|
||||
post.revise(Discourse.system_user, { raw: body }, skip_revision: true, skip_validations: true)
|
||||
updated += 1
|
||||
|
||||
print_status(updated, total)
|
||||
end
|
||||
|
||||
puts "", "Done. #{updated} posts updated.", ""
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue