<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[11927] sites/trunk/common/includes/slack/props: Profiles: Include links in Slack props messages.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/11927">11927</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/11927","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-06-29 21:38:19 +0000 (Wed, 29 Jun 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Profiles: Include links in Slack props messages.

Previously they were unintentionally stripped out, because Slack escapes them in brackets, like `<http://example.org>`. That caused them to get stripped out by `handle_props_given()` in `wporg-profiles-activity-handler.php`.

For example, https://wordpress.slack.com/archives/C0FRG66LR/p1655471187798639.

This refactors `prepare_message()` to use the structured message blocks, rather than the concatenated text. That makes it easier to un-escape each part of the message according to its type.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkcommonincludesslackpropslibphp">sites/trunk/common/includes/slack/props/lib.php</a></li>
<li><a href="#sitestrunkcommonincludesslackpropsteststestlibphp">sites/trunk/common/includes/slack/props/tests/test-lib.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkcommonincludesslackpropslibphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/common/includes/slack/props/lib.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/common/includes/slack/props/lib.php   2022-06-29 20:09:42 UTC (rev 11926)
+++ sites/trunk/common/includes/slack/props/lib.php     2022-06-29 21:38:19 UTC (rev 11927)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -45,8 +45,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // This is Slack's unintuitive way of giving messages a unique ID :|
</span><span class="cx" style="display: block; padding: 0 10px">        // https://api.slack.com/messaging/retrieving#individual_messages
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $message_id = sprintf( '%s-%s', $request->event->channel, $request->event->ts );
-       $message    = prepare_message( $request->event->text, $recipient_users );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $message_id    = sprintf( '%s-%s', $request->event->channel, $request->event->ts );
+       $channel_names = map_slack_channel_ids_to_names( $request->event->text );
+       $message       = prepare_message( $request->event->blocks[0]->elements[0]->elements, $recipient_users, $channel_names );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        add_activity_to_profile( compact( 'giver_user', 'recipient_ids', 'url', 'message_id', 'message' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -142,18 +143,61 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse Slack channel names and IDs out of message.
+ *
+ * This avoids having to make an API call to Slack to fetch the channels, like their docs recommend.
+ */
+function map_slack_channel_ids_to_names( string $message ) : array {
+       $map = array();
+
+       preg_match_all( '/<#(\w*\d*)\|([\w-]*)>/m', $message, $matches, PREG_SET_ORDER );
+
+       foreach ( $matches as $match ) {
+               $map[ $match[1] ] = $match[2];
+       }
+
+       return $map;
+}
+
+
+/**
+ * Prepare message to be sent to the Profiles API.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * Replace Slack IDs with w.org usernames, to better fit w.org profiles.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Un-escape URLs and other things Slack has escaped.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function prepare_message( string $original, array $user_map ) : string {
-       $search  = array();
-       $replace = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function prepare_message( array $elements, array $user_map, array $channel_map ) : string {
+       $prepared = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        foreach ( $user_map as $slack_id => $wporg_user ) {
-               $search[]  = sprintf( '<@%s>', $slack_id );
-               $replace[] = '@' . $wporg_user['user_login'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $elements as $element ) {
+               switch ( $element->type ) {
+                       case 'text':
+                               $prepared .= $element->text;
+                       break;
+
+                       case 'link':
+                               $prepared .= $element->url;
+                       break;
+
+                       case 'emoji':
+                               $prepared .= ":{$element->name}:";
+                       break;
+
+                       case 'user':
+                               $prepared .= '@' . $user_map[ $element->user_id ]['user_login'];
+                       break;
+
+                       case 'channel':
+                               $prepared .= '#' . $channel_map[ $element->channel_id ];
+                       break;
+
+                       default:
+                               // Ignore
+                       break;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return str_replace( $search, $replace, $original );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $prepared;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="sitestrunkcommonincludesslackpropsteststestlibphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/common/includes/slack/props/tests/test-lib.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/common/includes/slack/props/tests/test-lib.php        2022-06-29 20:09:42 UTC (rev 11926)
+++ sites/trunk/common/includes/slack/props/tests/test-lib.php  2022-06-29 21:38:19 UTC (rev 11927)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3,7 +3,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> namespace Dotorg\Slack\Props\Tests;
</span><span class="cx" style="display: block; padding: 0 10px"> use wpdbStub;
</span><span class="cx" style="display: block; padding: 0 10px"> use PHPUnit\Framework\TestCase;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-use function Dotorg\Slack\Props\{ is_valid_props, get_recipient_slack_ids, map_slack_users_to_wporg, prepare_message };
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use function Dotorg\Slack\Props\{ is_valid_props, get_recipient_slack_ids, map_slack_users_to_wporg, map_slack_channel_ids_to_names, prepare_message };
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * @group slack
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -104,15 +104,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $valid_request = self::get_valid_request();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $valid_users = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'U02RR6SGY',
-                                       'U02RQHNND',
-                                       'U3KJ0TK4L',
-                                       'U4L99HZB6',
-                                       'U024MFP4L',
-                                       'U6R2E3Y9Y',
-                                       'U023GFZJ07L',
-                                       'U1E5RLU1L',
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'U02RR6SGY',
+                       'U02RQHNND',
+                       'U3KJ0TK4L',
+                       'U4L99HZB6',
+                       'U024MFP4L',
+                       'U6R2E3Y9Y',
+                       'U023GFZJ07L',
+                       'U1E5RLU1L',
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $mentioned_twice = json_decode( json_encode( $valid_request->event->blocks ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $mentioned_twice[0]->elements[0]->elements[] = (object) array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -243,12 +243,49 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @covers ::map_slack_channel_ids_to_names
+        * @dataProvider data_map_slack_channel_ids_to_names
+        * @group unit
+        */
+       public function test_map_slack_channel_ids_to_names( string $message, array $expected ) : void {
+               $actual = map_slack_channel_ids_to_names( $message );
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public function data_map_slack_channel_ids_to_names() : array {
+               $cases = array(
+                       'none' => array(
+                               'message'  => 'thanks to <@U039G75HC> for great work on',
+                               'expected' => array(),
+                       ),
+
+                       'single team' => array(
+                               'message'  => 'thanks to <@U02RR7CQY> for great work on the <#C037W5S7X|community-team>',
+                               'expected' => array(
+                                       'C037W5S7X' => 'community-team',
+                               ),
+                       ),
+
+                       'multiple teams' => array(
+                               'message'  => 'thanks to <@U039G75HC> for great work in <#C037W5S7X|community-team> and <#C08M59V3P|meta-wordcamp>',
+                               'expected' => array(
+                                       'C037W5S7X' => 'community-team',
+                                       'C08M59V3P' => 'meta-wordcamp',
+                               ),
+                       ),
+               );
+
+               return $cases;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @covers ::prepare_message
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_prepare_message
</span><span class="cx" style="display: block; padding: 0 10px">         * @group unit
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_prepare_message( string $text, array $user_map, string $expected ) : void {
-               $actual = prepare_message( $text, $user_map );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_prepare_message( array $elements, array $user_map, array $channel_map, string $expected ) : void {
+               $actual = prepare_message( $elements, $user_map, $channel_map );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $expected, $actual );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,13 +295,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $cases = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'empty' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'text'     => '',
-                               'user_map' => array(),
-                               'expected' => '',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'elements'    => array(),
+                               'user_map'    => array(),
+                               'channel_map' => array(),
+                               'expected'    => '',
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        'valid' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'text' => $valid_request->event->text,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'elements' => $valid_request->event->blocks[0]->elements[0]->elements,
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'user_map' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'U023GFZJ07L' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'id' => 18752239,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -305,8 +344,75 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                'user_login' => 'webcommsat',
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               'channel_map' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'expected' => 'props to @Mamaduka for co-leading 5.9.3 RC 1, to @SergeyBiryukov for running mission control and to @davidbaumwald @pbiron @markjaquith @webcommsat @costdev @jeroenrotty for their help testing the release package :community: :wordpress:',
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       'escaped elements' => array(
+                               'elements' => array(
+                                       (object) array(
+                                               'type' => 'text',
+                                               'text' => 'Props to ',
+                                       ),
+                                       (object) array(
+                                               'type'    => 'user',
+                                               'user_id' => 'U04F2C6V5',
+                                       ),
+                                       (object) array(
+                                               'type' => 'text',
+                                               'text' => ' for title fix in ',
+                                       ),
+                                       (object) array(
+                                               'type' => 'link',
+                                               'url'  => 'https://github.com/WordPress/wordpress.org/pull/73',
+                                       ),
+                                       (object) array(
+                                               'type' => 'text',
+                                               'text' => ', and to ',
+                                       ),
+                                       (object) array(
+                                               'type'    => 'user',
+                                               'user_id' => 'U84ST75AL',
+                                       ),
+                                       (object) array(
+                                               'type' => 'text',
+                                               'text' => ' for facilitating the ',
+                                       ),
+                                       (object) array(
+                                               'type'       => 'channel',
+                                               'channel_id' => 'C037W5S7X',
+                                       ),
+                                       (object) array(
+                                               'type' => 'text',
+                                               'text' => ' triage ',
+                                       ),
+                                       (object) array(
+                                               'type'    => 'emoji',
+                                               'name'    => 'thank-you',
+                                       ),
+                                       (object) array(
+                                               'type'    => 'emoji',
+                                               'name'    => 'pizza',
+                                               'unicode' => '1f355',
+                                       ),
+                               ),
+
+                               'user_map' => array(
+                                       'U04F2C6V5' => array(
+                                               'user_login' => 'aurooba',
+                                       ),
+                                       'U84ST75AL' => array(
+                                               'user_login' => 'estelaris',
+                                       ),
+                               ),
+
+                               'channel_map' => array(
+                                       'C037W5S7X' => 'docs',
+                               ),
+
+                               'expected' => 'Props to @aurooba for title fix in https://github.com/WordPress/wordpress.org/pull/73, and to @estelaris for facilitating the #docs triage :thank-you::pizza:',
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $cases;
</span></span></pre>
</div>
</div>

</body>
</html>