2022-12-23 02:58:01 +00:00
|
|
|
import { getLogger } from "log4js";
|
|
|
|
import { Friend, Group } from "oicq";
|
|
|
|
import TelegramChat from "../client/TelegramChat";
|
|
|
|
import getAboutText from "../utils/getAboutText";
|
|
|
|
import { md5 } from "../utils/hashing";
|
|
|
|
import { getAvatar } from "../utils/urls";
|
|
|
|
import db from "./db";
|
2022-03-07 10:05:14 +00:00
|
|
|
|
2022-12-23 02:58:01 +00:00
|
|
|
const log = getLogger("ForwardPair");
|
2022-03-07 10:05:14 +00:00
|
|
|
|
|
|
|
export class Pair {
|
2022-10-17 13:19:24 +00:00
|
|
|
constructor(
|
|
|
|
public readonly qq: Friend | Group,
|
|
|
|
private _tg: TelegramChat,
|
|
|
|
public dbId: number,
|
|
|
|
private _joinNotice: boolean,
|
|
|
|
private _poke: boolean,
|
|
|
|
private _enable: boolean,
|
|
|
|
private _disableQ2TG: boolean,
|
2022-12-23 02:58:01 +00:00
|
|
|
private _disableTG2Q: boolean
|
|
|
|
) {}
|
2022-03-07 10:05:14 +00:00
|
|
|
|
2022-03-16 14:51:44 +00:00
|
|
|
// 更新 TG 群组的头像和简介
|
|
|
|
public async updateInfo() {
|
|
|
|
const avatarCache = await db.avatarCache.findFirst({
|
|
|
|
where: { forwardPairId: this.dbId },
|
|
|
|
});
|
|
|
|
const lastHash = avatarCache ? avatarCache.hash : null;
|
|
|
|
const avatar = await getAvatar(this.qqRoomId);
|
|
|
|
const newHash = md5(avatar);
|
|
|
|
if (!lastHash || lastHash.compare(newHash) !== 0) {
|
|
|
|
log.debug(`更新群头像: ${this.qqRoomId}`);
|
|
|
|
await this._tg.setProfilePhoto(avatar);
|
|
|
|
await db.avatarCache.upsert({
|
|
|
|
where: { forwardPairId: this.dbId },
|
|
|
|
update: { hash: newHash },
|
|
|
|
create: { forwardPairId: this.dbId, hash: newHash },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await this._tg.editAbout(await getAboutText(this.qq, false));
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
get qqRoomId() {
|
|
|
|
return this.qq instanceof Friend ? this.qq.user_id : -this.qq.group_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get tgId() {
|
|
|
|
return Number(this._tg.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
get tg() {
|
|
|
|
return this._tg;
|
|
|
|
}
|
|
|
|
|
|
|
|
set tg(value: TelegramChat) {
|
|
|
|
this._tg = value;
|
2022-10-17 13:19:24 +00:00
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { tgChatId: Number(value.id) },
|
|
|
|
})
|
2022-03-07 10:05:14 +00:00
|
|
|
.then(() => log.info(`出现了到超级群组的转换: ${value.id}`));
|
|
|
|
}
|
2022-08-14 04:09:30 +00:00
|
|
|
|
|
|
|
get joinNotice() {
|
|
|
|
return this._joinNotice;
|
|
|
|
}
|
|
|
|
|
|
|
|
set joinNotice(value) {
|
|
|
|
this._joinNotice = value;
|
2022-10-17 13:19:24 +00:00
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { joinNotice: value },
|
|
|
|
})
|
2022-08-14 04:09:30 +00:00
|
|
|
.then(() => 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
get poke() {
|
|
|
|
return this._poke;
|
|
|
|
}
|
|
|
|
|
|
|
|
set poke(value) {
|
|
|
|
this._poke = value;
|
2022-10-17 13:19:24 +00:00
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { poke: value },
|
|
|
|
})
|
2022-08-14 04:09:30 +00:00
|
|
|
.then(() => 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
get enable() {
|
|
|
|
return this._enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
set enable(value) {
|
|
|
|
this._enable = value;
|
2022-10-17 13:19:24 +00:00
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { enable: value },
|
|
|
|
})
|
|
|
|
.then(() => 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
get disableQ2TG() {
|
|
|
|
return this._disableQ2TG;
|
|
|
|
}
|
|
|
|
|
|
|
|
set disableQ2TG(value) {
|
|
|
|
this._disableQ2TG = value;
|
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { disableQ2TG: value },
|
|
|
|
})
|
|
|
|
.then(() => 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
get disableTG2Q() {
|
|
|
|
return this._disableTG2Q;
|
|
|
|
}
|
|
|
|
|
|
|
|
set disableTG2Q(value) {
|
|
|
|
this._disableTG2Q = value;
|
|
|
|
db.forwardPair
|
|
|
|
.update({
|
|
|
|
where: { id: this.dbId },
|
|
|
|
data: { disableTG2Q: value },
|
|
|
|
})
|
2022-08-14 04:09:30 +00:00
|
|
|
.then(() => 0);
|
|
|
|
}
|
2022-03-07 10:05:14 +00:00
|
|
|
}
|