FIX: stop counting PMs, deleted topics and whispers in directory and user stats

This commit is contained in:
Sam 2017-11-20 16:43:53 +11:00
parent 385372e384
commit 7e841a0495
2 changed files with 40 additions and 15 deletions

View File

@ -67,27 +67,28 @@ class DirectoryItem < ActiveRecord::Base
", period_type: period_types[period_type]
# Calculate new values and update records
#
#
# TODO
# WARNING: post_count is a wrong name, it should be reply_count (excluding topic post)
#
exec_sql "WITH x AS (SELECT
u.id user_id,
SUM(CASE WHEN ua.action_type = :was_liked_type THEN 1 ELSE 0 END) likes_received,
SUM(CASE WHEN ua.action_type = :like_type THEN 1 ELSE 0 END) likes_given,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :was_liked_type THEN 1 ELSE 0 END) likes_received,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :like_type THEN 1 ELSE 0 END) likes_given,
COALESCE((SELECT COUNT(topic_id) FROM topic_views AS v WHERE v.user_id = u.id AND v.viewed_at >= :since), 0) topics_entered,
COALESCE((SELECT COUNT(id) FROM user_visits AS uv WHERE uv.user_id = u.id AND uv.visited_at >= :since), 0) days_visited,
COALESCE((SELECT SUM(posts_read) FROM user_visits AS uv2 WHERE uv2.user_id = u.id AND uv2.visited_at >= :since), 0) posts_read,
SUM(CASE WHEN ua.action_type = :new_topic_type THEN 1 ELSE 0 END) topic_count,
SUM(CASE WHEN ua.action_type = :reply_type THEN 1 ELSE 0 END) post_count
SUM(CASE WHEN t2.id IS NOT NULL AND ua.action_type = :new_topic_type THEN 1 ELSE 0 END) topic_count,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :reply_type THEN 1 ELSE 0 END) post_count
FROM users AS u
LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND COALESCE(ua.created_at, :since) >= :since
LEFT OUTER JOIN topics AS t ON ua.target_topic_id = t.id AND t.archetype = 'regular'
LEFT OUTER JOIN posts AS p ON ua.target_post_id = p.id
LEFT OUTER JOIN posts AS p ON ua.target_post_id = p.id AND p.deleted_at IS NULL AND p.post_type = :regular_post_type AND NOT p.hidden
LEFT OUTER JOIN topics AS t ON p.topic_id = t.id AND t.archetype = 'regular' AND t.deleted_at IS NULL AND t.visible
LEFT OUTER JOIN topics AS t2 ON t2.id = ua.target_topic_id AND t2.archetype = 'regular' AND t2.deleted_at IS NULL AND t2.visible
LEFT OUTER JOIN categories AS c ON t.category_id = c.id
WHERE u.active
AND u.silenced_till IS NULL
AND t.deleted_at IS NULL
AND COALESCE(t.visible, true)
AND p.deleted_at IS NULL
AND (NOT (COALESCE(p.hidden, false)))
AND COALESCE(p.post_type, :regular_post_type) = :regular_post_type
AND u.id > 0
GROUP BY u.id)
UPDATE directory_items di SET

View File

@ -23,12 +23,36 @@ describe DirectoryItem do
UserActionCreator.enable
end
it "creates the record for the user" do
it "creates the record for the user and handles likes" do
post = create_post
_post2 = create_post(topic_id: post.topic_id, user: post.user)
user2 = Fabricate(:user)
PostAction.act(user2, post, PostActionType.types[:like])
DirectoryItem.refresh!
expect(DirectoryItem.where(period_type: DirectoryItem.period_types[:all])
.where(user_id: post.user.id)
.where(topic_count: 1).count).to eq(1)
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
expect(item1.topic_count).to eq(1)
expect(item1.likes_received).to eq(1)
expect(item1.post_count).to eq(1)
expect(item2.likes_given).to eq(1)
post.topic.trash!
DirectoryItem.refresh!
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
expect(item1.likes_given).to eq(0)
expect(item1.likes_received).to eq(0)
expect(item1.post_count).to eq(0)
expect(item1.topic_count).to eq(0)
end
it "handles users with no activity" do