From 72d2d3f2249f064325a062935369d3011a33b132 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: Mon, 22 Jul 2024 14:09:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A0=B4=E5=9D=8Ffile/db=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/db.ts | 2 +- src/common/utils/file.ts | 16 +-- src/core/src/core.ts | 30 ++--- src/onebot11/action/file/GetFile.ts | 126 +++++++++--------- .../msg/SendMsg/create-send-elements.ts | 43 +++--- .../action/msg/SendMsg/handle-forward-node.ts | 1 - src/onebot11/action/system/CleanCache.ts | 1 - src/onebot11/constructor.ts | 68 +++++----- src/onebot11/main.ts | 3 - 9 files changed, 142 insertions(+), 148 deletions(-) diff --git a/src/common/utils/db.ts b/src/common/utils/db.ts index 82ac9ada..dc1f008a 100644 --- a/src/common/utils/db.ts +++ b/src/common/utils/db.ts @@ -474,4 +474,4 @@ class DBUtil extends DBUtilBase { } -export const dbUtil = new DBUtil(); +// export const dbUtil = new DBUtil(); diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index c6d6bcff..453da6c9 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -4,7 +4,6 @@ import crypto from 'crypto'; import util from 'util'; import path from 'node:path'; import { log, logError } from './log'; -import { dbUtil } from '@/common/utils/db'; import * as fileType from 'file-type'; import { v4 as uuidv4 } from 'uuid'; import { napCatCore } from '@/core'; @@ -251,13 +250,14 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr } else { filePath = pathname; } - } else { - const cache = await dbUtil.getFileCacheByName(uri); - if (cache) { - filePath = cache.path; - } else { - filePath = uri; - } + } + else { + // const cache = await dbUtil.getFileCacheByName(uri); + // if (cache) { + // filePath = cache.path; + // } else { + // filePath = uri; + // } } res.isLocal = true; diff --git a/src/core/src/core.ts b/src/core/src/core.ts index f4ecc7d6..ea8ef8c8 100644 --- a/src/core/src/core.ts +++ b/src/core/src/core.ts @@ -17,7 +17,6 @@ import fs from 'node:fs'; import { appid, qqVersionConfigInfo } from '@/common/utils/QQBasicInfo'; import { hostname, systemVersion } from '@/common/utils/system'; import { genSessionConfig } from '@/core/sessionConfig'; -import { dbUtil } from '@/common/utils/db'; import { sleep } from '@/common/utils/helper'; import crypto from 'node:crypto'; import { rawFriends, friends, groupMembers, groups, selfInfo, stat } from '@/core/data'; @@ -84,20 +83,21 @@ export class NapCatCore { const dataPath = path.resolve(this.dataPath, './NapCat/data'); fs.mkdirSync(dataPath, { recursive: true }); logDebug('本账号数据/缓存目录:', dataPath); - dbUtil.init(path.resolve(dataPath, `./${arg.uin}-v2.db`)).then(() => { - this.initDataListener(); - this.onLoginSuccessFuncList.map(cb => { - new Promise((resolve, reject) => { - const result = cb(arg.uin, arg.uid); - if (result instanceof Promise) { - result.then(resolve).catch(reject); - } - }).then(); - }); - }).catch((e) => { - logError('数据库初始化失败', e); - }); + // dbUtil.init(path.resolve(dataPath, `./${arg.uin}-v2.db`)).then(() => { + // this.initDataListener(); + // this.onLoginSuccessFuncList.map(cb => { + // new Promise((resolve, reject) => { + // const result = cb(arg.uin, arg.uid); + // if (result instanceof Promise) { + // result.then(resolve).catch(reject); + // } + // }).then(); + // }); + // }).catch((e) => { + // logError('数据库初始化失败', e); + // }); // this.initDataListener(); + }).catch((e) => { logError('initSession failed', e); throw new Error(`启动失败: ${JSON.stringify(e)}`); @@ -475,7 +475,7 @@ export class NapCatCore { return loginList; } checkAdminEvent(groupCode: string, memberNew: GroupMember, memberOld: GroupMember | undefined ) : boolean { - if (memberNew.role !== memberOld.role) { + if (memberNew.role !== memberOld?.role) { log(`群 ${groupCode} ${memberNew.nick} 角色变更为 ${memberNew.role === 3 ? '管理员' : '群员' }`); return true; } diff --git a/src/onebot11/action/file/GetFile.ts b/src/onebot11/action/file/GetFile.ts index 16efdfed..77691e1d 100644 --- a/src/onebot11/action/file/GetFile.ts +++ b/src/onebot11/action/file/GetFile.ts @@ -1,15 +1,15 @@ import BaseAction from '../BaseAction'; import fs from 'fs/promises'; -import { dbUtil } from '@/common/utils/db'; import { ob11Config } from '@/onebot11/config'; import { log, logDebug } from '@/common/utils/log'; import { sleep } from '@/common/utils/helper'; import { uri2local } from '@/common/utils/file'; import { ActionName, BaseCheckResult } from '../types'; import { FileElement, RawMessage, VideoElement } from '@/core/entities'; -import { NTQQFileApi } from '@/core/apis'; +import { NTQQFileApi, NTQQMsgApi } from '@/core/apis'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import Ajv from 'ajv'; +import { MessageUnique } from '@/common/utils/MessageUnique'; export interface GetFilePayload { file: string; // 文件名或者fileUuid @@ -45,69 +45,67 @@ export class GetFileBase extends BaseAction { return { id: element.elementId, element: element.fileElement }; } protected async _handle(payload: GetFilePayload): Promise { - let cache = await dbUtil.getFileCacheByName(payload.file); - if (!cache) { - cache = await dbUtil.getFileCacheByUuid(payload.file); - } - if (!cache) { - throw new Error('file not found'); - } - const { enableLocalFile2Url } = ob11Config; - try { - await fs.access(cache.path, fs.constants.F_OK); - } catch (e) { - logDebug('local file not found, start download...'); - // if (cache.url) { - // const downloadResult = await uri2local(cache.url); - // if (downloadResult.success) { - // cache.path = downloadResult.path; - // dbUtil.updateFileCache(cache).then(); - // } else { - // throw new Error('file download failed. ' + downloadResult.errMsg); - // } - // } else { - // // 没有url的可能是私聊文件或者群文件,需要自己下载 - // log('需要调用 NTQQ 下载文件api'); - let msg = await dbUtil.getMsgByLongId(cache.msgId); - // log('文件 msg', msg); - if (msg) { - // 构建下载函数 - const downloadPath = await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, - cache.elementId, '', ''); - // await sleep(1000); - - // log('download result', downloadPath); - msg = await dbUtil.getMsgByLongId(cache.msgId); - // log('下载完成后的msg', msg); - cache.path = downloadPath!; - dbUtil.updateFileCache(cache).then(); - // log('下载完成后的msg', msg); - // } - } - - } - // log('file found', cache); - const res: GetFileResponse = { - file: cache.path, - url: cache.url, - file_size: cache.size.toString(), - file_name: cache.name - }; - if (enableLocalFile2Url) { - if (!cache.url) { - try { - res.base64 = await fs.readFile(cache.path, 'base64'); - } catch (e) { - throw new Error('文件下载失败. ' + e); - } - } - } - // if (autoDeleteFile) { - // setTimeout(() => { - // fs.unlink(cache.filePath) - // }, autoDeleteFileSecond * 1000) + throw new Error('file not found'); + // let cache = await dbUtil.getFileCacheByName(payload.file); + // if (!cache) { + // cache = await dbUtil.getFileCacheByUuid(payload.file); // } - return res; + // if (!cache) { + // throw new Error('file not found'); + // } + // const { enableLocalFile2Url } = ob11Config; + // try { + // await fs.access(cache.path, fs.constants.F_OK); + // } catch (e) { + // logDebug('local file not found, start download...'); + // // if (cache.url) { + // // const downloadResult = await uri2local(cache.url); + // // if (downloadResult.success) { + // // cache.path = downloadResult.path; + // // dbUtil.updateFileCache(cache).then(); + // // } else { + // // throw new Error('file download failed. ' + downloadResult.errMsg); + // // } + // // } else { + // // // 没有url的可能是私聊文件或者群文件,需要自己下载 + // // log('需要调用 NTQQ 下载文件api'); + // let peer = MessageUnique.getPeerByMsgId(cache.msgId); + // let msg = await NTQQMsgApi.getMsgsByMsgId(peer?.Peer!,cache.msgId); + // // log('文件 msg', msg); + // if (msg) { + // // 构建下载函数 + // const downloadPath = await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, + // cache.elementId, '', ''); + // // await sleep(1000); + + // // log('download result', downloadPath); + // let peer = MessageUnique.getPeerByMsgId(cache.msgId); + // msg = await NTQQMsgApi.getMsgsByMsgId(peer?.Peer!,cache.msgId); + // // log('下载完成后的msg', msg); + // cache.path = downloadPath!; + // dbUtil.updateFileCache(cache).then(); + // // log('下载完成后的msg', msg); + // // } + // } + + // } + // // log('file found', cache); + // const res: GetFileResponse = { + // file: cache.path, + // url: cache.url, + // file_size: cache.size.toString(), + // file_name: cache.name + // }; + // if (enableLocalFile2Url) { + // if (!cache.url) { + // try { + // res.base64 = await fs.readFile(cache.path, 'base64'); + // } catch (e) { + // throw new Error('文件下载失败. ' + e); + // } + // } + // } + //return res; } } diff --git a/src/onebot11/action/msg/SendMsg/create-send-elements.ts b/src/onebot11/action/msg/SendMsg/create-send-elements.ts index 80081f31..112cc775 100644 --- a/src/onebot11/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot11/action/msg/SendMsg/create-send-elements.ts @@ -5,13 +5,13 @@ import { Group, IdMusicSignPostData, NTQQFileApi, + NTQQMsgApi, SendArkElement, SendMessageElement, SendMsgElementConstructor, SignMusicWrapper } from '@/core'; import { getGroupMember } from '@/core/data'; -import { dbUtil } from '@/common/utils/db'; import { logDebug, logError } from '@/common/utils/log'; import { uri2local } from '@/common/utils/file'; import { ob11Config } from '@/onebot11/config'; @@ -30,25 +30,25 @@ async function handleOb11FileLikeMessage( ) { let uri = file; - const cache = await dbUtil.getFileCacheByName(file); - if (cache) { - if (fs.existsSync(cache.path)) { - uri = 'file://' + cache.path; - } else if (cache.url) { - uri = cache.url; - } else { - const fileMsgPeer = MessageUnique.getPeerByMsgId(cache.msgId); - if (fileMsgPeer) { - cache.path = await NTQQFileApi.downloadMedia( - fileMsgPeer.MsgId, fileMsgPeer.Peer.chatType, fileMsgPeer.Peer.peerUid, - cache.elementId, '', '' - ); - uri = 'file://' + cache.path; - dbUtil.updateFileCache(cache); - } - } - logDebug('找到文件缓存', uri); - } + // const cache = await dbUtil.getFileCacheByName(file); + // if (cache) { + // if (fs.existsSync(cache.path)) { + // uri = 'file://' + cache.path; + // } else if (cache.url) { + // uri = cache.url; + // } else { + // const fileMsgPeer = MessageUnique.getPeerByMsgId(cache.msgId); + // if (fileMsgPeer) { + // cache.path = await NTQQFileApi.downloadMedia( + // fileMsgPeer.MsgId, fileMsgPeer.Peer.chatType, fileMsgPeer.Peer.peerUid, + // cache.elementId, '', '' + // ); + // uri = 'file://' + cache.path; + // dbUtil.updateFileCache(cache); + // } + // } + // logDebug('找到文件缓存', uri); + // } const { path, isLocal, fileName, errMsg } = (await uri2local(uri)); @@ -87,7 +87,8 @@ const _handlers: { }, [OB11MessageDataType.reply]: async ({ data: { id } }) => { - const replyMsg = await dbUtil.getMsgByShortId(parseInt(id)); + const replyMsgM = MessageUnique.getMsgIdAndPeerByShortId(parseInt(id)); + const replyMsg = (await NTQQMsgApi.getMsgsByMsgId(replyMsgM?.Peer!, [replyMsgM?.MsgId!])).msgList[0]; return replyMsg ? SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsg.msgId, replyMsg.senderUin!, replyMsg.senderUin!) : undefined; diff --git a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts index 214aa470..b30b0d1b 100644 --- a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts +++ b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts @@ -4,7 +4,6 @@ import { selfInfo } from '@/core/data'; import createSendElements from '@/onebot11/action/msg/SendMsg/create-send-elements'; import { logDebug, logError } from '@/common/utils/log'; import { sleep } from '@/common/utils/helper'; -import fs from 'node:fs'; import { normalize, sendMsg } from '@/onebot11/action/msg/SendMsg/index'; import { MessageUnique } from '@/common/utils/MessageUnique'; diff --git a/src/onebot11/action/system/CleanCache.ts b/src/onebot11/action/system/CleanCache.ts index 0613cfeb..0e0cafc2 100644 --- a/src/onebot11/action/system/CleanCache.ts +++ b/src/onebot11/action/system/CleanCache.ts @@ -7,7 +7,6 @@ import { ChatCacheListItemBasic, CacheFileType } from '@/core/entities'; -import { dbUtil } from '@/common/utils/db'; import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file'; import { logError } from '@/common/utils/log'; diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 03e8f884..1318981b 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -42,7 +42,6 @@ import { ob11Config } from '@/onebot11/config'; import { deleteGroup, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/core/data'; import { NTQQFileApi, NTQQGroupApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis'; import { OB11GroupMsgEmojiLikeEvent } from '@/onebot11/event/notice/OB11MsgEmojiLikeEvent'; -import { napCatCore } from '@/core'; import { OB11FriendPokeEvent, OB11GroupPokeEvent } from './event/notice/OB11PokeEvent'; import { OB11BaseNoticeEvent } from './event/notice/OB11BaseNoticeEvent'; import { OB11GroupEssenceEvent } from './event/notice/OB11GroupEssenceEvent'; @@ -178,17 +177,17 @@ export class OB11Constructor { //console.log(message_data['data']['url']) // message_data["data"]["file_id"] = element.picElement.fileUuid message_data['data']['file_size'] = element.picElement.fileSize; - dbUtil.addFileCache({ - name: element.picElement.fileName, - path: element.picElement.sourcePath, - size: element.picElement.fileSize, - url: message_data['data']['url'], - uuid: element.picElement.fileUuid || '', - msgId: msg.msgId, - element: element.picElement, - elementType: ElementType.PIC, - elementId: element.elementId - }).then(); + // dbUtil.addFileCache({ + // name: element.picElement.fileName, + // path: element.picElement.sourcePath, + // size: element.picElement.fileSize, + // url: message_data['data']['url'], + // uuid: element.picElement.fileUuid || '', + // msgId: msg.msgId, + // element: element.picElement, + // elementType: ElementType.PIC, + // elementId: element.elementId + // }).then(); // 不自动下载图片 } @@ -203,17 +202,18 @@ export class OB11Constructor { message_data['data']['file_id'] = videoOrFileElement.fileUuid; message_data['data']['file_size'] = videoOrFileElement.fileSize; if (!element.videoElement) { - dbUtil.addFileCache({ - msgId: msg.msgId, - name: videoOrFileElement.fileName, - path: videoOrFileElement.filePath, - size: parseInt(videoOrFileElement.fileSize || '0'), - uuid: videoOrFileElement.fileUuid || '', - url: '', - element: element.videoElement || element.fileElement, - elementType: element.videoElement ? ElementType.VIDEO : ElementType.FILE, - elementId: element.elementId - }).then(); + // dbUtil.addFileCache({ + // msgId: msg.msgId, + // name: videoOrFileElement.fileName, + // path: videoOrFileElement.filePath, + // size: parseInt(videoOrFileElement.fileSize || '0'), + // uuid: videoOrFileElement.fileUuid || '', + // url: '', + // element: element.videoElement || element.fileElement, + // elementType: element.videoElement ? ElementType.VIDEO : ElementType.FILE, + // elementId: element.elementId + // }).then(); + // } } } else if (element.pttElement) { @@ -222,17 +222,17 @@ export class OB11Constructor { message_data['data']['path'] = element.pttElement.filePath; // message_data["data"]["file_id"] = element.pttElement.fileUuid message_data['data']['file_size'] = element.pttElement.fileSize; - dbUtil.addFileCache({ - name: element.pttElement.fileName, - path: element.pttElement.filePath, - size: parseInt(element.pttElement.fileSize) || 0, - url: '', - uuid: element.pttElement.fileUuid || '', - msgId: msg.msgId, - element: element.pttElement, - elementType: ElementType.PTT, - elementId: element.elementId - }).then(); + // dbUtil.addFileCache({ + // name: element.pttElement.fileName, + // path: element.pttElement.filePath, + // size: parseInt(element.pttElement.fileSize) || 0, + // url: '', + // uuid: element.pttElement.fileUuid || '', + // msgId: msg.msgId, + // element: element.pttElement, + // elementType: ElementType.PTT, + // elementId: element.elementId + // }).then(); } else if (element.arkElement) { diff --git a/src/onebot11/main.ts b/src/onebot11/main.ts index ca07b2c8..90df28ee 100644 --- a/src/onebot11/main.ts +++ b/src/onebot11/main.ts @@ -333,9 +333,6 @@ export class NapCatOnebot11 { const { debug, reportSelfMessage } = ob11Config; for (const message of msgList) { logDebug('收到新消息', message); - // if (message.senderUin !== selfInfo.uin){ - // message.msgShortId = await dbUtil.addMsg(message); - // } OB11Constructor.message(message).then((msg) => { logDebug('收到消息: ', msg); if (debug) {