mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: 改造接口调用
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
import { ChatType, GetFileListParam, Peer, RawMessage, SendMessageElement } from '@/core/entities';
|
||||
import { GetFileListParam, Peer, RawMessage, SendMessageElement } from '@/core/entities';
|
||||
import { friends, groups, selfInfo } from '@/core/data';
|
||||
import { log, logError, logWarn } from '@/common/utils/log';
|
||||
import { log, logWarn } from '@/common/utils/log';
|
||||
import { sleep } from '@/common/utils/helper';
|
||||
import { napCatCore, NTQQUserApi } from '@/core';
|
||||
import { MsgListener, onGroupFileInfoUpdateParamType } from '@/core/listeners';
|
||||
import { onGroupFileInfoUpdateParamType } from '@/core/listeners';
|
||||
import { GeneralCallResult } from '@/core/services/common';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { MessageUnique } from '../../../common/utils/MessageUnique';
|
||||
import { NTEventDispatch } from '@/common/utils/EventTask';
|
||||
async function LoadMessageIdList(Peer: Peer, msgId: string) {
|
||||
let msgList = await NTQQMsgApi.getMsgHistory(Peer, msgId, 50);
|
||||
for (let j = 0; j < msgList.msgList.length; j++) {
|
||||
let shortId = MessageUnique.createMsg(Peer, msgList.msgList[j].msgId);
|
||||
//console.log(`开始创建 ${shortId}<------>${msgList.msgList[j].msgId}`)
|
||||
}
|
||||
}
|
||||
async function loadMessageUnique() {
|
||||
@@ -44,61 +42,6 @@ setTimeout(() => {
|
||||
});
|
||||
}, 100);
|
||||
|
||||
// const sendMessagePool: Record<string, ((sendSuccessMsg: RawMessage) => void | Promise<void>) | null> = {};// peerUid: callbackFunc
|
||||
|
||||
// const sendSuccessCBMap: Record<string, ((sendSuccessMsg: RawMessage) => boolean | Promise<boolean>) | null> = {};// uuid: callbackFunc
|
||||
|
||||
const GroupFileInfoUpdateTasks: Map<string, ((groupFileListResult: onGroupFileInfoUpdateParamType) => void)> = new Map();
|
||||
|
||||
const sentMsgTasks: Map<string, (msg: RawMessage) => void> = new Map();
|
||||
|
||||
const msgListener = new MsgListener();
|
||||
|
||||
msgListener.onGroupFileInfoUpdate = (groupFileListResult: onGroupFileInfoUpdateParamType) => {
|
||||
for (const [uuid, cb] of GroupFileInfoUpdateTasks) {
|
||||
cb(groupFileListResult);
|
||||
GroupFileInfoUpdateTasks.delete(uuid);
|
||||
}
|
||||
};
|
||||
|
||||
msgListener.onAddSendMsg = (msgRecord: RawMessage) => {
|
||||
// console.log("sent msg", msgRecord, sendMessagePool);
|
||||
for (const [uuid, cb] of sentMsgTasks) {
|
||||
cb(msgRecord);
|
||||
sentMsgTasks.delete(uuid);
|
||||
}
|
||||
};
|
||||
|
||||
// msgListener.onMsgInfoListUpdate = (msgInfoList: RawMessage[]) => {
|
||||
// msgInfoList.forEach(msg => {
|
||||
// new Promise((resolve, reject) => {
|
||||
// for (const cbId in sendSuccessCBMap) {
|
||||
// const cb = sendSuccessCBMap[cbId]!;
|
||||
// const cbResult = cb(msg);
|
||||
// const checkResult = (result: boolean) => {
|
||||
// if (result) {
|
||||
// delete sendSuccessCBMap[cbId];
|
||||
// }
|
||||
// };
|
||||
// if (cbResult instanceof Promise) {
|
||||
// cbResult.then(checkResult);
|
||||
// } else {
|
||||
// checkResult(cbResult);
|
||||
// }
|
||||
// }
|
||||
// }).then().catch(log);
|
||||
// });
|
||||
// };
|
||||
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
napCatCore.onLoginSuccess(() => {
|
||||
napCatCore.addListener(msgListener);
|
||||
});
|
||||
}, 100);
|
||||
|
||||
|
||||
export class NTQQMsgApi {
|
||||
// static napCatCore: NapCatCore | null = null;
|
||||
// enum BaseEmojiType {
|
||||
@@ -127,43 +70,30 @@ export class NTQQMsgApi {
|
||||
static async getMsgsBySeqAndCount(peer: Peer, seq: string, count: number, desc: boolean, z: boolean) {
|
||||
return await napCatCore.session.getMsgService().getMsgsBySeqAndCount(peer, seq, count, desc, z);
|
||||
}
|
||||
|
||||
static async activateChat(peer: Peer) {
|
||||
// await this.fetchRecentContact();
|
||||
// await sleep(500);
|
||||
}
|
||||
|
||||
static async activateChatAndGetHistory(peer: Peer) {
|
||||
|
||||
}
|
||||
static async setMsgRead(peer: Peer) {
|
||||
return napCatCore.session.getMsgService().setMsgRead(peer);
|
||||
}
|
||||
static async getGroupFileList(GroupCode: string, params: GetFileListParam) {
|
||||
return new Promise<Array<any>>(async (resolve, reject) => {
|
||||
let complete = false;
|
||||
setTimeout(() => {
|
||||
if (!complete) {
|
||||
reject('获取群文件列表超时');
|
||||
}
|
||||
}, 5000);
|
||||
const GroupFileInfoUpdateCB = (groupFileListResult: onGroupFileInfoUpdateParamType) => {
|
||||
complete = true;
|
||||
resolve(groupFileListResult.item);
|
||||
};
|
||||
GroupFileInfoUpdateTasks.set(randomUUID(), GroupFileInfoUpdateCB);
|
||||
await napCatCore.session.getRichMediaService().getGroupFileList(GroupCode, params);
|
||||
});
|
||||
let data = await NTEventDispatch.CallNormalEvent<
|
||||
(GroupCode: string, params: GetFileListParam) => Promise<unknown>,
|
||||
(groupFileListResult: onGroupFileInfoUpdateParamType) => void
|
||||
>(
|
||||
'NodeIKernelRichMediaService/sendMsg',
|
||||
'NodeIKernelMsgListener/onGroupFileInfoUpdate',
|
||||
1,
|
||||
5000,
|
||||
(groupFileListResult: onGroupFileInfoUpdateParamType) => {
|
||||
return true;
|
||||
},
|
||||
GroupCode,
|
||||
params
|
||||
);
|
||||
return data[1].item;
|
||||
}
|
||||
static async getMsgHistory(peer: Peer, msgId: string, count: number) {
|
||||
// 消息时间从旧到新
|
||||
return napCatCore.session.getMsgService().getMsgsIncludeSelf(peer, msgId, count, true);
|
||||
}
|
||||
|
||||
static async fetchRecentContact() {
|
||||
|
||||
}
|
||||
|
||||
static async recallMsg(peer: Peer, msgIds: string[]) {
|
||||
await napCatCore.session.getMsgService().recallMsg({
|
||||
chatType: peer.chatType,
|
||||
@@ -199,59 +129,7 @@ export class NTQQMsgApi {
|
||||
}
|
||||
});
|
||||
return retMsg;
|
||||
//const result = napCatCore.session.getMsgService().sendMsg(msgId, peer, msgElements, new Map());
|
||||
}
|
||||
// static async sendMsg(peer: Peer, msgElements: SendMessageElement[], waitComplete = true, timeout = 10000): Promise<RawMessage> {
|
||||
// const peerUid = peer.peerUid;
|
||||
// // 等待上一个相同的peer发送完
|
||||
// let checkLastSendUsingTime = 0;
|
||||
// const waitLastSend: () => Promise<void> = async () => {
|
||||
// if (checkLastSendUsingTime > timeout) {
|
||||
// throw ('发送超时');
|
||||
// }
|
||||
// const lastSending = sendMessagePool[peer.peerUid];
|
||||
// if (lastSending) {
|
||||
// // log("有正在发送的消息,等待中...")
|
||||
// await sleep(500);
|
||||
// checkLastSendUsingTime += 500;
|
||||
// return await waitLastSend();
|
||||
// } else {
|
||||
// return;
|
||||
// }
|
||||
// };
|
||||
// await waitLastSend();
|
||||
|
||||
// return new Promise(async (resolve, reject) => {
|
||||
// let completed = false;
|
||||
// let sentMessage: RawMessage | null = null;
|
||||
// const sendSuccessCBId = randomUUID() as string;
|
||||
// sendSuccessCBMap[sendSuccessCBId] = (msgRecord: RawMessage) => {
|
||||
// if (msgRecord.msgId === sentMessage?.msgId) {
|
||||
// if (msgRecord.sendStatus === 2) {
|
||||
// delete sendSuccessCBMap[sendSuccessCBId];
|
||||
// completed = true;
|
||||
// resolve(msgRecord);
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// return false;
|
||||
// };
|
||||
// sendMessagePool[peerUid] = async (rawMessage: RawMessage) => {
|
||||
// // console.log('收到sent 消息', rawMessage.msgId);
|
||||
// delete sendMessagePool[peerUid];
|
||||
// sentMessage = rawMessage;
|
||||
// };
|
||||
// setTimeout(() => {
|
||||
// if (completed) return;
|
||||
// delete sendMessagePool[peerUid];
|
||||
// delete sendSuccessCBMap[sendSuccessCBId];
|
||||
// reject('发送超时');
|
||||
// }, timeout);
|
||||
// let msgId = await NTQQMsgApi.getMsgUnique(await NTQQMsgApi.getServerTime());
|
||||
// const result = napCatCore.session.getMsgService().sendMsg(msgId, peer, msgElements, new Map());
|
||||
// });
|
||||
// }
|
||||
static async getMsgUnique(time: string) {
|
||||
return napCatCore.session.getMsgService().getMsgUniqueId(time);
|
||||
}
|
||||
@@ -302,38 +180,6 @@ export class NTQQMsgApi {
|
||||
}
|
||||
throw new Error('转发消息超时');
|
||||
}
|
||||
// static async multiForwardMsg2(srcPeer: Peer, destPeer: Peer, msgIds: string[]): Promise<RawMessage> {
|
||||
// const msgInfos = msgIds.map(id => {
|
||||
// return { msgId: id, senderShowName: selfInfo.nick };
|
||||
// });
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// let complete = false;
|
||||
// const onSentCB = (msg: RawMessage) => {
|
||||
// const arkElement = msg.elements.find(ele => ele.arkElement);
|
||||
// if (!arkElement) {
|
||||
// // log("收到的不是转发消息")
|
||||
// return;
|
||||
// }
|
||||
// const forwardData: any = JSON.parse(arkElement.arkElement.bytesData);
|
||||
// if (forwardData.app != 'com.tencent.multimsg') {
|
||||
// return;
|
||||
// }
|
||||
// if (msg.peerUid == destPeer.peerUid && msg.senderUid == selfInfo.uid) {
|
||||
// complete = true;
|
||||
// resolve(msg);
|
||||
// }
|
||||
// };
|
||||
// sentMsgTasks.set(randomUUID(), onSentCB);
|
||||
// setTimeout(() => {
|
||||
// if (!complete) {
|
||||
// reject('转发消息超时');
|
||||
// }
|
||||
// }, 5000);
|
||||
// napCatCore.session.getMsgService().multiForwardMsgWithComment(msgInfos, srcPeer, destPeer, [], new Map());
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
static async markallMsgAsRead() {
|
||||
return napCatCore.session.getMsgService().setAllC2CAndGroupMsgRead();
|
||||
}
|
||||
|
Reference in New Issue
Block a user