Merge pull request #735 from MrPetovan/bug/4014-twitter-use-rich-text-quote-tweets

[twitter] Use rich text for quote tweet
This commit is contained in:
Michael Vogel 2018-09-12 18:58:20 +00:00 committed by GitHub
commit bec71230b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1138,27 +1138,34 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
$tags_arr = []; $tags_arr = [];
foreach ($item->entities->hashtags AS $hashtag) { foreach ($item->entities->hashtags AS $hashtag) {
$url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]"; $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($hashtag->text) . ']' . $hashtag->text . '[/url]';
$tags_arr["#" . $hashtag->text] = $url; $tags_arr['#' . $hashtag->text] = $url;
$body = str_replace("#" . $hashtag->text, $url, $body); $body = str_replace('#' . $hashtag->text, $url, $body);
} }
foreach ($item->entities->user_mentions AS $mention) { foreach ($item->entities->user_mentions AS $mention) {
$url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]"; $url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
$tags_arr["@" . $mention->screen_name] = $url; $tags_arr['@' . $mention->screen_name] = $url;
$body = str_replace("@" . $mention->screen_name, $url, $body); $body = str_replace('@' . $mention->screen_name, $url, $body);
} }
if (isset($item->entities->urls)) { if (isset($item->entities->urls)) {
$type = ""; $type = '';
$footerurl = ""; $footerurl = '';
$footerlink = ""; $footerlink = '';
$footer = ""; $footer = '';
foreach ($item->entities->urls as $url) { foreach ($item->entities->urls as $url) {
$plain = str_replace($url->url, '', $plain); $plain = str_replace($url->url, '', $plain);
if ($url->url && $url->expanded_url && $url->display_url) { if ($url->url && $url->expanded_url && $url->display_url) {
// Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
if (isset($item->quoted_status_id_str)
&& substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
$body = str_replace($url->url, '', $body);
continue;
}
$expanded_url = Network::finalUrl($url->expanded_url); $expanded_url = Network::finalUrl($url->expanded_url);
$oembed_data = OEmbed::fetchURL($expanded_url); $oembed_data = OEmbed::fetchURL($expanded_url);
@ -1167,34 +1174,29 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
continue; continue;
} }
// Quickfix: Workaround for URL with "[" and "]" in it // Quickfix: Workaround for URL with '[' and ']' in it
if (strpos($expanded_url, "[") || strpos($expanded_url, "]")) { if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
$expanded_url = $url->url; $expanded_url = $url->url;
} }
if ($type == "") { if ($type == '') {
$type = $oembed_data->type; $type = $oembed_data->type;
} }
if ($oembed_data->type == "video") { if ($oembed_data->type == 'video') {
//$body = str_replace($url->url,
// "[video]".$expanded_url."[/video]", $body);
//$dontincludemedia = true;
$type = $oembed_data->type; $type = $oembed_data->type;
$footerurl = $expanded_url; $footerurl = $expanded_url;
$footerlink = "[url=" . $expanded_url . "]" . $url->display_url . "[/url]"; $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
$body = str_replace($url->url, $footerlink, $body); $body = str_replace($url->url, $footerlink, $body);
//} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) { } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
} elseif (($oembed_data->type == "photo") && isset($oembed_data->url)) { $body = str_replace($url->url, '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]', $body);
$body = str_replace($url->url, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body); } elseif ($oembed_data->type != 'link') {
//$dontincludemedia = true; $body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
} elseif ($oembed_data->type != "link") {
$body = str_replace($url->url, "[url=" . $expanded_url . "]" . $url->display_url . "[/url]", $body);
} else { } else {
$img_str = Network::fetchUrl($expanded_url, true, $redirects, 4); $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
$tempfile = tempnam(get_temppath(), "cache"); $tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str); file_put_contents($tempfile, $img_str);
// See http://php.net/manual/en/function.exif-imagetype.php#79283 // See http://php.net/manual/en/function.exif-imagetype.php#79283
@ -1206,14 +1208,13 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
unlink($tempfile); unlink($tempfile);
if (substr($mime, 0, 6) == "image/") { if (substr($mime, 0, 6) == 'image/') {
$type = "photo"; $type = 'photo';
$body = str_replace($url->url, "[img]" . $expanded_url . "[/img]", $body); $body = str_replace($url->url, '[img]' . $expanded_url . '[/img]', $body);
//$dontincludemedia = true;
} else { } else {
$type = $oembed_data->type; $type = $oembed_data->type;
$footerurl = $expanded_url; $footerurl = $expanded_url;
$footerlink = "[url=" . $expanded_url . "]" . $url->display_url . "[/url]"; $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
$body = str_replace($url->url, $footerlink, $body); $body = str_replace($url->url, $footerlink, $body);
} }
@ -1221,33 +1222,36 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
} }
} }
if ($footerurl != "") { // Footer will be taken care of with a share block in the case of a quote
if (empty($item->quoted_status)) {
if ($footerurl != '') {
$footer = add_page_info($footerurl, false, $picture); $footer = add_page_info($footerurl, false, $picture);
} }
if (($footerlink != "") && (trim($footer) != "")) { if (($footerlink != '') && (trim($footer) != '')) {
$removedlink = trim(str_replace($footerlink, "", $body)); $removedlink = trim(str_replace($footerlink, '', $body));
if (($removedlink == "") || strstr($body, $removedlink)) { if (($removedlink == '') || strstr($body, $removedlink)) {
$body = $removedlink; $body = $removedlink;
} }
$body .= $footer; $body .= $footer;
} }
if (($footer == "") && ($picture != "")) { if ($footer == '' && $picture != '') {
$body .= "\n\n[img]" . $picture . "[/img]\n"; $body .= "\n\n[img]" . $picture . "[/img]\n";
} elseif (($footer == "") && ($picture == "")) { } elseif ($footer == '' && $picture == '') {
$body = add_page_info_to_body($body); $body = add_page_info_to_body($body);
} }
} }
}
// it seems as if the entities aren't always covering all mentions. So the rest will be checked here // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
$tags = get_tags($body); $tags = get_tags($body);
if (count($tags)) { if (count($tags)) {
foreach ($tags as $tag) { foreach ($tags as $tag) {
if (strstr(trim($tag), " ")) { if (strstr(trim($tag), ' ')) {
continue; continue;
} }
@ -1267,7 +1271,7 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
$basetag = str_replace('_', ' ', substr($tag, 1)); $basetag = str_replace('_', ' ', substr($tag, 1));
$url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
$body = str_replace($tag, $url, $body); $body = str_replace($tag, $url, $body);
$tags_arr["#" . $basetag] = $url; $tags_arr['#' . $basetag] = $url;
} elseif (strpos($tag, '@') === 0) { } elseif (strpos($tag, '@') === 0) {
if (strpos($tag, '[url=')) { if (strpos($tag, '[url=')) {
continue; continue;
@ -1276,14 +1280,14 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
$basetag = substr($tag, 1); $basetag = substr($tag, 1);
$url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
$body = str_replace($tag, $url, $body); $body = str_replace($tag, $url, $body);
$tags_arr["@" . $basetag] = $url; $tags_arr['@' . $basetag] = $url;
} }
} }
} }
$tags = implode($tags_arr, ","); $tags = implode($tags_arr, ',');
return ["body" => $body, "tags" => $tags, "plain" => $plain]; return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
} }
/** /**
@ -1451,7 +1455,7 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
$postarray['allow_cid'] = ''; $postarray['allow_cid'] = '';
} }
if (is_string($post->full_text)) { if (!empty($post->full_text)) {
$postarray['body'] = $post->full_text; $postarray['body'] = $post->full_text;
} else { } else {
$postarray['body'] = $post->text; $postarray['body'] = $post->text;
@ -1510,8 +1514,6 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
return []; return [];
} }
$postarray['body'] = $statustext;
$postarray['body'] .= "\n" . share_header( $postarray['body'] .= "\n" . share_header(
$quoted['author-name'], $quoted['author-name'],
$quoted['author-link'], $quoted['author-link'],