fix: 加载卡顿,群成员名片变动

This commit is contained in:
linyuchen 2024-06-01 20:18:38 +08:00
parent f47f0800de
commit d1f68553f1
5 changed files with 256 additions and 210 deletions

View File

@ -25,7 +25,7 @@ import {
selfInfo, selfInfo,
uidMaps, uidMaps,
} from '../common/data' } from '../common/data'
import { hookNTQQApiCall, hookNTQQApiReceive, ReceiveCmdS, registerReceiveHook } from '../ntqqapi/hook' import { hookNTQQApiCall, hookNTQQApiReceive, ReceiveCmdS, registerReceiveHook, startHook } from '../ntqqapi/hook'
import { OB11Constructor } from '../onebot11/constructor' import { OB11Constructor } from '../onebot11/constructor'
import { import {
ChatType, ChatType,
@ -200,6 +200,7 @@ function onLoad() {
} }
async function startReceiveHook() { async function startReceiveHook() {
startHook().then()
if (getConfigUtil().getConfig().enablePoke) { if (getConfigUtil().getConfig().enablePoke) {
crychic.loadNode() crychic.loadNode()
crychic.registerPokeHandler((id, isGroup) => { crychic.registerPokeHandler((id, isGroup) => {
@ -436,14 +437,33 @@ function onLoad() {
uidMaps[value] = key uidMaps[value] = key
} }
}) })
startReceiveHook().then() try{
NTQQGroupApi.getGroups(true).then(groups=> { log('start get groups')
for (let group of groups) { const _groups = await NTQQGroupApi.getGroups()
log('_groups', _groups)
await Promise.all(
_groups.map(async (group) => {
try {
const members = await NTQQGroupApi.getGroupMembers(group.groupCode)
group.members = members
groups.push(group)
} catch (e) {
log('获取群成员失败', e)
} }
})
)
} }
).catch(log) catch (e) {
log('获取群列表失败', e)
}
finally {
log('start activate group member info')
NTQQGroupApi.activateMemberInfoChange().then().catch(log) NTQQGroupApi.activateMemberInfoChange().then().catch(log)
NTQQGroupApi.activateMemberListChange().then().catch(log) NTQQGroupApi.activateMemberListChange().then().catch(log)
startReceiveHook().then()
}
const config = getConfigUtil().getConfig() const config = getConfigUtil().getConfig()
if (config.ob11.enableHttp) { if (config.ob11.enableHttp) {
ob11HTTPServer.start(config.ob11.httpPort) ob11HTTPServer.start(config.ob11.httpPort)

View File

@ -35,14 +35,20 @@ export class NTQQGroupApi {
}) })
} }
static async getGroups(forced = false) { static async getGroups(forced = false) {
let cbCmd = ReceiveCmdS.GROUPS // let cbCmd = ReceiveCmdS.GROUPS
if (process.platform != 'win32') { // if (process.platform != 'win32') {
cbCmd = ReceiveCmdS.GROUPS_STORE // cbCmd = ReceiveCmdS.GROUPS_STORE
} // }
const result = await callNTQQApi<{ const result = await callNTQQApi<{
updateType: number updateType: number
groupList: Group[] groupList: Group[]
}>({ methodName: NTQQApiMethod.GROUPS, args: [{ force_update: forced }, undefined], cbCmd }) }>({
methodName: NTQQApiMethod.GROUPS,
args: [{ force_update: forced }, undefined],
cbCmd: [ReceiveCmdS.GROUPS, ReceiveCmdS.GROUPS_STORE],
afterFirstCmd: false,
})
log('get groups result', result)
return result.groupList return result.groupList
} }
static async getGroupMembers(groupQQ: string, num = 3000): Promise<GroupMember[]> { static async getGroupMembers(groupQQ: string, num = 3000): Promise<GroupMember[]> {

View File

@ -22,6 +22,7 @@ import { NTQQGroupApi } from './api/group'
import { log } from '@/common/utils' import { log } from '@/common/utils'
import { isNumeric, sleep } from '@/common/utils' import { isNumeric, sleep } from '@/common/utils'
import { OB11Constructor } from '../onebot11/constructor' import { OB11Constructor } from '../onebot11/constructor'
import { OB11GroupCardEvent } from '../onebot11/event/notice/OB11GroupCardEvent'
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {} export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
@ -324,13 +325,16 @@ async function processGroupEvent(payload: { groupList: Group[] }) {
} }
} }
export async function startHook() {
// 群列表变动 // 群列表变动
registerReceiveHook<{ groupList: Group[]; updateType: number }>(ReceiveCmdS.GROUPS, (payload) => { registerReceiveHook<{ groupList: Group[]; updateType: number }>(ReceiveCmdS.GROUPS, (payload) => {
// updateType 3是群列表变动2是群成员变动 // updateType 3是群列表变动2是群成员变动
// log("群列表变动", payload.updateType, payload.groupList) // log("群列表变动", payload.updateType, payload.groupList)
if (payload.updateType != 2) { if (payload.updateType != 2) {
updateGroups(payload.groupList).then() updateGroups(payload.groupList).then()
} else { }
else {
if (process.platform == 'win32') { if (process.platform == 'win32') {
processGroupEvent(payload).then() processGroupEvent(payload).then()
} }
@ -341,7 +345,8 @@ registerReceiveHook<{ groupList: Group[]; updateType: number }>(ReceiveCmdS.GROU
// log("群列表变动", payload.updateType, payload.groupList) // log("群列表变动", payload.updateType, payload.groupList)
if (payload.updateType != 2) { if (payload.updateType != 2) {
updateGroups(payload.groupList).then() updateGroups(payload.groupList).then()
} else { }
else {
if (process.platform != 'win32') { if (process.platform != 'win32') {
processGroupEvent(payload).then() processGroupEvent(payload).then()
} }
@ -359,6 +364,12 @@ registerReceiveHook<{
for (const member of members) { for (const member of members) {
const existMember = await getGroupMember(groupCode, member.uin) const existMember = await getGroupMember(groupCode, member.uin)
if (existMember) { if (existMember) {
if (member.cardName != existMember.cardName) {
log('群成员名片变动', `${groupCode}: ${existMember.uin}`, existMember.cardName, '->', member.cardName)
postOb11Event(
new OB11GroupCardEvent(parseInt(groupCode), parseInt(member.uin), member.cardName, existMember.cardName),
)
}
Object.assign(existMember, member) Object.assign(existMember, member)
} }
} }
@ -391,7 +402,8 @@ registerReceiveHook<{
let existFriend = friends.find((f) => f.uin == friend.uin) let existFriend = friends.find((f) => f.uin == friend.uin)
if (!existFriend) { if (!existFriend) {
friends.push(friend) friends.push(friend)
} else { }
else {
Object.assign(existFriend, friend) Object.assign(existFriend, friend)
} }
} }
@ -503,7 +515,8 @@ registerReceiveHook<{
} }
}) })
}) })
} else { }
else {
NTQQMsgApi.activateChat(peer).then() NTQQMsgApi.activateChat(peer).then()
} }
} }
@ -516,7 +529,8 @@ registerCallHook(NTQQApiMethod.DELETE_ACTIVE_CHAT, async (payload) => {
let chatType = ChatType.friend let chatType = ChatType.friend
if (isNumeric(peerUid)) { if (isNumeric(peerUid)) {
chatType = ChatType.group chatType = ChatType.group
} else { }
else {
// 检查是否好友 // 检查是否好友
if (!(await getFriend(peerUid))) { if (!(await getFriend(peerUid))) {
chatType = ChatType.temp chatType = ChatType.temp
@ -528,3 +542,5 @@ registerCallHook(NTQQApiMethod.DELETE_ACTIVE_CHAT, async (payload) => {
log('重新激活聊天窗口', peer, { result: r.result, errMsg: r.errMsg }) log('重新激活聊天窗口', peer, { result: r.result, errMsg: r.errMsg })
}) })
}) })
}

