创建触发式自定义徽章查询

:bookmark: 本指南说明了如何在 Discourse 中创建触发式自定义徽章查询,包括徽章类型、触发徽章的约束条件以及一个示例查询。

:person_raising_hand: 所需用户级别:管理员

:warning: 此功能默认禁用。要启用它,请遵循本指南

在 Discourse 中定义徽章时,您会遇到一个“触发”(Trigger)选项,其中包含以下选择:

  • 每日更新 (Update daily)
  • 当用户对帖子进行操作时 (When a user acts on post)
  • 当用户编辑或创建帖子时 (When a user edits or creates a post)
  • 当用户更改信任级别时 (When a user changes trust level)
  • 当用户被编辑或创建时 (When a user is edited or created)

这些触发器会强制徽章运行的频率高于每日一次,确保用户在接近发生操作时就能收到新徽章的通知。

徽章类型

您可以定义两种类型的徽章:

  1. 以帖子为目标的徽章
  2. 不以帖子为目标的徽章

所有徽章 SQL 定义都要求您选择 user_idgranted_at 列。如果您的徽章以帖子为目标,则还必须选择名为 post_id 的列。

:information_source: 如果这些列不可直接使用,您可以为其设置别名。例如:u.id as user_id

触发徽章的约束条件

由于触发徽章每分钟可以运行一次,因此您需要在徽章定义中提供更多“提示”。仅返回完整的徽章集合是不够的;您还必须提供关于如何对子集执行徽章的提示。

基于用户的触发器

如果您的触发器是基于用户的,请提供一个子句来说明如何根据 :user_ids 过滤它。

基于帖子的触发器

如果您的触发器是基于帖子的,请提供信息说明如何根据 :post_ids 触发它。

:exclamation: 请记住,完整的回填(backfill)无论如何每天都会运行,因此您必须考虑到这一点,并包含对 :backfill 参数的处理。

您的触发徽章查询将始终包含 :backfill 参数以及 :post_ids 参数或 :user_ids 参数。

触发徽章查询示例

这是一个触发“当用户对帖子进行操作时”的徽章示例。在这种情况下,“增量”(delta)应用程序将接收 :post_ids 参数:

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) )

子句 (:backfill OR p.id IN (:post_ids) ) 允许过滤结果。当每日作业运行时,:backfill 为 true,因此会扫描整个集合。当增量作业运行时,:backfill 为 false,并且设置了 :post_ids

为什么需要手动提示

徽章授予查询在“子查询”中运行您的徽章查询。通常,当子句位于主查询上时,PostgreSQL 优化器在扫描完整集合时会遇到困难。虽然它可能很好地处理简单的查询,但在处理更复杂的聚合时可能会出现故障。

为避免潜在问题,添加了此约束,允许您在最合适的位置应用过滤器。

需要帮助?

如果您在编写徽章查询时遇到困难,请在 Support#支持)中发帖提问——描述您想实现的目标,并附上您正在进行的工作。社区将尽力提供帮助。

:information_source: 徽章触发器可能很复杂。通常,“每日”更新就足够了,您可以跳过触发查询更复杂的方面。

38 个赞
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
Grant badges based on gender profile field?
Badges for X Number of Solutions Created?
Discourse-Math badge
Assigned a badge based on multiple badge assignment
Problem testing Badge Query from Data Explorer
Understanding and using badges
First Post badge
Grant Badge when user visits link
Custom Icons for admins and moderators (and other groups)
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
Plugin or other option to require a post and a like?
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?
Grant Custom Badge Upon Reading ToS
Set badges to be auto granted?
Access to category after 10 posts (automatic group membership)
Add badge for adding profile picture
How do you assign a badge automatically as soon as someone is added to a group?
Moderator - Badges, Stars, etc. - Positive Feedback?
Hidden Settings on Discourse Hosted Sites
Require LinkedIn or GitHub account to advance from TL0 to TL1. Possible?
Need some Help with my Triggered Custom Badge Query
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`?