2013-05-27 21:00:53 -04:00
|
|
|
TopicStatusUpdate = Struct.new(:topic, :user) do
|
|
|
|
def update!(status, enabled)
|
|
|
|
status = Status.new(status, enabled)
|
|
|
|
|
|
|
|
Topic.transaction do
|
2013-07-03 21:47:12 -04:00
|
|
|
change(status)
|
|
|
|
highest_post_number = topic.highest_post_number
|
|
|
|
|
|
|
|
create_moderator_post_for(status)
|
|
|
|
update_read_state_for(status, highest_post_number)
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def change(status)
|
2014-04-07 02:38:51 -04:00
|
|
|
if status.pinned? || status.pinned_globally?
|
|
|
|
topic.update_pinned status.enabled?, status.pinned_globally?
|
2013-05-27 21:00:53 -04:00
|
|
|
elsif status.autoclosed?
|
|
|
|
topic.update_column 'closed', status.enabled?
|
|
|
|
else
|
|
|
|
topic.update_column status.name, status.enabled?
|
|
|
|
end
|
2013-07-03 16:44:45 -04:00
|
|
|
|
2013-12-19 11:24:21 -05:00
|
|
|
if topic.auto_close_at && (status.reopening_topic? || status.manually_closing_topic?)
|
2013-07-03 16:44:45 -04:00
|
|
|
topic.reload.set_auto_close(nil).save
|
|
|
|
end
|
2013-12-02 00:32:24 -05:00
|
|
|
|
|
|
|
# pick up the changes right away as opposed to waiting for
|
|
|
|
# the schedule
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(topic.category)
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_moderator_post_for(status)
|
|
|
|
topic.add_moderator_post(user, message_for(status), options_for(status))
|
2013-07-03 21:47:12 -04:00
|
|
|
topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_read_state_for(status, old_highest_read)
|
|
|
|
if status.autoclosed?
|
|
|
|
# let's pretend all the people that read up to the autoclose message
|
|
|
|
# actually read the topic
|
|
|
|
PostTiming.pretend_read(topic.id, old_highest_read, topic.highest_post_number)
|
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_for(status)
|
2013-06-06 17:04:10 -04:00
|
|
|
if status.autoclosed?
|
2013-12-06 16:39:35 -05:00
|
|
|
num_minutes = topic.auto_close_started_at ? ((Time.zone.now - topic.auto_close_started_at) / 1.minute).round : topic.age_in_minutes
|
|
|
|
if num_minutes.minutes >= 2.days
|
|
|
|
I18n.t "#{status.locale_key}_days", count: (num_minutes.minutes / 1.day).round
|
|
|
|
else
|
|
|
|
num_hours = (num_minutes.minutes / 1.hour).round
|
|
|
|
if num_hours >= 2
|
|
|
|
I18n.t "#{status.locale_key}_hours", count: num_hours
|
|
|
|
else
|
|
|
|
I18n.t "#{status.locale_key}_minutes", count: num_minutes
|
|
|
|
end
|
|
|
|
end
|
2013-06-06 17:04:10 -04:00
|
|
|
else
|
|
|
|
I18n.t status.locale_key
|
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def options_for(status)
|
|
|
|
{ bump: status.reopening_topic? }
|
|
|
|
end
|
|
|
|
|
|
|
|
Status = Struct.new(:name, :enabled) do
|
2014-04-07 02:38:51 -04:00
|
|
|
%w(pinned_globally pinned autoclosed closed).each do |status|
|
2013-05-27 21:00:53 -04:00
|
|
|
define_method("#{status}?") { name == status }
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def disabled?
|
|
|
|
!enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def locale_key
|
|
|
|
"topic_statuses.#{name}_#{enabled? ? 'enabled' : 'disabled'}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def reopening_topic?
|
|
|
|
(closed? || autoclosed?) && disabled?
|
|
|
|
end
|
2013-07-03 16:44:45 -04:00
|
|
|
|
|
|
|
def manually_closing_topic?
|
|
|
|
closed? && enabled?
|
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
end
|