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

@ -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