From 8f5baa47ecffc75c3a6ae02d3b21c6c46a83b8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Thu, 14 Nov 2024 11:10:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20log=E6=9C=80=E4=BD=B3=E5=AE=9E?= =?UTF-8?q?=E8=B7=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/forward-msg-builder.ts | 8 +- src/common/log.ts | 185 ++++++++++++++++-------------- 2 files changed, 101 insertions(+), 92 deletions(-) diff --git a/src/common/forward-msg-builder.ts b/src/common/forward-msg-builder.ts index 55ecd65f..c091c542 100644 --- a/src/common/forward-msg-builder.ts +++ b/src/common/forward-msg-builder.ts @@ -54,11 +54,7 @@ export class ForwardMsgBuilder { const id = crypto.randomUUID(); const isGroupMsg = msg.some(m => m.isGroupMsg); if (!source) { - source = isGroupMsg ? "群聊的聊天记录" : - msg.length - ? Array.from(new Set(msg.slice(0, 4).map(m => m.senderName))) - .join('和') + '的聊天记录' - : '聊天记录'; + source = isGroupMsg ? "群聊的聊天记录" : msg.map(m => m.senderName).filter((v, i, a) => a.indexOf(v) === i).slice(0, 4).join('和') + '的聊天记录'; } if (!news) { news = msg.length === 0 ? [{ @@ -111,7 +107,7 @@ export class ForwardMsgBuilder { senderName: msg.senderName, isGroupMsg: msg.groupId !== undefined, msg: msg.msg.map(m => ({ - preview: m.valid? m.toPreview() : "[该消息类型暂不支持查看]", + preview: m.valid ? m.toPreview() : "[该消息类型暂不支持查看]", })) })), source, news, summary, prompt); } diff --git a/src/common/log.ts b/src/common/log.ts index 346baaed..569d8200 100644 --- a/src/common/log.ts +++ b/src/common/log.ts @@ -74,26 +74,30 @@ export class LogWrapper { } files.forEach(file => { const filePath = path.join(logDir, file); - fs.stat(filePath, (err, stats) => { + this.deleteOldLogFile(filePath, oneWeekAgo); + }); + }); + } + + private deleteOldLogFile(filePath: string, oneWeekAgo: number) { + fs.stat(filePath, (err, stats) => { + if (err) { + this.logger.error('Failed to get file stats', err); + return; + } + if (stats.mtime.getTime() < oneWeekAgo) { + fs.unlink(filePath, err => { if (err) { - this.logger.error('Failed to get file stats', err); - return; - } - if (stats.mtime.getTime() < oneWeekAgo) { - fs.unlink(filePath, err => { - if (err) { - if (err.code === 'ENOENT') { - this.logger.warn(`File already deleted: ${file}`); - } else { - this.logger.error('Failed to delete old log file', err); - } - } else { - this.logger.info(`Deleted old log file: ${file}`); - } - }); + if (err.code === 'ENOENT') { + this.logger.warn(`File already deleted: ${filePath}`); + } else { + this.logger.error('Failed to delete old log file', err); + } + } else { + this.logger.info(`Deleted old log file: ${filePath}`); } }); - }); + } }); } @@ -198,7 +202,7 @@ export function rawMessageToText(msg: RawMessage, recursiveLevel = 0): string { tokens.push(`群聊 [${msg.peerName}(${msg.peerUin})]`); } if (msg.senderUin !== '0') { - tokens.push(`[${msg.sendMemberName || msg.sendRemarkName || msg.sendNickName}(${msg.senderUin})]`); + tokens.push(`[${msg.sendMemberName ?? msg.sendRemarkName ?? msg.sendNickName}(${msg.senderUin})]`); } } else if (msg.chatType == ChatType.KCHATTYPEDATALINE) { tokens.push('移动设备'); @@ -206,76 +210,85 @@ export function rawMessageToText(msg: RawMessage, recursiveLevel = 0): string { tokens.push(`临时消息 (${msg.peerUin})`); } - function msgElementToText(element: MessageElement) { - if (element.textElement) { - if (element.textElement.atType === AtType.notAt) { - const originalContentLines = element.textElement.content.split('\n'); - return `${originalContentLines[0]}${originalContentLines.length > 1 ? ' ...' : ''}`; - } else if (element.textElement.atType === AtType.atAll) { - return `@全体成员`; - } else if (element.textElement.atType === AtType.atUser) { - return `${element.textElement.content} (${element.textElement.atUid})`; - } - } - - if (element.replyElement) { - const recordMsgOrNull = msg.records.find( - record => element.replyElement!.sourceMsgIdInRecords === record.msgId, - ); - return `[回复消息 ${recordMsgOrNull && - recordMsgOrNull.peerUin != '284840486' && recordMsgOrNull.peerUin != '1094950020' - ? - rawMessageToText(recordMsgOrNull, recursiveLevel + 1) : - `未找到消息记录 (MsgId = ${element.replyElement.sourceMsgIdInRecords})` - }]`; - } - - if (element.picElement) { - return '[图片]'; - } - - if (element.fileElement) { - return `[文件 ${element.fileElement.fileName}]`; - } - - if (element.videoElement) { - return '[视频]'; - } - - if (element.pttElement) { - return `[语音 ${element.pttElement.duration}s]`; - } - - if (element.arkElement) { - return '[卡片消息]'; - } - - if (element.faceElement) { - return `[表情 ${element.faceElement.faceText ?? ''}]`; - } - - if (element.marketFaceElement) { - return element.marketFaceElement.faceName; - } - - if (element.markdownElement) { - return '[Markdown 消息]'; - } - - if (element.multiForwardMsgElement) { - return '[转发消息]'; - } - - if (element.elementType === ElementType.GreyTip) { - return '[灰条消息]'; - } - - return `[未实现 (ElementType = ${element.elementType})]`; - } - for (const element of msg.elements) { - tokens.push(msgElementToText(element)); + tokens.push(msgElementToText(element, msg, recursiveLevel)); } return tokens.join(' '); } + +function msgElementToText(element: MessageElement, msg: RawMessage, recursiveLevel: number): string { + if (element.textElement) { + return textElementToText(element.textElement); + } + + if (element.replyElement) { + return replyElementToText(element.replyElement, msg, recursiveLevel); + } + + if (element.picElement) { + return '[图片]'; + } + + if (element.fileElement) { + return `[文件 ${element.fileElement.fileName}]`; + } + + if (element.videoElement) { + return '[视频]'; + } + + if (element.pttElement) { + return `[语音 ${element.pttElement.duration}s]`; + } + + if (element.arkElement) { + return '[卡片消息]'; + } + + if (element.faceElement) { + return `[表情 ${element.faceElement.faceText ?? ''}]`; + } + + if (element.marketFaceElement) { + return element.marketFaceElement.faceName; + } + + if (element.markdownElement) { + return '[Markdown 消息]'; + } + + if (element.multiForwardMsgElement) { + return '[转发消息]'; + } + + if (element.elementType === ElementType.GreyTip) { + return '[灰条消息]'; + } + + return `[未实现 (ElementType = ${element.elementType})]`; +} + +function textElementToText(textElement: any): string { + if (textElement.atType === AtType.notAt) { + const originalContentLines = textElement.content.split('\n'); + return `${originalContentLines[0]}${originalContentLines.length > 1 ? ' ...' : ''}`; + } else if (textElement.atType === AtType.atAll) { + return `@全体成员`; + } else if (textElement.atType === AtType.atUser) { + return `${textElement.content} (${textElement.atUid})`; + } + return ''; +} + +function replyElementToText(replyElement: any, msg: RawMessage, recursiveLevel: number): string { + const recordMsgOrNull = msg.records.find( + record => replyElement.sourceMsgIdInRecords === record.msgId, + ); + return `[回复消息 ${recordMsgOrNull && + recordMsgOrNull.peerUin != '284840486' && recordMsgOrNull.peerUin != '1094950020' + ? + rawMessageToText(recordMsgOrNull, recursiveLevel + 1) : + `未找到消息记录 (MsgId = ${replyElement.sourceMsgIdInRecords})` + }]`; +} \ No newline at end of file