Making WordPress.org


Ignore:
Timestamp:
09/09/2026 10:34:02 AM (2 months ago)
Author:
paulkevan
Message:

Slack: update subgroup command to allow channels to be displayed for all members.

File:
1 edited

Legend:

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

    r14848 r15161  
    1515
    1616function invalidate_channel_lists() {
    17         wp_cache_delete( 'bot_active_channels', CACHE_GROUP );
     17        wp_cache_delete( 'active_channels', CACHE_GROUP );
    1818        wp_cache_delete( 'archived_channels', CACHE_GROUP );
    1919}
    … …  
    846846
    847847function list_all_private_channels( $include_archived = false ) {
    848         $active = wp_cache_get( 'bot_active_channels', CACHE_GROUP );
     848        $active = wp_cache_get( 'active_channels', CACHE_GROUP );
    849849        if ( false === $active ) {
    850                 $r      = api_call( 'conversations.list', [
     850                // The bot token only lists channels the bot was invited to. Merge in the
     851                // owner's listing, as the archived branch below already does.
     852                $params = [
    851853                        'exclude_archived' => true,
    852854                        'types'            => 'private_channel',
    853855                        'limit'            => 999,
     856                ];
     857                [ $bot_active, $user_active ] = api_multi_call( [
     858                        [ 'conversations.list', $params, null ],
     859                        [ 'conversations.list', $params, SUBGROUP_USER_TOKEN ],
    854860                ] );
    855                 $active = $r['channels'] ?? [];
    856                 wp_cache_set( 'bot_active_channels', $active, CACHE_GROUP, CACHE_TTL_ACTIVE_CHANNELS );
     861
     862                // Tag what the bot is in: it cannot act on anything else.
     863                $active = [];
     864                foreach ( $bot_active['channels'] ?? [] as $g ) {
     865                        $g['bot_member']    = true;
     866                        $active[ $g['id'] ] = $g;
     867                }
     868                foreach ( $user_active['channels'] ?? [] as $g ) {
     869                        if ( ! isset( $active[ $g['id'] ] ) ) {
     870                                $g['bot_member']    = false;
     871                                $active[ $g['id'] ] = $g;
     872                        }
     873                }
     874                $active = array_values( $active );
     875                wp_cache_set( 'active_channels', $active, CACHE_GROUP, CACHE_TTL_ACTIVE_CHANNELS );
    857876        }
    858877
    … …  
    879898                foreach ( $bot['channels'] ?? [] as $g ) {
    880899                        if ( ! empty( $g['is_archived'] ) ) {
     900                                $g['bot_member']      = true;
    881901                                $archived[ $g['id'] ] = $g;
    882902                        }
    … …  
    884904                foreach ( $user['channels'] ?? [] as $g ) {
    885905                        if ( ! empty( $g['is_archived'] ) && ! isset( $archived[ $g['id'] ] ) ) {
     906                                $g['bot_member']      = false;
    886907                                $archived[ $g['id'] ] = $g;
    887908                        }
    … …  
    907928                if ( $g['id'] === $channel_id ) {
    908929                        return $g;
     930                }
     931        }
     932        return null;
     933}
     934
     935/*
     936 * Read member lists with the bot token where the bot is a member, and with the
     937 * owner token everywhere else.
     938 */
     939function members_token( $channel_id ) {
     940        foreach ( list_all_private_channels() as $g ) {
     941                if ( $g['id'] === $channel_id ) {
     942                        return empty( $g['bot_member'] ) ? SUBGROUP_USER_TOKEN : null;
    909943                }
    910944        }
    … …  
    929963        $calls = [];
    930964        foreach ( $to_fetch as $id ) {
    931                 $calls[] = [ 'conversations.members', [ 'channel' => $id, 'limit' => 999 ], null ];
     965                $calls[] = [ 'conversations.members', [ 'channel' => $id, 'limit' => 999 ], members_token( $id ) ];
    932966        }
    933967        $results = api_multi_call( $calls );
    … …  
    945979                        'channel' => $channel_id,
    946980                        'limit'   => 999,
    947                 ] );
     981                ], members_token( $channel_id ) );
    948982                $members = $r['members'] ?? [];
    949983                wp_cache_set( $key, $members, CACHE_GROUP, CACHE_TTL_MEMBERS );
    … …  
    10391073                                'text' => [ 'type' => 'mrkdwn', 'text' => implode( "\n", $lines ) ],
    10401074                        ];
    1041                         if ( ! $is_member ) {
     1075                        // Joining runs as the bot, so only offer it where the bot can invite.
     1076                        if ( ! $is_member && ! empty( $g['bot_member'] ) ) {
    10421077                                $block['accessory'] = [
    10431078                                        'type'      => 'button',
    … …  
    11291164                                'text' => [ 'type' => 'mrkdwn', 'text' => implode( "\n", $lines ) ],
    11301165                        ];
    1131                         $blocks[] = [
    1132                                 'type'     => 'actions',
    1133                                 'elements' => [
    1134                                         [
    1135                                                 'type'      => 'button',
    1136                                                 'text'      => [ 'type' => 'plain_text', 'text' => 'Rename' ],
    1137                                                 'action_id' => 'rename_subgroup',
    1138                                                 'value'     => $g['id'],
    1139                                         ],
    1140                                         [
    1141                                                 'type'      => 'button',
    1142                                                 'text'      => [ 'type' => 'plain_text', 'text' => 'Rotate' ],
    1143                                                 'action_id' => 'rotate_subgroup',
    1144                                                 'value'     => $g['id'],
    1145                                                 'confirm'   => [
    1146                                                         'title'   => [ 'type' => 'plain_text', 'text' => 'Rotate channel?' ],
    1147                                                         'text'    => [ 'type' => 'mrkdwn', 'text' => "Archive `{$g['name']}` with a year suffix and create a fresh copy with only you as a member." ],
    1148                                                         'confirm' => [ 'type' => 'plain_text', 'text' => 'Rotate' ],
    1149                                                         'deny'    => [ 'type' => 'plain_text', 'text' => 'Cancel' ],
     1166                        // Rename, Rotate and Archive act on the channel, so they need the bot in it.
     1167                        if ( ! empty( $g['bot_member'] ) ) {
     1168                                $blocks[] = [
     1169                                        'type'     => 'actions',
     1170                                        'elements' => [
     1171                                                [
     1172                                                        'type'      => 'button',
     1173                                                        'text'      => [ 'type' => 'plain_text', 'text' => 'Rename' ],
     1174                                                        'action_id' => 'rename_subgroup',
     1175                                                        'value'     => $g['id'],
     1176                                                ],
     1177                                                [
     1178                                                        'type'      => 'button',
     1179                                                        'text'      => [ 'type' => 'plain_text', 'text' => 'Rotate' ],
     1180                                                        'action_id' => 'rotate_subgroup',
     1181                                                        'value'     => $g['id'],
     1182                                                        'confirm'   => [
     1183                                                                'title'   => [ 'type' => 'plain_text', 'text' => 'Rotate channel?' ],
     1184                                                                'text'    => [ 'type' => 'mrkdwn', 'text' => "Archive `{$g['name']}` with a year suffix and create a fresh copy with only you as a member." ],
     1185                                                                'confirm' => [ 'type' => 'plain_text', 'text' => 'Rotate' ],
     1186                                                                'deny'    => [ 'type' => 'plain_text', 'text' => 'Cancel' ],
     1187                                                        ],
     1188                                                ],
     1189                                                [
     1190                                                        'type'      => 'button',
     1191                                                        'text'      => [ 'type' => 'plain_text', 'text' => 'Archive' ],
     1192                                                        'style'     => 'danger',
     1193                                                        'action_id' => 'archive_subgroup',
     1194                                                        'value'     => $g['id'],
     1195                                                        'confirm'   => [
     1196                                                                'title'   => [ 'type' => 'plain_text', 'text' => 'Archive channel?' ],
     1197                                                                'text'    => [ 'type' => 'mrkdwn', 'text' => "Archive `{$g['name']}`?" ],
     1198                                                                'confirm' => [ 'type' => 'plain_text', 'text' => 'Archive' ],
     1199                                                                'deny'    => [ 'type' => 'plain_text', 'text' => 'Cancel' ],
     1200                                                                'style'   => 'danger',
     1201                                                        ],
    11501202                                                ],
    11511203                                        ],
    1152                                         [
    1153                                                 'type'      => 'button',
    1154                                                 'text'      => [ 'type' => 'plain_text', 'text' => 'Archive' ],
    1155                                                 'style'     => 'danger',
    1156                                                 'action_id' => 'archive_subgroup',
    1157                                                 'value'     => $g['id'],
    1158                                                 'confirm'   => [
    1159                                                         'title'   => [ 'type' => 'plain_text', 'text' => 'Archive channel?' ],
    1160                                                         'text'    => [ 'type' => 'mrkdwn', 'text' => "Archive `{$g['name']}`?" ],
    1161                                                         'confirm' => [ 'type' => 'plain_text', 'text' => 'Archive' ],
    1162                                                         'deny'    => [ 'type' => 'plain_text', 'text' => 'Cancel' ],
    1163                                                         'style'   => 'danger',
    1164                                                 ],
    1165                                         ],
    1166                                 ],
    1167                         ];
     1204                                ];
     1205                        }
    11681206                }
    11691207        }
Note: See TracChangeset for help on using the changeset viewer.