diff --git a/composer.json b/composer.json index 96029ebdee..bbdd9d0134 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,7 @@ "npm-asset/socket.io-client": "^2.0", "npm-asset/swiped-events": "^1.0.9", "npm-asset/timeago": "^1.6.3", + "npm-asset/unicode-emoji-json": "^0.5.0", "phpoffice/phpspreadsheet": "^2.0", "raoul2000/yii2-jcrop-widget": "^1.0", "symfony/amazon-mailer": "^5.4", diff --git a/composer.lock b/composer.lock index f8a4edfdb3..200a697cef 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1999d0a4f117ac2c69cf9743553d7b08", + "content-hash": "54bec7841b0d448a8ad955ef8ff41b07", "packages": [ { "name": "async-aws/core", @@ -4115,6 +4115,18 @@ }, "type": "npm-asset" }, + { + "name": "npm-asset/unicode-emoji-json", + "version": "0.5.0", + "dist": { + "type": "tar", + "url": "https://registry.npmjs.org/unicode-emoji-json/-/unicode-emoji-json-0.5.0.tgz" + }, + "type": "npm-asset", + "license": [ + "MIT" + ] + }, { "name": "npm-asset/ws", "version": "7.4.6", diff --git a/protected/humhub/modules/content/helpers/EmojiHelper.php b/protected/humhub/modules/content/helpers/EmojiHelper.php new file mode 100644 index 0000000000..cfe5cb42f8 --- /dev/null +++ b/protected/humhub/modules/content/helpers/EmojiHelper.php @@ -0,0 +1,50 @@ +runtimeCache->getOrSet('emoji-helper-data', function () { + $dataPath = Yii::getAlias(self::DATA_PATH); + + if (!is_file($dataPath)) { + return []; + } + + $data = (array) json_decode(file_get_contents($dataPath)); + + $emojis = []; + foreach ($data as $e => $emoji) { + $emojis[$emoji->name] = $e; + } + + return $emojis; + }); + } + + public static function getUnicode(string $name): ?string + { + return self::getData()[$name] ?? null; + } + + public static function findEmoji(string $content): ?string + { + return preg_match('/:([a-z\d\-\+][a-z\d\-\+\s_]*):/i', $content, $matches) + ? $matches[1] + : null; + } +} diff --git a/protected/humhub/modules/content/widgets/richtext/converter/RichTextToEmailHtmlConverter.php b/protected/humhub/modules/content/widgets/richtext/converter/RichTextToEmailHtmlConverter.php index 1b663932f4..8f4c7ffaa4 100644 --- a/protected/humhub/modules/content/widgets/richtext/converter/RichTextToEmailHtmlConverter.php +++ b/protected/humhub/modules/content/widgets/richtext/converter/RichTextToEmailHtmlConverter.php @@ -3,6 +3,7 @@ namespace humhub\modules\content\widgets\richtext\converter; use humhub\libs\Html; +use humhub\modules\content\helpers\EmojiHelper; use humhub\modules\content\widgets\richtext\extensions\link\LinkParserBlock; use humhub\modules\file\actions\DownloadAction; use humhub\modules\file\models\File; @@ -123,4 +124,20 @@ class RichTextToEmailHtmlConverter extends RichTextToHtmlConverter { return '
' . nl2br($this->renderAbsy($block['content'])) . "
\n"; } + + /** + * @marker : + */ + protected function parseEmoji($markdown) + { + return ($emoji = EmojiHelper::findEmoji($markdown)) + ? [['emoji', [['text', $emoji]]], strlen($emoji) + 2] + : [['text', ':'], 1]; + } + + protected function renderEmoji($element) + { + $emojiName = $this->renderAbsy($element[1]); + return EmojiHelper::getUnicode($emojiName) ?? ':' . $emojiName . ':'; + } } diff --git a/protected/humhub/modules/content/widgets/richtext/converter/RichTextToPlainTextConverter.php b/protected/humhub/modules/content/widgets/richtext/converter/RichTextToPlainTextConverter.php index b38fcb80d9..5627cd8fce 100644 --- a/protected/humhub/modules/content/widgets/richtext/converter/RichTextToPlainTextConverter.php +++ b/protected/humhub/modules/content/widgets/richtext/converter/RichTextToPlainTextConverter.php @@ -3,6 +3,7 @@ namespace humhub\modules\content\widgets\richtext\converter; use humhub\libs\Helpers; +use humhub\modules\content\helpers\EmojiHelper; use humhub\modules\content\widgets\richtext\extensions\link\LinkParserBlock; use humhub\modules\content\widgets\richtext\extensions\link\RichTextLinkExtension; use humhub\modules\content\widgets\richtext\ProsemirrorRichText; @@ -187,4 +188,20 @@ class RichTextToPlainTextConverter extends RichTextToMarkdownConverter return $result; } + + /** + * @marker : + */ + protected function parseEmoji($markdown) + { + return ($emoji = EmojiHelper::findEmoji($markdown)) + ? [['emoji', [['text', $emoji]]], strlen($emoji) + 2] + : [['text', ':'], 1]; + } + + protected function renderEmoji($element) + { + $emojiName = $this->renderAbsy($element[1]); + return EmojiHelper::getUnicode($emojiName) ?? ':' . $emojiName . ':'; + } } diff --git a/protected/humhub/modules/notification/targets/MailTarget.php b/protected/humhub/modules/notification/targets/MailTarget.php index e9c0827fa7..6e1401b224 100644 --- a/protected/humhub/modules/notification/targets/MailTarget.php +++ b/protected/humhub/modules/notification/targets/MailTarget.php @@ -8,9 +8,10 @@ namespace humhub\modules\notification\targets; -use Yii; +use humhub\modules\content\widgets\richtext\converter\RichTextToPlainTextConverter; use humhub\modules\notification\components\BaseNotification; use humhub\modules\user\models\User; +use Yii; use yii\helpers\ArrayHelper; use yii\helpers\Url; @@ -70,7 +71,7 @@ class MailTarget extends BaseTarget $mail = Yii::$app->mailer->compose($this->view, $viewParams) ->setTo($recipient->email) - ->setSubject(str_replace("\n", " ", trim($notification->getMailSubject()))); + ->setSubject(RichTextToPlainTextConverter::process($notification->getMailSubject())); if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) { $mail->setReplyTo($replyTo); }