From 2513339955ebdfa457a5d6126716091110045e37 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 3 Dec 2024 13:43:27 +1100 Subject: [PATCH] FEATURE: Show when a badge has been granted for a post (#29696) Co-authored-by: Joffrey JAFFEUX Co-authored-by: Jarek Radosz Co-authored-by: Alan Guo Xiang Tan --- .../addon/controllers/admin-badges/show.js | 14 +- .../addon/templates/admin-badges/show.hbs | 21 +- .../discourse/app/components/badge-button.gjs | 8 +- .../discourse/app/components/user-badge.gjs | 2 +- .../discourse/app/lib/transform-post.js | 4 + .../discourse/app/widgets/poster-name.js | 25 +++ .../components/badge-button-test.js | 18 ++ .../components/widgets/poster-name-test.js | 36 ++++ .../stylesheets/common/base/topic-post.scss | 13 ++ app/models/badge.rb | 43 ++-- app/models/user_badge.rb | 13 ++ app/serializers/badge_serializer.rb | 3 +- app/serializers/post_serializer.rb | 13 ++ app/serializers/web_hook_post_serializer.rb | 1 + config/locales/client.en.yml | 6 + config/locales/server.en.yml | 1 + config/site_settings.yml | 3 + ...20241121000131_add_post_header_to_badge.rb | 6 + lib/topic_view.rb | 19 ++ .../schemas/json/badge_create_response.json | 6 +- .../api/schemas/json/badge_list_response.json | 6 +- .../schemas/json/badge_update_response.json | 6 +- .../schemas/json/post_update_response.json | 4 + .../schemas/json/topic_create_response.json | 4 + spec/serializers/post_serializer_spec.rb | 195 ++++++++++++++++++ spec/system/grant_badge_spec.rb | 39 ++++ 26 files changed, 479 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20241121000131_add_post_header_to_badge.rb diff --git a/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js b/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js index 76cd13c24a3..dc7d5735b26 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js @@ -27,6 +27,7 @@ const FORM_FIELDS = [ "badge_grouping_id", "trigger", "badge_type_id", + "show_in_post_header", ]; export default class AdminBadgesShowController extends Controller { @@ -40,8 +41,6 @@ export default class AdminBadgesShowController extends Controller { @tracked model; @tracked previewLoading = false; @tracked selectedGraphicType = null; - @tracked userBadges; - @tracked userBadgesAll; @cached get formData() { @@ -80,6 +79,17 @@ export default class AdminBadgesShowController extends Controller { return this.model.system; } + @action + postHeaderDescription(data) { + return this.disableBadgeOnPosts(data) && !data.system; + } + + @action + disableBadgeOnPosts(data) { + const { listable, show_posts } = data; + return !listable || !show_posts; + } + setup() { // this is needed because the model doesnt have default values // Using `set` here isn't ideal, but we don't know that tracking is set up on the model yet. diff --git a/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs b/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs index 67735c6bf64..1548b18feae 100644 --- a/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs +++ b/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs @@ -245,7 +245,7 @@ - + + + + + + + {{#if (this.postHeaderDescription data)}} + {{i18n "admin.badges.show_in_post_header_disabled"}} + {{/if}} + + diff --git a/app/assets/javascripts/discourse/app/components/badge-button.gjs b/app/assets/javascripts/discourse/app/components/badge-button.gjs index 2abddb36646..c5d22a5bca5 100644 --- a/app/assets/javascripts/discourse/app/components/badge-button.gjs +++ b/app/assets/javascripts/discourse/app/components/badge-button.gjs @@ -10,6 +10,10 @@ export default class BadgeButton extends Component { } } + get showName() { + return this.args.showName ?? true; + } + diff --git a/app/assets/javascripts/discourse/app/components/user-badge.gjs b/app/assets/javascripts/discourse/app/components/user-badge.gjs index a16286d6faf..0c2f3b0056d 100644 --- a/app/assets/javascripts/discourse/app/components/user-badge.gjs +++ b/app/assets/javascripts/discourse/app/components/user-badge.gjs @@ -15,7 +15,7 @@ export default class UserBadge extends Component {