refactor: Uid/Uin转换V2版本

This commit is contained in:
手瓜一十雪 2024-08-04 18:01:31 +08:00
parent 491ec04b46
commit 8817dc6b10
10 changed files with 90 additions and 29 deletions

View File

@ -2,7 +2,7 @@ import { Peer } from '@/core';
import crypto, { randomInt, randomUUID } from 'crypto';
import { logError } from './log';
class LimitedHashTable<K, V> {
export class LimitedHashTable<K, V> {
private keyToValue: Map<K, V> = new Map();
private valueToKey: Map<V, K> = new Map();
private maxSize: number;

View File

@ -350,6 +350,20 @@ export function getDefaultQQVersionConfigInfo(): QQVersionConfigType {
buildId: '26702'
};
}
export async function promisePipeline(promises: Promise<any>[], callback: (result: any) => boolean): Promise<void> {
let callbackCalled = false;
for (const promise of promises) {
if (callbackCalled) break;
try {
const result = await promise;
if (!callbackCalled) {
callbackCalled = callback(result);
}
} catch (error) {
console.error("Error in promise pipeline:", error);
}
}
}
export function getQQVersionConfigPath(exePath: string = ""): string | undefined {
let configVersionInfoPath;

View File

@ -1,7 +1,8 @@
import { FriendRequest, FriendV2, SimpleInfo, User } from '@/core/entities';
import { BuddyListReqType, napCatCore, NodeIKernelBuddyListener, NodeIKernelProfileService, OnBuddyChangeParams } from '@/core';
import { NTEventDispatch } from '@/common/utils/EventTask';
import { LimitedHashTable } from '@/common/utils/MessageUnique';
import { CacheClassFuncAsyncExtend } from '@/common/utils/helper';
export class NTQQFriendApi {
static async getBuddyV2(refresh = false): Promise<FriendV2[]> {
let uids: string[] = [];
@ -13,6 +14,24 @@ export class NTQQFriendApi {
);
return Array.from(data.values());
}
@CacheClassFuncAsyncExtend(5000, 'getBuddyIdMap', true)
static async getBuddyIdMapCache(refresh = false): Promise<LimitedHashTable<string, string>> {
return await NTQQFriendApi.getBuddyIdMap(refresh);
}
static async getBuddyIdMap(refresh = false): Promise<LimitedHashTable<string, string>> {
let uids: string[] = [];
let retMap: LimitedHashTable<string, string> = new LimitedHashTable<string, string>(5000);
const buddyService = napCatCore.session.getBuddyService();
const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
uids.push(...buddyListV2.data.flatMap(item => item.buddyUids));
const data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>(
'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids
);
data.forEach((value, key) => {
retMap.set(value.uin!, value.uid!);
});
return retMap;
}
static async getBuddyV2ExWithCate(refresh = false) {
let uids: string[] = [];
let categoryMap: Map<string, any> = new Map();

View File

@ -1,5 +1,5 @@
import { GroupMember, GroupRequestOperateTypes, GroupMemberRole, GroupNotify, Group, MemberExtSourceType, GroupNotifyTypes, ChatType, Peer } from '../entities';
import { GeneralCallResult, NTQQUserApi, napCatCore } from '@/core';
import { GroupMember, GroupRequestOperateTypes, GroupMemberRole, GroupNotify, Group, MemberExtSourceType, GroupNotifyTypes, ChatType, Peer, GroupListUpdateType } from '../entities';
import { GeneralCallResult, NTQQUserApi, NodeIKernelGroupListener, napCatCore } from '@/core';
import { NTEventDispatch } from '@/common/utils/EventTask';
import { log } from '@/common/utils/log';
import { groupMembers } from '../data';
@ -9,14 +9,15 @@ export class NTQQGroupApi {
return napCatCore.session.getGroupService().setHeader(gc, filePath);
}
static async getGroups(forced = false) {
type ListenerType = NodeIKernelGroupListener['onGroupListUpdate'];
let [_retData, _updateType, groupList] = await NTEventDispatch.CallNormalEvent
<(force: boolean) => Promise<any>, (updateType: number, groupList: Group[]) => void>
<(force: boolean) => Promise<any>, ListenerType>
(
'NodeIKernelGroupService/getGroupList',
'NodeIKernelGroupListener/onGroupListUpdate',
1,
5000,
() => true,
(updateType) => true,
forced
);
return groupList;

View File

@ -214,15 +214,25 @@ export class NTQQUserApi {
logWarn("uin转换到uid时异常", Uin);
return false;
})
static async getUidByUinV2(Uin: string) {
let uid = (await napCatCore.session.getProfileService().getUidByUin('FriendsServiceImpl', [Uin])).get(Uin);
if (uid) return uid;
uid = (await napCatCore.session.getGroupService().getUidByUins([Uin])).uids.get(Uin);
return uid;
}
static async getUinByUidV2(Uid: string) {
let uid = (await napCatCore.session.getProfileService().getUinByUid('FriendsServiceImpl', [Uid])).get(Uid);
if (uid) return uid;
uid = (await napCatCore.session.getGroupService().getUinByUids([Uid])).uins.get(Uid);
return uid;
}
static async getUidByUin(Uin: string) {
//Uid 接口转
let ret = await NTEventDispatch.CallNoListenerEvent
<(Uin: string[]) => Promise<{ uidInfo: Map<string, string> }>>(
'NodeIKernelUixConvertService/getUid',
5000,
[Uin]
);
let uid = ret.uidInfo.get(Uin);
if (!requireMinNTQQBuild('26702')) {
let uidV2 = NTQQUserApi.getUidByUinV2(Uin);
if (uidV2) return uidV2;
}
// 通用转换开始尝试
let uid = (await napCatCore.session.getUixConvertService().getUid([Uin])).uinInfo.get(Uin);
// Uid 好友转
if (!uid) {
Array.from(friends.values()).forEach((t) => {
@ -256,9 +266,10 @@ export class NTQQUserApi {
logWarn("uid转换到uin时异常", Uid);
return false;
})
static async getUinByUid(Uid: string | undefined) {
if (!Uid) {
return '';
static async getUinByUid(Uid: string) {
if (!requireMinNTQQBuild('26702')) {
let uinV2 = NTQQUserApi.getUidByUinV2(Uid);
if (uinV2) return uinV2;
}
let ret = await NTEventDispatch.CallNoListenerEvent
<(Uin: string[]) => Promise<{ uinInfo: Map<string, string> }>>(

View File

@ -20,7 +20,7 @@ import { hostname, systemVersion } from '@/common/utils/system';
import { genSessionConfig } from '@/core/sessionConfig';
import { sleep } from '@/common/utils/helper';
import crypto from 'node:crypto';
import { friends, groupMembers, groups, selfInfo, stat } from '@/core/data';
import { groupMembers, groups, selfInfo, stat } from '@/core/data';
import { GroupMember, RawMessage } from '@/core/entities';
import { NTEventDispatch } from '@/common/utils/EventTask';
import {
@ -33,6 +33,7 @@ import {
setLogSelfInfo
} from '@/common/utils/log';
import { napCatConfig } from '@/core/utils/config';
import { NTQQFriendApi } from './apis';
export interface OnLoginSuccess {
(uin: string, uid: string): void | Promise<void>;
@ -262,8 +263,13 @@ export class NapCatCore {
this.session.getBuddyService().getBuddyList(true).then(arg => {
// console.log('getBuddyList', arg);
});
} else {
// NTQQFriendApi.getBuddyV2(true).then((res) => {
// res.forEach((item) => {
// CachedIdMap.set(item.uid!, item.uin!);
// });
// }).catch();
}
interface SelfStatusInfo {
uid: string
status: number

View File

@ -3,9 +3,11 @@ import {
type Group,
type GroupMember,
type SelfInfo,
type BuddyCategoryType
type BuddyCategoryType,
FriendV2
} from './entities';
import { isNumeric } from '@/common/utils/helper';
import { LimitedHashTable } from '@/common/utils/MessageUnique';
import { NTQQGroupApi } from '@/core/apis';
export const selfInfo: SelfInfo = {
@ -14,8 +16,6 @@ export const selfInfo: SelfInfo = {
nick: '',
online: true
};
// 未来只在此处保留 selfInfo stat
// groupCode -> Group
export const groups: Map<string, Group> = new Map<string, Group>();
export function deleteGroup(groupQQ: string) {
@ -26,9 +26,11 @@ export function deleteGroup(groupQQ: string) {
// 群号 -> 群成员map(uid=>GroupMember)
export const groupMembers: Map<string, Map<string, GroupMember>> = new Map<string, Map<string, GroupMember>>();
// uid -> Friend 下面这俩个准备移除 QQ里面自带缓存
export const friends: Map<string, Friend> = new Map<string, Friend>();
//转换列表
//export const CachedIdMap = new LimitedHashTable<string, string>(1000);
export async function getGroup(qq: string | number): Promise<Group | undefined> {
let group = groups.get(qq.toString());
if (!group) {

View File

@ -96,7 +96,7 @@ export class GroupListener implements IGroupListener {
onGroupFirstBulletinNotify(...args: unknown[]) {
}
onGroupListUpdate(updateType: number, groupList: Group[]) {
onGroupListUpdate(updateType: GroupListUpdateType, groupList: Group[]) {
}
onGroupNotifiesUpdated(dboubt: boolean, notifies: GroupNotify[]) {

View File

@ -9,7 +9,7 @@ import {
import { GeneralCallResult } from '@/core/services/common';
export interface NodeIKernelGroupService {
getUidByUin(uin: string): Promise<string>;
getUinByUid(uid: string): Promise<string>;
@ -17,8 +17,16 @@ export interface NodeIKernelGroupService {
getGroupMemberLevelInfo(groupCode: string): Promise<unknown>;
//26702
getGroupHonorList(groupCodes: Array<string>): unknown;
getUinByUids(uins: string[]): Promise<unknown>;
getUidByUins(uins: string[]): Promise<unknown>;
getUinByUids(uins: string[]): Promise<{
errCode: number,
errMsg: string,
uins: Map<string, string>
}>;
getUidByUins(uins: string[]): Promise<{
errCode: number,
errMsg: string,
uids: Map<string, string>
}>;
//26702(其实更早 但是我不知道)
checkGroupMemberCache(arrayList: Array<string>): Promise<unknown>;
//26702(其实更早 但是我不知道)

View File

@ -15,9 +15,9 @@ export enum ProfileBizType {
}
export interface NodeIKernelProfileService {
getUidByUin(uin: string): Promise<string>;
getUidByUin(callfrom: string, uin: Array<string>): Promise<Map<string,string>>;//uin->uid
getUinByUid(uid: string): Promise<string>;
getUinByUid(callfrom: string, uid: Array<string>): Promise<Map<string,string>>;
// {
// coreInfo: CoreInfo,
// baseInfo: BaseInfo,