Merge pull request #9989 from annando/issue-9912

Issue 9912: Process Markdown content from Peertube
This commit is contained in:
Tobias Diekershoff 2021-03-06 14:04:28 +01:00 committed by GitHub
commit 30ae5220b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 4 deletions

View file

@ -1503,6 +1503,10 @@ class Item
*/ */
private static function getLanguage(array $item) private static function getLanguage(array $item)
{ {
if (!empty($item['language'])) {
return $item['language'];
}
if (!in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]) || empty($item['body'])) { if (!in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]) || empty($item['body'])) {
return ''; return '';
} }

View file

@ -24,6 +24,7 @@ namespace Friendica\Protocol\ActivityPub;
use Friendica\Content\PageInfo; use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML; use Friendica\Content\Text\HTML;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -65,6 +66,26 @@ class Processor
return $body; return $body;
} }
/**
* Convert the language array into a language JSON
*
* @param array $languages
* @return string language JSON
*/
private static function processLanguages(array $languages)
{
$codes = array_keys($languages);
$lang = [];
foreach ($codes as $code) {
$lang[$code] = 1;
}
if (empty($lang)) {
return '';
}
return json_encode($lang);
}
/** /**
* Replaces emojis in the body * Replaces emojis in the body
* *
@ -193,13 +214,13 @@ class Processor
continue 2; continue 2;
} }
$item['body'] .= "\n[audio]" . $attach['url'] . '[/audio]'; $item['body'] = '[audio]' . $attach['url'] . "[/audio]\n" . $item['body'];
} elseif ($filetype == 'video') { } elseif ($filetype == 'video') {
if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) { if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
continue 2; continue 2;
} }
$item['body'] .= "\n[video]" . $attach['url'] . '[/video]'; $item['body'] = '[video]' . $attach['url'] . "[/video]\n" . $item['body'];
} }
} }
} }
@ -463,9 +484,21 @@ class Processor
*/ */
private static function processContent($activity, $item) private static function processContent($activity, $item)
{ {
$item['title'] = HTML::toBBCode($activity['name']); if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) {
$item['title'] = Markdown::toBBCode($activity['name']);
$content = Markdown::toBBCode($activity['content']);
} elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) {
$item['title'] = $activity['name'];
$content = $activity['content'];
} else {
// By default assume "text/html"
$item['title'] = HTML::toBBCode($activity['name']);
$content = HTML::toBBCode($activity['content']);
}
$content = HTML::toBBCode($activity['content']); if (!empty($activity['languages'])) {
$item['language'] = self::processLanguages($activity['languages']);
}
if (!empty($activity['emojis'])) { if (!empty($activity['emojis'])) {
$content = self::replaceEmojis($content, $activity['emojis']); $content = self::replaceEmojis($content, $activity['emojis']);

View file

@ -982,6 +982,28 @@ class Receiver
return false; return false;
} }
/**
* Converts the language element (Used by Peertube)
*
* @param array $languages
* @return array Languages
*/
public static function processLanguages(array $languages)
{
if (empty($languages)) {
return [];
}
$language_list = [];
foreach ($languages as $language) {
if (!empty($language['_:identifier']) && !empty($language['as:name'])) {
$language_list[$language['_:identifier']] = $language['as:name'];
}
}
return $language_list;
}
/** /**
* Convert tags from JSON-LD format into a simplified format * Convert tags from JSON-LD format into a simplified format
* *
@ -1345,6 +1367,7 @@ class Receiver
$object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value'); $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
$object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value'); $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
$object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value'); $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
$object_data['mediatype'] = JsonLD::fetchElement($object, 'as:mediaType', '@value');
$object_data = self::getSource($object, $object_data); $object_data = self::getSource($object, $object_data);
$object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value'); $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
$object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value'); $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
@ -1356,6 +1379,7 @@ class Receiver
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []); $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []); $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
$object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []); $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
$object_data['languages'] = self::processLanguages(JsonLD::fetchElementArray($object, 'sc:inLanguage') ?? []);
$object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application'); $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
$object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value'); $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id'); $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');