From 1477a0e91031c9a142e96e748ff7e80896dbbda4 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 6 Oct 2017 14:28:26 +0200 Subject: [PATCH] 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. --- lib/email/receiver.rb | 24 +++++++++++++++--------- lib/tasks/posts.rake | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 69569f82b54..0cbefdd1601 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -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) diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake index 8a4bc69b6a3..6c13d53ae4a 100644 --- a/lib/tasks/posts.rake +++ b/lib/tasks/posts.rake @@ -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