perf: 用单独的模块管理 forwardPair

This commit is contained in:
Clansty 2022-02-23 20:25:26 +08:00
parent 32422506df
commit 042f90d119
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
3 changed files with 59 additions and 11 deletions

View File

@ -99,7 +99,7 @@ export default class OicqClient extends Client {
});
}
public getEntity(roomId: number) {
public getChat(roomId: number) {
if (roomId > 0) {
return this.pickFriend(roomId);
}

View File

@ -0,0 +1,50 @@
import { Friend, Group } from 'oicq';
import TelegramChat from '../client/TelegramChat';
import { Api } from 'telegram';
import OicqClient from '../client/OicqClient';
import Telegram from '../client/Telegram';
import db from './db';
type Pair = {
qq: Friend | Group;
tg: TelegramChat;
}
class ForwardPairsInternal {
private pairs: Pair[] = [];
// 在 forwardController 创建时初始化
public async init(oicq: OicqClient, tgBot: Telegram) {
const dbValues = await db.forwardPair.findMany();
for (const i of dbValues) {
this.pairs.push({
qq: oicq.getChat(i.qqRoomId),
tg: await tgBot.getChat(i.tgChatId),
});
}
}
public async add(qq: Friend | Group, tg: TelegramChat) {
this.pairs.push({ qq, tg });
return await db.forwardPair.create({
data: {
qqRoomId: qq instanceof Friend ? qq.user_id : -qq.group_id,
tgChatId: Number(tg.id),
},
});
}
public find(target: Friend | Group | TelegramChat | Api.Chat) {
if (target instanceof Friend) {
return this.pairs.find(e => e.qq instanceof Friend && e.qq.user_id === target.user_id);
}
else if (target instanceof Group) {
return this.pairs.find(e => e.qq instanceof Group && e.qq.group_id === target.group_id);
}
else {
return this.pairs.find(e => e.tg.id.eq(target.id));
}
}
}
export default new ForwardPairsInternal();

View File

@ -11,6 +11,7 @@ import commands from '../constants/commands';
import OicqClient from '../client/OicqClient';
import { md5B64 } from '../utils/hashing';
import TelegramChat from '../client/TelegramChat';
import forwardPairs from '../providers/forwardPairs';
const DEFAULT_FILTER_ID = 114; // 514
@ -111,7 +112,7 @@ export default class ConfigService {
private async createGroupAndLink(roomId: number, title?: string) {
this.log.info(`创建群组并关联:${roomId}`);
const qEntity = this.oicq.getEntity(roomId);
const qEntity = this.oicq.getChat(roomId);
if (!title) {
// TS 这边不太智能
if (qEntity instanceof Friend) {
@ -139,9 +140,7 @@ export default class ConfigService {
// 关联写入数据库
await status.edit({ text: '正在写数据库…' });
const dbPair = await db.forwardPair.create({
data: { qqRoomId: roomId, tgChatId: Number(chat.id) },
});
const dbPair = await forwardPairs.add(qEntity, chatForBot);
isFinish = true;
// 更新头像
@ -182,12 +181,11 @@ export default class ConfigService {
public async createLinkGroup(qqRoomId: number, tgChatId: number) {
let message: string;
try {
const qGroup = this.oicq.gl.get(-qqRoomId);
const tgChat = (await this.tgBot.getChat(tgChatId)).entity as Api.Chat;
message = `QQ群${qGroup.group_name} (<code>${qGroup.group_id}</code>)已与 Telegram 群 ${tgChat.title} (<code>${tgChatId}</code>)关联`;
await db.forwardPair.create({
data: { qqRoomId, tgChatId },
});
const qGroup = this.oicq.getChat(qqRoomId) as Group;
const tgChat = await this.tgBot.getChat(tgChatId);
message = `QQ群${qGroup.group_id} (<code>${qGroup.group_id}</code>)已与 ` +
`Telegram 群 ${(tgChat.entity as Api.Chat).title} (<code>${tgChatId}</code>)关联`;
await forwardPairs.add(qGroup, tgChat);
}
catch (e) {
message = `错误:<code>${e}</code>`;