Skip to content

A platform for community discussion. Free, open, simple.

FIX: Don't require restart for first flag badge working with custom flag (#35733)

Ted Johanssondd82ea10b3d8
diff --git a/lib/badge_queries.rb b/lib/badge_queries.rb
index 6423747a9d0..cd5383df611 100644
--- a/lib/badge_queries.rb
+++ b/lib/badge_queries.rb
@@ -73,8 +73,14 @@ module BadgeQueries
SELECT pa.user_id, min(pa.id) id
FROM post_actions pa
JOIN badge_posts p on p.id = pa.post_id
- WHERE post_action_type_id IN (#{PostActionType.flag_types_without_additional_message.values.join(",")}) AND
- (:backfill OR pa.post_id IN (:post_ids) )
+ WHERE post_action_type_id IN (
+ SELECT f.id
+ FROM flags f
+ WHERE name != 'like'
+ AND score_type IS FALSE
+ AND require_message IS FALSE
+ )
+ AND (:backfill OR pa.post_id IN (:post_ids))
GROUP BY pa.user_id
) x
JOIN post_actions pa1 on pa1.id = x.id
diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb
index eb5e6364f9b..08a60d3efea 100644
--- a/spec/models/badge_spec.rb
+++ b/spec/models/badge_spec.rb
@@ -271,6 +271,79 @@ RSpec.describe Badge do
end
end
+ describe "FirstFlag badge" do
+ fab!(:flagging_user, :user)
+ fab!(:badge_enabled_category) { Fabricate(:category, allow_badges: true) }
+ fab!(:flagged_post) do
+ Fabricate(:post, topic: Fabricate(:topic, category: badge_enabled_category))
+ end
+ let(:first_flag_badge) { Badge.find(Badge::FirstFlag) }
+
+ context "when using an out-of-the-box flag" do
+ let!(:flag_post_action) do
+ Fabricate(:flag_post_action, post: flagged_post, user: flagging_user)
+ end
+
+ it "grants the badge" do
+ expect { BadgeGranter.backfill(first_flag_badge) }.to change {
+ UserBadge.where(user_id: flagging_user.id, badge_id: Badge::FirstFlag).count
+ }.by(1)
+ end
+ end
+
+ context "when using a custom flag" do
+ let!(:custom_flag) { Fabricate(:flag, name: "stahp", applies_to: %w[Post]) }
+ let!(:flag_post_action) do
+ Fabricate(
+ :flag_post_action,
+ post: flagged_post,
+ user: flagging_user,
+ post_action_type_id: PostActionType.types[:custom_stahp],
+ )
+ end
+
+ it "grants the badge" do
+ expect { BadgeGranter.backfill(first_flag_badge) }.to change {
+ UserBadge.where(user_id: flagging_user.id, badge_id: Badge::FirstFlag).count
+ }.by(1)
+ end
+ end
+
+ context "when the flag requires message" do
+ let!(:flag_post_action) do
+ Fabricate(
+ :flag_post_action,
+ post: flagged_post,
+ user: flagging_user,
+ post_action_type_id: PostActionType.types[:notify_user],
+ )
+ end
+
+ it "does not grant the badge" do
+ expect { BadgeGranter.backfill(first_flag_badge) }.not_to change {
+ UserBadge.where(user_id: flagging_user.id, badge_id: Badge::FirstFlag).count
+ }
+ end
+ end
+
+ context "when the flag is a like" do
+ let!(:flag_post_action) do
+ Fabricate(
+ :flag_post_action,
+ post: flagged_post,
+ user: flagging_user,
+ post_action_type_id: PostActionType.types[:like],
+ )
+ end
+
+ it "does not grant the badge" do
+ expect { BadgeGranter.backfill(first_flag_badge) }.not_to change {
+ UserBadge.where(user_id: flagging_user.id, badge_id: Badge::FirstFlag).count
+ }
+ end
+ end
+ end
+
describe "#seed" do
let(:badge_id) { Badge.maximum(:id) + 1 }