From 2cb0027f5690fe11aee973782283d0c4b2c0749a Mon Sep 17 00:00:00 2001 From: gudzpoz Date: Tue, 14 Nov 2023 10:52:34 +0800 Subject: [PATCH] Pass emojis in remote mastodon posts in mastodon api --- src/Content/Text/BBCode.php | 14 +++++++++----- src/Factory/Api/Mastodon/Emoji.php | 17 +++++++++++++---- src/Factory/Api/Mastodon/Status.php | 4 ++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 88df9511d4..fab4fba023 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1234,7 +1234,7 @@ class BBCode } /** - * Expand Youtube and Vimeo links to + * Expand Youtube and Vimeo links to * * @param string $text * @return string @@ -1387,7 +1387,7 @@ class BBCode "\n[hr]", "[hr]\n", " [hr]", "[hr] ", "\n[attachment ", " [attachment ", "\n[/attachment]", "[/attachment]\n", " [/attachment]", "[/attachment] ", "[table]\n", "[table] ", " [table]", "\n[/table]", " [/table]", "[/table] ", - " \n", "\t\n", "[/li]\n", "\n[li]", "\n[*]", + " \n", "\t\n", "[/li]\n", "\n[li]", "\n[*]", ]; $replace = [ "[th]", "[th]", "[th]", "[/th]", "[/th]", "[/th]", @@ -1480,14 +1480,14 @@ class BBCode if ($simple_html == self::INTERNAL) { //Ensure to always start with

if possible $heading_count = 0; - for ($level = 6; $level > 0; $level--) { + for ($level = 6; $level > 0; $level--) { if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) { $heading_count++; } } if ($heading_count > 0) { $heading = min($heading_count + 3, 6); - for ($level = 6; $level > 0; $level--) { + for ($level = 6; $level > 0; $level--) { if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) { $text = preg_replace("(\[h$level\](.*?)\[\/h$level\])ism", "

$1

", $text); $heading--; @@ -1548,7 +1548,11 @@ class BBCode $text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism", '$2', $text); // Mastodon Emoji (internal tag, do not document for users) - $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", '$2', $text); + if ($simple_html == self::MASTODON_API) { + $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", '$2', $text); + } else { + $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", '$2', $text); + } // Check for CSS classes // @deprecated since 2021.12, left for backward-compatibility reasons diff --git a/src/Factory/Api/Mastodon/Emoji.php b/src/Factory/Api/Mastodon/Emoji.php index b7f4ee6ead..712bddb48d 100644 --- a/src/Factory/Api/Mastodon/Emoji.php +++ b/src/Factory/Api/Mastodon/Emoji.php @@ -34,19 +34,28 @@ class Emoji extends BaseFactory /** * Creates an emoji collection from shortcode => image mappings. * + * Only emojis with shortcodes of the form of ':shortcode:' are passed in the collection. + * * @param array $smilies + * @param bool $extract_url * * @return Emojis */ - public function createCollectionFromArray(array $smilies): Emojis + public function createCollectionFromArray(array $smilies, bool $extract_url = true): Emojis { $prototype = null; $emojis = []; - foreach ($smilies as $shortcode => $icon) { - if (preg_match('/src="(.+?)"/', $icon, $matches)) { - $url = $matches[1]; + foreach ($smilies as $shortcode => $url) { + if (substr($shortcode, 0, 1) == ':' && substr($shortcode, -1) == ':') { + if ($extract_url) { + if (preg_match('/src="(.+?)"/', $url, $matches)) { + $url = $matches[1]; + } else { + continue; + } + } $shortcode = trim($shortcode, ':'); if ($prototype === null) { diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 4cdc8a78fc..fb73432f03 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -292,6 +292,10 @@ class Status extends BaseFactory if (DI::baseUrl()->isLocalUrl($item['uri'])) { $used_smilies = Smilies::extractUsedSmilies($item['body'] ?: $item['raw-body']); $emojis = $this->mstdnEmojiFactory->createCollectionFromArray($used_smilies)->getArrayCopy(true); + } else { + if (preg_match_all("(\[emoji=(.*?)](.*?)\[/emoji])ism", $item['body'] ?: $item['raw-body'], $matches)) { + $emojis = $this->mstdnEmojiFactory->createCollectionFromArray(array_combine($matches[2], $matches[1]), false)->getArrayCopy(true); + } } if ($is_reshare) { -- 2.39.5