Making WordPress.org


Ignore:
Timestamp:
02/13/2023 11:31:27 PM (3 years ago)
Author:
iandunn
Message:

Props: Find Slack user IDs in bulleted lists

See https://wordpress.slack.com/archives/C02QB8GMM/p1676068080079459

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/common/includes/slack/props/lib.php

    r11927 r12396  
    88 * Adds props in Slack to w.org profiles.
    99 *
    10  * Receives webhook notifications for all new messages in `#props`,
     10 * Receives webhook notifications for all new messages in `#props`. See `dotorg/slack/props.php` for the caller.
    1111 */
    1212function handle_props_message( object $request ) : string {
     
    8787/**
    8888 * Parse the mentioned Slack user IDs from a message event.
    89  *
    90  * This assumes that the app is configured to escape usernames.
    9189 */
    9290function get_recipient_slack_ids( array $blocks ) : array {
     
    9593    foreach ( $blocks as $block ) {
    9694        foreach ( $block->elements as $element ) {
    97             foreach ( $element->elements as $inner_element ) {
    98                 if ( 'user' !== $inner_element->type ) {
    99                     continue;
    100                 }
    101 
    102                 $ids[] = $inner_element->user_id;
    103             }
     95            $ids = array_merge( $ids, get_user_ids_from_element( $element ) );
    10496        }
    10597    }
     
    10799    return array_unique( $ids );
    108100}
     101
     102/**
     103 * Recursively parse any mentioned Slack user IDs from a message element.
     104 *
     105 * This assumes that the app is configured to escape usernames.
     106 */
     107function get_user_ids_from_element( object $element ) : array {
     108    $ids = array();
     109
     110    if ( 'user' === $element->type ) {
     111        $ids[] = $element->user_id;
     112    }
     113
     114    if ( isset( $element->elements ) ) {
     115        foreach ( $element->elements as $inner_element ) {
     116            $ids = array_merge( $ids, get_user_ids_from_element( $inner_element ) );
     117        }
     118    }
     119
     120    return $ids;
     121}
     122
    109123
    110124/**
Note: See TracChangeset for help on using the changeset viewer.