@@ -83,11 +83,9 @@ def initialize(user)
8383
8484 after_create do
8585 changed_to_category ( category )
86- TopicUser . change (
87- user_id , id ,
88- notification_level : TopicUser ::NotificationLevel ::WATCHING ,
89- notifications_reason_id : TopicUser ::NotificationReasons ::CREATED_TOPIC
90- )
86+ TopicUser . change ( user_id , id ,
87+ notification_level : TopicUser . notification_levels [ :watching ] ,
88+ notifications_reason_id : TopicUser . notification_reasons [ :created_topic ] )
9189 if archetype == Archetype . private_message
9290 DraftSequence . next! ( user , Draft ::NEW_PRIVATE_MESSAGE )
9391 else
@@ -206,8 +204,17 @@ def links_grouped
206204 end
207205
208206 def update_status ( property , status , user )
207+
209208 Topic . transaction do
210- update_column ( property , status )
209+
210+ # Special case: if it's pinned, update that
211+ if property . to_sym == :pinned
212+ update_pinned ( status )
213+ else
214+ # otherwise update the column
215+ update_column ( property , status )
216+ end
217+
211218 key = "topic_statuses.#{ property } _"
212219 key << ( status ? 'enabled' : 'disabled' )
213220
@@ -506,7 +513,7 @@ def toggle_star(user, starred)
506513
507514 # Enable/disable the mute on the topic
508515 def toggle_mute ( user , muted )
509- TopicUser . change ( user , self . id , notification_level : muted? ( user ) ? TopicUser :: NotificationLevel :: REGULAR : TopicUser :: NotificationLevel :: MUTED )
516+ TopicUser . change ( user , self . id , notification_level : muted? ( user ) ? TopicUser . notification_levels [ :regular ] : TopicUser . notification_levels [ :muted ] )
510517 end
511518
512519 def slug
@@ -526,7 +533,17 @@ def relative_url(post_number=nil)
526533 def muted? ( user )
527534 return false unless user && user . id
528535 tu = topic_users . where ( user_id : user . id ) . first
529- tu && tu . notification_level == TopicUser ::NotificationLevel ::MUTED
536+ tu && tu . notification_level == TopicUser . notification_levels [ :muted ]
537+ end
538+
539+ def clear_pin_for ( user )
540+ return unless user . present?
541+
542+ TopicUser . change ( user . id , id , cleared_pinned_at : Time . now )
543+ end
544+
545+ def update_pinned ( status )
546+ update_column ( :pinned_at , status ? Time . now : nil )
530547 end
531548
532549 def draft_key
@@ -535,18 +552,18 @@ def draft_key
535552
536553 # notification stuff
537554 def notify_watch! ( user )
538- TopicUser . change ( user , id , notification_level : TopicUser :: NotificationLevel :: WATCHING )
555+ TopicUser . change ( user , id , notification_level : TopicUser . notification_levels [ :watching ] )
539556 end
540557
541558 def notify_tracking! ( user )
542- TopicUser . change ( user , id , notification_level : TopicUser :: NotificationLevel :: TRACKING )
559+ TopicUser . change ( user , id , notification_level : TopicUser . notification_levels [ :tracking ] )
543560 end
544561
545562 def notify_regular! ( user )
546- TopicUser . change ( user , id , notification_level : TopicUser :: NotificationLevel :: REGULAR )
563+ TopicUser . change ( user , id , notification_level : TopicUser . notification_levels [ :regular ] )
547564 end
548565
549566 def notify_muted! ( user )
550- TopicUser . change ( user , id , notification_level : TopicUser :: NotificationLevel :: MUTED )
567+ TopicUser . change ( user , id , notification_level : TopicUser . notification_levels [ :muted ] )
551568 end
552569end
0 commit comments