From 2f91ce87b7017078a44169cd7d51afdad8e25d4b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 17 Oct 2018 01:14:57 -0400 Subject: [PATCH 1/3] [twitter] Add custom handling for mentions --- twitter/twitter.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index 9f07fa52c..b17d9ad19 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -553,6 +553,9 @@ function twitter_post_hook(App $a, array &$b) $connection->setTimeouts(10, 30); $max_char = 280; + + $b['body'] = twitter_update_mentions($b['body']); + $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8); $msg = $msgarr["text"]; @@ -1859,3 +1862,23 @@ function twitter_is_retweet(App $a, $uid, $body) return !isset($result->errors); } + +function twitter_update_mentions($body) +{ + $URLSearchString = "^\[\]"; + $return = preg_replace_callback( + "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + function ($matches) { + if (strpos($matches[1], 'twitter.com')) { + $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1); + } else { + $return = $matches[2] . ' (' . $matches[1] . ')'; + } + + return $return; + }, + $body + ); + + return $return; +} From 8153276e63ad4c65eeeaa8ca323f3e5419a56eb4 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 17 Oct 2018 01:15:15 -0400 Subject: [PATCH 2/3] [twitter] Add custom handling for foreign reshares --- twitter/twitter.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index b17d9ad19..1cb716259 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -554,6 +554,14 @@ function twitter_post_hook(App $a, array &$b) $max_char = 280; + // Handling non-native reshares + $b['body'] = Friendica\Content\Text\BBCode::convertShare( + $b['body'], + function (array $attributes, array $author_contact, $content) { + return twitter_convert_share($attributes, $author_contact, $content); + } + ); + $b['body'] = twitter_update_mentions($b['body']); $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8); @@ -1882,3 +1890,14 @@ function twitter_update_mentions($body) return $return; } + +function twitter_convert_share(array $attributes, array $author_contact, $content) +{ + if ($author_contact['network'] == Protocol::TWITTER) { + $mention = '@' . $author_contact['nickname']; + } else { + $mention = Protocol::formatMention($attributes['profile'], $attributes['author']); + } + + return 'RT ' . $mention . ': ' . $content; +} From 4eff1447659b006d920a73051d6969e9052bae9a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 17 Oct 2018 08:19:47 -0400 Subject: [PATCH 3/3] [twitter] Add support for $is_quote_share in twitter_convert_share --- twitter/twitter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 1cb716259..c69a1f816 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1891,7 +1891,7 @@ function twitter_update_mentions($body) return $return; } -function twitter_convert_share(array $attributes, array $author_contact, $content) +function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share) { if ($author_contact['network'] == Protocol::TWITTER) { $mention = '@' . $author_contact['nickname']; @@ -1899,5 +1899,5 @@ function twitter_convert_share(array $attributes, array $author_contact, $conten $mention = Protocol::formatMention($attributes['profile'], $attributes['author']); } - return 'RT ' . $mention . ': ' . $content; + return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link']; }