mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: fix
This commit is contained in:
@@ -30,4 +30,13 @@ export class LRUCache<K, V> {
|
|||||||
}
|
}
|
||||||
this.cache.set(key, value);
|
this.cache.set(key, value);
|
||||||
}
|
}
|
||||||
|
public resetCapacity(newCapacity: number): void {
|
||||||
|
this.capacity = newCapacity;
|
||||||
|
while (this.cache.size > this.capacity) {
|
||||||
|
const firstKey = this.cache.keys().next().value;
|
||||||
|
if (firstKey !== undefined) {
|
||||||
|
this.cache.delete(firstKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -2,8 +2,8 @@ import { Peer } from '@/core';
|
|||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
|
|
||||||
export class LimitedHashTable<K, V> {
|
export class LimitedHashTable<K, V> {
|
||||||
private keyToValue: Map<K, V> = new Map();
|
private readonly keyToValue: Map<K, V> = new Map();
|
||||||
private valueToKey: Map<V, K> = new Map();
|
private readonly valueToKey: Map<V, K> = new Map();
|
||||||
private maxSize: number;
|
private maxSize: number;
|
||||||
|
|
||||||
constructor(maxSize: number) {
|
constructor(maxSize: number) {
|
||||||
@@ -75,8 +75,8 @@ export class LimitedHashTable<K, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MessageUniqueWrapper {
|
class MessageUniqueWrapper {
|
||||||
private msgDataMap: LimitedHashTable<string, number>;
|
private readonly msgDataMap: LimitedHashTable<string, number>;
|
||||||
private msgIdMap: LimitedHashTable<string, number>;
|
private readonly msgIdMap: LimitedHashTable<string, number>;
|
||||||
|
|
||||||
constructor(maxMap: number = 1000) {
|
constructor(maxMap: number = 1000) {
|
||||||
this.msgIdMap = new LimitedHashTable<string, number>(maxMap);
|
this.msgIdMap = new LimitedHashTable<string, number>(maxMap);
|
||||||
|
@@ -9,33 +9,39 @@ export class RequestUtil {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = client.get(url, (res) => {
|
const req = client.get(url, (res) => {
|
||||||
let cookies: { [key: string]: string } = {};
|
let cookies: { [key: string]: string } = {};
|
||||||
const handleRedirect = (res: http.IncomingMessage) => {
|
|
||||||
//console.log(res.headers.location);
|
res.on('data', () => { }); // Necessary to consume the stream
|
||||||
|
res.on('end', () => {
|
||||||
|
this.handleRedirect(res, url, cookies)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.headers['set-cookie']) {
|
||||||
|
this.extractCookies(res.headers['set-cookie'], cookies);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error: any) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async handleRedirect(res: http.IncomingMessage, url: string, cookies: { [key: string]: string }): Promise<{ [key: string]: string }> {
|
||||||
if (res.statusCode === 301 || res.statusCode === 302) {
|
if (res.statusCode === 301 || res.statusCode === 302) {
|
||||||
if (res.headers.location) {
|
if (res.headers.location) {
|
||||||
const redirectUrl = new URL(res.headers.location, url);
|
const redirectUrl = new URL(res.headers.location, url);
|
||||||
RequestUtil.HttpsGetCookies(redirectUrl.href).then((redirectCookies) => {
|
const redirectCookies = await this.HttpsGetCookies(redirectUrl.href);
|
||||||
// 合并重定向过程中的cookies
|
// 合并重定向过程中的cookies
|
||||||
cookies = { ...cookies, ...redirectCookies };
|
return { ...cookies, ...redirectCookies };
|
||||||
resolve(cookies);
|
|
||||||
}).catch((err) => {
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
resolve(cookies);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
resolve(cookies);
|
|
||||||
}
|
}
|
||||||
};
|
return cookies;
|
||||||
res.on('data', () => {
|
}
|
||||||
}); // Necessary to consume the stream
|
|
||||||
res.on('end', () => {
|
private static extractCookies(setCookieHeaders: string[], cookies: { [key: string]: string }) {
|
||||||
handleRedirect(res);
|
setCookieHeaders.forEach((cookie) => {
|
||||||
});
|
|
||||||
if (res.headers['set-cookie']) {
|
|
||||||
//console.log(res.headers['set-cookie']);
|
|
||||||
res.headers['set-cookie'].forEach((cookie) => {
|
|
||||||
const parts = cookie.split(';')[0].split('=');
|
const parts = cookie.split(';')[0].split('=');
|
||||||
const key = parts[0];
|
const key = parts[0];
|
||||||
const value = parts[1];
|
const value = parts[1];
|
||||||
@@ -44,13 +50,6 @@ export class RequestUtil {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
req.on('error', (error: any) => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 请求和回复都是JSON data传原始内容 自动编码json
|
// 请求和回复都是JSON data传原始内容 自动编码json
|
||||||
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: {
|
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: {
|
||||||
|
@@ -357,16 +357,14 @@ export class NTQQFileApi {
|
|||||||
|
|
||||||
async getImageSize(filePath: string): Promise<ISizeCalculationResult> {
|
async getImageSize(filePath: string): Promise<ISizeCalculationResult> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
imageSize(filePath, (err, dimensions) => {
|
imageSize(filePath, (err: Error | null, dimensions) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(new Error(err.message));
|
||||||
} else {
|
} else if (!dimensions) {
|
||||||
if (!dimensions) {
|
|
||||||
reject(new Error('获取图片尺寸失败'));
|
reject(new Error('获取图片尺寸失败'));
|
||||||
} else {
|
} else {
|
||||||
resolve(dimensions);
|
resolve(dimensions);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -15,7 +15,7 @@ export class NTQQFriendApi {
|
|||||||
}
|
}
|
||||||
async getBuddyV2SimpleInfoMap(refresh = false) {
|
async getBuddyV2SimpleInfoMap(refresh = false) {
|
||||||
const buddyService = this.context.session.getBuddyService();
|
const buddyService = this.context.session.getBuddyService();
|
||||||
const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
|
const buddyListV2 = await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
|
||||||
const uids = buddyListV2.data.flatMap(item => item.buddyUids);
|
const uids = buddyListV2.data.flatMap(item => item.buddyUids);
|
||||||
return await this.core.eventWrapper.callNoListenerEvent(
|
return await this.core.eventWrapper.callNoListenerEvent(
|
||||||
'NodeIKernelProfileService/getCoreAndBaseInfo',
|
'NodeIKernelProfileService/getCoreAndBaseInfo',
|
||||||
@@ -44,7 +44,7 @@ export class NTQQFriendApi {
|
|||||||
async getBuddyV2ExWithCate(refresh = false) {
|
async getBuddyV2ExWithCate(refresh = false) {
|
||||||
const categoryMap: Map<string, any> = new Map();
|
const categoryMap: Map<string, any> = new Map();
|
||||||
const buddyService = this.context.session.getBuddyService();
|
const buddyService = this.context.session.getBuddyService();
|
||||||
const buddyListV2 = refresh ? (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data : (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data;
|
const buddyListV2 = (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data;
|
||||||
const uids = buddyListV2.flatMap(item => {
|
const uids = buddyListV2.flatMap(item => {
|
||||||
item.buddyUids.forEach(uid => {
|
item.buddyUids.forEach(uid => {
|
||||||
categoryMap.set(uid, { categoryId: item.categoryId, categoryName: item.categroyName });
|
categoryMap.set(uid, { categoryId: item.categoryId, categoryName: item.categroyName });
|
||||||
|
@@ -319,7 +319,7 @@ export class NTQQGroupApi {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async tryGetGroupMembersV2(modeListener = false, groupQQ: string, num = 30, timeout = 100): Promise<{
|
async tryGetGroupMembersV2(groupQQ: string, modeListener = false, num = 30, timeout = 100): Promise<{
|
||||||
infos: Map<string, GroupMember>;
|
infos: Map<string, GroupMember>;
|
||||||
finish: boolean;
|
finish: boolean;
|
||||||
hasNext: boolean | undefined;
|
hasNext: boolean | undefined;
|
||||||
@@ -372,7 +372,7 @@ export class NTQQGroupApi {
|
|||||||
infos: new Map([...(resMode2?.infos ?? []), ...result.result.infos]),
|
infos: new Map([...(resMode2?.infos ?? []), ...result.result.infos]),
|
||||||
finish: result.result.finish,
|
finish: result.result.finish,
|
||||||
hasNext: resMode2?.hasNext,
|
hasNext: resMode2?.hasNext,
|
||||||
listenerMode: resMode2?.hasNext !== undefined ? true : false
|
listenerMode: resMode2?.hasNext !== undefined
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -144,7 +144,7 @@ export class NTQQMsgApi {
|
|||||||
params,
|
params,
|
||||||
],
|
],
|
||||||
() => true,
|
() => true,
|
||||||
() => true, // Todo: 应当通过 groupFileListResult 判断
|
() => true, // 应当通过 groupFileListResult 判断
|
||||||
1,
|
1,
|
||||||
5000,
|
5000,
|
||||||
);
|
);
|
||||||
@@ -194,7 +194,7 @@ export class NTQQMsgApi {
|
|||||||
async sendMsg(peer: Peer, msgElements: SendMessageElement[], waitComplete = true, timeout = 10000) {
|
async sendMsg(peer: Peer, msgElements: SendMessageElement[], waitComplete = true, timeout = 10000) {
|
||||||
//唉?!我有个想法
|
//唉?!我有个想法
|
||||||
if (peer.chatType === ChatType.KCHATTYPETEMPC2CFROMGROUP && peer.guildId && peer.guildId !== '') {
|
if (peer.chatType === ChatType.KCHATTYPETEMPC2CFROMGROUP && peer.guildId && peer.guildId !== '') {
|
||||||
const member = await this.core.apis.GroupApi.getGroupMember(peer.guildId, peer.peerUid!);
|
const member = await this.core.apis.GroupApi.getGroupMember(peer.guildId, peer.peerUid);
|
||||||
if (member) {
|
if (member) {
|
||||||
await this.PrepareTempChat(peer.peerUid, peer.guildId, member.nick);
|
await this.PrepareTempChat(peer.peerUid, peer.guildId, member.nick);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user