Skip to content

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.rb
index c2ce008150a..f28e98f8732 100644
--- a/lib/badge_queries.rb
+++ b/lib/badge_queries.rb
@@ -189,8 +189,7 @@ SQL
<<-SQL
SELECT tl.user_id, post_id, current_timestamp granted_at
FROM 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_id
WHERE NOT tl.internal
AND tl.clicks >= #{count}
GROUP BY tl.user_id, tl.post_id
diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb
index 5bc1d892eb2..4b34e907fcc 100644
--- a/spec/models/badge_spec.rb
+++ b/spec/models/badge_spec.rb
@@ -97,4 +97,37 @@ describe Badge do
expect(Badge.display_name('Not In Translations')).to eq('Not In Translations')
end
end
+
+ 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
+ end
end