Making WordPress.org

Changeset 10824


Ignore:
Timestamp:
03/17/2021 01:34:10 AM (4 years ago)
Author:
dd32
Message:

Slack: Update the /subgroup command, take two.

This fixes a few errors in the initial change to upgrade to the new conversations.* api endpoints.

See r10823

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/slack/subgroup.php

    r10823 r10824  
    22
    33namespace Dotorg\Slack\Subgroup;
     4use const Dotorg\Slack\WORDPRESSORG_USER_ID as WORDPRESSORG_SLACK_USER_ID;
    45
    56require dirname( dirname( __DIR__ ) ) . '/includes/slack-config.php';
     
    2223// Confirm it came from Slack.
    2324if ( $_POST['token'] !== WEBHOOK_TOKEN ) {
    24     die;
     25    die( "Invalid Token" );
    2526}
    2627
    2728$current_group_id = $_POST['channel_id'];
    2829
    29 // If it didn't come from a group, die.
    30 if ( $current_group_id[0] !== 'G' ) {
    31     die;
    32 }
     30// Note: We no longer check to see if the calling channel is a group, as the below list command will limit it to private channels.
     31// Groups can begin with `G` (old created groups) or `C` (private channel), public channels also begin with `C` so the below check is safer.
    3332
    3433// Get a list of all groups @wordpressdotorg is in.
     
    4140        'limit'            => 999,
    4241    ]
    43 );
     42)['channels'];
    4443
    4544// Find the group that we are in right now.
    46 foreach ( $groups['channels'] as $group ) {
     45foreach ( $groups as $group ) {
    4746    if ( $group['id'] === $current_group_id ) {
    4847        $found = true;
     
    6160}
    6261
     62// Get the current groups members.
     63$members = api_call(
     64    // https://api.slack.com/methods/conversations.members
     65    'conversations.members',
     66    [
     67        'channel' => $group['id'],
     68        'limit'   => 999,
     69    ]
     70)['members'];
     71
    6372// Ensure the calling user is in the main group.
    64 if ( ! in_array( $_POST['user_id'], $group['members'], true ) ) {
     73if ( ! in_array( $_POST['user_id'], $members, true ) ) {
    6574    die;
    6675}
     
    7988            'conversations.create',
    8089            [
    81                 'name' => $subcommand,
     90                'name'       => $subcommand,
    8291                'is_private' => true,
    8392            ]
     
    95104                'text'    => sprintf(
    96105                    'Group %s created by <@%s>.',
    97                     $new_group['group']['name'],
     106                    $new_group['channel']['name'],
    98107                    $_POST['user_id']
    99108                ),
     
    103112
    104113        // Invite all current parent channel members to the new channel.
    105         $members = api_call(
    106             // https://api.slack.com/methods/conversations.members
    107             'conversations.members',
    108             [
    109                 'channel' => 'CMYKV0D7B',
    110                 'limit'            => 999,
    111             ]
    112         );
    113         if ( empty( $members['members'] ) ) {
    114             die( sprintf( "Group %s created, but could not invite members.", $new_group['group']['name'] ) );
    115         }
    116 
     114        // Note: Cannot invite self.
    117115        api_call(
    118116            // https://api.slack.com/methods/conversations.invite
    119117            'conversations.invite',
    120118            [
    121                 'channel' => $new_group['group']['id'],
    122                 'users'   => implode( ',', $members['members'] ),
     119                'channel' => $new_group['channel']['id'],
     120                'users'   => implode( ',', array_diff( $members, [ WORDPRESSORG_SLACK_USER_ID ] ) ),
    123121            ]
    124122        );
     
    152150        foreach ( $groups as $group ) {
    153151            if ( strpos( $group['name'], $parent_group['name'] . '-' ) === 0 ) {
    154                 if ( ! in_array( $_POST['user_id'], $group['members'], true ) ) {
     152
     153                $group_members = api_call(
     154                    // https://api.slack.com/methods/conversations.members
     155                    'conversations.members',
     156                    [
     157                        'channel' => $group['id'],
     158                        'limit'   => 999,
     159                    ]
     160                );
     161
     162                if ( ! in_array( $_POST['user_id'], $group_members['members'], true ) ) {
    155163                    $groups_to_add[] = $group['name'];
    156164                }
Note: See TracChangeset for help on using the changeset viewer.