Skip to content

Commit

Permalink
Merge branch 'clansty:rainbowcat' into rainbowcat
Browse files Browse the repository at this point in the history
  • Loading branch information
Nofated095 authored Feb 3, 2024
2 parents 2ed24c5 + bd8092c commit cd3e2e1
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 90 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"nodejs-base64": "^2.0.0",
"prisma": "5.8.0",
"prompts": "^2.4.2",
"quote-api": "https://github.com/Clansty/quote-api/archive/37a0e48a434b94bb04c04c7d86d9f0d2295df869.tar.gz",
"quote-api": "https://github.com/Clansty/quote-api/archive/014b21138afbbe0e12c91b00561414b1e851fc0f.tar.gz",
"sharp": "^0.33.2",
"silk-sdk": "^0.2.2",
"telegram": "^2.19.10",
"telegram": "https://github.com/clansty/gramjs/releases/download/2.19.10%2Brevert_media/telegram-2.19.10.tgz",
"tmp-promise": "^3.0.3",
"undici": "^6.4.0",
"zincsearch-node": "^2.1.0",
Expand Down
73 changes: 38 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,23 @@ model QqBot {
}

model Message {
id Int @id @default(autoincrement())
qqRoomId BigInt @db.BigInt
qqSenderId BigInt @db.BigInt
time Int
brief String?
seq Int
rand BigInt @db.BigInt
pktnum Int
tgChatId BigInt @db.BigInt
tgMsgId Int
instanceId Int @default(0)
instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
tgFileId BigInt? @db.BigInt
tgMessageText String?
nick String? // /抱 的时候会用到
tgSenderId BigInt? @db.BigInt
id Int @id @default(autoincrement())
qqRoomId BigInt @db.BigInt
qqSenderId BigInt @db.BigInt
time Int
brief String?
seq Int
rand BigInt @db.BigInt
pktnum Int
tgChatId BigInt @db.BigInt
tgMsgId Int
instanceId Int @default(0)
instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
tgFileId BigInt? @db.BigInt
tgMessageText String?
nick String? // /抱 的时候会用到
tgSenderId BigInt? @db.BigInt
richHeaderUsed Boolean @default(false)
@@index([qqRoomId, qqSenderId, seq, rand, pktnum, time, instanceId])
@@index([tgChatId, tgMsgId, instanceId])
Expand Down
7 changes: 7 additions & 0 deletions src/constants/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ export default {
index = index % arr.length;
return arr[index];
},
tgColor(index: number) {
// https://github.com/telegramdesktop/tdesktop/blob/7049929a59176a996c4257d5a09df08b04ac3b22/Telegram/SourceFiles/ui/chat/chat_style.cpp#L1043
// https://github.com/LyoSU/quote-api/blob/master/utils/quote-generate.js#L163
const arr = [...new Intl.Segmenter().segment('❤️🧡💜💚🩵💙🩷')].map(x => x.segment);
index = index % arr.length;
return arr[index];
},
};
1 change: 1 addition & 0 deletions src/constants/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum flags {
NO_DELETE_MESSAGE = 1 << 4,
NO_AUTO_CREATE_PM = 1 << 5,
COLOR_EMOJI_PREFIX = 1 << 6,
RICH_HEADER = 1 << 7,
}

