Permalink
Browse files

fix(slacknotes): handle slack-resolved URLs in pipes format

  • Loading branch information...
eteubert committed Jan 13, 2019
1 parent 5b39746 commit b08ec53d267b82b0a023c116f27ecaee8f83af14
Showing with 62 additions and 35 deletions.
  1. +6 −3 lib/modules/slack_shownotes/message.php
  2. +56 −32 tests/slack_message_test.php
@@ -8,10 +8,13 @@ public static function extract_links($message)
preg_match_all("/<(http[^>]*)>/", $message["text"], $links);
return array_reduce($links[1], function ($agg, $url) use ($message) {
$url_segments = explode("|", $url);
$canonical_url = $url_segments[0];
$agg[] = [
"link" => $url,
"title" => self::get_url_title_via_attachment($url, $message),
"source" => self::get_source_via_attachment($url, $message),
"link" => $canonical_url,
"title" => self::get_url_title_via_attachment($canonical_url, $message),
"source" => self::get_source_via_attachment($canonical_url, $message),
];
return $agg;
}, []);
@@ -2,10 +2,11 @@
use \Podlove\Modules\SlackShownotes\Message;
class SlackMessageTest extends WP_UnitTestCase {
public function testExtractLinkMatchedWithAttachment()
{
$data = <<<EOT
class SlackMessageTest extends WP_UnitTestCase
{
public function testExtractLinkMatchedWithAttachment()
{
$data = <<<EOT
{
"client_msg_id": "a651c38b-2bc3-4ce3-8a48-2b131af24887",
"type": "message",
@@ -31,18 +32,18 @@ public function testExtractLinkMatchedWithAttachment()
}
EOT;
$message = json_decode($data, true);
$result = Message::extract_links($message);
$message = json_decode($data, true);
$result = Message::extract_links($message);
$this->assertEquals(count($result), 1);
$this->assertEquals($result[0]['link'], "https://freakshow.fm/fs228-letty-im-datenteich");
$this->assertEquals($result[0]['title'], "FS228 Letty im Datenteich");
$this->assertEquals($result[0]['source'], "Freak Show");
}
$this->assertEquals(count($result), 1);
$this->assertEquals($result[0]['link'], "https://freakshow.fm/fs228-letty-im-datenteich");
$this->assertEquals($result[0]['title'], "FS228 Letty im Datenteich");
$this->assertEquals($result[0]['source'], "Freak Show");
}
public function testLinkWithMissingAttachment()
{
$data = <<<EOT
public function testLinkWithMissingAttachment()
{
$data = <<<EOT
{
"client_msg_id": "5510681d-1c33-41aa-a0ee-62779cd9e8ad",
"type": "message",
@@ -52,18 +53,41 @@ public function testLinkWithMissingAttachment()
}
EOT;
$message = json_decode($data, true);
$result = Message::extract_links($message);
$message = json_decode($data, true);
$result = Message::extract_links($message);
$this->assertEquals(count($result), 1);
$this->assertEquals($result[0]['link'], "http://www.spiegel.de/politik/ausland/kongos-regierung-kappt-das-internet-nach-praesidentenwahl-a-1245955.html");
$this->assertEquals($result[0]['title'], null);
$this->assertEquals($result[0]['source'], 'spiegel.de');
}
public function testLinkWithPipes()
{
// when Slack detects something that looks like an URL it makes a canonical version
// for example: golem.de is expanded to <https://golem.de|golem.de>
$data = <<<EOT
{
"client_msg_id": "5510681d-1c33-41aa-a0ee-62779cd9e8ad",
"type": "message",
"text": "Link mit pipe <https://golem.de|golem.de>",
"user": "UF53JT12T",
"ts": "1546266336.001000"
}
EOT;
$message = json_decode($data, true);
$result = Message::extract_links($message);
$this->assertEquals(count($result), 1);
$this->assertEquals($result[0]['link'], "http://www.spiegel.de/politik/ausland/kongos-regierung-kappt-das-internet-nach-praesidentenwahl-a-1245955.html");
$this->assertEquals($result[0]['title'], NULL);
$this->assertEquals($result[0]['source'], 'spiegel.de');
}
$this->assertEquals(count($result), 1);
$this->assertEquals($result[0]['link'], "https://golem.de");
$this->assertEquals($result[0]['title'], null);
$this->assertEquals($result[0]['source'], 'golem.de');
}
public function testMultipleLinksWithAttachments()
{
$data = <<<EOT
public function testMultipleLinksWithAttachments()
{
$data = <<<EOT
{
"client_msg_id": "7b5c8c33-be8f-4974-84ea-14ccbc4bc4aa",
"type": "message",
@@ -103,13 +127,13 @@ public function testMultipleLinksWithAttachments()
}
EOT;
$message = json_decode($data, true);
$result = Message::extract_links($message);
$message = json_decode($data, true);
$result = Message::extract_links($message);
$this->assertEquals(count($result), 2);
$this->assertEquals($result[0]['link'], "https://www.zeit.de/gesellschaft/zeitgeschehen/2018-12/vatikan-papst-franziskus-sprecher-ruecktritt");
$this->assertEquals($result[0]['title'], "Vatikan: Sprecher von Papst Franziskus treten zurück");
$this->assertEquals($result[1]['link'], "https://www.zeit.de/2019/01/demokratieverdrossenheit-misstrauen-aufschwung-buerger-generationenkonflikt");
$this->assertEquals($result[1]['title'], "Demokratieverdrossenheit: Warum trauen so viele der Demokratie nicht, obwohl wir einen Aufschwung erleben?");
}
$this->assertEquals(count($result), 2);
$this->assertEquals($result[0]['link'], "https://www.zeit.de/gesellschaft/zeitgeschehen/2018-12/vatikan-papst-franziskus-sprecher-ruecktritt");
$this->assertEquals($result[0]['title'], "Vatikan: Sprecher von Papst Franziskus treten zurück");
$this->assertEquals($result[1]['link'], "https://www.zeit.de/2019/01/demokratieverdrossenheit-misstrauen-aufschwung-buerger-generationenkonflikt");
$this->assertEquals($result[1]['title'], "Demokratieverdrossenheit: Warum trauen so viele der Demokratie nicht, obwohl wir einen Aufschwung erleben?");
}
}

0 comments on commit b08ec53

Please sign in to comment.