mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor: pre-release
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import {ipcMain} from "electron";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import {hookApiCallbacks} from "./hook";
|
||||
import {log} from "../common/utils";
|
||||
import { ChatType } from "./types";
|
||||
import { ipcMain } from "electron";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { ReceiveCmd, hookApiCallbacks, registerReceiveHook, removeReceiveHook } from "./hook";
|
||||
import { log } from "../common/utils";
|
||||
import { ChatType, Friend, SelfInfo, User } from "./types";
|
||||
import { Group } from "./types";
|
||||
import { GroupMember } from "./types";
|
||||
import { RawMessage } from "./types";
|
||||
import { User } from "./types";
|
||||
import {SendMessageElement} from "./types";
|
||||
import { SendMessageElement } from "./types";
|
||||
|
||||
interface IPCReceiveEvent {
|
||||
eventName: string
|
||||
@@ -31,7 +30,7 @@ export enum NTQQApiMethod {
|
||||
LIKE_FRIEND = "nodeIKernelProfileLikeService/setBuddyProfileLike",
|
||||
UPDATE_MSG = "nodeIKernelMsgListener/onMsgInfoListUpdate",
|
||||
SELF_INFO = "fetchAuthData",
|
||||
FRIENDS = "nodeIKernelProfileService/getBuddyProfileList",
|
||||
FRIENDS = "nodeIKernelBuddyService/getBuddyList",
|
||||
GROUPS = "nodeIKernelGroupService/getGroupList",
|
||||
GROUP_MEMBER_SCENE = "nodeIKernelGroupService/createMemberListScene",
|
||||
GROUP_MEMBERS = "nodeIKernelGroupService/getNextMemberList",
|
||||
@@ -58,26 +57,65 @@ export interface Peer {
|
||||
guildId?: ""
|
||||
}
|
||||
|
||||
enum CallBackType {
|
||||
UUID,
|
||||
METHOD
|
||||
}
|
||||
|
||||
function callNTQQApi<ReturnType>(channel: NTQQApiChannel, className: NTQQApiClass, methodName: NTQQApiMethod, args: unknown[] = []) {
|
||||
|
||||
function callNTQQApi<ReturnType>(channel: NTQQApiChannel, className: NTQQApiClass, methodName: NTQQApiMethod, args: unknown[] = [], cbCmd: ReceiveCmd | null = null, timeout = 5) {
|
||||
const uuid = uuidv4();
|
||||
// log("callNTQQApi", channel, className, methodName, args, uuid)
|
||||
return new Promise((resolve: (data: ReturnType) => void, reject) => {
|
||||
// log("callNTQQApiPromise", channel, className, methodName, args, uuid)
|
||||
hookApiCallbacks[uuid] = resolve;
|
||||
const _timeout = timeout * 1000
|
||||
let success = false
|
||||
if (!cbCmd) {
|
||||
// QQ后端会返回结果,并且可以插根据uuid识别
|
||||
hookApiCallbacks[uuid] = (r: ReturnType) => {
|
||||
success = true
|
||||
resolve(r)
|
||||
};
|
||||
}
|
||||
else {
|
||||
// 这里的callback比较特殊,QQ后端先返回是否调用成功,再返回一条结果数据
|
||||
hookApiCallbacks[uuid] = (result: GeneralCallResult) => {
|
||||
log(`${methodName} callback`, result)
|
||||
if (result.result == 0) {
|
||||
const hookId = registerReceiveHook<ReturnType>(cbCmd, (payload) => {
|
||||
log(methodName, "second callback", cbCmd, payload);
|
||||
removeReceiveHook(hookId);
|
||||
success = true
|
||||
resolve(payload);
|
||||
})
|
||||
}
|
||||
else {
|
||||
success = true
|
||||
reject(`ntqq api call failed, ${result.errMsg}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
setTimeout(() => {
|
||||
// log("ntqq api timeout", success, channel, className, methodName)
|
||||
if (!success) {
|
||||
log(`ntqq api timeout ${channel}, ${className}, ${methodName}`)
|
||||
reject(`ntqq api timeout ${channel}, ${className}, ${methodName}`)
|
||||
}
|
||||
}, _timeout)
|
||||
|
||||
ipcMain.emit(
|
||||
channel,
|
||||
{},
|
||||
{type: 'request', callbackId: uuid, eventName: className + "-" + channel[channel.length - 1]},
|
||||
{ type: 'request', callbackId: uuid, eventName: className + "-" + channel[channel.length - 1] },
|
||||
[methodName, ...args],
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export let sendMessagePool: Record<string, ((sendSuccessMsg: RawMessage)=>void) | null> = {}// peerUid: callbackFunnc
|
||||
export let sendMessagePool: Record<string, ((sendSuccessMsg: RawMessage) => void) | null> = {}// peerUid: callbackFunnc
|
||||
|
||||
interface GeneralCallResult{
|
||||
result:0,
|
||||
interface GeneralCallResult {
|
||||
result: number, // 0: success
|
||||
errMsg: string
|
||||
}
|
||||
|
||||
@@ -97,41 +135,63 @@ export class NTQQApi {
|
||||
}
|
||||
|
||||
static getSelfInfo() {
|
||||
return callNTQQApi<User>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.GLOBAL_DATA, NTQQApiMethod.SELF_INFO, [])
|
||||
return callNTQQApi<SelfInfo>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.GLOBAL_DATA, NTQQApiMethod.SELF_INFO, [], null, 2)
|
||||
|
||||
}
|
||||
|
||||
static getFriends(forced = false) {
|
||||
return callNTQQApi<GeneralCallResult>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.FRIENDS, [{force_update: forced}, undefined])
|
||||
}
|
||||
|
||||
static getGroups(forced = false) {
|
||||
return callNTQQApi<GeneralCallResult>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUPS, [{force_update: forced}, undefined])
|
||||
}
|
||||
|
||||
static async getGroupMembers(groupQQ: string, num = 5000) {
|
||||
const sceneId = callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBER_SCENE, [{
|
||||
groupCode: groupQQ,
|
||||
scene: "groupMemberList_MainWindow"
|
||||
}]
|
||||
)
|
||||
return callNTQQApi<GroupMember[]>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBERS,
|
||||
[{
|
||||
sceneId: sceneId,
|
||||
num: num
|
||||
},
|
||||
null
|
||||
])
|
||||
}
|
||||
|
||||
static async getUserInfo(uid: string) {
|
||||
const result = await callNTQQApi<GeneralCallResult>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
||||
[{force: true, uids: [uid]}, undefined])
|
||||
log("get user info result", result);
|
||||
return result[0].payload.profiles.get(uid);
|
||||
|
||||
const result = await callNTQQApi<{ info: User }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
||||
[{ force: true, uids: [uid] }, undefined], ReceiveCmd.USER_INFO)
|
||||
return result.info
|
||||
|
||||
}
|
||||
|
||||
// static async getFriends(forced = false) {
|
||||
// const data = await callNTQQApi<{ data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: Friend[] }[] }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.FRIENDS, [{ force_update: forced }, undefined], ReceiveCmd.FRIENDS)
|
||||
// let _friends: Friend[] = [];
|
||||
// for (const fData of data.data) {
|
||||
// _friends.push(...fData.buddyList)
|
||||
// }
|
||||
// return _friends
|
||||
// }
|
||||
|
||||
// static async getGroups(forced = false) {
|
||||
// let cbCmd = ReceiveCmd.GROUPS
|
||||
// if (process.platform != "win32") {
|
||||
// cbCmd = ReceiveCmd.GROUPS_UNIX
|
||||
// }
|
||||
// const result = await callNTQQApi<{ updateType: number, groupList: Group[] }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUPS, [{ force_update: forced }, undefined], cbCmd)
|
||||
// return result.groupList
|
||||
// }
|
||||
|
||||
static async getGroupMembers(groupQQ: string, num = 3000) {
|
||||
const sceneId = await callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBER_SCENE, [{
|
||||
groupCode: groupQQ,
|
||||
scene: "groupMemberList_MainWindow"
|
||||
}])
|
||||
// log("get group member sceneId", sceneId);
|
||||
try {
|
||||
const result = await callNTQQApi<{result:{infos: any}}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBERS,
|
||||
[{
|
||||
sceneId: sceneId,
|
||||
num: num
|
||||
},
|
||||
null
|
||||
])
|
||||
// log("members info", typeof result.result.infos, Object.keys(result.result.infos))
|
||||
let values = result.result.infos.values()
|
||||
|
||||
values = Array.from(values) as GroupMember[]
|
||||
// log("members info", values);
|
||||
return values
|
||||
} catch (e) {
|
||||
log(`get group ${groupQQ} members failed`, e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static getFileType(filePath: string) {
|
||||
return callNTQQApi<{
|
||||
ext: string
|
||||
@@ -184,40 +244,40 @@ export class NTQQApi {
|
||||
}
|
||||
}
|
||||
|
||||
static recallMsg(peer: Peer, msgIds: string[]){
|
||||
return callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.RECALL_MSG, [{peer, msgIds}, null])
|
||||
static recallMsg(peer: Peer, msgIds: string[]) {
|
||||
return callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.RECALL_MSG, [{ peer, msgIds }, null])
|
||||
}
|
||||
|
||||
static sendMsg(peer: Peer, msgElements: SendMessageElement[]){
|
||||
static sendMsg(peer: Peer, msgElements: SendMessageElement[]) {
|
||||
const sendTimeout = 10 * 1000
|
||||
|
||||
return new Promise<RawMessage>((resolve, reject)=>{
|
||||
return new Promise<RawMessage>((resolve, reject) => {
|
||||
const peerUid = peer.peerUid;
|
||||
let usingTime = 0;
|
||||
let success = false;
|
||||
|
||||
const checkSuccess = ()=>{
|
||||
if (!success){
|
||||
const checkSuccess = () => {
|
||||
if (!success) {
|
||||
sendMessagePool[peerUid] = null;
|
||||
reject("发送超时")
|
||||
}
|
||||
}
|
||||
setTimeout(checkSuccess, sendTimeout);
|
||||
|
||||
const checkLastSend = ()=>{
|
||||
const checkLastSend = () => {
|
||||
let lastSending = sendMessagePool[peerUid]
|
||||
if (sendTimeout < usingTime){
|
||||
if (sendTimeout < usingTime) {
|
||||
sendMessagePool[peerUid] = null;
|
||||
reject("发送超时")
|
||||
}
|
||||
if (!!lastSending){
|
||||
if (!!lastSending) {
|
||||
// log("有正在发送的消息,等待中...")
|
||||
usingTime += 100;
|
||||
setTimeout(checkLastSend, 100);
|
||||
}
|
||||
else{
|
||||
else {
|
||||
log("可以进行发送消息,设置发送成功回调", sendMessagePool)
|
||||
sendMessagePool[peerUid] = (rawMessage: RawMessage)=>{
|
||||
sendMessagePool[peerUid] = (rawMessage: RawMessage) => {
|
||||
success = true;
|
||||
sendMessagePool[peerUid] = null;
|
||||
resolve(rawMessage);
|
||||
|
Reference in New Issue
Block a user