diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index c6a9e4b35b3..0701bbe554c 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -84,8 +84,8 @@ class TopicTrackingState def self.treat_as_new_topic_clause User.where("CASE - WHEN COALESCE(u.new_topic_duration_minutes, :default_duration) = :always THEN u.created_at - WHEN COALESCE(u.new_topic_duration_minutes, :default_duration) = :last_visit THEN COALESCE(u.previous_visit_at,u.created_at) + WHEN COALESCE(u.new_topic_duration_minutes, :default_duration) = :always THEN us.new_since + WHEN COALESCE(u.new_topic_duration_minutes, :default_duration) = :last_visit THEN COALESCE(u.previous_visit_at,us.new_since) ELSE (:now::timestamp - INTERVAL '1 MINUTE' * COALESCE(u.new_topic_duration_minutes, :default_duration)) END", now: DateTime.now, @@ -119,6 +119,7 @@ class TopicTrackingState c.name AS category_name, tu.notification_level FROM users u + INNER JOIN user_stats AS us ON us.user_id = u.id FULL OUTER JOIN topics ON 1=1 LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id LEFT JOIN categories c ON c.id = topics.category_id diff --git a/app/models/user.rb b/app/models/user.rb index d82be171581..a68173e52bf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -466,9 +466,9 @@ class User < ActiveRecord::Base duration = new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes case duration when User::NewTopicDuration::ALWAYS - created_at + user_stat.new_since when User::NewTopicDuration::LAST_VISIT - previous_visit_at || created_at + previous_visit_at || user_stat.new_since else duration.minutes.ago end @@ -563,7 +563,7 @@ class User < ActiveRecord::Base end def create_user_stat - stat = UserStat.new + stat = UserStat.new(new_since: Time.now) stat.user_id = id stat.save! end diff --git a/db/migrate/20140303185354_add_new_since_to_user_stats.rb b/db/migrate/20140303185354_add_new_since_to_user_stats.rb new file mode 100644 index 00000000000..f0e1a071cdc --- /dev/null +++ b/db/migrate/20140303185354_add_new_since_to_user_stats.rb @@ -0,0 +1,10 @@ +class AddNewSinceToUserStats < ActiveRecord::Migration + def change + add_column :user_stats, :new_since, :datetime + execute "UPDATE user_stats AS us + SET new_since = u.created_at + FROM users AS u + WHERE u.id = us.user_id" + change_column :user_stats, :new_since, :datetime, null: false + end +end diff --git a/spec/models/user_stat_spec.rb b/spec/models/user_stat_spec.rb index 4e65a7a1312..a0fdf465d95 100644 --- a/spec/models/user_stat_spec.rb +++ b/spec/models/user_stat_spec.rb @@ -7,6 +7,9 @@ describe UserStat do it "is created automatically when a user is created" do user = Fabricate(:evil_trout) user.user_stat.should be_present + + # It populates the `new_since` field by default + user.user_stat.new_since.should be_present end context '#update_view_counts' do