2022-12-23 02:58:01 +00:00
|
|
|
|
import { Friend, Group } from 'oicq';
|
2022-02-23 12:25:26 +00:00
|
|
|
|
import TelegramChat from '../client/TelegramChat';
|
|
|
|
|
import OicqClient from '../client/OicqClient';
|
|
|
|
|
import Telegram from '../client/Telegram';
|
|
|
|
|
import db from './db';
|
2022-02-26 10:15:40 +00:00
|
|
|
|
import { Entity } from 'telegram/define';
|
2022-03-01 08:14:04 +00:00
|
|
|
|
import { BigInteger } from 'big-integer';
|
2022-03-07 10:05:14 +00:00
|
|
|
|
import { Pair } from './Pair';
|
2022-03-27 11:35:57 +00:00
|
|
|
|
import { getLogger, Logger } from 'log4js';
|
2023-02-23 06:08:53 +00:00
|
|
|
|
import Instance from './Instance';
|
2022-02-23 12:25:26 +00:00
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
|
export default class ForwardPairs {
|
|
|
|
|
private pairs: Pair[] = [];
|
2022-03-27 11:35:57 +00:00
|
|
|
|
private readonly log: Logger;
|
2022-03-01 08:14:04 +00:00
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
|
private constructor(private readonly instanceId: number) {
|
2022-03-27 11:35:57 +00:00
|
|
|
|
this.log = getLogger(`ForwardPairs - ${instanceId}`);
|
2022-03-01 08:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 12:25:26 +00:00
|
|
|
|
// 在 forwardController 创建时初始化
|
2022-03-07 10:05:14 +00:00
|
|
|
|
private async init(oicq: OicqClient, tgBot: Telegram) {
|
|
|
|
|
const dbValues = await db.forwardPair.findMany({
|
|
|
|
|
where: { instanceId: this.instanceId },
|
|
|
|
|
});
|
2022-02-23 12:25:26 +00:00
|
|
|
|
for (const i of dbValues) {
|
2022-03-27 11:35:57 +00:00
|
|
|
|
try {
|
|
|
|
|
const qq = oicq.getChat(Number(i.qqRoomId));
|
|
|
|
|
const tg = await tgBot.getChat(Number(i.tgChatId));
|
|
|
|
|
if (qq && tg) {
|
2022-10-18 07:06:44 +00:00
|
|
|
|
this.pairs.push(new Pair(qq, tg, i.id, i.joinNotice, i.poke, i.enable, i.disableQ2TG, i.disableTG2Q));
|
2022-03-27 11:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
this.log.warn(`初始化遇到问题,QQ: ${i.qqRoomId} TG: ${i.tgChatId}`);
|
|
|
|
|
}
|
2022-02-23 12:25:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
|
public static async load(instanceId: number, oicq: OicqClient, tgBot: Telegram) {
|
|
|
|
|
const instance = new this(instanceId);
|
|
|
|
|
await instance.init(oicq, tgBot);
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 12:25:26 +00:00
|
|
|
|
public async add(qq: Friend | Group, tg: TelegramChat) {
|
2022-03-01 08:14:04 +00:00
|
|
|
|
const dbEntry = await db.forwardPair.create({
|
2022-02-23 12:25:26 +00:00
|
|
|
|
data: {
|
|
|
|
|
qqRoomId: qq instanceof Friend ? qq.user_id : -qq.group_id,
|
|
|
|
|
tgChatId: Number(tg.id),
|
2022-03-07 10:05:14 +00:00
|
|
|
|
instanceId: this.instanceId,
|
2022-02-23 12:25:26 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
2022-10-18 07:06:44 +00:00
|
|
|
|
this.pairs.push(new Pair(qq, tg, dbEntry.id, true, true, true, false, false));
|
2022-03-01 08:14:04 +00:00
|
|
|
|
return dbEntry;
|
2022-02-23 12:25:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 06:38:40 +00:00
|
|
|
|
public async remove(pair: Pair) {
|
|
|
|
|
this.pairs.splice(this.pairs.indexOf(pair), 1);
|
|
|
|
|
await db.forwardPair.delete({
|
|
|
|
|
where: { id: pair.dbId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 08:14:04 +00:00
|
|
|
|
public find(target: Friend | Group | TelegramChat | Entity | number | BigInteger) {
|
2022-03-06 15:32:31 +00:00
|
|
|
|
if (!target) return null;
|
2022-02-23 12:25:26 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-03-01 08:14:04 +00:00
|
|
|
|
else if (typeof target === 'number' || 'eq' in target) {
|
|
|
|
|
return this.pairs.find(e => e.qqRoomId === target || e.tg.id.eq(target));
|
2022-02-24 10:27:06 +00:00
|
|
|
|
}
|
2022-02-23 12:25:26 +00:00
|
|
|
|
else {
|
|
|
|
|
return this.pairs.find(e => e.tg.id.eq(target.id));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-23 06:08:53 +00:00
|
|
|
|
|
|
|
|
|
public async initMapInstance(instances: Instance[]) {
|
|
|
|
|
for (const forwardPair of this.pairs) {
|
|
|
|
|
for (const instance of instances) {
|
|
|
|
|
const instanceTgUserId = instance.userMe.id.toString();
|
|
|
|
|
if (forwardPair.instanceMapForTg[instanceTgUserId]) continue;
|
|
|
|
|
try {
|
|
|
|
|
const group = instance.oicq.getChat(forwardPair.qqRoomId) as Group;
|
|
|
|
|
if (!group) continue;
|
|
|
|
|
forwardPair.instanceMapForTg[instanceTgUserId] = group;
|
|
|
|
|
this.log.info('MapInstance', { group: forwardPair.qqRoomId, tg: instanceTgUserId, qq: instance.qqUin });
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-23 12:25:26 +00:00
|
|
|
|
}
|