From d1d661f6e1beb331005dcf79c259e642fde1fb7a Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 11 Nov 2013 10:52:44 +1100 Subject: [PATCH] add logic that auto closes any topics that missed the queued job (due to redis flush or whatever) --- app/jobs/regular/close_topic.rb | 7 ++----- app/jobs/scheduled/periodical_updates.rb | 2 ++ app/models/topic.rb | 15 +++++++++++++++ spec/models/topic_spec.rb | 12 +++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/jobs/regular/close_topic.rb b/app/jobs/regular/close_topic.rb index e9e7e8060..f1d50b49c 100644 --- a/app/jobs/regular/close_topic.rb +++ b/app/jobs/regular/close_topic.rb @@ -2,12 +2,9 @@ module Jobs class CloseTopic < Jobs::Base def execute(args) - topic = Topic.where(id: args[:topic_id]).first - if topic and topic.auto_close_at and !topic.closed? and !topic.deleted_at + if topic = Topic.where(id: args[:topic_id]).first closer = User.where(id: args[:user_id]).first - if Guardian.new(closer).can_moderate?(topic) - topic.update_status('autoclosed', true, closer) - end + topic.auto_close(closer) end end diff --git a/app/jobs/scheduled/periodical_updates.rb b/app/jobs/scheduled/periodical_updates.rb index 7836c8834..aaabcf288 100644 --- a/app/jobs/scheduled/periodical_updates.rb +++ b/app/jobs/scheduled/periodical_updates.rb @@ -25,6 +25,8 @@ module Jobs # Refresh Hot Topics HotTopic.refresh! + # Automatically close stuff that we missed + Topic.auto_close end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 07113b6da..2d84debbc 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -599,6 +599,21 @@ class Topic < ActiveRecord::Base set_auto_close(num_days) end + def self.auto_close + Topic.where("NOT closed AND auto_close_at < ? AND auto_close_user_id IS NOT NULL", 5.minutes.from_now).each do |t| + t.auto_close + end + end + + def auto_close(closer = nil) + if auto_close_at && !closed? && !deleted_at && auto_close_at < 5.minutes.from_now + closer ||= auto_close_user + if Guardian.new(closer).can_moderate?(self) + update_status('autoclosed', true, closer) + end + end + end + def set_auto_close(num_days, by_user=nil) num_days = num_days.to_i self.auto_close_at = (num_days > 0 ? num_days.days.from_now : nil) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index eab083ab7..259fc1fa1 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1051,7 +1051,17 @@ describe Topic do topic = Fabricate(:topic, category: Fabricate(:category, auto_close_days: 14), user: mod) Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id)) topic.auto_close_at = 12.hours.from_now - topic.save.should be_true + topic.save + + topic.reload + topic.closed.should == false + + Timecop.freeze(24.hours.from_now) do + Topic.auto_close + topic.reload + topic.closed.should == true + end + end end end