Use different parser modes for Mastodon and Twitter-API

This commit is contained in:
Michael 2022-11-06 20:02:43 +00:00
parent 205f19da38
commit 210200d330
8 changed files with 27 additions and 28 deletions

View File

@ -55,15 +55,16 @@ class BBCode
// Update this value to the current date whenever changes are made to BBCode::convert // Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-07-28'; const VERSION = '2021-07-28';
const INTERNAL = 0; const INTERNAL = 0;
const EXTERNAL = 1; const EXTERNAL = 1;
const API = 2; const MASTODON_API = 2;
const DIASPORA = 3; const DIASPORA = 3;
const CONNECTORS = 4; const CONNECTORS = 4;
const OSTATUS = 7; const TWITTER_API = 5;
const TWITTER = 8; const OSTATUS = 7;
const BACKLINK = 8; const TWITTER = 8;
const ACTIVITYPUB = 9; const BACKLINK = 8;
const ACTIVITYPUB = 9;
const TOP_ANCHOR = '<br class="top-anchor">'; const TOP_ANCHOR = '<br class="top-anchor">';
const BOTTOM_ANCHOR = '<br class="button-anchor">'; const BOTTOM_ANCHOR = '<br class="button-anchor">';
@ -473,7 +474,7 @@ class BBCode
private static function proxyUrl(string $image, int $simplehtml = self::INTERNAL, int $uriid = 0, string $size = ''): string private static function proxyUrl(string $image, int $simplehtml = self::INTERNAL, int $uriid = 0, string $size = ''): string
{ {
// Only send proxied pictures to API and for internal display // Only send proxied pictures to API and for internal display
if (!in_array($simplehtml, [self::INTERNAL, self::API])) { if (!in_array($simplehtml, [self::INTERNAL, self::MASTODON_API, self::TWITTER_API])) {
return $image; return $image;
} elseif ($uriid > 0) { } elseif ($uriid > 0) {
return Post\Link::getByLink($uriid, $image, $size); return Post\Link::getByLink($uriid, $image, $size);
@ -1190,7 +1191,8 @@ class BBCode
$mention = $attributes['author'] . ' (' . ($author_contact['addr'] ?? '') . ')'; $mention = $attributes['author'] . ' (' . ($author_contact['addr'] ?? '') . ')';
switch ($simplehtml) { switch ($simplehtml) {
case self::API: case self::MASTODON_API:
case self::TWITTER_API:
$text = ($is_quote_share? '<br>' : '') . $text = ($is_quote_share? '<br>' : '') .
'<b><a href="' . $attributes['link'] . '">' . html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b><br>\n" . '<b><a href="' . $attributes['link'] . '">' . html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b><br>\n" .
'<blockquote class="shared_content" dir="auto">' . $content . '</blockquote>'; '<blockquote class="shared_content" dir="auto">' . $content . '</blockquote>';
@ -1634,7 +1636,7 @@ class BBCode
/// @todo Have a closer look at the different html modes /// @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, [self::API, self::ACTIVITYPUB])) { if (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
$text = self::removeAttachment($text); $text = self::removeAttachment($text);
} elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) { } elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) {
$text = self::removeAttachment($text, true); $text = self::removeAttachment($text, true);
@ -1971,14 +1973,11 @@ class BBCode
$text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism", $text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'<a href="$2" class="mention hashtag" rel="tag">$1<span>$3</span></a>', '<a href="$2" class="mention hashtag" rel="tag">$1<span>$3</span></a>',
$text); $text);
} elseif (in_array($simple_html, [self::INTERNAL, self::EXTERNAL])) { } elseif (in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::TWITTER_API])) {
$text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'<bdi>$1<a href="$2" class="userinfo mention" title="$3">$3</a></bdi>', '<bdi>$1<a href="$2" class="userinfo mention" title="$3">$3</a></bdi>',
$text); $text);
$text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism", } elseif ($simple_html == self::MASTODON_API) {
'<a class="mention hashtag status-link" href="$2" rel="tag">$1<span>$3</span></a>',
$text);
} elseif ($simple_html == self::API) {
$text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'<a class="u-url mention status-link" href="$2" rel="nofollow noopener noreferrer" target="_blank" title="$3">$1<span>$3</span></a>', '<a class="u-url mention status-link" href="$2" rel="nofollow noopener noreferrer" target="_blank" title="$3">$1<span>$3</span></a>',
$text); $text);
@ -1990,7 +1989,7 @@ class BBCode
} }
if (!$for_plaintext) { if (!$for_plaintext) {
if (in_array($simple_html, [self::OSTATUS, self::API, self::ACTIVITYPUB])) { if (in_array($simple_html, [self::OSTATUS, self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
$text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text); $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
$text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text); $text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
} }

View File

@ -116,7 +116,7 @@ class Plaintext
* @see \Friendica\Content\Text\BBCode::getAttachedData * @see \Friendica\Content\Text\BBCode::getAttachedData
* *
*/ */
public static function getPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '') public static function getPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::MASTODON_API, $target_network = '')
{ {
// Remove hashtags // Remove hashtags
$URLSearchString = '^\[\]'; $URLSearchString = '^\[\]';

View File

@ -65,13 +65,13 @@ class DirectMessage extends BaseFactory
if (!empty($text_mode)) { if (!empty($text_mode)) {
$title = $mail['title']; $title = $mail['title'];
if ($text_mode == 'html') { if ($text_mode == 'html') {
$text = BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API); $text = BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API);
} elseif ($text_mode == 'plain') { } elseif ($text_mode == 'plain') {
$text = HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API), 0); $text = HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API), 0);
} }
} else { } else {
$title = ''; $title = '';
$text = $mail['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API), 0); $text = $mail['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API), 0);
} }
$pcid = Contact::getPublicIdByUserId($uid); $pcid = Contact::getPublicIdByUserId($uid);

