From 8c748f03f91dcc59ba33974287fa0653d37c8788 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Mar 2021 08:43:25 +0000 Subject: [PATCH] Issue 9912: Process Markdown content from Peertube --- src/Model/Item.php | 4 +++ src/Protocol/ActivityPub/Processor.php | 41 +++++++++++++++++++++++--- src/Protocol/ActivityPub/Receiver.php | 24 +++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 299736d14d..7c859ed221 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1503,6 +1503,10 @@ class 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'])) { return ''; } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 069239c044..82eb4d0294 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -24,6 +24,7 @@ namespace Friendica\Protocol\ActivityPub; use Friendica\Content\PageInfo; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; +use Friendica\Content\Text\Markdown; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; @@ -65,6 +66,26 @@ class Processor 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 * @@ -193,13 +214,13 @@ class Processor continue 2; } - $item['body'] .= "\n[audio]" . $attach['url'] . '[/audio]'; + $item['body'] = '[audio]' . $attach['url'] . "[/audio]\n" . $item['body']; } elseif ($filetype == 'video') { if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) { 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) { - $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'])) { $content = self::replaceEmojis($content, $activity['emojis']); diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index eef82f1533..519d91a660 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -982,6 +982,28 @@ class Receiver 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 * @@ -1345,6 +1367,7 @@ class Receiver $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value'); $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@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['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@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['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []); $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_data, 'generator', '@value'); $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');