Render emojis in email content (#7062)

* Render emojis in email content

* Cache emoji data and display emoji on email subject

* Fix finding emoji in content
This commit is contained in:
Yuriy Bakhtin 2024-06-19 14:40:58 +02:00 committed by GitHub
parent ab03007f7b
commit ee3a5bf0ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 101 additions and 3 deletions

View File

@ -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",

14
composer.lock generated
View File

@ -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",

View File

@ -0,0 +1,50 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\content\helpers;
use Yii;
/**
* @since 1.16
*/
class EmojiHelper
{
public const DATA_PATH = '@npm/unicode-emoji-json/data-by-emoji.json';
public static function getData(): array
{
return Yii::$app->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;
}
}

View File

@ -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 '<p>' . nl2br($this->renderAbsy($block['content'])) . "</p>\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 . ':';
}
}

View File

@ -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 . ':';
}
}

View File

@ -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);
}