0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-solved/db/fixtures/001_badges.rb
David Battersby 6e9b28f17c
DEV: add upcoming change for Solved badges (#41613)
We want to enable the Solved badges by default so that communities can
more easily acknowledge their engaged and contributing members.

For sites that have the Solved plugin enabled — we will show an upcoming
change labelled "Enabled Solved Badges".

When the upcoming change is enabled (either manually or via
auto-promotion) we will update all 4 solved badges to enabled. When
opting out of this upcoming change we will disable all 4 solved badges.
2026-07-15 18:15:07 +04:00

67 lines
2.3 KiB
Ruby
Vendored

# frozen_string_literal: true
first_solution_query = <<~SQL
SELECT post_id, user_id, created_at AS granted_at
FROM (
SELECT p.id AS post_id, p.user_id, dsta.created_at,
ROW_NUMBER() OVER (PARTITION BY p.user_id ORDER BY dsta.created_at) AS row_number
FROM discourse_solved_solved_topics dsst
JOIN discourse_solved_topic_answers dsta ON dsta.solved_topic_id = dsst.id
JOIN badge_posts p ON dsta.answer_post_id = p.id
JOIN topics t ON p.topic_id = t.id
WHERE p.user_id <> t.user_id -- ignore topics solved by OP
AND (:backfill OR p.id IN (:post_ids))
) x
WHERE row_number = 1
SQL
Badge.seed(:name) do |badge|
badge.name = "Solved 1"
badge.default_icon = "square-check"
badge.badge_type_id = BadgeType::Bronze
badge.default_badge_grouping_id = BadgeGrouping::Community
badge.query = first_solution_query
badge.listable = true
badge.target_posts = true
badge.default_enabled = true
badge.trigger = Badge::Trigger::PostRevision
badge.auto_revoke = true
badge.show_posts = true
badge.system = true
end
def solved_query_with_count(min_count)
<<~SQL
SELECT p.user_id, MAX(dsta.created_at) AS granted_at
FROM discourse_solved_solved_topics dsst
JOIN discourse_solved_topic_answers dsta ON dsta.solved_topic_id = dsst.id
JOIN badge_posts p ON dsta.answer_post_id = p.id
JOIN topics t ON p.topic_id = t.id
WHERE p.user_id <> t.user_id -- ignore topics solved by OP
AND (:backfill OR p.id IN (:post_ids))
GROUP BY p.user_id
HAVING COUNT(*) >= #{min_count}
SQL
end
[
["Solved 2", BadgeType::Silver, 10],
["Solved 3", BadgeType::Gold, 50],
["Solved 4", BadgeType::Gold, 150],
].each do |name, level, count|
Badge.seed(:name) do |badge|
badge.name = name
badge.default_icon = "square-check"
badge.badge_type_id = level
badge.default_badge_grouping_id = BadgeGrouping::Community
badge.query = solved_query_with_count(count)
badge.listable = true
badge.default_allow_title = true
badge.target_posts = false
badge.default_enabled = true
badge.trigger = Badge::Trigger::PostRevision
badge.auto_revoke = true
badge.show_posts = false
badge.system = true
end
end