perf: 优化预览消息

This commit is contained in:
kuaifan 2024-11-01 21:17:23 +08:00
parent 4ba9cc88dd
commit 312acdab51
8 changed files with 75 additions and 37 deletions

View File

@ -185,7 +185,7 @@ class WebSocketDialog extends AbstractModel
* @param $data * @param $data
* @param int $userid 会员ID * @param int $userid 会员ID
* @param bool $hasData 已存在的消息类型 * @param bool $hasData 已存在的消息类型
* @return array|mixed * @return array
*/ */
public static function synthesizeData($data, $userid, $hasData = false) public static function synthesizeData($data, $userid, $hasData = false)
{ {
@ -250,6 +250,16 @@ class WebSocketDialog extends AbstractModel
$data['last_msg'] = $data['last_msg'] ?? WebSocketDialogMsg::whereDialogId($data['id'])->orderByDesc('id')->first()?->toArray(); $data['last_msg'] = $data['last_msg'] ?? WebSocketDialogMsg::whereDialogId($data['id'])->orderByDesc('id')->first()?->toArray();
} }
// 最后消息处理
if ($data['last_msg']) {
foreach ($data['last_msg']['emoji'] as &$value) {
unset($value['userids']);
}
if ($data['last_msg']['type'] === 'text') {
$data['last_msg']['msg']['text'] = WebSocketDialogMsg::previewTextMsg($data['last_msg']['msg']);
}
}
// 对方信息 // 对方信息
$data['pinyin'] = Base::cn2pinyin($data['name']); $data['pinyin'] = Base::cn2pinyin($data['name']);
$data['quick_msgs'] = []; $data['quick_msgs'] = [];

View File

