A platform for community discussion. Free, open, simple.
FIX: `BadgeQueries.sharing_badge` not counting unique visitors (#36281)
Alan Guo Xiang Tan0464682ab83f
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 31385448947..202ff24064d 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -5294,17 +5294,17 @@ en:name: Nice Sharedescription: Shared a post with 25 unique visitorslong_description: |- This badge is granted for sharing a link that was clicked by 25 outside visitors. Thanks for spreading the word about our discussions, and this community.+ This badge is granted for sharing a link that was clicked by 25 unique outside visitors. Thanks for spreading the word about our discussions, and this community.good_share:name: Good Sharedescription: Shared a post with 300 unique visitorslong_description: |- This badge is granted for sharing a link that was clicked by 300 outside visitors. Good work! You’ve shown off a great discussion to a bunch of new people and helped this community grow.+ This badge is granted for sharing a link that was clicked by 300 unique outside visitors. Good work! You’ve shown off a great discussion to a bunch of new people and helped this community grow.great_share:name: Great Sharedescription: Shared a post with 1000 unique visitorslong_description: |- This badge is granted for sharing a link that was clicked by 1000 outside visitors. Wow! You’ve promoted an interesting discussion to a huge new audience, and helped us grow our community in a big way!+ This badge is granted for sharing a link that was clicked by 1000 unique outside visitors. Wow! You’ve promoted an interesting discussion to a huge new audience, and helped us grow our community in a big way!first_like:name: First Likedescription: Liked a postdiff --git a/lib/badge_queries.rb b/lib/badge_queries.rbindex cd5383df611..42a9f677479 100644--- a/lib/badge_queries.rb+++ b/lib/badge_queries.rb@@ -199,18 +199,15 @@ module BadgeQueriesenddef self.sharing_badge(count)+ # Not that DISTINCT(i.ip_address, i.current_user_id) works because there is an underlying assumption that+ # `IncomingLink#ip_address` is not set when `IncomingLink#current_user_id` is set.<<~SQL- SELECT views.user_id, i2.post_id, current_timestamp granted_at- FROM- (- SELECT i.user_id, MIN(i.id) i_id- FROM incoming_links i- JOIN badge_posts p on p.id = i.post_id- JOIN users u on u.id = i.user_id- GROUP BY i.user_id,i.post_id- HAVING COUNT(*) >= #{count}- ) as views- JOIN incoming_links i2 ON i2.id = views.i_id+ SELECT i.user_id, i.post_id, CURRENT_TIMESTAMP granted_at+ FROM incoming_links i+ JOIN badge_posts p on p.id = i.post_id+ JOIN users u on u.id = i.user_id+ GROUP BY i.user_id,i.post_id+ HAVING COUNT(DISTINCT(i.ip_address, i.current_user_id)) >= #{count}SQLenddiff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rbindex dec0b1c11d8..fdc149349d2 100644--- a/spec/services/badge_granter_spec.rb+++ b/spec/services/badge_granter_spec.rb@@ -211,19 +211,65 @@ RSpec.describe BadgeGranter do}end- it "does not grant sharing badges to deleted users" do- post = Fabricate(:post)- incoming_links = Fabricate.times(25, :incoming_link, post: post, user: user)- user_id = user.id- user.destroy!+ context "for sharing badges" do+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, category:) }+ fab!(:post) { Fabricate(:post, topic:) }+ fab!(:current_user, :user)++ it "grants nice share badge when threshold has been reached" do+ Fabricate.times(25, :incoming_link, post:, user:)++ BadgeGranter.backfill(Badge.find(Badge::NiceShare))++ expect(UserBadge.where(user_id: user.id, badge_id: Badge::NiceShare).count).to eq(1)+ end++ it "does not grant nice share badge when duplicates on IP address exists" do+ Fabricate.times(23, :incoming_link, post:, user:)- nice_share = Badge.find(Badge::NiceShare)- first_share = Badge.find(Badge::FirstShare)+ _incoming_link_with_ip_address =+ Fabricate(:incoming_link, post:, user:, ip_address: "1.1.1.1")++ _incoming_link_with_ip_address_duplicate =+ Fabricate(:incoming_link, post:, user:, ip_address: "1.1.1.1")++ expect(IncomingLink.where(post:, user:).count).to eq(25)++ BadgeGranter.backfill(Badge.find(Badge::NiceShare))++ expect(UserBadge.where(user_id: user.id, badge_id: Badge::NiceShare).count).to eq(0)+ end- BadgeGranter.backfill(nice_share)- BadgeGranter.backfill(first_share)+ it "does not grant nice share badge when duplicates on current_user_id exists" do+ Fabricate.times(23, :incoming_link, post:, user:, ip_address: nil)- expect(UserBadge.where(user_id: user_id).count).to eq(0)+ _incoming_link_with_current_user_id =+ Fabricate(:incoming_link, post:, user:, current_user_id: current_user.id, ip_address: nil)++ _incoming_link_with_current_user_id_duplicate =+ Fabricate(:incoming_link, post:, user:, current_user_id: current_user.id, ip_address: nil)++ expect(IncomingLink.where(post:, user:).count).to eq(25)++ BadgeGranter.backfill(Badge.find(Badge::NiceShare))++ expect(UserBadge.where(user_id: user.id, badge_id: Badge::NiceShare).count).to eq(0)+ end++ it "does not grant sharing badges to deleted users" do+ incoming_links = Fabricate.times(25, :incoming_link, post: post, user: user)+ user_id = user.id+ user.destroy!++ nice_share = Badge.find(Badge::NiceShare)+ first_share = Badge.find(Badge::FirstShare)++ BadgeGranter.backfill(nice_share)+ BadgeGranter.backfill(first_share)++ expect(UserBadge.where(user_id: user_id).count).to eq(0)+ endendit "auto revokes badges from users when badge is set to auto revoke and user no longer satisfy the badge's query" do