From 7e0d21b5bb47e28da95a17631b9d145d92e3c0e4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Apr 2021 09:15:36 +0000 Subject: [PATCH] Audio attachments are now displayed as audio elements --- src/Model/Item.php | 103 +++++++++++++++++++++++---------------- view/templates/audio.tpl | 4 ++ 2 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 view/templates/audio.tpl diff --git a/src/Model/Item.php b/src/Model/Item.php index c814ac9d83..765812b9ff 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2643,8 +2643,40 @@ class Item return $s; } - $as = ''; - $vhead = false; + $s = self::addMediaAttachments($item, $s); + + // Map. + if (strpos($s, '
') !== false && !empty($item['coord'])) { + $x = Map::byCoordinates(trim($item['coord'])); + if ($x) { + $s = preg_replace('/\
/', '$0' . $x, $s); + } + } + + // Replace friendica image url size with theme preference. + if (!empty($a->theme_info['item_image_size'])) { + $ps = $a->theme_info['item_image_size']; + $s = preg_replace('|(]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s); + } + + $s = HTML::applyContentFilter($s, $filter_reasons); + + $hook_data = ['item' => $item, 'html' => $s]; + Hook::callAll('prepare_body_final', $hook_data); + + 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']; @@ -2652,22 +2684,6 @@ class Item '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)); @@ -2677,37 +2693,40 @@ class Item $filesubtype = 'unkn'; } - $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url'])); - $title .= ' ' . ($attachment['size'] ?? 0) . ' ' . DI::l10n()->t('bytes'); - - $icon = '
'; - $as .= '' . $icon . ''; - } + 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 ($as != '') { - $s .= '
'.$as.'
'; - } + if (!empty($attachment['size'])) { + $title .= ' ' . $attachment['size'] . ' ' . DI::l10n()->t('bytes'); + } - // Map. - if (strpos($s, '
') !== false && !empty($item['coord'])) { - $x = Map::byCoordinates(trim($item['coord'])); - if ($x) { - $s = preg_replace('/\
/', '$0' . $x, $s); + /// @todo Use a template + $icon = '
'; + $attached .= '' . $icon . ''; } } - // Replace friendica image url size with theme preference. - if (!empty($a->theme_info['item_image_size'])) { - $ps = $a->theme_info['item_image_size']; - $s = preg_replace('|(]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s); + if ($attached != '') { + $content .= '
' . $attached . '
'; } - $s = HTML::applyContentFilter($s, $filter_reasons); - - $hook_data = ['item' => $item, 'html' => $s]; - Hook::callAll('prepare_body_final', $hook_data); - - return $hook_data['html']; + return $content; } /** diff --git a/view/templates/audio.tpl b/view/templates/audio.tpl new file mode 100644 index 0000000000..0b0467ab24 --- /dev/null +++ b/view/templates/audio.tpl @@ -0,0 +1,4 @@ + +
-- 2.39.5