Creazione di query personalizzate con badge attivati

:bookmark: This guide explains how to create triggered custom badge queries in Discourse, including the types of badges, constraints for triggered badges, and an example query.

:person_raising_hand: Required user level: Administrator

:warning: This feature is disabled by default. To enable it, follow this guide.

When defining badges in Discourse, you’ll encounter a “Trigger” option with the following choices:

  • Update daily
  • When a user acts on a post
  • When a user edits or creates a post
  • When a user changes trust level
  • When a user is edited or created
  • After a post is processed

These triggers force badges to run at intervals more frequent than daily, ensuring users are notified of new badges closer to when the action occurred.

Types of badges

There are two types of badges you can define:

  1. Badges that target posts
  2. Badges that do not target posts

All badge SQL definitions require you to select the columns user_id and granted_at. If your badge targets posts, you must also select a column named post_id.

:information_source: If these columns are not directly available, you can alias them. For example: u.id as user_id

Constraints for triggered badges

Since triggered badges can run once a minute, you need to provide more “hinting” in the badge definition. It’s not sufficient to return the full set of badges; you must also provide hints about how to execute the badge on a subset.

User-based triggers

If your trigger is user-based, supply a clause on how to filter it based on :user_ids.

Post-based triggers

If your trigger is post-based, supply information on how to trigger it based on :post_ids.

:exclamation: Remember that a full backfill runs daily regardless, so you must consider this and include handling of the :backfill parameter.

Your triggered badge query will always include the :backfill param and either the :post_ids param or :user_ids param.

Example of a triggered badge query

Here’s an example of a badge triggered “when a user acts on a post”. In this case, “delta” applications will receive the :post_ids parameter:

SELECT p.user_id, p.id post_id, p.updated_at granted_at
FROM badge_posts p
WHERE p.like_count >= 25
AND (:backfill OR p.id IN (:post_ids) )

The clause (:backfill OR p.id IN (:post_ids) ) allows filtering of the results. When the daily job runs, :backfill is true, so the entire set is scanned. When delta jobs run, :backfill is false, and :post_ids is set.

Why manual hinting is necessary

The badge grant query runs your badge query in a “subquery”. Often, the PostgreSQL optimizer struggles when scanning the full set when the clause is on the main query. While it may handle trivial queries well, it can falter with more complex aggregates.

To avoid potential issues, this constraint was added, allowing you to apply filters in the most appropriate location.

Need help?

If you’re having trouble writing a badge query, post a question in Support - describe what you’re trying to achieve and include your work in progress. The community will try to help.

:information_source: Badge triggers can be complex. Often, “daily” updates suffice, and you can skip the more intricate aspects of triggered queries.

38 Mi Piace
Creating Custom Badges and Triggers
Creating custom badge with custom rules
How to grant badges using Automation?
Two custom badges on moderation
Pre approval email in the dashboard waiting to go
Help me get Discourse set up for the first time
Understanding and using badges
First Post badge
Grant badges based on gender profile field?
Assigned a badge based on multiple badge assignment
Problem testing Badge Query from Data Explorer
Discourse-Math badge
Badges for X Number of Solutions Created?
A silver badge for _each_ 5 bronze badges
Automatically set active members to follow a category
A silver badge for _each_ 5 bronze badges
A silver badge for _each_ 5 bronze badges
About Badges
What is the 'backfill`?
Query to create some groups based on activity
Are badges customizable?
Badge query working in explorer but not in badge SQL?
Badges for posting
Gamification beyond Leaderboard
Bulk Award a badge to a set of users
What does admin_js.admin.badges.trigger_type.user_change mean in Discourse?
Set badges to be auto granted?
Access to category after 10 posts (automatic group membership)
Add badge for adding profile picture
Grant Custom Badge Upon Reading ToS
Moderator - Badges, Stars, etc. - Positive Feedback?
Configure Patreon integration with Discourse
Hidden Settings on Discourse Hosted Sites
Require LinkedIn or GitHub account to advance from TL0 to TL1. Possible?
Exclude certain categories from the Trust Levels
How does Meta handle the bug reporter badge?
Restricting users to join any one private group
An automatic group of group owners
Is it possible to have a badge for 100 solutions?
Grant a badge for all members of a group
Can I change the threshold of the Solved badges?
Grant badges through Automation
What is the 'backfill`?
Grant Badge when user visits link