2013-05-27 18:00:53 -07:00
TopicStatusUpdate = Struct . new ( :topic , :user ) do
2015-07-29 16:34:21 +02:00
def update! ( status , enabled , opts = { } )
2013-05-27 18:00:53 -07:00
status = Status . new ( status , enabled )
Topic . transaction do
2015-07-29 16:34:21 +02:00
change ( status , opts )
2013-07-04 11:47:12 +10:00
highest_post_number = topic . highest_post_number
2015-07-29 16:34:21 +02:00
create_moderator_post_for ( status , opts [ :message ] )
2013-07-04 11:47:12 +10:00
update_read_state_for ( status , highest_post_number )
2013-05-27 18:00:53 -07:00
end
end
private
2015-07-29 16:34:21 +02:00
def change ( status , opts = { } )
2014-04-07 16:38:51 +10:00
if status . pinned? || status . pinned_globally?
2015-07-29 16:34:21 +02:00
topic . update_pinned ( status . enabled? , status . pinned_globally? , opts [ :until ] )
2013-05-27 18:00:53 -07:00
elsif status . autoclosed?
2014-10-29 21:26:32 +01:00
topic . update_column ( 'closed' , status . enabled? )
2013-05-27 18:00:53 -07:00
else
2014-10-29 21:26:32 +01:00
topic . update_column ( status . name , status . enabled? )
2013-05-27 18:00:53 -07:00
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 16:32:24 +11:00
2016-05-24 14:27:48 -04:00
# remove featured topics if we close/archive/make them invisible. Previously we used
# to run the whole featuring logic but that could be very slow and have concurrency
# errors on large sites with many autocloses and topics being created.
if ( ( status . enabled? && ( status . autoclosed? || status . closed? || status . archived? ) ) ||
( status . disabled? && status . visible? ) )
CategoryFeaturedTopic . where ( topic_id : topic . id ) . delete_all
end
2013-05-27 18:00:53 -07:00
end
2014-12-05 19:37:43 +01:00
def create_moderator_post_for ( status , message = nil )
topic . add_moderator_post ( user , message || message_for ( status ) , options_for ( status ) )
2013-07-04 11:47:12 +10: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
2014-10-29 21:26:32 +01:00
# actually read the topic
2013-07-04 11:47:12 +10:00
PostTiming . pretend_read ( topic . id , old_highest_read , topic . highest_post_number )
end
2013-05-27 18:00:53 -07:00
end
def message_for ( status )
2013-06-06 17:04:10 -04:00
if status . autoclosed?
2014-10-29 21:26:32 +01:00
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 )
2015-12-08 18:13:23 +05:30
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
2014-10-29 21:26:32 +01:00
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 )
2013-12-06 16:39:35 -05:00
else
2014-10-29 21:26:32 +01:00
I18n . t ( " #{ locale_key } _minutes " , count : num_minutes )
2013-12-06 16:39:35 -05:00
end
2013-06-06 17:04:10 -04:00
end
2013-05-27 18:00:53 -07:00
end
def options_for ( status )
2015-07-24 16:39:03 -04:00
{ bump : status . reopening_topic? ,
post_type : Post . types [ :small_action ] ,
action_code : status . action_code }
2013-05-27 18:00:53 -07:00
end
Status = Struct . new ( :name , :enabled ) do
2016-05-24 14:27:48 -04:00
%w( pinned_globally pinned autoclosed closed visible archived ) . each do | status |
2013-05-27 18:00:53 -07:00
define_method ( " #{ status } ? " ) { name == status }
end
def enabled?
enabled
end
def disabled?
! enabled?
end
2015-07-24 16:39:03 -04:00
def action_code
" #{ name } . #{ enabled? ? 'enabled' : 'disabled' } "
end
2013-05-27 18:00:53 -07:00
def locale_key
2016-06-10 21:37:33 -05:00
" topic_statuses. #{ action_code . tr ( '.' , '_' ) } "
2013-05-27 18:00:53 -07:00
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 18:00:53 -07:00
end
end