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?
You seem to be right on this – you cannot access group resources with the ID number, only the name.
The ID referenced here is not an ID, but the name, per
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:
Storing the group name alongside the id
Hitting g/<group-name>/members.json with a GET request to get the members
Loop through those to delete each so you can re-sync.
This is an awesome problem to be looking into. Thanks for raising it, @Judith! Groups are so powerful and have so much promise! 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.
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.
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.
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.