Merge pull request #7843 from annando/ap-attachments

Improve the look of fetched feeds and the BBCode processing of attachments
This commit is contained in:
Hypolite Petovan 2019-11-18 08:16:13 -05:00 committed by GitHub
commit 6d53036a9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 52 deletions

View file

@ -389,20 +389,21 @@ class BBCode extends BaseObject
* Remove [attachment] BBCode and replaces it with a regular [url] * Remove [attachment] BBCode and replaces it with a regular [url]
* *
* @param string $body * @param string $body
* @param boolean $no_link_desc No link description
* *
* @return string with replaced body * @return string with replaced body
*/ */
public static function removeAttachment($body) public static function removeAttachment($body, $no_link_desc = false)
{ {
return preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism", return preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism",
function ($match) { function ($match) use ($no_link_desc) {
$attach_data = self::getAttachmentData($match[0]); $attach_data = self::getAttachmentData($match[0]);
if (empty($attach_data['url'])) { if (empty($attach_data['url'])) {
return $match[0]; return $match[0];
} elseif (empty($attach_data['title'])) { } elseif (empty($attach_data['title']) || $no_link_desc) {
return '[url]' . $attach_data['url'] . '[/url]'; return '[url]' . $attach_data['url'] . "[/url]\n";
} else { } else {
return '[url=' . $attach_data['url'] . ']' . $attach_data['title'] . '[/url]'; return '[url=' . $attach_data['url'] . ']' . $attach_data['title'] . "[/url]\n";
} }
}, $body); }, $body);
} }
@ -622,14 +623,6 @@ class BBCode extends BaseObject
} }
$return = ''; $return = '';
if (in_array($simplehtml, [7, 9])) {
// Only add the link when it isn't already part of the body
if (substr_count($text, $data['url']) == 1) {
$return = self::convertUrlForActivityPub($data['url']);
}
} elseif (($simplehtml != 4) && ($simplehtml != 0)) {
$return = sprintf('<a href="%s" target="_blank">%s</a><br>', $data['url'], $data['title']);
} else {
try { try {
if ($tryoembed && OEmbed::isAllowedURL($data['url'])) { if ($tryoembed && OEmbed::isAllowedURL($data['url'])) {
$return = OEmbed::getHTML($data['url'], $data['title']); $return = OEmbed::getHTML($data['url'], $data['title']);
@ -670,7 +663,6 @@ class BBCode extends BaseObject
$return .= '</div>'; $return .= '</div>';
} }
} }
}
return trim(($data['text'] ?? '') . ' ' . $return . ' ' . ($data['after'] ?? '')); return trim(($data['text'] ?? '') . ' ' . $return . ' ' . ($data['after'] ?? ''));
} }
@ -1244,7 +1236,7 @@ class BBCode extends BaseObject
* - 5: Unused * - 5: Unused
* - 6: Unused * - 6: Unused
* - 7: Used for dfrn, OStatus * - 7: Used for dfrn, OStatus
* - 8: Used for WP backlink text setting * - 8: Used for Twitter, WP backlink text setting
* - 9: ActivityPub * - 9: ActivityPub
* *
* @param string $text * @param string $text
@ -1380,8 +1372,15 @@ class BBCode extends BaseObject
} while ($oldtext != $text); } while ($oldtext != $text);
} }
/// @todo Have a closer look at the different html modes
// Handle attached links or videos // Handle attached links or videos
if (in_array($simple_html, [9])) {
$text = self::removeAttachment($text);
} elseif (!in_array($simple_html, [0, 4])) {
$text = self::removeAttachment($text, true);
} else {
$text = self::convertAttachment($text, $simple_html, $try_oembed); $text = self::convertAttachment($text, $simple_html, $try_oembed);
}
// leave open the posibility of [map=something] // leave open the posibility of [map=something]
// this is replaced in Item::prepareBody() which has knowledge of the item location // this is replaced in Item::prepareBody() which has knowledge of the item location
@ -1771,7 +1770,7 @@ class BBCode extends BaseObject
* - #[url=<anything>]<term>[/url] * - #[url=<anything>]<term>[/url]
* - [url=<anything>]#<term>[/url] * - [url=<anything>]#<term>[/url]
*/ */
$text = preg_replace_callback("/(?:#\[url\=.*?\]|\[url\=.*?\]#)(.*?)\[\/url\]/ism", function($matches) { $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function($matches) {
return '#<a href="' return '#<a href="'
. System::baseUrl() . '/search?tag=' . rawurlencode($matches[1]) . System::baseUrl() . '/search?tag=' . rawurlencode($matches[1])
. '" class="tag" rel="tag" title="' . XML::escape($matches[1]) . '">' . '" class="tag" rel="tag" title="' . XML::escape($matches[1]) . '">'

View file

@ -1020,6 +1020,9 @@ class Transmitter
{ {
$attachments = []; $attachments = [];
// Currently deactivated, since it creates side effects on Mastodon and Pleroma.
// It will be reactivated, once this cleared.
/*
$attach_data = BBCode::getAttachmentData($item['body']); $attach_data = BBCode::getAttachmentData($item['body']);
if (!empty($attach_data['url'])) { if (!empty($attach_data['url'])) {
$attachment = ['type' => 'Page', $attachment = ['type' => 'Page',
@ -1047,7 +1050,7 @@ class Transmitter
$attachments[] = $attachment; $attachments[] = $attachment;
} }
*/
$arr = explode('[/attach],', $item['attach']); $arr = explode('[/attach],', $item['attach']);
if (count($arr)) { if (count($arr)) {
foreach ($arr as $r) { foreach ($arr as $r) {

View file

@ -15,6 +15,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Protocol\ActivityNamespace; use Friendica\Protocol\ActivityNamespace;
use Friendica\Util\ParseUrl;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\XML; use Friendica\Util\XML;
@ -399,7 +400,7 @@ class Feed {
// Remove a possible link to the item itself // Remove a possible link to the item itself
$item["body"] = str_replace($item["plink"], '', $item["body"]); $item["body"] = str_replace($item["plink"], '', $item["body"]);
$item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]); $item["body"] = trim(preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]));
// Replace the content when the title is longer than the body // Replace the content when the title is longer than the body
$replace = (strlen($item["title"]) > strlen($item["body"])); $replace = (strlen($item["title"]) > strlen($item["body"]));
@ -415,8 +416,21 @@ class Feed {
} }
if ($replace) { if ($replace) {
$item["body"] = $item["title"]; $item["body"] = trim($item["title"]);
} }
$data = ParseUrl::getSiteinfoCached($item['plink'], true);
if (!empty($data['text']) && !empty($data['title']) && (mb_strlen($item['body']) < mb_strlen($data['text']))) {
// When the fetched page info text is longer than the body, we do try to enhance the body
if ((strpos($data['title'], $item['body']) === false) && (strpos($data['text'], $item['body']) === false)) {
// The body is not part of the fetched page info title or page info text. So we add the text to the body
$item['body'] .= "\n\n" . $data['text'];
} else {
// Else we replace the body with the page info text
$item['body'] = $data['text'];
}
}
// We always strip the title since it will be added in the page information // We always strip the title since it will be added in the page information
$item["title"] = ""; $item["title"] = "";
$item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]); $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);