export default flags;
54 changes: 25 additions & 29 deletions src/controllers/ForwardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,32 @@ export default class ForwardController {
});
if (existed) return;
// 开始转发过程
let tgMessages: Api.Message | Api.Message[] = await this.forwardService.forwardFromQq(event, pair);
if (!tgMessages) return;
if (!Array.isArray(tgMessages)) {
tgMessages = [tgMessages];
}
for (const tgMessage of tgMessages) {
// 更新数据库
await db.message.create({
data: {
qqRoomId: pair.qqRoomId,
qqSenderId: event.sender.user_id,
time: event.time,
brief: event.raw_message,
seq: event.seq,
rand: event.rand,
pktnum: event.pktnum,
tgChatId: pair.tgId,
tgMsgId: tgMessage.id,
instanceId: this.instance.id,
tgMessageText: tgMessage.message,
tgFileId: forwardHelper.getMessageDocumentId(tgMessage),
nick: event.nickname,
tgSenderId: BigInt(this.tgBot.me.id.toString()),
},
});
await this.forwardService.addToZinc(pair.dbId, tgMessage.id, {
text: event.raw_message,
let { tgMessage, richHeaderUsed } = await this.forwardService.forwardFromQq(event, pair);
if (!tgMessage) return;
// 更新数据库
await db.message.create({
data: {
qqRoomId: pair.qqRoomId,
qqSenderId: event.sender.user_id,
time: event.time,
brief: event.raw_message,
seq: event.seq,
rand: event.rand,
pktnum: event.pktnum,
tgChatId: pair.tgId,
tgMsgId: tgMessage.id,
instanceId: this.instance.id,
tgMessageText: tgMessage.message,
tgFileId: forwardHelper.getMessageDocumentId(tgMessage),
nick: event.nickname,
});
}
tgSenderId: BigInt(this.tgBot.me.id.toString()),
richHeaderUsed,
},
});
await this.forwardService.addToZinc(pair.dbId, tgMessage.id, {
text: event.raw_message,
nick: event.nickname,
});
}
catch (e) {
this.log.error('处理 QQ 消息时遇到问题', e);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/QuotLyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class {
title: message.nick,
photo: { url: getAvatarUrl(message.qqSenderId) },
};
if (message.qqRoomId > 0) {
if (message.qqRoomId > 0 || message.richHeaderUsed) {
quoteMessage.text = message.tgMessageText;
}
else if (message.tgMessageText.includes('\n')) {
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class {
photo = await convert.cachedBuffer(`${sender.photo.photoId.toString(16)}.jpg`, () => this.tgBot.downloadEntityPhoto(sender));
}
messageFrom = {
id: Number(message.tgSenderId),
id: sender.color || Number(message.tgSenderId),
name: message.nick,
title: message.nick,
username: sender.username,
Expand Down
51 changes: 45 additions & 6 deletions src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import ReplyKeyboardHide = Api.ReplyKeyboardHide;
import env from '../models/env';
import { CustomFile } from 'telegram/client/uploads';
import flags from '../constants/flags';
import BigInteger from 'big-integer';

const NOT_CHAINABLE_ELEMENTS = ['flash', 'record', 'video', 'location', 'share', 'json', 'xml', 'poke'];

Expand Down Expand Up @@ -331,7 +332,6 @@ export default class ForwardService {
}
}
message = message.trim();
message = messageHeader + (message && messageHeader ? '\n' : '') + message;

// 处理回复
if (event.source) {
Expand Down Expand Up @@ -366,28 +366,66 @@ export default class ForwardService {
}


let richHeaderUsed = false;
// 发送消息
const messageToSend: SendMessageParams = {
forceDocument: forceDocument as any, // 恼
};
message && (messageToSend.message = message);
if (files.length === 1) {
messageToSend.file = files[0];
}
else if (files.length) {
messageToSend.file = files;
}
else if ((pair.flags | this.instance.flags) & flags.RICH_HEADER) {
// 没有文件时才能显示链接预览
richHeaderUsed = true;
const url = new URL('https://q2tg-header.clansty.workers.dev');
url.searchParams.set('name', sender);
url.searchParams.set('title', 'title' in event.sender ? event.sender.title : '');
url.searchParams.set('role', 'role' in event.sender ? event.sender.role : '');
url.searchParams.set('id', event.sender.user_id.toString());
// https://github.com/tdlib/td/blob/437c2d0c6e0ad104022d5ad86ddc8aedc41cb7a8/td/telegram/MessageContent.cpp#L2575
// https://github.com/tdlib/td/blob/437c2d0c6e0ad104022d5ad86ddc8aedc41cb7a8/td/generate/scheme/telegram_api.tl#L1841
// https://github.com/gram-js/gramjs/pull/633
messageToSend.file = new Api.InputMediaWebPage({
url: url.toString(),
forceSmallMedia: true,
optional: true,
});
messageToSend.linkPreview = { showAboveText: true };
}

if (!richHeaderUsed) {
message = messageHeader + (message && messageHeader ? '\n' : '') + message;
}
message && (messageToSend.message = message);

buttons.length && (messageToSend.buttons = _.chunk(buttons, 3));
replyTo && (messageToSend.replyTo = replyTo);

const tgMessage = await pair.tg.sendMessage(messageToSend);
let tgMessage: Api.Message;
try {
tgMessage = await pair.tg.sendMessage(messageToSend);
}
catch (e) {
if (richHeaderUsed) {
this.log.warn('Rich Header 发送错误', messageToSend.file, e);
delete messageToSend.file;
delete messageToSend.linkPreview;
message = messageHeader + (message && messageHeader ? '\n' : '') + message;
message && (messageToSend.message = message);
tgMessage = await pair.tg.sendMessage(messageToSend);
}
else throw e;
}

if (this.instance.workMode === 'personal' && event.message_type === 'group' && event.atall) {
await tgMessage.pin({ notify: false });
}

tempFiles.forEach(it => it.cleanup());
return tgMessage;
return { tgMessage, richHeaderUsed };
}
catch (e) {
this.log.error('从 QQ 到 TG 的消息转发失败', e);
Expand All @@ -396,11 +434,12 @@ export default class ForwardService {
}
catch {
}
return null;
return {};
}
}

public async forwardFromTelegram(message: Api.Message, pair: Pair): Promise<Array<QQMessageSent>> {
// console.log(message);
try {
const tempFiles: FileResult[] = [];
let chain: Sendable = [];
Expand All @@ -415,7 +454,7 @@ export default class ForwardService {
'') +
': \n';
if ((pair.flags | this.instance.flags) & flags.COLOR_EMOJI_PREFIX) {
messageHeader = emoji.color(message.senderId.toJSNumber()) + messageHeader;
messageHeader = emoji.tgColor((message.sender as Api.User)?.color || message.senderId.toJSNumber()) + messageHeader;
}
if (message.photo instanceof Api.Photo ||
// stickers 和以文件发送的图片都是这个
Expand Down

0 comments on commit cd3e2e1

Please sign in to comment.