From 7af0d9e87b6023dc7e195676194c14e0aa3087d3 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Thu, 14 Nov 2024 14:29:38 +0800 Subject: [PATCH] refactor: simplify code --- src/core/packet/context/operationContext.ts | 41 +++++++-------------- src/core/packet/utils/crypto/sha1Stream.ts | 16 +++++--- src/shell/napcat.ts | 2 +- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/core/packet/context/operationContext.ts b/src/core/packet/context/operationContext.ts index 66b36cce..3cf21980 100644 --- a/src/core/packet/context/operationContext.ts +++ b/src/core/packet/context/operationContext.ts @@ -65,35 +65,22 @@ export class PacketOperationContext { } async UploadResources(msg: PacketMsg[], groupUin: number = 0) { - const reqList = []; - for (const m of msg) { - for (const e of m.msg) { + const chatType = groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C; + const peerUid = groupUin ? String(groupUin) : this.context.napcore.basicInfo.uid; + const reqList = msg.flatMap(m => + m.msg.map(e => { if (e instanceof PacketMsgPicElement) { - reqList.push(this.context.highway.uploadImage({ - chatType: groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C, - peerUid: groupUin ? String(groupUin) : this.context.napcore.basicInfo.uid - }, e)); + return this.context.highway.uploadImage({ chatType, peerUid }, e); + } else if (e instanceof PacketMsgVideoElement) { + return this.context.highway.uploadVideo({ chatType, peerUid }, e); + } else if (e instanceof PacketMsgPttElement) { + return this.context.highway.uploadPtt({ chatType, peerUid }, e); + } else if (e instanceof PacketMsgFileElement) { + return this.context.highway.uploadFile({ chatType, peerUid }, e); } - if (e instanceof PacketMsgVideoElement) { - reqList.push(this.context.highway.uploadVideo({ - chatType: groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C, - peerUid: groupUin ? String(groupUin) : this.context.napcore.basicInfo.uid - }, e)); - } - if (e instanceof PacketMsgPttElement) { - reqList.push(this.context.highway.uploadPtt({ - chatType: groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C, - peerUid: groupUin ? String(groupUin) : this.context.napcore.basicInfo.uid - }, e)); - } - if (e instanceof PacketMsgFileElement) { - reqList.push(this.context.highway.uploadFile({ - chatType: groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C, - peerUid: groupUin ? String(groupUin) : this.context.napcore.basicInfo.uid - }, e)); - } - } - } + return null; + }).filter(Boolean) + ); const res = await Promise.allSettled(reqList); this.context.logger.info(`上传资源${res.length}个,失败${res.filter(r => r.status === 'rejected').length}个`); res.forEach((result, index) => { diff --git a/src/core/packet/utils/crypto/sha1Stream.ts b/src/core/packet/utils/crypto/sha1Stream.ts index 7eb5421c..18604e44 100644 --- a/src/core/packet/utils/crypto/sha1Stream.ts +++ b/src/core/packet/utils/crypto/sha1Stream.ts @@ -45,11 +45,17 @@ export class Sha1Stream { let e = this._state[4]; for (let i = 0; i < 80; i++) { - const [f, k] = (i < 20) ? [(b & c) | ((~b) & d), 0x5A827999] : - (i < 40) ? [b ^ c ^ d, 0x6ED9EBA1] : - (i < 60) ? [(b & c) | (b & d) | (c & d), 0x8F1BBCDC] : - [b ^ c ^ d, 0xCA62C1D6]; - const temp = (this.rotateLeft(a, 5) + f + k + e + w[i]) >>> 0; + let temp; + if (i < 20) { + temp = ((b & c) | (~b & d)) + 0x5A827999; + } else if (i < 40) { + temp = (b ^ c ^ d) + 0x6ED9EBA1; + } else if (i < 60) { + temp = ((b & c) | (b & d) | (c & d)) + 0x8F1BBCDC; + } else { + temp = (b ^ c ^ d) + 0xCA62C1D6; + } + temp += ((this.rotateLeft(a, 5) + e + w[i]) >>> 0); e = d; d = c; c = this.rotateLeft(b, 30) >>> 0; diff --git a/src/shell/napcat.ts b/src/shell/napcat.ts index af883e20..39b82bcf 100644 --- a/src/shell/napcat.ts +++ b/src/shell/napcat.ts @@ -290,7 +290,7 @@ export async function NCoreInitShell() { await initializeEngine(engine, basicInfoWrapper, dataPathGlobal, systemPlatform, systemVersion); await initializeLoginService(loginService, basicInfoWrapper, dataPathGlobal, systemVersion, hostname); - let quickLoginUin = cmdOptions.qq; + const quickLoginUin = cmdOptions.qq; const historyLoginList = (await loginService.getLoginList()).LocalLoginInfoList; const dataTimestape = new Date().getTime().toString();