FIX: correct counts on user summary

This commit is contained in:
Sam 2016-01-24 16:39:01 +11:00
parent 4b8e12d138
commit d0ee32f3ce
5 changed files with 43 additions and 6 deletions

View File

@ -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 <<SQL
UPDATE user_stats s
SET likes_given = d.likes_given,
likes_received = d.likes_received,
topic_count = d.topic_count,
post_count = d.post_count
FROM directory_items d
WHERE s.user_id = d.user_id AND
d.period_type = 1 AND
( s.likes_given <> 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

View File

@ -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

View File

@ -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

View File

@ -354,8 +354,10 @@ class PostCreator
@user.user_stat.first_post_created_at = @post.created_at
end
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

View File

@ -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)