FEATURE: Add script to modify user group memberships through badges #206
Conversation
This change adds a new script, `user_group_membership_through_badge` which provides the option to automate user group memberships through badges. Given a badge name and a group, this script adds users with the specified badged to the the given group on login or on a recurring schedule. It also has an optional flag which when enabled ensures only users with the specified badge are members of the given group
|
||
DiscourseAutomation::Scriptable::USER_GROUP_MEMBERSHIP_THROUGH_BADGE = | ||
"user_group_membership_through_badge" | ||
DiscourseAutomation::Scriptable::USER_GROUP_MEMBERSHIP_THROUGH_BADGE_BULK_MODIFY_START_COUNT = 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷🏾 Arbitrary limit alert. I wonder if this is even needed, the add_user_to_group_through_custom_field
script doesn't do this at the moment.
Better safe than sorry, we probably don't want to be doing 1000s of Group#add
calls anyways
if user_ids_to_add.count < bulk_modify_start_count | ||
User | ||
.where(id: user_ids_to_add) | ||
.each do |user| | ||
group.add(user) | ||
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user) | ||
end | ||
else | ||
group.bulk_add(user_ids_to_add) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we always do bulk_add
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How essential are the user_added_to_group
and user_removed_from_group
events? The bulk methods understandably don't trigger these.
I considered going all in with the bulk methods but decided against it for two main reasons:
- We lose the events and ability to trigger other automations off of the badge-group sync
- Compatibility with
exisiting add_user_to_group_through_custom_field
script. It does not perform bulk adds
if user_ids_to_remove.count < bulk_modify_start_count | ||
User | ||
.where(id: user_ids_to_remove) | ||
.each do |user| | ||
group.remove(user) | ||
GroupActionLogger.new(Discourse.system_user, group).log_remove_user_from_group(user) | ||
end | ||
else | ||
group.bulk_remove(user_ids_to_remove) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, maybe only bulk_remove
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Thanks @jjaffeux |
This change adds a new script,
user_group_membership_through_badge
which provides the option to automate user group memberships through badges.Given a badge name and a group, it adds users with the specified badged to the the given group either on login or on a recurring schedule.
It also has an optional flag to synchronize these two in "strict mode". It does this by ensuring only users with the specified badge belong to the given group.