diff --git a/plugins/poll/lib/tasks/migrate_old_polls.rake b/plugins/poll/lib/tasks/migrate_old_polls.rake index f916422fa..ea86d9bac 100644 --- a/plugins/poll/lib/tasks/migrate_old_polls.rake +++ b/plugins/poll/lib/tasks/migrate_old_polls.rake @@ -1,3 +1,28 @@ +CLOSED_POLL_PREFIXES = { + "ar" => "هذا التصويت مغلق", + "ca" => "Enquesta tancada", + "de" => "Beendete Umfrage", + "es" => "Encuesta cerrada", + "fi" => "Suljettu kysely", + "fr" => "Sondage fermé", + "he" => "הצבעה סגורה", + "it" => "Sondaggio Chiuso", + "ko" => "투표 닫기", + "pl_PL" => "Zamknięta ankieta", + "pt" => "Votação encerrada", + "pt_BR" => "Votação encerrada", + "ru" => "Завершившийся опрос", + "sq" => "Sondazh i Mbyllur", + "te" => "మూసేసిన ఓటు", + "tr_TR" => "Bitmiş Anket", + "zh_CN" => "已关闭的投票:", +} + +def poll_was_closed?(title) + prefix = CLOSED_POLL_PREFIXES[SiteSetting.default_locale] || "Closed Poll" + title.start_with?(prefix) +end + desc "Migrate old polls to new syntax" task "poll:migrate_old_polls" => :environment do require "timecop" @@ -11,6 +36,8 @@ task "poll:migrate_old_polls" => :environment do # load the post from the db if post = Post.find_by(id: post_id) putc "." + # skip if already migrated + next if post.custom_fields.include?("polls") # go back in time Timecop.freeze(post.created_at + 1.minute) do post.raw = post.raw.gsub(/\n\n([ ]*[-\*\+] )/, "\n\\1") + "\n\n" @@ -42,6 +69,11 @@ task "poll:migrate_old_polls" => :environment do # submit vote DiscoursePoll::Poll.vote(post_id, "poll", [selected_option["id"]], user_id) rescue nil end + # close the poll + if post.topic.archived? || post.topic.closed? || poll_was_closed?(post.topic.title) + post.custom_fields["polls"]["poll"]["status"] = "closed" + post.save_custom_fields(true) + end end end end