View File

@ -143,12 +143,12 @@ class Status extends BaseFactory
$title = sprintf("[h4]%s[/h4]\n", $item['title']); $title = sprintf("[h4]%s[/h4]\n", $item['title']);
} }
$statusnetHtml = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($title . ($item['raw-body'] ?? $item['body'])), BBCode::API); $statusnetHtml = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($title . ($item['raw-body'] ?? $item['body'])), BBCode::TWITTER_API);
$friendicaHtml = BBCode::convertForUriId($item['uri-id'], $title . $item['body'], BBCode::EXTERNAL); $friendicaHtml = BBCode::convertForUriId($item['uri-id'], $title . $item['body'], BBCode::EXTERNAL);
$text .= Post\Media::addAttachmentsToBody($item['uri-id'], $this->contentItem->addSharedPost($item)); $text .= Post\Media::addAttachmentsToBody($item['uri-id'], $this->contentItem->addSharedPost($item));
$text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $text, BBCode::API), 0)); $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $text, BBCode::TWITTER_API), 0));
$geo = []; $geo = [];

View File

@ -616,7 +616,7 @@ class Event
$title = BBCode::convertForUriId($event['uri-id'], Strings::escapeHtml($event['summary'])); $title = BBCode::convertForUriId($event['uri-id'], Strings::escapeHtml($event['summary']));
if (!$title) { if (!$title) {
list($title, $_trash) = explode("<br", BBCode::convertForUriId($event['uri-id'], Strings::escapeHtml($event['desc'])), BBCode::API); list($title, $_trash) = explode("<br", BBCode::convertForUriId($event['uri-id'], Strings::escapeHtml($event['desc'])), BBCode::TWITTER_API);
} }
$author_link = $event['author-link']; $author_link = $event['author-link'];

View File

@ -78,7 +78,7 @@ class Show extends BaseApi
foreach ($profileFields as $profileField) { foreach ($profileFields as $profileField) {
$custom_fields[] = [ $custom_fields[] = [
'label' => $profileField->label, 'label' => $profileField->label,
'value' => BBCode::convert($profileField->value, false, BBCode::API), 'value' => BBCode::convert($profileField->value, false, BBCode::TWITTER_API),
]; ];
} }

View File

@ -67,7 +67,7 @@ class ScheduledStatus extends BaseDataTransferObject
$this->scheduled_at = DateTimeFormat::utc($delayed_post['delayed'], DateTimeFormat::JSON); $this->scheduled_at = DateTimeFormat::utc($delayed_post['delayed'], DateTimeFormat::JSON);
$this->params = [ $this->params = [
'text' => BBCode::convert(BBCode::setMentionsToNicknames($parameters['item']['body'] ?? ''), false, BBCode::API), 'text' => BBCode::convert(BBCode::setMentionsToNicknames($parameters['item']['body'] ?? ''), false, BBCode::MASTODON_API),
'media_ids' => $media_ids, 'media_ids' => $media_ids,
'sensitive' => null, 'sensitive' => null,
'spoiler_text' => $parameters['item']['title'] ?? '', 'spoiler_text' => $parameters['item']['title'] ?? '',

View File

@ -132,7 +132,7 @@ class Status extends BaseDataTransferObject
$this->muted = $userAttributes->muted; $this->muted = $userAttributes->muted;
$this->bookmarked = $userAttributes->bookmarked; $this->bookmarked = $userAttributes->bookmarked;
$this->pinned = $userAttributes->pinned; $this->pinned = $userAttributes->pinned;
$this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::API); $this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::MASTODON_API);
$this->reblog = $reblog; $this->reblog = $reblog;
$this->application = $application->toArray(); $this->application = $application->toArray();
$this->account = $account->toArray(); $this->account = $account->toArray();