@ -541,29 +541,33 @@ class WebSocketDialogMsg extends AbstractModel
/** /**
* 预览消息 * 预览消息
* @param bool $preserveHtml 保留html格式 * @param WebSocketDialogMsg|array $data 消息数据
* @param null|array $data * @param bool $preserveHtml 保留html格式
* @return string * @return string
*/ */
public function previewMsg($preserveHtml = false, $data = null) public static function previewMsg($data, $preserveHtml = false)
{ {
if ($data === null) { if ($data instanceof WebSocketDialogMsg) {
$data = [ $data = [
'type' => $this->type, 'type' => $data->type,
'msg' => $this->msg, 'msg' => $data->msg,
]; ];
} }
if (!is_array($data)) {
return '';
}
switch ($data['type']) { switch ($data['type']) {
case 'text': case 'text':
return $this->previewTextMsg($data['msg']['text'], $preserveHtml); return self::previewTextMsg($data['msg'], $preserveHtml);
case 'vote': case 'vote':
$action = Doo::translate("投票"); $action = Doo::translate("投票");
return "[{$action}] {$this->previewTextMsg($data['msg']['text'], $preserveHtml)}"; return "[{$action}] " . self::previewTextMsg($data['msg'], $preserveHtml);
case 'word-chain': case 'word-chain':
$action = Doo::translate("接龙"); $action = Doo::translate("接龙");
return "[{$action}] {$this->previewTextMsg($data['msg']['text'], $preserveHtml)}"; return "[{$action}] " . self::previewTextMsg($data['msg'], $preserveHtml);
case 'record': case 'record':
$action = Doo::translate("语音"); $action = Doo::translate("语音");
@ -574,25 +578,25 @@ class WebSocketDialogMsg extends AbstractModel
return "[{$action}] ${$data['msg']['name']}"; return "[{$action}] ${$data['msg']['name']}";
case 'file': case 'file':
return $this->previewFileMsg($data['msg']); return self::previewFileMsg($data['msg']);
case 'tag': case 'tag':
$action = Doo::translate($data['msg']['action'] === 'remove' ? '取消标注' : '标注'); $action = Doo::translate($data['msg']['action'] === 'remove' ? '取消标注' : '标注');
return "[{$action}] {$this->previewMsg(false, $data['msg']['data'])}"; return "[{$action}] " . self::previewMsg($data['msg']['data']);
case 'top': case 'top':
$action = Doo::translate($data['msg']['action'] === 'remove' ? '取消置顶' : '置顶'); $action = Doo::translate($data['msg']['action'] === 'remove' ? '取消置顶' : '置顶');
return "[{$action}] {$this->previewMsg(false, $data['msg']['data'])}"; return "[{$action}] " . self::previewMsg($data['msg']['data']);
case 'todo': case 'todo':
$action = Doo::translate($data['msg']['action'] === 'remove' ? '取消待办' : ($data['msg']['action'] === 'done' ? '完成' : '设待办')); $action = Doo::translate($data['msg']['action'] === 'remove' ? '取消待办' : ($data['msg']['action'] === 'done' ? '完成' : '设待办'));
return "[{$action}] {$this->previewMsg(false, $data['msg']['data'])}"; return "[{$action}] " . self::previewMsg($data['msg']['data']);
case 'notice': case 'notice':
return Doo::translate($data['msg']['notice']); return Doo::translate($data['msg']['notice']);
case 'template': case 'template':
return $this->previewTemplateMsg($data['msg']); return self::previewTemplateMsg($data['msg']);
default: default:
$action = Doo::translate("未知的消息"); $action = Doo::translate("未知的消息");
@ -605,7 +609,7 @@ class WebSocketDialogMsg extends AbstractModel
* @param $msg * @param $msg
* @return string * @return string
*/ */
private function previewFileMsg($msg) private static function previewFileMsg($msg)
{ {
if ($msg['type'] == 'img') { if ($msg['type'] == 'img') {
$action = Doo::translate("图片"); $action = Doo::translate("图片");
@ -623,7 +627,7 @@ class WebSocketDialogMsg extends AbstractModel
* @param $msg * @param $msg
* @return string * @return string
*/ */
private function previewTemplateMsg($msg) private static function previewTemplateMsg($msg)
{ {
if (!empty($msg['title_raw'])) { if (!empty($msg['title_raw'])) {
return $msg['title_raw']; return $msg['title_raw'];
@ -689,20 +693,24 @@ class WebSocketDialogMsg extends AbstractModel
{ {
$msg = $this->msg; $msg = $this->msg;
if ($this->type === 'text') { if ($this->type === 'text') {
$msg['text'] = $this->previewTextMsg($msg['text']); $msg['text'] = self::previewTextMsg($msg);
} }
return $msg; return $msg;
} }
/** /**
* 返回文本预览消息 * 返回文本预览消息
* @param $text * @param array $msgData
* @param bool $preserveHtml 保留html格式 * @param bool $preserveHtml 保留html格式
* @return string|string[]|null * @return string|string[]|null
*/ */
private function previewTextMsg($text, $preserveHtml = false) public static function previewTextMsg($msgData, $preserveHtml = false)
{ {
$text = $msgData['text'] ?? '';
if (!$text) return ''; if (!$text) return '';
if ($msgData['type'] === 'md') {
$text = Base::markdown2html($text);
}
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?alt=\"(\S+)\"[^>]*?>/", "[$1]", $text); $text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?alt=\"(\S+)\"[^>]*?>/", "[$1]", $text);
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?>/", "[动画表情]", $text); $text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?>/", "[动画表情]", $text);
$text = preg_replace("/<img\s+class=\"browse\"[^>]*?>/", "[图片]", $text); $text = preg_replace("/<img\s+class=\"browse\"[^>]*?>/", "[图片]", $text);

View File

@ -7,6 +7,8 @@ use App\Models\Setting;
use App\Models\Tmp; use App\Models\Tmp;
use Cache; use Cache;
use Carbon\Carbon; use Carbon\Carbon;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Exception\CommonMarkException;
use Overtrue\Pinyin\Pinyin; use Overtrue\Pinyin\Pinyin;
use Redirect; use Redirect;
use Request; use Request;
@ -3102,4 +3104,18 @@ class Base
return $newArray; return $newArray;
} }
/**
* MD(markdown) html
* @param $markdown
* @return \League\CommonMark\Output\RenderedContentInterface|mixed
*/
public static function markdown2html($markdown)
{
$converter = new CommonMarkConverter();
try {
return $converter->convert($markdown);
} catch (CommonMarkException $e) {
return $markdown;
}
}
} }

View File

@ -114,7 +114,7 @@ class EmailNoticeTask extends AbstractTask
foreach ($items as $item) { foreach ($items as $item) {
$item->cancelAppend(); $item->cancelAppend();
$item->userInfo = User::userid2basic($item->userid); $item->userInfo = User::userid2basic($item->userid);
$item->preview = $item->previewMsg(true); $item->preview = WebSocketDialogMsg::previewMsg($item, true);
$item->preview = str_replace('<p>', '<p style="margin:0;padding:0">', $item->preview); $item->preview = str_replace('<p>', '<p style="margin:0;padding:0">', $item->preview);
if (empty($dialogId)) { if (empty($dialogId)) {
$dialogId = $item->dialog_id; $dialogId = $item->dialog_id;

View File

@ -194,7 +194,7 @@ class WebSocketDialogMsgTask extends AbstractTask
} }
$this->endArray[] = new PushUmengMsg($umengUserid, [ $this->endArray[] = new PushUmengMsg($umengUserid, [
'title' => $umengTitle, 'title' => $umengTitle,
'body' => $msg->previewMsg(), 'body' => WebSocketDialogMsg::previewMsg($msg),
'description' => "MID:{$msg->id}", 'description' => "MID:{$msg->id}",
'seconds' => 3600, 'seconds' => 3600,
'badge' => 1, 'badge' => 1,

View File

@ -27,6 +27,7 @@
"laravel/framework": "^v8.83.27", "laravel/framework": "^v8.83.27",
"laravel/tinker": "^v2.6.1", "laravel/tinker": "^v2.6.1",
"lasserafn/php-initial-avatar-generator": "^4.2", "lasserafn/php-initial-avatar-generator": "^4.2",
"league/commonmark": "^2.5",
"maatwebsite/excel": "^3.1.31", "maatwebsite/excel": "^3.1.31",
"madnest/madzipper": "^v1.1.0", "madnest/madzipper": "^v1.1.0",
"mews/captcha": "^3.2.6", "mews/captcha": "^3.2.6",

20
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "61663f2d5fbd196fc797c40ed46a9b2e", "content-hash": "827e08585b58695c5bee24b1f30fd08a",
"packages": [ "packages": [
{ {
"name": "asm89/stack-cors", "name": "asm89/stack-cors",
@ -2187,16 +2187,16 @@
}, },
{ {
"name": "league/commonmark", "name": "league/commonmark",
"version": "2.4.2", "version": "2.5.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/thephpleague/commonmark.git", "url": "https://github.com/thephpleague/commonmark.git",
"reference": "91c24291965bd6d7c46c46a12ba7492f83b1cadf" "reference": "b650144166dfa7703e62a22e493b853b58d874b0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/91c24291965bd6d7c46c46a12ba7492f83b1cadf", "url": "https://api.github.com/repos/thephpleague/commonmark/zipball/b650144166dfa7703e62a22e493b853b58d874b0",
"reference": "91c24291965bd6d7c46c46a12ba7492f83b1cadf", "reference": "b650144166dfa7703e62a22e493b853b58d874b0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2209,8 +2209,8 @@
}, },
"require-dev": { "require-dev": {
"cebe/markdown": "^1.0", "cebe/markdown": "^1.0",
"commonmark/cmark": "0.30.3", "commonmark/cmark": "0.31.1",
"commonmark/commonmark.js": "0.30.0", "commonmark/commonmark.js": "0.31.1",
"composer/package-versions-deprecated": "^1.8", "composer/package-versions-deprecated": "^1.8",
"embed/embed": "^4.4", "embed/embed": "^4.4",
"erusev/parsedown": "^1.0", "erusev/parsedown": "^1.0",
@ -2232,7 +2232,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-main": "2.5-dev" "dev-main": "2.6-dev"
} }
}, },
"autoload": { "autoload": {
@ -2289,7 +2289,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-02-02T11:59:32+00:00" "time": "2024-08-16T11:46:16+00:00"
}, },
{ {
"name": "league/config", "name": "league/config",
@ -10812,5 +10812,5 @@
"ext-zip": "*" "ext-zip": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

@ -236,14 +236,17 @@ import {MarkdownPreview} from "../store/markdown";
/** /**
* 返回文本信息预览格式 * 返回文本信息预览格式
* @param text * @param msgData
* @param imgClassName * @param imgClassName
* @returns {*} * @returns {*}
*/ */
getMsgTextPreview(text, imgClassName = null) { getMsgTextPreview({type, text}, imgClassName = null) {
if (!text) { if (!text) {
return ''; return '';
} }
if (type === 'md') {
text = MarkdownPreview(text);
}
// //
text = text.replace(/<img\s+class="emoticon"[^>]*?alt="(\S+)"[^>]*?>/g, "[$1]") text = text.replace(/<img\s+class="emoticon"[^>]*?alt="(\S+)"[^>]*?>/g, "[$1]")
text = text.replace(/<img\s+class="emoticon"[^>]*?>/g, `[${$A.L('动画表情')}]`) text = text.replace(/<img\s+class="emoticon"[^>]*?>/g, `[${$A.L('动画表情')}]`)
@ -398,11 +401,11 @@ import {MarkdownPreview} from "../store/markdown";
} }
switch (data.type) { switch (data.type) {
case 'text': case 'text':
return $A.getMsgTextPreview(data.msg.type === 'md' ? MarkdownPreview(data.msg.text) : data.msg.text, imgClassName) return $A.getMsgTextPreview(data.msg, imgClassName)
case 'vote': case 'vote':
return `[${$A.L('投票')}]` + $A.getMsgTextPreview(data.msg.text, imgClassName) return `[${$A.L('投票')}]` + $A.getMsgTextPreview(data.msg, imgClassName)
case 'word-chain': case 'word-chain':
return `[${$A.L('接龙')}]` + $A.getMsgTextPreview(data.msg.text, imgClassName) return `[${$A.L('接龙')}]` + $A.getMsgTextPreview(data.msg, imgClassName)
case 'record': case 'record':
return `[${$A.L('语音')}]` return `[${$A.L('语音')}]`
case 'meeting': case 'meeting':