discourse/app/models/topic_status_update.rb

113 lines
3.1 KiB
Ruby
Raw Normal View History

2013-05-28 09:00:53 +08:00
TopicStatusUpdate = Struct.new(:topic, :user) do
2015-07-29 22:34:21 +08:00
def update!(status, enabled, opts={})
2013-05-28 09:00:53 +08:00
status = Status.new(status, enabled)
Topic.transaction do
2015-07-29 22:34:21 +08:00
change(status, opts)
highest_post_number = topic.highest_post_number
2015-07-29 22:34:21 +08:00
create_moderator_post_for(status, opts[:message])
update_read_state_for(status, highest_post_number)
2013-05-28 09:00:53 +08:00
end
end
private
2015-07-29 22:34:21 +08:00
def change(status, opts={})
if status.pinned? || status.pinned_globally?
2015-07-29 22:34:21 +08:00
topic.update_pinned(status.enabled?, status.pinned_globally?, opts[:until])
2013-05-28 09:00:53 +08:00
elsif status.autoclosed?
topic.update_column('closed', status.enabled?)
2013-05-28 09:00:53 +08:00
else
topic.update_column(status.name, status.enabled?)
2013-05-28 09:00:53 +08:00
end
if topic.auto_close_at && (status.reopening_topic? || status.manually_closing_topic?)
topic.reload.set_auto_close(nil).save
end
# pick up the changes right away as opposed to waiting for
# the schedule
CategoryFeaturedTopic.feature_topics_for(topic.category)
2013-05-28 09:00:53 +08:00
end
def create_moderator_post_for(status, message=nil)
topic.add_moderator_post(user, message || message_for(status), options_for(status))
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-28 09:00:53 +08:00
end
def message_for(status)
if status.autoclosed?
locale_key = status.locale_key
locale_key << "_lastpost" if topic.auto_close_based_on_last_post
message_for_autoclosed(locale_key)
end
end
def message_for_autoclosed(locale_key)
num_minutes = ((
if topic.auto_close_based_on_last_post
topic.auto_close_hours.hours
elsif topic.auto_close_started_at
Time.zone.now - topic.auto_close_started_at
else
Time.zone.now - topic.created_at
end
) / 1.minute).round
if num_minutes.minutes >= 2.days
I18n.t("#{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("#{locale_key}_hours", count: num_hours)
else
I18n.t("#{locale_key}_minutes", count: num_minutes)
end
end
2013-05-28 09:00:53 +08:00
end
def options_for(status)
{ bump: status.reopening_topic?,
post_type: Post.types[:small_action],
action_code: status.action_code }
2013-05-28 09:00:53 +08:00
end
Status = Struct.new(:name, :enabled) do
%w(pinned_globally pinned autoclosed closed).each do |status|
2013-05-28 09:00:53 +08:00
define_method("#{status}?") { name == status }
end
def enabled?
enabled
end
def disabled?
!enabled?
end
def action_code
"#{name}.#{enabled? ? 'enabled' : 'disabled'}"
end
2013-05-28 09:00:53 +08:00
def locale_key
"topic_statuses.#{action_code.gsub('.', '_')}"
2013-05-28 09:00:53 +08:00
end
def reopening_topic?
(closed? || autoclosed?) && disabled?
end
def manually_closing_topic?
closed? && enabled?
end
2013-05-28 09:00:53 +08:00
end
end