Skip to content

Commit

Permalink
feat: 只撤回一端的消息
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Sep 17, 2024
1 parent a2b4808 commit b39490c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
1 change: 1 addition & 0 deletions main/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ model Message {
nick String? // /抱 的时候会用到
tgSenderId BigInt? @db.BigInt
richHeaderUsed Boolean @default(false)
ignoreDelete Boolean @default(false) // rmq rmt
@@index([qqRoomId, qqSenderId, seq, rand, pktnum, time, instanceId])
@@index([tgChatId, tgMsgId, instanceId])
Expand Down
2 changes: 1 addition & 1 deletion main/src/client/QQClient/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface QQEntity {

getVideoUrl(fid: string, md5: string | Buffer): Promise<string>;

recallMsg(paramOrMessageId: number, rand?: number, timeOrPktNum?: number): Promise<boolean>;
recallMsg(seqOrMessageId: number, rand?: number, timeOrPktNum?: number): Promise<boolean>;

sendMsg(content: Sendable, source?: Quotable): Promise<MessageRet>;

Expand Down
12 changes: 12 additions & 0 deletions main/src/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ const inChatCommands = [
command: 'q',
description: '生成 QuotLy 图片',
}),
new Api.BotCommand({
command: 'rm',
description: '在双端撤回被回复的消息',
}),
new Api.BotCommand({
command: 'rmt',
description: '在 TG 撤回被回复的消息',
}),
new Api.BotCommand({
command: 'rmq',
description: '在 QQ 撤回被回复的消息',
}),
];

const groupInChatCommands = [
Expand Down
2 changes: 1 addition & 1 deletion main/src/controllers/DeleteMessageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class DeleteMessageController {
private onTelegramMessage = async (message: Api.Message) => {
const pair = this.instance.forwardPairs.find(message.chat);
if (!pair) return false;
if (message.message?.startsWith('/rm')) {
if (message.message?.split('@')?.[0] === '/rm') {
// 撤回消息
await this.deleteMessageService.handleTelegramMessageRm(message, pair);
return true;
Expand Down
6 changes: 6 additions & 0 deletions main/src/controllers/InChatCommandsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export default class InChatCommandsController {
message: '设置' + (result ? '成功' : '失败'),
});
return true;
case '/rmt':
await this.service.rmt(message, pair);
return true;
case '/rmq':
await this.service.rmq(message, pair);
return true;
}
};
}
10 changes: 4 additions & 6 deletions main/src/services/DeleteMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default class DeleteMessageService {
if (messageInfo) {
try {
if (this.lock(`qq-${pair.qqRoomId}-${messageInfo.seq}`)) return;
if (messageInfo.ignoreDelete) return;
const mapQq = pair.instanceMapForTg[messageInfo.tgSenderId.toString()];
mapQq && this.recallQqMessage(mapQq, messageInfo.seq, Number(messageInfo.rand), messageInfo.pktnum, pair, false, true);
// 假如 mapQQ 是普通成员,机器人是管理员,上面撤回失败了也可以由机器人撤回
Expand All @@ -78,9 +79,7 @@ export default class DeleteMessageService {
this.recallQqMessage(pair.qq, messageInfo.seq, Number(messageInfo.rand),
pair.qq.dm ? messageInfo.time : messageInfo.pktnum,
pair, isOthersMsg, !!mapQq);
await db.message.delete({
where: { id: messageInfo.id },
});
// 有 lock 了,这里不需要删除数据库了
}
catch (e) {
this.log.error(e);
Expand Down Expand Up @@ -164,6 +163,7 @@ export default class DeleteMessageService {
});
if (!message) return;
if (this.lock(`tg-${pair.tgId}-${message.tgMsgId}`)) return;
if (message.ignoreDelete) return;
if ((pair.flags | this.instance.flags) & flags.DISABLE_DELETE_MESSAGE) {
await pair.tg.editMessages({
message: message.tgMsgId,
Expand All @@ -173,9 +173,7 @@ export default class DeleteMessageService {
}
else {
await pair.tg.deleteMessages(message.tgMsgId);
await db.message.delete({
where: { id: message.id },
});
// 有 lock 了,这里不需要删除数据库了
}
}
catch (e) {
Expand Down
53 changes: 53 additions & 0 deletions main/src/services/InChatCommandsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,57 @@ export default class InChatCommandsService {
});
}
}

public async rmt(message: Api.Message, pair: Pair) {
if (!message.replyToMsgId) {
const reply = await message.reply({
message: '<i>请回复一条消息</i>',
});
setTimeout(() => {
reply.delete({ revoke: true });
message.delete({ revoke: true });
}, 5000);
return;
}
await db.message.updateMany({
where: {
tgChatId: pair.tgId,
tgMsgId: message.replyToMsgId,
},
data: {
ignoreDelete: true,
},
});
await message.delete({ revoke: true });
await pair.tg.deleteMessages([message.replyToMsgId]);
}

public async rmq(message: Api.Message, pair: Pair) {
if (!message.replyToMsgId) {
const reply = await message.reply({
message: '<i>请回复一条消息</i>',
});
setTimeout(() => {
reply.delete({ revoke: true });
message.delete({ revoke: true });
}, 5000);
return;
}
const dbEntry = await db.message.findFirst({
where: {
tgChatId: pair.tgId,
tgMsgId: message.replyToMsgId,
},
});
await db.message.update({
where: {
id: dbEntry.id,
},
data: {
ignoreDelete: true,
},
});
await message.delete({ revoke: true });
await pair.qq.recallMsg(dbEntry.seq, Number(dbEntry.rand), pair.qq.dm ? dbEntry.time : dbEntry.pktnum);
}
}

0 comments on commit b39490c

Please sign in to comment.