A platform for community discussion. Free, open, simple.
FIX: Do not award badges for links in restricted categories. (#6492)
Bianca Nenciuc68a456baa19
diff --git a/lib/badge_queries.rb b/lib/badge_queries.rbindex c2ce008150a..f28e98f8732 100644--- a/lib/badge_queries.rb+++ b/lib/badge_queries.rb@@ -189,8 +189,7 @@ SQL<<-SQLSELECT tl.user_id, post_id, current_timestamp granted_atFROM topic_links tl- JOIN posts p ON p.id = post_id AND p.deleted_at IS NULL- JOIN topics t ON t.id = p.topic_id AND t.deleted_at IS NULL AND t.archetype <> 'private_message'+ JOIN badge_posts p ON p.id = post_idWHERE NOT tl.internalAND tl.clicks >= #{count}GROUP BY tl.user_id, tl.post_iddiff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rbindex 5bc1d892eb2..4b34e907fcc 100644--- a/spec/models/badge_spec.rb+++ b/spec/models/badge_spec.rb@@ -97,4 +97,37 @@ describe Badge doexpect(Badge.display_name('Not In Translations')).to eq('Not In Translations')endend++ context "PopularLink badge" do+ before do+ badge = Badge.find(Badge::PopularLink)+ badge.query = BadgeQueries.linking_badge(2)+ badge.save!+ end++ it "is awarded" do+ post = create_post(raw: "https://www.discourse.org/")++ TopicLinkClick.create_from(url: "https://www.discourse.org/", post_id: post.id, topic_id: post.topic.id, ip: "192.168.0.100")+ BadgeGranter.backfill(Badge.find(Badge::PopularLink))+ expect(UserBadge.where(user_id: post.user.id, badge_id: Badge::PopularLink).count).to eq(0)++ TopicLinkClick.create_from(url: "https://www.discourse.org/", post_id: post.id, topic_id: post.topic.id, ip: "192.168.0.101")+ BadgeGranter.backfill(Badge.find(Badge::PopularLink))+ expect(UserBadge.where(user_id: post.user.id, badge_id: Badge::PopularLink).count).to eq(1)+ end++ it "is not awarded for links in a restricted category" do+ category = Fabricate(:category)+ post = create_post(raw: "https://www.discourse.org/", category: category)++ category.set_permissions({})+ category.save!++ TopicLinkClick.create_from(url: "https://www.discourse.org/", post_id: post.id, topic_id: post.topic.id, ip: "192.168.0.100")+ TopicLinkClick.create_from(url: "https://www.discourse.org/", post_id: post.id, topic_id: post.topic.id, ip: "192.168.0.101")+ BadgeGranter.backfill(Badge.find(Badge::PopularLink))+ expect(UserBadge.where(user_id: post.user.id, badge_id: Badge::PopularLink).count).to eq(0)+ end+ endend