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,9 +1,10 @@
|
||||
import {BrowserWindow} from 'electron';
|
||||
import {getConfigUtil, log} from "../common/utils";
|
||||
import {NTQQApiClass, sendMessagePool} from "./ntcall";
|
||||
import { Group } from "./types";
|
||||
import {NTQQApi, NTQQApiClass, sendMessagePool} from "./ntcall";
|
||||
import { Group, User } from "./types";
|
||||
import { RawMessage } from "./types";
|
||||
import {groups, msgHistory} from "../common/data";
|
||||
import {friends, groups, msgHistory} from "../common/data";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export let hookApiCallbacks: Record<string, (apiReturn: any)=>void>={}
|
||||
|
||||
@@ -13,7 +14,8 @@ export enum ReceiveCmd {
|
||||
SELF_SEND_MSG = "nodeIKernelMsgListener/onAddSendMsg",
|
||||
USER_INFO = "nodeIKernelProfileListener/onProfileDetailInfoChanged",
|
||||
GROUPS = "nodeIKernelGroupListener/onGroupListUpdate",
|
||||
GROUPS_UNIX = "onGroupListUpdate"
|
||||
GROUPS_UNIX = "onGroupListUpdate",
|
||||
FRIENDS = "onBuddyListChange"
|
||||
}
|
||||
|
||||
interface NTQQApiReturnData<PayloadType=unknown> extends Array<any> {
|
||||
@@ -32,14 +34,14 @@ interface NTQQApiReturnData<PayloadType=unknown> extends Array<any> {
|
||||
|
||||
let receiveHooks: Array<{
|
||||
method: ReceiveCmd,
|
||||
hookFunc: (payload: any) => void
|
||||
hookFunc: (payload: any) => void,
|
||||
id: string
|
||||
}> = []
|
||||
|
||||
export function hookNTQQApiReceive(window: BrowserWindow) {
|
||||
const originalSend = window.webContents.send;
|
||||
const patchSend = (channel: string, ...args: NTQQApiReturnData) => {
|
||||
// 判断是否是列表
|
||||
log(`received ntqq api message: ${channel}`, JSON.stringify(args))
|
||||
// log(`received ntqq api message: ${channel}`, JSON.stringify(args))
|
||||
if (args?.[1] instanceof Array) {
|
||||
for (let receiveData of args?.[1]) {
|
||||
const ntQQApiMethodName = receiveData.cmdName;
|
||||
@@ -73,32 +75,53 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
|
||||
window.webContents.send = patchSend;
|
||||
}
|
||||
|
||||
export function registerReceiveHook<PayloadType>(method: ReceiveCmd, hookFunc: (payload: PayloadType) => void) {
|
||||
export function registerReceiveHook<PayloadType>(method: ReceiveCmd, hookFunc: (payload: PayloadType) => void): string {
|
||||
const id = uuidv4()
|
||||
receiveHooks.push({
|
||||
method,
|
||||
hookFunc
|
||||
hookFunc,
|
||||
id
|
||||
})
|
||||
return id;
|
||||
}
|
||||
|
||||
function updateGroups(_groups: Group[]){
|
||||
export function removeReceiveHook(id: string){
|
||||
const index = receiveHooks.findIndex(h=>h.id === id)
|
||||
receiveHooks.splice(index, 1);
|
||||
}
|
||||
|
||||
async function updateGroups(_groups: Group[]){
|
||||
for(let group of _groups){
|
||||
let existGroup = groups.find(g=>g.groupCode == group.groupCode)
|
||||
if (!existGroup){
|
||||
groups.push(group)
|
||||
log("update group")
|
||||
let _membeers = await NTQQApi.getGroupMembers(group.groupCode)
|
||||
if (_membeers){
|
||||
group.members = _membeers
|
||||
}
|
||||
log("update group members", group.members)
|
||||
}
|
||||
else{
|
||||
Object.assign(existGroup, group);
|
||||
group.members = [...existGroup.members]
|
||||
}
|
||||
}
|
||||
groups.length = 0;
|
||||
groups.push(..._groups)
|
||||
}
|
||||
|
||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS, (payload)=>updateGroups(payload.groupList))
|
||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS_UNIX, (payload)=>updateGroups(payload.groupList))
|
||||
|
||||
registerReceiveHook<any>(ReceiveCmd.USER_INFO, (payload)=>{
|
||||
log("user info", payload);
|
||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS, (payload)=>updateGroups(payload.groupList).then())
|
||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS_UNIX, (payload)=>updateGroups(payload.groupList).then())
|
||||
registerReceiveHook<{data:{categoryId: number, categroyName: string, categroyMbCount: number, buddyList: User[]}[]}>(ReceiveCmd.FRIENDS, payload=>{
|
||||
friends.length = 0
|
||||
for (const fData of payload.data) {
|
||||
friends.push(...fData.buddyList)
|
||||
}
|
||||
})
|
||||
|
||||
// registerReceiveHook<any>(ReceiveCmd.USER_INFO, (payload)=>{
|
||||
// log("user info", payload);
|
||||
// })
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
msgHistory[message.msgId] = message;
|
||||
|
Reference in New Issue
Block a user