From 7e0d21b5bb47e28da95a17631b9d145d92e3c0e4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Apr 2021 09:15:36 +0000 Subject: [PATCH 1/3] Audio attachments are now displayed as audio elements --- src/Model/Item.php | 107 +++++++++++++++++++++++---------------- view/templates/audio.tpl | 4 ++ 2 files changed, 67 insertions(+), 44 deletions(-) create mode 100644 view/templates/audio.tpl diff --git a/src/Model/Item.php b/src/Model/Item.php index c814ac9d8..765812b9f 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2643,50 +2643,7 @@ class Item return $s; } - $as = ''; - $vhead = false; - foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { - $mime = $attachment['mimetype']; - - $author = ['uid' => 0, 'id' => $item['author-id'], - 'network' => $item['author-network'], 'url' => $item['author-link']]; - $the_url = Contact::magicLinkByContact($author, $attachment['url']); - - if (strpos($mime, 'video') !== false) { - if (!$vhead) { - $vhead = true; - DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('videos_head.tpl')); - } - - $as .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [ - '$video' => [ - 'id' => $item['author-id'], - 'title' => DI::l10n()->t('View Video'), - 'src' => $the_url, - 'mime' => $mime, - ], - ]); - } - - $filetype = strtolower(substr($mime, 0, strpos($mime, '/'))); - if ($filetype) { - $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1)); - $filesubtype = str_replace('.', '-', $filesubtype); - } else { - $filetype = 'unkn'; - $filesubtype = 'unkn'; - } - - $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url'])); - $title .= ' ' . ($attachment['size'] ?? 0) . ' ' . DI::l10n()->t('bytes'); - - $icon = '
'; - $as .= '' . $icon . ''; - } - - if ($as != '') { - $s .= '
'.$as.'
'; - } + $s = self::addMediaAttachments($item, $s); // Map. if (strpos($s, '
') !== false && !empty($item['coord'])) { @@ -2710,6 +2667,68 @@ class Item return $hook_data['html']; } + /** + * Add media attachments to the content + * + * @param array $item + * @param string $content + * @return modified content + */ + private static function addMediaAttachments(array $item, string $content) + { + $attached = ''; + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { + $mime = $attachment['mimetype']; + + $author = ['uid' => 0, 'id' => $item['author-id'], + 'network' => $item['author-network'], 'url' => $item['author-link']]; + $the_url = Contact::magicLinkByContact($author, $attachment['url']); + + $filetype = strtolower(substr($mime, 0, strpos($mime, '/'))); + if ($filetype) { + $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1)); + $filesubtype = str_replace('.', '-', $filesubtype); + } else { + $filetype = 'unkn'; + $filesubtype = 'unkn'; + } + + if (($filetype == 'video')) { + $attached .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [ + '$video' => [ + 'id' => $item['author-id'], + 'src' => $the_url, + 'mime' => $mime, + ], + ]); + } elseif ($filetype == 'audio') { + $attached .= Renderer::replaceMacros(Renderer::getMarkupTemplate('audio.tpl'), [ + '$audio' => [ + 'id' => $item['author-id'], + 'src' => $the_url, + 'mime' => $mime, + ], + ]); + } else { + $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url'])); + + if (!empty($attachment['size'])) { + $title .= ' ' . $attachment['size'] . ' ' . DI::l10n()->t('bytes'); + } + + /// @todo Use a template + $icon = '
'; + $attached .= '' . $icon . ''; + } + } + + if ($attached != '') { + $content .= '
' . $attached . '
'; + } + + return $content; + } + /** * get private link for item * diff --git a/view/templates/audio.tpl b/view/templates/audio.tpl new file mode 100644 index 000000000..0b0467ab2 --- /dev/null +++ b/view/templates/audio.tpl @@ -0,0 +1,4 @@ + +
From e8a539b68dff5a02c79fd5b21795f54f198ee3de Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Apr 2021 11:44:43 +0000 Subject: [PATCH 2/3] Move template to content/ --- src/Model/Item.php | 18 ++++++++++++------ view/templates/{ => content}/audio.tpl | 0 2 files changed, 12 insertions(+), 6 deletions(-) rename view/templates/{ => content}/audio.tpl (100%) diff --git a/src/Model/Item.php b/src/Model/Item.php index 765812b9f..76402280c 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2676,7 +2676,8 @@ class Item */ private static function addMediaAttachments(array $item, string $content) { - $attached = ''; + $leading = ''; + $trailing = ''; foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { $mime = $attachment['mimetype']; @@ -2694,7 +2695,8 @@ class Item } if (($filetype == 'video')) { - $attached .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [ + /// @todo Move the template to /content as well + $leading .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [ '$video' => [ 'id' => $item['author-id'], 'src' => $the_url, @@ -2702,7 +2704,7 @@ class Item ], ]); } elseif ($filetype == 'audio') { - $attached .= Renderer::replaceMacros(Renderer::getMarkupTemplate('audio.tpl'), [ + $leading .= Renderer::replaceMacros(Renderer::getMarkupTemplate('content/audio.tpl'), [ '$audio' => [ 'id' => $item['author-id'], 'src' => $the_url, @@ -2718,12 +2720,16 @@ class Item /// @todo Use a template $icon = '
'; - $attached .= '' . $icon . ''; + $trailing .= '' . $icon . ''; } } - if ($attached != '') { - $content .= '
' . $attached . '
'; + if ($leading != '') { + $content = '
' . $leading . '
' . $content; + } + + if ($trailing != '') { + $content .= '
' . $trailing . '
'; } return $content; diff --git a/view/templates/audio.tpl b/view/templates/content/audio.tpl similarity index 100% rename from view/templates/audio.tpl rename to view/templates/content/audio.tpl From 770b9359a40d3bf8f19dc029487f3a5cc09661d2 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Apr 2021 11:45:03 +0000 Subject: [PATCH 3/3] Harmonize the video display --- src/Content/Text/BBCode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 6c46163f7..437330ce7 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -50,7 +50,7 @@ use Friendica\Util\XML; class BBCode { // Update this value to the current date whenever changes are made to BBCode::convert - const VERSION = '2021-03-21'; + const VERSION = '2021-04-05'; const INTERNAL = 0; const API = 2; @@ -1669,7 +1669,7 @@ class BBCode if ($try_oembed) { // html5 video and audio $text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism", - '', $text); + '', $text); $text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", $try_oembed_callback, $text); $text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", $try_oembed_callback, $text);