Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
Richard Kiessig Posted October 20, 2020 Posted October 20, 2020 I've struggled trying many different format permutations and nothing works to specify the secondaryGroups parameter in the Edit Member API call: https://invisioncommunity.com/developers/rest-api?endpoint=core/members/POSTitem Specifying [n,n] does NOT work. Does anyone have a working example to set it? Does it need to be in some particular physical format?
CoffeeCake Posted October 21, 2020 Posted October 21, 2020 I asked questions about secondary groups. Can't recall if we ever found a working solution.
Richard Kiessig Posted October 21, 2020 Author Posted October 21, 2020 By poking around, I found that a member's secondary groups is stored in the 'mgroup_others' field as a comma delimited list of group ids in the core_members table. So if Invision finds it too much trouble to properly document their API or to fix bugs, I'll have to use database access to update that list, which is atrocious.
Martin A. Posted October 21, 2020 Posted October 21, 2020 (edited) Looks like the API input need to be an array. if( isset( \IPS\Request::i()->secondaryGroups ) AND \is_array( \IPS\Request::i()->secondaryGroups ) ) { foreach( \IPS\Request::i()->secondaryGroups as $groupId ) { .... $endpoint = '/core/members/1'; $curl = curl_init( $communityUrl . 'api' . $endpoint . '?key=' . $apiKey ); $postData = [ 'secondaryGroups' => [ 7, 8 ] ]; curl_setopt_array( $curl, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => http_build_query( $postData ), ) ); $response = curl_exec( $curl ); Edited October 21, 2020 by Martin A. bfarber 1
Nathan Explosion Posted October 21, 2020 Posted October 21, 2020 5 minutes ago, Martin A. said: Looks like the API input need to be an array. Totally correct - this should work: 6 minutes ago, Martin A. said: 'secondaryGroups' => [ 7, 8 ] But this won't work: Quote 'secondaryGroups' => '[ 7, 8 ]' Reference:
Solution Richard Kiessig Posted October 22, 2020 Author Solution Posted October 22, 2020 Thanks, Nathan and Martin. Your replies helped. I am not using PHP, I'm using C#/.NET, and Postman for testing. I think it's a deficiency of the documentation that it assumes that the API will be called from PHP. By reading about how PHP is resolving the arrays to POST fields, I found the problem. The actual POST format of the fields is NOT actually (for example): Field Name Value secondaryGroups [7,8] it's: Field Name Value secondaryGroups[0] 7 secondaryGroups[1] 8 This works. I'd add that it's an annoyance that this sets and replaces any existing secondary groups. This requires reading existing ones and taking them into account, for adding new groups. Since this is an update operation, a more common use case would be to add new groups (or delete old ones). A suggestion, which would be backwards compatible, would be to add a new field, e.g. secondaryGroupsOp, with values 'replace', 'add', 'remove'. 'replace' would be current behavior, 'add' would add the group (doing nothing if the user were already in that group), 'remove' would remove the group. Another approach occurs to me, also (I think) backwards compatible: if a '+' is prepended to the id, then add it to the existing groups; '-', remove it. wegorz23 1
Recommended Posts