refactor: normalize naming

This commit is contained in:
Wesley F. Young 2024-08-26 10:04:21 +08:00
parent 0bba329999
commit bc62feb71b
14 changed files with 29 additions and 33 deletions

View File

@ -1,4 +1,4 @@
import { NodeQQNTWrapperUtil, NTApiContext, WrapperNodeApi } from '@/core/wrapper'; import { NodeQQNTWrapperUtil, StableNTApiWrapper, WrapperNodeApi } from '@/core/wrapper';
import path from 'node:path'; import path from 'node:path';
import fs from 'node:fs'; import fs from 'node:fs';
import { InstanceContext } from './wrapper'; import { InstanceContext } from './wrapper';
@ -30,7 +30,7 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi {
export class NapCatCore { export class NapCatCore {
readonly context: InstanceContext; readonly context: InstanceContext;
readonly apis: NTApiContext; readonly apis: StableNTApiWrapper;
readonly eventWrapper: LegacyNTEventWrapper; readonly eventWrapper: LegacyNTEventWrapper;
// readonly eventChannel: NTEventChannel; // readonly eventChannel: NTEventChannel;
NapCatDataPath: string; NapCatDataPath: string;

View File

@ -15,7 +15,7 @@ export interface InstanceContext {
readonly pathWrapper: NapCatPathWrapper; readonly pathWrapper: NapCatPathWrapper;
} }
export interface NTApiContext { export interface StableNTApiWrapper {
FileApi: NTQQFileApi, FileApi: NTQQFileApi,
SystemApi: NTQQSystemApi, SystemApi: NTQQSystemApi,
CollectionApi: NTQQCollectionApi, CollectionApi: NTQQCollectionApi,

View File

@ -1,5 +1,5 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { OB11ForwardMessage, OB11Message, OB11MessageData } from '@/onebot'; import { OB11ForwardMessage } from '@/onebot';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
@ -14,10 +14,6 @@ const SchemaData = {
type Payload = FromSchema<typeof SchemaData>; type Payload = FromSchema<typeof SchemaData>;
interface Response {
messages: (OB11Message & { content: OB11MessageData })[];
}
export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> { export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> {
actionName = ActionName.GoCQHTTP_GetForwardMsg; actionName = ActionName.GoCQHTTP_GetForwardMsg;
payloadSchema = SchemaData; payloadSchema = SchemaData;
@ -39,7 +35,7 @@ export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> {
} }
const msgList = data.msgList; const msgList = data.msgList;
const messages = (await Promise.all(msgList.map(async msg => { const messages = (await Promise.all(msgList.map(async msg => {
const resMsg = await this.obContext.apiContext.MsgApi const resMsg = await this.obContext.apis.MsgApi
.parseMessage(msg); .parseMessage(msg);
if (!resMsg) return; if (!resMsg) return;
resMsg.message_id = MessageUnique.createMsg({ resMsg.message_id = MessageUnique.createMsg({

View File

@ -53,7 +53,7 @@ export default class GetFriendMsgHistory extends BaseAction<Payload, Response> {
})); }));
//转换消息 //转换消息
const ob11MsgList = (await Promise.all( const ob11MsgList = (await Promise.all(
msgList.map(msg => this.obContext.apiContext.MsgApi.parseMessage(msg))) msgList.map(msg => this.obContext.apis.MsgApi.parseMessage(msg)))
).filter(msg => msg !== undefined); ).filter(msg => msg !== undefined);
return { 'messages': ob11MsgList }; return { 'messages': ob11MsgList };
} }

View File

@ -48,7 +48,7 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction<Payload, Resp
//转换消息 //转换消息
const ob11MsgList = (await Promise.all( const ob11MsgList = (await Promise.all(
msgList.map(msg => this.obContext.apiContext.MsgApi.parseMessage(msg))) msgList.map(msg => this.obContext.apis.MsgApi.parseMessage(msg)))
).filter(msg => msg !== undefined); ).filter(msg => msg !== undefined);
return { 'messages': ob11MsgList }; return { 'messages': ob11MsgList };
} }

View File

@ -33,7 +33,7 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
throw new Error(downloadResult.errMsg); throw new Error(downloadResult.errMsg);
} }
const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name, payload.folder_id); const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name, payload.folder_id);
await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId({ await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId({
chatType: ChatType.KCHATTYPEGROUP, chatType: ChatType.KCHATTYPEGROUP,
peerUid: payload.group_id.toString(), peerUid: payload.group_id.toString(),
}, [sendFileEle], [], true); }, [sendFileEle], [], true);

View File

@ -45,7 +45,7 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction<Payload, null>
throw new Error(downloadResult.errMsg); throw new Error(downloadResult.errMsg);
} }
const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name); const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name);
await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], [], true); await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], [], true);
return null; return null;
} }
} }