View File

@ -107,7 +107,7 @@ interface NTQQApiParams {
channel?: NTQQApiChannel channel?: NTQQApiChannel
classNameIsRegister?: boolean classNameIsRegister?: boolean
args?: unknown[] args?: unknown[]
cbCmd?: ReceiveCmd | null cbCmd?: ReceiveCmd | ReceiveCmd[] | null
cmdCB?: (payload: any) => boolean cmdCB?: (payload: any) => boolean
afterFirstCmd?: boolean // 是否在methodName调用完之后再去hook cbCmd afterFirstCmd?: boolean // 是否在methodName调用完之后再去hook cbCmd
timeoutSecond?: number timeoutSecond?: number
@ -147,7 +147,8 @@ export function callNTQQApi<ReturnType>(params: NTQQApiParams) {
success = true success = true
resolve(r) resolve(r)
} }
} else { }
else {
// 这里的callback比较特殊QQ后端先返回是否调用成功再返回一条结果数据 // 这里的callback比较特殊QQ后端先返回是否调用成功再返回一条结果数据
const secondCallback = () => { const secondCallback = () => {
const hookId = registerReceiveHook<ReturnType>(cbCmd, (payload) => { const hookId = registerReceiveHook<ReturnType>(cbCmd, (payload) => {
@ -158,7 +159,8 @@ export function callNTQQApi<ReturnType>(params: NTQQApiParams) {
success = true success = true
resolve(payload) resolve(payload)
} }
} else { }
else {
removeReceiveHook(hookId) removeReceiveHook(hookId)
success = true success = true
resolve(payload) resolve(payload)
@ -170,7 +172,8 @@ export function callNTQQApi<ReturnType>(params: NTQQApiParams) {
log(`${methodName} callback`, result) log(`${methodName} callback`, result)
if (result?.result == 0 || result === undefined) { if (result?.result == 0 || result === undefined) {
afterFirstCmd && secondCallback() afterFirstCmd && secondCallback()
} else { }
else {
success = true success = true
reject(`ntqq api call failed, ${result.errMsg}`) reject(`ntqq api call failed, ${result.errMsg}`)
} }
@ -188,7 +191,8 @@ export function callNTQQApi<ReturnType>(params: NTQQApiParams) {
channel, channel,
{ {
sender: { sender: {
send: (..._args: unknown[]) => {}, send: (..._args: unknown[]) => {
},
}, },
}, },
{ type: 'request', callbackId: uuid, eventName }, { type: 'request', callbackId: uuid, eventName },

View File

@ -1 +1 @@
export const version = '3.26.4' export const version = '3.26.5'