mirror of
https://git.friendi.ca/friendica/friendica-addons.git
synced 2024-11-25 10:03:45 +00:00
[twitter] Rework twitter_expand_entities
- Uses Twitter-provided entity indices to avoid blanket string replacement and unwarranted tag search
This commit is contained in:
parent
96925a3a75
commit
1df61e8dc2
1 changed files with 117 additions and 139 deletions
|
@ -1202,38 +1202,65 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
|
||||||
return $contact_id;
|
return $contact_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function twitter_expand_entities(App $a, $body, $item, $picture)
|
/**
|
||||||
|
* Replaces Twitter entities with Friendica-friendly links.
|
||||||
|
*
|
||||||
|
* The Twitter API gives indices for each entity, which allows for fine-grained replacement.
|
||||||
|
*
|
||||||
|
* First, we need to collect everything that needs to be replaced, what we will replace it with, and the start index.
|
||||||
|
* Then we sort the indices decreasingly, and we replace from the end of the body to the start in order for the next
|
||||||
|
* index to be correct even after the last replacement.
|
||||||
|
*
|
||||||
|
* @param string $body
|
||||||
|
* @param stdClass $status
|
||||||
|
* @param string $picture
|
||||||
|
* @return array
|
||||||
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||||
|
*/
|
||||||
|
function twitter_expand_entities($body, stdClass $status, $picture)
|
||||||
{
|
{
|
||||||
$plain = $body;
|
$plain = $body;
|
||||||
|
|
||||||
$tags_arr = [];
|
$tags = [];
|
||||||
|
|
||||||
foreach ($item->entities->hashtags AS $hashtag) {
|
$replacementList = [];
|
||||||
$url = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
|
|
||||||
$tags_arr['#' . $hashtag->text] = $url;
|
foreach ($status->entities->hashtags AS $hashtag) {
|
||||||
$body = str_replace('#' . $hashtag->text, $url, $body);
|
$replace = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
|
||||||
|
$tags['#' . $hashtag->text] = $replace;
|
||||||
|
|
||||||
|
$replacementList[$hashtag->indices[0]] = [
|
||||||
|
'replace' => $replace,
|
||||||
|
'length' => $hashtag->indices[1] - $hashtag->indices[0],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($item->entities->user_mentions AS $mention) {
|
foreach ($status->entities->user_mentions AS $mention) {
|
||||||
$url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
|
$replace = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
|
||||||
$tags_arr['@' . $mention->screen_name] = $url;
|
$tags['@' . $mention->screen_name] = $replace;
|
||||||
$body = str_replace('@' . $mention->screen_name, $url, $body);
|
|
||||||
|
$replacementList[$mention->indices[0]] = [
|
||||||
|
'replace' => $replace,
|
||||||
|
'length' => $mention->indices[1] - $mention->indices[0],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($item->entities->urls)) {
|
// This URL if set will be used to add an attachment at the bottom of the post
|
||||||
$type = '';
|
$attachmentUrl = '';
|
||||||
$footerurl = '';
|
|
||||||
$footerlink = '';
|
|
||||||
$footer = '';
|
|
||||||
|
|
||||||
foreach ($item->entities->urls as $url) {
|
foreach ($status->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.
|
// Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
|
||||||
if (!empty($item->quoted_status) && isset($item->quoted_status_id_str)
|
if (!empty($status->quoted_status) && isset($status->quoted_status_id_str)
|
||||||
&& substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
|
&& substr($url->expanded_url, -strlen($status->quoted_status_id_str)) == $status->quoted_status_id_str
|
||||||
$body = str_replace($url->url, '', $body);
|
) {
|
||||||
|
$replacementList[$url->indices[0]] = [
|
||||||
|
'replace' => '',
|
||||||
|
'length' => $url->indices[1] - $url->indices[0],
|
||||||
|
];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1252,20 +1279,13 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
|
||||||
$expanded_url = $url->url;
|
$expanded_url = $url->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type == '') {
|
|
||||||
$type = $oembed_data->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($oembed_data->type == 'video') {
|
if ($oembed_data->type == 'video') {
|
||||||
$type = $oembed_data->type;
|
$attachmentUrl = $expanded_url;
|
||||||
$footerurl = $expanded_url;
|
$replace = '';
|
||||||
$footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
|
|
||||||
|
|
||||||
$body = str_replace($url->url, $footerlink, $body);
|
|
||||||
} 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);
|
$replace = '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]';
|
||||||
} elseif ($oembed_data->type != 'link') {
|
} elseif ($oembed_data->type != 'link') {
|
||||||
$body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
|
$replace = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
|
||||||
} else {
|
} else {
|
||||||
$img_str = Network::fetchUrl($final_url, true, 4);
|
$img_str = Network::fetchUrl($final_url, true, 4);
|
||||||
|
|
||||||
|
@ -1282,83 +1302,41 @@ 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';
|
$replace = '[img]' . $final_url . '[/img]';
|
||||||
$body = str_replace($url->url, '[img]' . $final_url . '[/img]', $body);
|
|
||||||
} else {
|
} else {
|
||||||
$type = $oembed_data->type;
|
$attachmentUrl = $expanded_url;
|
||||||
$footerurl = $expanded_url;
|
$replace = '';
|
||||||
$footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$body = str_replace($url->url, $footerlink, $body);
|
$replacementList[$url->indices[0]] = [
|
||||||
}
|
'replace' => $replace,
|
||||||
|
'length' => $url->indices[1] - $url->indices[0],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
krsort($replacementList);
|
||||||
|
|
||||||
|
foreach ($replacementList as $startIndex => $parameters) {
|
||||||
|
$body = Strings::substringReplace($body, $parameters['replace'], $startIndex, $parameters['length']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Footer will be taken care of with a share block in the case of a quote
|
// Footer will be taken care of with a share block in the case of a quote
|
||||||
if (empty($item->quoted_status)) {
|
if (empty($status->quoted_status)) {
|
||||||
if ($footerurl != '') {
|
$footer = '';
|
||||||
$footer = add_page_info($footerurl, false, $picture);
|
if ($attachmentUrl) {
|
||||||
}
|
$footer = add_page_info($attachmentUrl, false, $picture);
|
||||||
|
|
||||||
if (($footerlink != '') && (trim($footer) != '')) {
|
|
||||||
$removedlink = trim(str_replace($footerlink, '', $body));
|
|
||||||
|
|
||||||
if (($removedlink == '') || strstr($body, $removedlink)) {
|
|
||||||
$body = $removedlink;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (trim($footer)) {
|
||||||
$body .= $footer;
|
$body .= $footer;
|
||||||
}
|
} elseif ($picture) {
|
||||||
|
|
||||||
if ($footer == '' && $picture != '') {
|
|
||||||
$body .= "\n\n[img]" . $picture . "[/img]\n";
|
$body .= "\n\n[img]" . $picture . "[/img]\n";
|
||||||
} elseif ($footer == '' && $picture == '') {
|
} else {
|
||||||
$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
|
|
||||||
$tags = BBCode::getTags($body);
|
|
||||||
|
|
||||||
if (count($tags)) {
|
|
||||||
foreach ($tags as $tag) {
|
|
||||||
if (strstr(trim($tag), ' ')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($tag, '#') === 0) {
|
|
||||||
if (strpos($tag, '[url=')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't link tags that are already embedded in links
|
|
||||||
if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$basetag = str_replace('_', ' ', substr($tag, 1));
|
|
||||||
$url = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
|
|
||||||
$body = str_replace($tag, $url, $body);
|
|
||||||
$tags_arr['#' . $basetag] = $url;
|
|
||||||
} elseif (strpos($tag, '@') === 0) {
|
|
||||||
if (strpos($tag, '[url=')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$basetag = substr($tag, 1);
|
|
||||||
$url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
|
|
||||||
$body = str_replace($tag, $url, $body);
|
|
||||||
$tags_arr['@' . $basetag] = $url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$tags = implode($tags_arr, ',');
|
|
||||||
|
|
||||||
return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
|
return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
|
||||||
}
|
}
|
||||||
|
@ -1554,9 +1532,9 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
|
||||||
// Search for media links
|
// Search for media links
|
||||||
$picture = twitter_media_entities($post, $postarray);
|
$picture = twitter_media_entities($post, $postarray);
|
||||||
|
|
||||||
$converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
|
$converted = twitter_expand_entities($postarray['body'], $post, $picture);
|
||||||
$postarray['body'] = $converted["body"];
|
$postarray['body'] = $converted['body'];
|
||||||
$postarray['tag'] = $converted["tags"];
|
$postarray['tag'] = implode($converted['tags'], ',');
|
||||||
$postarray['created'] = DateTimeFormat::utc($post->created_at);
|
$postarray['created'] = DateTimeFormat::utc($post->created_at);
|
||||||
$postarray['edited'] = DateTimeFormat::utc($post->created_at);
|
$postarray['edited'] = DateTimeFormat::utc($post->created_at);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue