Skip to content

Commit

Permalink
Merge pull request #3925 from ushahidi/twitter-urls-fix
Browse files Browse the repository at this point in the history
fix(Twitter): urls are now formatted with the std https://twitter.com…
  • Loading branch information
AmTryingMyBest authored Apr 21, 2020
2 parents 3037be1 + 3241f62 commit 8c4e0f9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
48 changes: 48 additions & 0 deletions migrations/20200409232153_rewrite_twitter_urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Phinx\Migration\AbstractMigration;

class RewriteTwitterUrls extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
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'";
$this->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);
}
}
13 changes: 11 additions & 2 deletions src/App/DataSource/Twitter/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/App/DataSource/TwitterDataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 8c4e0f9

Please sign in to comment.