Groups API - manually syncing group membership?

I seem to have boxed myself into a corner here…

I have a membership site (also in Rails) and I’m using the Discourse API in order to automatically create and populate restricted forum groups for all of my user groups. This works great; I’m even giving each group a wiki post to start with. (I’ve posted my code.)

Now I’ve come across the issue that sometimes I need to manually sync up the group membership, for example when merging groups on my site or when the coordinator accidentally deleted/added people via the forum. For this purpose, I want to delete ALL Discourse group members and then re-add the ones that should be in it.

First problem: the Discourse API doesn’t seem to come with a DELETE ALL for group members, so I have to specify the names of everyone that should be deleted.

Second problem: I cannot actually retrieve the Discourse group’s current membership, because for whatever reason it’s asking me for the group NAME rather than ID. I’ve only stored the group ID on my site, because that’s what Discourse uses for the related PUT/POST/DELETE requests.

Third problem: It should be obvious, but I don’t see a way to retrieve the group’s name by supplying its ID, unless I want to retrieve all 200+ groups and iterate through them until I find one whose ID matches. This seems a ridiculous amount of trouble and also contravenes Rails conventions.

What am I missing?

EDIT: @codinghorror was really helpful last time - any ideas now?

4 Likes

I’m not sure but maybe @justin or @blake could check in on this during their upcoming work week?

4 Likes

You seem to be right on this – you cannot access group resources with the ID number, only the name.

https://github.com/discourse/discourse/blob/70e50a2255ebb30c78891a7f846e833ab85b0a8b/app/controllers/groups_controller.rb#L113

The ID referenced here is not an ID, but the name, per

https://github.com/discourse/discourse/blob/70e50a2255ebb30c78891a7f846e833ab85b0a8b/app/controllers/groups_controller.rb#L569

It probably makes sense to refactor the /g/<group-name> route to match how we now handle topics and categories. Something like /g/<group-name>/<id> or /g/patrons/41 and allow /g/41 to function the same way.

Until we fix that up, though, it seems the only way to accomplish what you’re looking for is:

  1. Storing the group name alongside the id
  2. Hitting g/<group-name>/members.json with a GET request to get the members
  3. Loop through those to delete each so you can re-sync.
3 Likes

This is an awesome problem to be looking into. Thanks for raising it, @Judith! Groups are so powerful and have so much promise! :rocket: I noticed this issue recently as well, when looking at maintaining group membership from wordpress. It would be pretty neat to be able to automagically add/remove people from groups for country and region of the world they say they are based in, languages they speak, themes they are interested in, etc, when they sign up and update their profiles in wordpress.

@justin’s suggestion of storing both group name and group ID is a good one and I’ll pass it on to the folks working on this integration now.

One feature I’d also love to see added to the UI is the ability for admins or group owners to bulk remove all members from a group, and re-add them. This would allow them to reset their group memberships manually from time to time based on exports from wordpress, even if they don’t have an automated systme in place to add/remove people from groups as they update their profiles in wordpress.

3 Likes

I recommend using SSO between your main site and Discourse. That way you get group membership handling for free, and will have a sync_sso endpoint to update the membership in a straightforward way.

4 Likes

At first I was only using SSO, but then I ran into the problem that it only updates group membership when people log in. In the case of people not being re-elected, or otherwise losing the right to access a group, or gaining the right to access a group (and should receive email notifications for messages in the restricted group subforum), I need to add/remove them via the API.

1 Like

The sync_sso exists to address this exact problem! You can trigger what is effectively a “virtual” sso login anytime a membership change to fully sync up a user.

6 Likes

Hey!

Is this feature now available? Any updates on this?

Thanks!

1 Like

Any updates? @tobiaseigen

1 Like