diff --git a/migrations/20200409232153_rewrite_twitter_urls.php b/migrations/20200409232153_rewrite_twitter_urls.php new file mode 100644 index 0000000000..2b25731fed --- /dev/null +++ b/migrations/20200409232153_rewrite_twitter_urls.php @@ -0,0 +1,48 @@ +execute($sql); + } + + public function down() + { + // 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); + } +} diff --git a/src/App/DataSource/Twitter/Twitter.php b/src/App/DataSource/Twitter/Twitter.php index a75315d7db..111ec2fe09 100644 --- a/src/App/DataSource/Twitter/Twitter.php +++ b/src/App/DataSource/Twitter/Twitter.php @@ -187,13 +187,22 @@ 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, 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',