Add script to add user to user group

Other than the topic @JammyDodger linked to, there have been a few other requests for similar functionality

There have also been requests to be able to display groups in a hierarchical manner:

I’m noting this just to make sure that adding an Automation script is the best way to deal with this, or if it’s functionality that should get added to the core Discourse groups code.

I’ve been fiddling around with this for fun. It’s kind of related to some recent work I’ve been doing with Zapier. Feel free to ignore the rest of this post.

Assuming an Automation script is the right approach, the only stumbling block is that the Automation plugin’s group component only accepts a single group. So either the group component needs to be updated to accept more than one group, or a new groups component needs to be added. I added a groups component to my local dev branch to test it out. With that in place:

Add a user to a parent group when they are added to any of a list of subgroups:

Remove a user from a parent group if they have been removed from all of the listed subgroups:

If it helps, the triggers and scripts are in a plugin: https://github.com/scossar/automation-script-example/blob/main/plugin.rb#L13-L98. Note that this code assumes that a groups component has been added to https://github.com/discourse/discourse-automation/tree/main/assets/javascripts/discourse/components/fields.

3 Likes