View File

@ -36,7 +36,7 @@ class GetMsg extends BaseAction<Payload, OB11Message> {
const msg = await NTQQMsgApi.getMsgsByMsgId( const msg = await NTQQMsgApi.getMsgsByMsgId(
peer, peer,
[msgIdWithPeer?.MsgId || payload.message_id.toString()]); [msgIdWithPeer?.MsgId || payload.message_id.toString()]);
const retMsg = await this.obContext.apiContext.MsgApi.parseMessage(msg.msgList[0], 'array'); const retMsg = await this.obContext.apis.MsgApi.parseMessage(msg.msgList[0], 'array');
if (!retMsg) throw Error('消息为空'); if (!retMsg) throw Error('消息为空');
try { try {
retMsg.message_id = MessageUnique.createMsg(peer, msg.msgList[0].msgId)!; retMsg.message_id = MessageUnique.createMsg(peer, msg.msgList[0].msgId)!;

View File

@ -137,9 +137,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
// log("send msg:", peer, sendElements) // log("send msg:", peer, sendElements)
const { sendElements, deleteAfterSentFiles } = await this.obContext.apiContext.MsgApi const { sendElements, deleteAfterSentFiles } = await this.obContext.apis.MsgApi
.createSendElements(messages, peer); .createSendElements(messages, peer);
const returnMsg = await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles); const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles);
return { message_id: returnMsg!.id! }; return { message_id: returnMsg!.id! };
} }
@ -180,7 +180,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
//完成子卡片生成跳过后续 //完成子卡片生成跳过后续
continue; continue;
} }
const { sendElements } = await this.obContext.apiContext.MsgApi const { sendElements } = await this.obContext.apis.MsgApi
.createSendElements(OB11Data, destPeer); .createSendElements(OB11Data, destPeer);
//拆分消息 //拆分消息
const MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO); const MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO);
@ -188,7 +188,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
const AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0); const AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0);
const MsgNodeList: Promise<RawMessage | undefined>[] = []; const MsgNodeList: Promise<RawMessage | undefined>[] = [];
for (const sendElementsSplitElement of AllElement) { for (const sendElementsSplitElement of AllElement) {
MsgNodeList.push(this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(selfPeer, sendElementsSplitElement, [], true).catch(_ => undefined)); MsgNodeList.push(this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(selfPeer, sendElementsSplitElement, [], true).catch(_ => undefined));
} }
(await Promise.allSettled(MsgNodeList)).map((result) => { (await Promise.allSettled(MsgNodeList)).map((result) => {
if (result.status === 'fulfilled' && result.value) { if (result.status === 'fulfilled' && result.value) {

View File

@ -23,7 +23,7 @@ export default class GetRecentContact extends BaseAction<Payload, any> {
const FastMsg = await NTQQMsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]); const FastMsg = await NTQQMsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]);
if (FastMsg.msgList.length > 0) { if (FastMsg.msgList.length > 0) {
//扩展ret.info.changedList //扩展ret.info.changedList
const lastestMsg = await this.obContext.apiContext.MsgApi.parseMessage(FastMsg.msgList[0], 'array'); const lastestMsg = await this.obContext.apis.MsgApi.parseMessage(FastMsg.msgList[0], 'array');
return { return {
lastestMsg: lastestMsg, lastestMsg: lastestMsg,
peerUin: t.peerUin, peerUin: t.peerUin,

View File

@ -19,7 +19,7 @@ export async function NT2PrivateEvent(core: NapCatCore, obContext: NapCatOneBot1
if (element.grayTipElement) { if (element.grayTipElement) {
if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_JSON) { if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_JSON) {
if (element.grayTipElement.jsonGrayTipElement.busiId == 1061) { if (element.grayTipElement.jsonGrayTipElement.busiId == 1061) {
const PokeEvent = await obContext.apiContext.FriendApi.parsePrivatePokeEvent(element.grayTipElement); const PokeEvent = await obContext.apis.FriendApi.parsePrivatePokeEvent(element.grayTipElement);
if (PokeEvent) return PokeEvent; if (PokeEvent) return PokeEvent;
} }
} }
@ -56,15 +56,15 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A
if (element.grayTipElement && element.grayTipElement.groupElement) { if (element.grayTipElement && element.grayTipElement.groupElement) {
const groupElement = element.grayTipElement.groupElement; const groupElement = element.grayTipElement.groupElement;
if (groupElement.type == TipGroupElementType.memberIncrease) { if (groupElement.type == TipGroupElementType.memberIncrease) {
const MemberIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupMemberIncreaseEvent(msg.peerUid, element.grayTipElement); const MemberIncreaseEvent = await obContext.apis.GroupApi.parseGroupMemberIncreaseEvent(msg.peerUid, element.grayTipElement);
if (MemberIncreaseEvent) return MemberIncreaseEvent; if (MemberIncreaseEvent) return MemberIncreaseEvent;
} else if (groupElement.type === TipGroupElementType.ban) { } else if (groupElement.type === TipGroupElementType.ban) {
const BanEvent = await obContext.apiContext.GroupApi.parseGroupBanEvent(msg.peerUid, element.grayTipElement); const BanEvent = await obContext.apis.GroupApi.parseGroupBanEvent(msg.peerUid, element.grayTipElement);
if (BanEvent) return BanEvent; if (BanEvent) return BanEvent;
} else if (groupElement.type == TipGroupElementType.kicked) { } else if (groupElement.type == TipGroupElementType.kicked) {
NTQQGroupApi.quitGroup(msg.peerUid).then(); NTQQGroupApi.quitGroup(msg.peerUid).then();
try { try {
const KickEvent = await obContext.apiContext.GroupApi.parseGroupKickEvent(msg.peerUid, element.grayTipElement); const KickEvent = await obContext.apis.GroupApi.parseGroupKickEvent(msg.peerUid, element.grayTipElement);
if (KickEvent) return KickEvent; if (KickEvent) return KickEvent;
} catch (e) { } catch (e) {
return new OB11GroupDecreaseEvent( return new OB11GroupDecreaseEvent(
@ -90,11 +90,11 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A
} }
if (element.grayTipElement) { if (element.grayTipElement) {
if (element.grayTipElement.xmlElement?.templId === '10382') { if (element.grayTipElement.xmlElement?.templId === '10382') {
const emojiLikeEvent = await obContext.apiContext.GroupApi.parseGroupEmjioLikeEvent(msg.peerUid, element.grayTipElement); const emojiLikeEvent = await obContext.apis.GroupApi.parseGroupEmjioLikeEvent(msg.peerUid, element.grayTipElement);
if (emojiLikeEvent) return emojiLikeEvent; if (emojiLikeEvent) return emojiLikeEvent;
} }
if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) {
const GroupIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupIncreaseEvent(msg.peerUid, element.grayTipElement); const GroupIncreaseEvent = await obContext.apis.GroupApi.parseGroupIncreaseEvent(msg.peerUid, element.grayTipElement);
if (GroupIncreaseEvent) return GroupIncreaseEvent; if (GroupIncreaseEvent) return GroupIncreaseEvent;
} }

View File

@ -53,8 +53,8 @@ async function handleMsg(core: NapCatCore, obContext: NapCatOneBot11Adapter, msg
} }
} }
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape)); replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
const { sendElements, deleteAfterSentFiles } = await obContext.apiContext.MsgApi.createSendElements(replyMessage, peer); const { sendElements, deleteAfterSentFiles } = await obContext.apis.MsgApi.createSendElements(replyMessage, peer);
obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(core.context.logger.logError); obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(core.context.logger.logError);
} }
} }

View File

@ -14,7 +14,7 @@ import {
NodeIKernelGroupListener, NodeIKernelGroupListener,
} from '@/core'; } from '@/core';
import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config'; import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config';
import { OneBotApiContextType } from '@/onebot/types'; import { StableOneBotApiWrapper } from '@/onebot/types';
import { import {
OB11ActiveHttpAdapter, OB11ActiveHttpAdapter,
OB11ActiveWebSocketAdapter, OB11ActiveWebSocketAdapter,
@ -44,7 +44,7 @@ export class NapCatOneBot11Adapter {
readonly context: InstanceContext; readonly context: InstanceContext;
configLoader: OB11ConfigLoader; configLoader: OB11ConfigLoader;
apiContext: OneBotApiContextType; apis: StableOneBotApiWrapper;
networkManager: OB11NetworkManager; networkManager: OB11NetworkManager;
actions: ActionMap; actions: ActionMap;
@ -54,7 +54,7 @@ export class NapCatOneBot11Adapter {
this.core = core; this.core = core;
this.context = context; this.context = context;
this.configLoader = new OB11ConfigLoader(core, pathWrapper.configPath); this.configLoader = new OB11ConfigLoader(core, pathWrapper.configPath);
this.apiContext = { this.apis = {
GroupApi: new OneBotGroupApi(this, core), GroupApi: new OneBotGroupApi(this, core),
UserApi: new OneBotUserApi(this, core), UserApi: new OneBotUserApi(this, core),
FriendApi: new OneBotFriendApi(this, core), FriendApi: new OneBotFriendApi(this, core),
@ -259,7 +259,7 @@ export class NapCatOneBot11Adapter {
if (msg.sendStatus == SendStatusType.KSEND_STATUS_SUCCESS && !msgIdSend.get(msg.msgId)) { if (msg.sendStatus == SendStatusType.KSEND_STATUS_SUCCESS && !msgIdSend.get(msg.msgId)) {
msgIdSend.put(msg.msgId, true); msgIdSend.put(msg.msgId, true);
// 完成后再post // 完成后再post
this.apiContext.MsgApi.parseMessage(msg) this.apis.MsgApi.parseMessage(msg)
.then((ob11Msg) => { .then((ob11Msg) => {
if (!ob11Msg) return; if (!ob11Msg) return;
ob11Msg.target_id = parseInt(msg.peerUin); ob11Msg.target_id = parseInt(msg.peerUin);
@ -457,7 +457,7 @@ export class NapCatOneBot11Adapter {
private async emitMsg(message: RawMessage) { private async emitMsg(message: RawMessage) {
const { debug, reportSelfMessage, messagePostFormat } = this.configLoader.configData; const { debug, reportSelfMessage, messagePostFormat } = this.configLoader.configData;
this.context.logger.logDebug('收到新消息 RawMessage', message); this.context.logger.logDebug('收到新消息 RawMessage', message);
this.apiContext.MsgApi.parseMessage(message, messagePostFormat).then((ob11Msg) => { this.apis.MsgApi.parseMessage(message, messagePostFormat).then((ob11Msg) => {
if (!ob11Msg) return; if (!ob11Msg) return;
this.context.logger.logDebug('转化为 OB11Message', ob11Msg); this.context.logger.logDebug('转化为 OB11Message', ob11Msg);
if (debug) { if (debug) {

View File

@ -1,6 +1,6 @@
import { OneBotFriendApi, OneBotGroupApi, OneBotMsgApi, OneBotUserApi } from '../api'; import { OneBotFriendApi, OneBotGroupApi, OneBotMsgApi, OneBotUserApi } from '../api';
export interface OneBotApiContextType { export interface StableOneBotApiWrapper {
FriendApi: OneBotFriendApi; FriendApi: OneBotFriendApi;
UserApi: OneBotUserApi; UserApi: OneBotUserApi;
GroupApi: OneBotGroupApi; GroupApi: OneBotGroupApi;