2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserStat do
|
2013-09-11 14:50:26 -04:00
|
|
|
it "is created automatically when a user is created" do
|
|
|
|
user = Fabricate(:evil_trout)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat).to be_present
|
2014-03-03 14:31:29 -05:00
|
|
|
|
|
|
|
# It populates the `new_since` field by default
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat.new_since).to be_present
|
2013-09-11 14:50:26 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#update_view_counts" do
|
2013-10-03 23:28:49 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with topics_entered" do
|
2013-10-03 23:28:49 -04:00
|
|
|
context "without any views" do
|
|
|
|
it "doesn't increase the user's topics_entered" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
|
|
|
UserStat.update_view_counts
|
|
|
|
stat.reload
|
|
|
|
}.not_to change(stat, :topics_entered)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a view" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2014-08-04 05:07:55 -04:00
|
|
|
let!(:view) { TopicViewItem.add(topic.id, "127.0.0.1", user.id) }
|
2013-10-03 23:28:49 -04:00
|
|
|
|
2023-04-10 22:28:08 -04:00
|
|
|
before do
|
|
|
|
user.update_column :last_seen_at, 1.second.ago
|
|
|
|
stat.update_column :topics_entered, 0
|
|
|
|
end
|
2013-10-03 23:28:49 -04:00
|
|
|
|
|
|
|
it "adds one to the topics entered" do
|
2023-04-10 22:28:08 -04:00
|
|
|
expect { UserStat.update_view_counts }.to change { stat.reload.topics_entered }.to 1
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't record a second view as a different topic" do
|
2014-08-04 05:07:55 -04:00
|
|
|
TopicViewItem.add(topic.id, "127.0.0.1", user.id)
|
2023-04-10 22:28:08 -04:00
|
|
|
expect { UserStat.update_view_counts }.to change { stat.reload.topics_entered }.to 1
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with posts_read_count" do
|
2013-10-03 23:28:49 -04:00
|
|
|
context "without any post timings" do
|
|
|
|
it "doesn't increase the user's posts_read_count" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
|
|
|
UserStat.update_view_counts
|
|
|
|
stat.reload
|
|
|
|
}.not_to change(stat, :posts_read_count)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a post timing" do
|
|
|
|
let!(:post) { Fabricate(:post) }
|
|
|
|
let!(:post_timings) do
|
|
|
|
PostTiming.record_timing(
|
|
|
|
msecs: 1234,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
user_id: user.id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-04-10 22:28:08 -04:00
|
|
|
before do
|
|
|
|
user.update_column :last_seen_at, 1.second.ago
|
|
|
|
stat.update_column :posts_read_count, 0
|
|
|
|
end
|
2013-10-03 23:28:49 -04:00
|
|
|
|
|
|
|
it "increases posts_read_count" do
|
2023-04-10 22:28:08 -04:00
|
|
|
expect { UserStat.update_view_counts }.to change { stat.reload.posts_read_count }.to 1
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-04 21:44:36 -04:00
|
|
|
describe "ensure consistency!" do
|
|
|
|
it "can update first unread" do
|
|
|
|
post = create_post
|
|
|
|
|
|
|
|
freeze_time 10.minutes.from_now
|
|
|
|
create_post(topic_id: post.topic_id)
|
|
|
|
|
|
|
|
post.user.update!(last_seen_at: Time.now)
|
|
|
|
|
|
|
|
UserStat.ensure_consistency!
|
|
|
|
|
|
|
|
post.user.user_stat.reload
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(post.user.user_stat.first_unread_at).to eq_time(Time.zone.now)
|
2019-04-04 21:44:36 -04:00
|
|
|
end
|
2020-09-03 02:02:15 -04:00
|
|
|
|
|
|
|
it "updates first unread pm timestamp correctly" do
|
2021-12-15 03:51:46 -05:00
|
|
|
freeze_time
|
|
|
|
|
2021-11-23 02:26:55 -05:00
|
|
|
user = Fabricate(:user, last_seen_at: Time.zone.now)
|
|
|
|
user_2 = Fabricate(:user, last_seen_at: Time.zone.now)
|
|
|
|
pm_topic = Fabricate(:private_message_topic, user: user, recipient: user_2)
|
2020-09-03 02:02:15 -04:00
|
|
|
create_post(user: user, topic_id: pm_topic.id)
|
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
user.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
# user that is not tracking PM topic
|
|
|
|
TopicUser.change(
|
|
|
|
user_2.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:regular],
|
|
|
|
)
|
|
|
|
|
2021-11-22 23:51:54 -05:00
|
|
|
# User that has not been seen recently
|
|
|
|
user_3 = Fabricate(:user, last_seen_at: 1.year.ago)
|
2020-09-03 02:02:15 -04:00
|
|
|
pm_topic.allowed_users << user_3
|
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
user_3.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
2021-11-22 23:51:54 -05:00
|
|
|
user_3_orig_first_unread_pm_at = user_3.user_stat.first_unread_pm_at
|
|
|
|
|
|
|
|
# User that is not related to the PM
|
|
|
|
user_4 = Fabricate(:user, last_seen_at: Time.zone.now)
|
|
|
|
user_4_orig_first_unread_pm_at = user_4.user_stat.first_unread_pm_at
|
|
|
|
|
|
|
|
# User for another PM topic
|
2021-11-23 02:26:55 -05:00
|
|
|
user_5 = Fabricate(:user, last_seen_at: Time.zone.now)
|
|
|
|
user_6 = Fabricate(:user, last_seen_at: 10.minutes.ago)
|
|
|
|
pm_topic_2 = Fabricate(:private_message_topic, user: user_5, recipient: user_6)
|
2021-11-22 23:51:54 -05:00
|
|
|
create_post(user: user_5, topic_id: pm_topic_2.id)
|
2020-09-03 02:02:15 -04:00
|
|
|
|
2021-11-22 23:51:54 -05:00
|
|
|
TopicUser.change(
|
|
|
|
user_5.id,
|
|
|
|
pm_topic_2.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
2020-09-03 02:02:15 -04:00
|
|
|
)
|
|
|
|
|
2021-11-22 23:51:54 -05:00
|
|
|
# User out of last seen limit
|
2021-11-23 02:26:55 -05:00
|
|
|
TopicUser.change(
|
|
|
|
user_6.id,
|
|
|
|
pm_topic_2.id,
|
2021-11-22 23:51:54 -05:00
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
2021-11-23 02:26:55 -05:00
|
|
|
create_post(user: user_6, topic_id: pm_topic_2.id)
|
2021-11-22 23:51:54 -05:00
|
|
|
user_6_orig_first_unread_pm_at = user_6.user_stat.first_unread_pm_at
|
|
|
|
|
2021-11-23 02:26:55 -05:00
|
|
|
create_post(user: user_2, topic_id: pm_topic.id)
|
|
|
|
create_post(user: user_6, topic_id: pm_topic_2.id)
|
2021-11-22 23:51:54 -05:00
|
|
|
pm_topic.update!(updated_at: 10.minutes.from_now)
|
|
|
|
pm_topic_2.update!(updated_at: 20.minutes.from_now)
|
|
|
|
|
|
|
|
stub_const(UserStat, "UPDATE_UNREAD_USERS_LIMIT", 4) do
|
2021-11-23 02:26:55 -05:00
|
|
|
UserStat.ensure_consistency!(1.hour.ago)
|
2021-11-22 23:51:54 -05:00
|
|
|
end
|
2020-09-03 02:02:15 -04:00
|
|
|
|
2021-11-23 02:26:55 -05:00
|
|
|
# User affected
|
|
|
|
expect(user.user_stat.reload.first_unread_pm_at).to be_within(1.seconds).of(
|
|
|
|
pm_topic.reload.updated_at,
|
|
|
|
)
|
|
|
|
expect(user_2.user_stat.reload.first_unread_pm_at).to be_within(1.seconds).of(
|
|
|
|
UserStat::UPDATE_UNREAD_MINUTES_AGO.minutes.ago,
|
|
|
|
)
|
2021-11-22 23:51:54 -05:00
|
|
|
expect(user_3.user_stat.reload.first_unread_pm_at).to eq_time(user_3_orig_first_unread_pm_at)
|
2021-11-23 02:26:55 -05:00
|
|
|
expect(user_4.user_stat.reload.first_unread_pm_at).to be_within(1.seconds).of(
|
|
|
|
UserStat::UPDATE_UNREAD_MINUTES_AGO.minutes.ago,
|
|
|
|
)
|
2021-11-22 23:51:54 -05:00
|
|
|
expect(user_5.user_stat.reload.first_unread_pm_at).to eq_time(pm_topic_2.reload.updated_at)
|
|
|
|
expect(user_6.user_stat.reload.first_unread_pm_at).to eq_time(user_6_orig_first_unread_pm_at)
|
2020-09-03 02:02:15 -04:00
|
|
|
end
|
2019-04-04 21:44:36 -04:00
|
|
|
end
|
|
|
|
|
2013-10-03 23:28:49 -04:00
|
|
|
describe "update_time_read!" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2013-10-03 23:28:49 -04:00
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
|
2018-12-02 16:35:09 -05:00
|
|
|
it "always expires redis key" do
|
|
|
|
# this tests implementation which is not 100% ideal
|
|
|
|
# that said, redis key leaks are not good
|
|
|
|
stat.update_time_read!
|
2019-12-03 04:05:53 -05:00
|
|
|
ttl = Discourse.redis.ttl(UserStat.last_seen_key(user.id))
|
2018-12-02 16:35:09 -05:00
|
|
|
expect(ttl).to be > 0
|
|
|
|
expect(ttl).to be <= UserStat::MAX_TIME_READ_DIFF
|
|
|
|
end
|
|
|
|
|
2013-10-03 23:28:49 -04:00
|
|
|
it "makes no changes if nothing is cached" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del(UserStat.last_seen_key(user.id))
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(0)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "makes a change if time read is below threshold" do
|
2018-01-16 23:49:35 -05:00
|
|
|
freeze_time
|
|
|
|
UserStat.cache_last_seen(user.id, (Time.now - 10).to_f)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(10)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "makes no change if time read is above threshold" do
|
2018-01-16 23:49:35 -05:00
|
|
|
freeze_time
|
|
|
|
|
2013-10-03 23:28:49 -04:00
|
|
|
t = Time.now - 1 - UserStat::MAX_TIME_READ_DIFF
|
2018-01-16 23:49:35 -05:00
|
|
|
UserStat.cache_last_seen(user.id, t.to_f)
|
|
|
|
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(0)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
2019-12-30 06:19:59 -05:00
|
|
|
|
|
|
|
describe "update_distinct_badge_count" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
fab!(:badge1) { Fabricate(:badge) }
|
|
|
|
fab!(:badge2) { Fabricate(:badge) }
|
|
|
|
|
|
|
|
it "updates counts correctly" do
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(0)
|
|
|
|
BadgeGranter.grant(badge1, user)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(1)
|
|
|
|
BadgeGranter.grant(badge1, user)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(1)
|
|
|
|
BadgeGranter.grant(badge2, user)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(2)
|
|
|
|
user.reload.user_badges.destroy_all
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can update counts for all users simultaneously" do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
stat2 = user2.user_stat
|
|
|
|
|
|
|
|
BadgeGranter.grant(badge1, user)
|
|
|
|
BadgeGranter.grant(badge1, user)
|
|
|
|
BadgeGranter.grant(badge2, user)
|
|
|
|
|
|
|
|
BadgeGranter.grant(badge1, user2)
|
|
|
|
|
|
|
|
# Set some clearly incorrect values
|
|
|
|
stat.update(distinct_badge_count: 999)
|
|
|
|
stat2.update(distinct_badge_count: 999)
|
|
|
|
|
|
|
|
UserStat.ensure_consistency!
|
|
|
|
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(2)
|
|
|
|
expect(stat2.reload.distinct_badge_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores disabled badges" do
|
|
|
|
BadgeGranter.grant(badge1, user)
|
|
|
|
BadgeGranter.grant(badge2, user)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(2)
|
|
|
|
|
|
|
|
badge2.update(enabled: false)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(1)
|
|
|
|
|
|
|
|
badge2.update(enabled: true)
|
|
|
|
expect(stat.reload.distinct_badge_count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2021-07-27 07:05:33 -04:00
|
|
|
|
|
|
|
describe ".update_draft_count" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "updates draft_count" do
|
|
|
|
Draft.create!(user: user, draft_key: "topic_1", data: {})
|
|
|
|
Draft.create!(user: user, draft_key: "new_topic", data: {})
|
|
|
|
Draft.create!(user: user, draft_key: "topic_2", data: {})
|
|
|
|
UserStat.update_all(draft_count: 0)
|
|
|
|
|
|
|
|
UserStat.update_draft_count(user.id)
|
|
|
|
expect(user.user_stat.draft_count).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
2021-08-26 12:16:00 -04:00
|
|
|
|
|
|
|
describe "#update_pending_posts" do
|
|
|
|
subject(:update_pending_posts) { stat.update_pending_posts }
|
|
|
|
|
|
|
|
let!(:reviewable) { Fabricate(:reviewable_queued_post) }
|
2023-07-18 07:50:31 -04:00
|
|
|
let(:user) { reviewable.target_created_by }
|
2021-08-26 12:16:00 -04:00
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stat.update!(pending_posts_count: 0) # the reviewable callback will have set this to 1 already.
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets 'pending_posts_count'" do
|
|
|
|
expect { update_pending_posts }.to change { stat.pending_posts_count }.to 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "publishes a message to clients" do
|
|
|
|
MessageBus.expects(:publish).with(
|
|
|
|
"/u/#{user.username_lower}/counters",
|
|
|
|
{ pending_posts_count: 1 },
|
|
|
|
user_ids: [user.id],
|
|
|
|
group_ids: [Group::AUTO_GROUPS[:staff]],
|
|
|
|
)
|
|
|
|
update_pending_posts
|
|
|
|
end
|
|
|
|
end
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|