From d0ee32f3ceab92c146abc28c1e9f04b9a4ec4ebf Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 24 Jan 2016 16:39:01 +1100 Subject: [PATCH] FIX: correct counts on user summary --- app/models/directory_item.rb | 27 ++++++++++++++++++++++++++- app/models/user_action.rb | 5 ++++- app/models/user_stat.rb | 7 +++++-- lib/post_creator.rb | 6 ++++-- spec/components/post_creator_spec.rb | 4 ++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/models/directory_item.rb b/app/models/directory_item.rb index 61fc7e400c9..680b35c082d 100644 --- a/app/models/directory_item.rb +++ b/app/models/directory_item.rb @@ -13,7 +13,12 @@ class DirectoryItem < ActiveRecord::Base end def self.period_types - @types ||= Enum.new(:all, :yearly, :monthly, :weekly, :daily, :quarterly) + @types ||= Enum.new(all: 1, + yearly: 2, + monthly: 3, + weekly: 4, + daily: 5, + quarterly: 6) end def self.refresh! @@ -70,6 +75,26 @@ class DirectoryItem < ActiveRecord::Base new_topic_type: UserAction::NEW_TOPIC, reply_type: UserAction::REPLY, regular_post_type: Post.types[:regular] + + if period_type == :all + exec_sql < d.likes_given OR + s.likes_received <> d.likes_received OR + s.topic_count <> d.topic_count OR + s.post_count <> d.post_count + ) + +SQL + end end end end diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 320b94d078c..2fa8490eb4d 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -242,10 +242,13 @@ SQL action.save! user_id = hash[:user_id] - update_like_count(user_id, hash[:action_type], 1) topic = Topic.includes(:category).find_by(id: hash[:target_topic_id]) + if topic && !topic.private_message? + update_like_count(user_id, hash[:action_type], 1) + end + # move into Topic perhaps group_ids = nil if topic && topic.category && topic.category.read_restricted diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 3eb8ab98196..21590b9c80d 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -30,8 +30,11 @@ class UserStat < ActiveRecord::Base (SELECT pt.user_id, COUNT(*) AS c FROM users AS u - INNER JOIN post_timings AS pt ON pt.user_id = u.id - WHERE u.last_seen_at > :seen_at + JOIN post_timings AS pt ON pt.user_id = u.id + JOIN topics t ON t.id = pt.topic_id + WHERE u.last_seen_at > :seen_at AND + t.archetype = 'regular' AND + t.deleted_at IS NULL GROUP BY pt.user_id) AS X WHERE X.user_id = user_stats.user_id AND X.c <> posts_read_count diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 30a379c3be7..f7151442b25 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -354,8 +354,10 @@ class PostCreator @user.user_stat.first_post_created_at = @post.created_at end - @user.user_stat.post_count += 1 - @user.user_stat.topic_count += 1 if @post.is_first_post? + unless @post.topic.private_message? + @user.user_stat.post_count += 1 + @user.user_stat.topic_count += 1 if @post.is_first_post? + end # We don't count replies to your own topics if !@opts[:import_mode] && @user.id != @topic.user_id diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index b3195b3c59b..01006058e1b 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -478,6 +478,10 @@ describe PostCreator do expect(unrelated.notifications.count).to eq(0) expect(post.topic.subtype).to eq(TopicSubtype.user_to_user) + # PMs do not increase post count or topic count + expect(post.user.user_stat.post_count).to eq(0) + expect(post.user.user_stat.topic_count).to eq(0) + # archive this message and ensure archive is cleared for all users on reply UserArchivedMessage.create(user_id: target_user2.id, topic_id: post.topic_id)