From 1aef7ab22256f676d7991ddceffe07f85b5774e6 Mon Sep 17 00:00:00 2001 From: rowasc Date: Thu, 9 Apr 2020 23:48:01 +0000 Subject: [PATCH 1/8] fix(Twitter): urls are now formatted with the std https://twitter.com/{userid}/status/{tweetId} - This change was necessary because the old /statuses path stopped working at some point, which means all the twitter urls we grabbed from that point onwards were broken. Not super great - There doesn't seem to be any way to get the correct URL from the API response we get from /search API, which is very dissapointing because if the format changes again this will stop working. The only thing that saves us here is that because this is a more user facing url, its likely that it will be redirected even if it changes, rather than just stop working --- .../20200409232153_rewrite_twitter_urls.php | 38 +++++++++++++++++++ src/App/DataSource/Twitter/Twitter.php | 11 +++++- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 migrations/20200409232153_rewrite_twitter_urls.php diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php new file mode 100644 index 0000000000..2623f6bfb4 --- /dev/null +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -0,0 +1,38 @@ +getAdapter()->getConnection(); + $update = $pdo->prepare($sql); + $update->execute(); + } +} diff --git a/src/App/DataSource/Twitter/Twitter.php b/src/App/DataSource/Twitter/Twitter.php index a75315d7db..f518d391cc 100644 --- a/src/App/DataSource/Twitter/Twitter.php +++ b/src/App/DataSource/Twitter/Twitter.php @@ -187,13 +187,20 @@ public function fetch($limit = false) ) { continue; } + $user_id = $user['id_str']; // @todo Check for similar messages in the database before saving $messages[] = [ 'type' => MessageType::TWITTER, 'contact_type' => Contact::TWITTER, - 'from' => $user['id_str'], + 'from' => $user_id, 'to' => null, - 'message' => 'https://twitter.com/statuses/' . $id, + /** + * Best compromise I could find was just make the proper urls with user_id rather than only + * tweet id (for which there is an unofficial formula)... since there doesn't seem to be a way to grab the URL from the + * API itself in the v1.1 search endpoint. + * Fun fact: if the user id is wrong, twitter still takes you to the correct Tweet... they just use the tweet id + **/ + 'message' => "https://twitter.com/$user_id/status/$id", 'title' => 'From twitter on ' . $date, 'datetime' => $date, 'data_source_message_id' => $id, From 9d56472f9e4e1c95722816d8e5e8a03131eda94b Mon Sep 17 00:00:00 2001 From: rowasc Date: Thu, 9 Apr 2020 23:52:27 +0000 Subject: [PATCH 2/8] fix(Twitter): urls are now formatted with the std https://twitter.com/{userid}/status/{tweetId} - use execute --- migrations/20200409232153_rewrite_twitter_urls.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php index 2623f6bfb4..c829dad8ee 100644 --- a/migrations/20200409232153_rewrite_twitter_urls.php +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -31,8 +31,6 @@ public function change() INNER JOIN contacts on contacts.id = messages.contact_id SET messages.message = REPLACE(messages.message, concat('https://twitter.com/statuses/', messages.data_source_message_id), concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id)) WHERE `messages`.`type` = 'twitter'"; - $pdo = $this->getAdapter()->getConnection(); - $update = $pdo->prepare($sql); - $update->execute(); + $this->execute($sql); } } From bd2280c2bd739f4758bb799ee93899d549b06b31 Mon Sep 17 00:00:00 2001 From: rowasc Date: Thu, 9 Apr 2020 23:55:17 +0000 Subject: [PATCH 3/8] fix(Twitter): urls are now formatted with the std https://twitter.com/{userid}/status/{tweetId} - use execute --- migrations/20200409232153_rewrite_twitter_urls.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php index c829dad8ee..31d699ad56 100644 --- a/migrations/20200409232153_rewrite_twitter_urls.php +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -25,7 +25,7 @@ class RewriteTwitterUrls extends AbstractMigration * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ - public function change() + public function up() { $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id @@ -33,4 +33,12 @@ public function change() WHERE `messages`.`type` = 'twitter'"; $this->execute($sql); } + + public function down() { + $sql = "UPDATE messages + INNER JOIN contacts on contacts.id = messages.contact_id + SET messages.message = REPLACE(messages.message, concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id), concat('https://twitter.com/statuses/', messages.data_source_message_id)) + WHERE `messages`.`type` = 'twitter'"; + $this->execute($sql); + } } From 12e2cedb34932ecc3a0eb643e2a055bf3c920bdc Mon Sep 17 00:00:00 2001 From: rowasc Date: Fri, 10 Apr 2020 02:23:05 +0000 Subject: [PATCH 4/8] Fix twitter test with new tweet url --- tests/unit/App/DataSource/TwitterDataSourceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/App/DataSource/TwitterDataSourceTest.php b/tests/unit/App/DataSource/TwitterDataSourceTest.php index 31cb2d908f..6c7d73121d 100644 --- a/tests/unit/App/DataSource/TwitterDataSourceTest.php +++ b/tests/unit/App/DataSource/TwitterDataSourceTest.php @@ -271,7 +271,7 @@ function ($a, $b, $c, $d) use ($mockTwitterOAuth) { 'type' => 'twitter', 'contact_type' => 'twitter', 'from' => '1112222223', // twitter user id - 'message' => 'https://twitter.com/statuses/abc123', + 'message' => 'https://twitter.com/1112222223/status/abc123', 'to' => null, 'title' => 'From twitter on Thu Apr 06 15:24:15 +0000 2017', 'data_source_message_id' => 'abc123', @@ -282,7 +282,7 @@ function ($a, $b, $c, $d) use ($mockTwitterOAuth) { 'type' => 'twitter', 'contact_type' => 'twitter', 'from' => '1112222225', // twitter user id - 'message' => 'https://twitter.com/statuses/abc125', + 'message' => 'https://twitter.com/1112222225/status/abc125', 'to' => null, 'title' => 'From twitter on Thu Apr 06 15:24:15 +0000 2017', 'data_source_message_id' => 'abc125', @@ -293,7 +293,7 @@ function ($a, $b, $c, $d) use ($mockTwitterOAuth) { 'type' => 'twitter', 'contact_type' => 'twitter', 'from' => '12344494949', //twitter user id - 'message' => 'https://twitter.com/statuses/abc126', + 'message' => 'https://twitter.com/12344494949/status/abc126', 'to' => null, 'title' => 'From twitter on Thu Apr 06 15:24:15 +0000 2017', 'data_source_message_id' => 'abc126', From de2adf21d2b12ecb1c3869852de888b92df7093c Mon Sep 17 00:00:00 2001 From: Romina Date: Thu, 16 Apr 2020 08:40:54 -0300 Subject: [PATCH 5/8] Fix style issues --- src/App/DataSource/Twitter/Twitter.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/App/DataSource/Twitter/Twitter.php b/src/App/DataSource/Twitter/Twitter.php index f518d391cc..bd44c99a88 100644 --- a/src/App/DataSource/Twitter/Twitter.php +++ b/src/App/DataSource/Twitter/Twitter.php @@ -195,10 +195,12 @@ public function fetch($limit = false) 'from' => $user_id, 'to' => null, /** - * Best compromise I could find was just make the proper urls with user_id rather than only - * tweet id (for which there is an unofficial formula)... since there doesn't seem to be a way to grab the URL from the - * API itself in the v1.1 search endpoint. - * Fun fact: if the user id is wrong, twitter still takes you to the correct Tweet... they just use the tweet id + * Best compromise I could find was just make the proper urls with user_id rather than only + * tweet id (for which there is an unofficial formula)... + * since there doesn't seem to be a way to grab the URL from the + * API itself in the v1.1 search endpoint. + * Fun fact: if the user id is wrong, twitter + * still takes you to the correct Tweet... they just use the tweet id **/ 'message' => "https://twitter.com/$user_id/status/$id", 'title' => 'From twitter on ' . $date, From 97a8399dbd51cd115fb8748607b62b9897c55086 Mon Sep 17 00:00:00 2001 From: Romina Date: Thu, 16 Apr 2020 09:00:07 -0300 Subject: [PATCH 6/8] Fix style issues --- src/App/DataSource/Twitter/Twitter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/DataSource/Twitter/Twitter.php b/src/App/DataSource/Twitter/Twitter.php index bd44c99a88..111ec2fe09 100644 --- a/src/App/DataSource/Twitter/Twitter.php +++ b/src/App/DataSource/Twitter/Twitter.php @@ -194,7 +194,7 @@ public function fetch($limit = false) 'contact_type' => Contact::TWITTER, 'from' => $user_id, 'to' => null, - /** + /** * Best compromise I could find was just make the proper urls with user_id rather than only * tweet id (for which there is an unofficial formula)... * since there doesn't seem to be a way to grab the URL from the From 3769936744eac4e69320ba954a43070af0a0a6b1 Mon Sep 17 00:00:00 2001 From: Romina Suarez Date: Fri, 17 Apr 2020 06:54:39 -0300 Subject: [PATCH 7/8] Fix lint --- .../20200409232153_rewrite_twitter_urls.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php index 31d699ad56..2428f32d37 100644 --- a/migrations/20200409232153_rewrite_twitter_urls.php +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -27,18 +27,21 @@ class RewriteTwitterUrls extends AbstractMigration */ public function up() { - $sql = "UPDATE messages - INNER JOIN contacts on contacts.id = messages.contact_id - SET messages.message = REPLACE(messages.message, concat('https://twitter.com/statuses/', messages.data_source_message_id), concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id)) - WHERE `messages`.`type` = 'twitter'"; + $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id " . + "SET messages.message = " . + "REPLACE(messages.message, concat('https://twitter.com/statuses/', messages.data_source_message_id), " . + "concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id)) " . + "WHERE `messages`.`type` = 'twitter'"; $this->execute($sql); } public function down() { - $sql = "UPDATE messages - INNER JOIN contacts on contacts.id = messages.contact_id - SET messages.message = REPLACE(messages.message, concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id), concat('https://twitter.com/statuses/', messages.data_source_message_id)) - WHERE `messages`.`type` = 'twitter'"; + // phpcs:ignore + $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id " . + "SET messages.message = REPLACE(messages.message, " . + "concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id), " . + "concat('https://twitter.com/statuses/', messages.data_source_message_id)) " . + "WHERE `messages`.`type` = 'twitter'"; $this->execute($sql); } } From bf7598e5a10f229c1b60ee238f322de918352732 Mon Sep 17 00:00:00 2001 From: Romina Suarez Date: Fri, 17 Apr 2020 11:11:45 -0300 Subject: [PATCH 8/8] Fix lint --- migrations/20200409232153_rewrite_twitter_urls.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php index 2428f32d37..2b25731fed 100644 --- a/migrations/20200409232153_rewrite_twitter_urls.php +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -35,7 +35,8 @@ public function up() $this->execute($sql); } - public function down() { + public function down() + { // phpcs:ignore $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id " . "SET messages.message = REPLACE(messages.message, " .