Refactor Api: GetFriendsWithCategory

This commit is contained in:
手瓜一十雪 2024-08-04 16:37:15 +08:00
parent 690a2f7d34
commit dfc7c7357a
5 changed files with 36 additions and 73 deletions

View File

@ -4,51 +4,21 @@ import { NTEventDispatch } from '@/common/utils/EventTask';
export class NTQQFriendApi { export class NTQQFriendApi {
static async getBuddyV2(refresh = false): Promise<FriendV2[]> { static async getBuddyV2(refresh = false): Promise<FriendV2[]> {
let uids: string[]; let uids: string[] = [];
let categoryMap: Map<string, any> = new Map(); const buddyService = napCatCore.session.getBuddyService();
if (!refresh) { const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
uids = (await napCatCore.session.getBuddyService().getBuddyListFromCache('0')).flatMap((item) => { uids.push(...buddyListV2.data.flatMap(item => item.buddyUids));
for (let i = 0; i < item.buddyUids.length; i++) { const data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>(
categoryMap.set(item.buddyUids[i], { categoryId: item.categoryId, categroyName: item.categroyName }); 'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids
);
return Array.from(data.values());
} }
return item.buddyUids; static async getBuddyV2ExWithCate(refresh = false) {
});
}
uids = (await (napCatCore.session.getBuddyService().getBuddyListV2('0', BuddyListReqType.KNOMAL))).data.flatMap((item) => item.buddyUids);
let data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>('NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids);
//遍历data
let retArr: FriendV2[] = [];
data.forEach((value, key) => {
let category = categoryMap.get(key);
if (category) {
retArr.push({
...value,
categoryId: category.categoryId,
categroyName: category.categroyName
});
}
})
return retArr;
}
static async getBuddyV2Ex(refresh = false): Promise<FriendV2[]> {
let uids: string[] = []; let uids: string[] = [];
let categoryMap: Map<string, any> = new Map(); let categoryMap: Map<string, any> = new Map();
const buddyService = napCatCore.session.getBuddyService(); const buddyService = napCatCore.session.getBuddyService();
const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
if (!refresh) {
const cachedBuddyList = await buddyService.getBuddyListFromCache('0');
cachedBuddyList.forEach(item => {
item.buddyUids.forEach(uid => {
categoryMap.set(uid, { categoryId: item.categoryId, categroyName: item.categroyName });
});
uids.push(...item.buddyUids);
});
}
const buddyListV2 = await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
uids.push(...buddyListV2.data.flatMap(item => item.buddyUids)); uids.push(...buddyListV2.data.flatMap(item => item.buddyUids));
const data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>( const data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>(
'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids 'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids
); );

View File

@ -15,12 +15,12 @@ import { DependsAdapter, DispatcherAdapter, GlobalAdapter, NodeIGlobalAdapter }
import path from 'node:path'; import path from 'node:path';
import os from 'node:os'; import os from 'node:os';
import fs from 'node:fs'; import fs from 'node:fs';
import { getFullQQVesion, QQVersionAppid, QQVersionQua } from '@/common/utils/QQBasicInfo'; import { getFullQQVesion, QQVersionAppid, QQVersionQua, requireMinNTQQBuild } from '@/common/utils/QQBasicInfo';
import { hostname, systemVersion } from '@/common/utils/system'; import { hostname, systemVersion } from '@/common/utils/system';
import { genSessionConfig } from '@/core/sessionConfig'; import { genSessionConfig } from '@/core/sessionConfig';
import { sleep } from '@/common/utils/helper'; import { sleep } from '@/common/utils/helper';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import { rawFriends, friends, groupMembers, groups, selfInfo, stat } from '@/core/data'; import { friends, groupMembers, groups, selfInfo, stat } from '@/core/data';
import { GroupMember, RawMessage } from '@/core/entities'; import { GroupMember, RawMessage } from '@/core/entities';
import { NTEventDispatch } from '@/common/utils/EventTask'; import { NTEventDispatch } from '@/common/utils/EventTask';
import { import {
@ -254,29 +254,16 @@ export class NapCatCore {
this.addListener(msgListener); this.addListener(msgListener);
// 好友相关 // 好友相关
const buddyListener = new BuddyListener(); const buddyListener = new BuddyListener();
buddyListener.onBuddyListChange = arg => {
rawFriends.length = 0;
rawFriends.push(...arg);
// console.log('onBuddyListChange', arg);
for (const categoryItem of arg) {
for (const friend of categoryItem.buddyList) {
// console.log("onBuddyListChange", friend)
const existFriend = friends.get(friend.uid);
if (existFriend) {
Object.assign(existFriend, friend);
}
else {
friends.set(friend.uid, friend);
}
}
// console.log("onBuddyListChange", friend)
}
};
this.addListener(buddyListener); this.addListener(buddyListener);
// 刷新一次好友列表 // 刷新一次好友列表 26702版本以下需要手动刷新一次获取 高版本NTQQ自带缓存
if (!requireMinNTQQBuild('26702')) {
this.session.getBuddyService().getBuddyList(true).then(arg => { this.session.getBuddyService().getBuddyList(true).then(arg => {
// console.log('getBuddyList', arg); // console.log('getBuddyList', arg);
}); });
}
interface SelfStatusInfo { interface SelfStatusInfo {
uid: string uid: string
status: number status: number

View File

@ -28,7 +28,6 @@ export const groupMembers: Map<string, Map<string, GroupMember>> = new Map<strin
// uid -> Friend 下面这俩个准备移除 QQ里面自带缓存 // uid -> Friend 下面这俩个准备移除 QQ里面自带缓存
export const friends: Map<string, Friend> = new Map<string, Friend>(); export const friends: Map<string, Friend> = new Map<string, Friend>();
export const rawFriends: Array<BuddyCategoryType> = []; // 带分组的好友列表
export async function getGroup(qq: string | number): Promise<Group | undefined> { export async function getGroup(qq: string | number): Promise<Group | undefined> {
let group = groups.get(qq.toString()); let group = groups.get(qq.toString());

View File

@ -1,12 +1,19 @@
import { rawFriends } from '@/core/data'; import { requireMinNTQQBuild } from '@/common/utils/QQBasicInfo';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { BuddyCategoryType } from '@/core/entities/'; import { BuddyCategoryType } from '@/core/entities/';
import { NTQQFriendApi } from '@/core';
import { OB11Constructor } from '@/onebot11/constructor';
export class GetFriendWithCategory extends BaseAction<void, Array<BuddyCategoryType>> { export class GetFriendWithCategory extends BaseAction<void, any> {
actionName = ActionName.GetFriendsWithCategory; actionName = ActionName.GetFriendsWithCategory;
protected async _handle(payload: void) { protected async _handle(payload: void) {
return rawFriends; if (requireMinNTQQBuild('26702')) {
//全新逻辑
return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2ExWithCate(true));
} else {
throw new Error('not support');
}
} }
} }

View File

@ -23,7 +23,7 @@ export default class GetFriendList extends BaseAction<Payload, OB11User[]> {
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
if (requireMinNTQQBuild('26702')) { if (requireMinNTQQBuild('26702')) {
//全新逻辑 //全新逻辑
return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2Ex(payload?.no_cache === true || payload?.no_cache === 'true')); return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(payload?.no_cache === true || payload?.no_cache === 'true'));
} }
if (friends.size === 0 || payload?.no_cache === true || payload?.no_cache === 'true') { if (friends.size === 0 || payload?.no_cache === true || payload?.no_cache === 'true') {
const _friends = await NTQQFriendApi.getFriends(true); const _friends = await NTQQFriendApi.getFriends(true);