feat: getForwardedMessages

This commit is contained in:
Wesley F. Young 2024-09-07 18:36:43 +08:00
parent d0e43472f9
commit dd360070d7
2 changed files with 55 additions and 5 deletions

View File

@ -69,5 +69,32 @@ export class LaanaMessageActionHandler {
message: await this.laana.utils.msg.rawMessageToLaana(msg),
};
},
getForwardedMessages: async (params) => {
const { rootMsgLaanaId, currentMsgId } = this.laana.utils.msg.decodeLaanaForwardMsgRefId(params.refId);
const decodedRootMsgId = this.laana.utils.msg.decodeLaanaMsgId(rootMsgLaanaId);
const rawForwardedMessages = await this.core.apis.MsgApi.getMultiMsg(
{
chatType: decodedRootMsgId.chatType,
peerUid: decodedRootMsgId.peerUid,
guildId: '',
},
decodedRootMsgId.msgId,
currentMsgId,
);
if (!rawForwardedMessages || rawForwardedMessages.result !== 0 || rawForwardedMessages.msgList.length === 0) {
throw new Error('获取转发消息失败');
}
return {
forwardMessage: {
refId: params.refId,
messages: await Promise.all(
rawForwardedMessages.msgList.map(async msg => {
return await this.laana.utils.msg.rawMessageToLaana(msg, rootMsgLaanaId);
}),
),
}
};
},
};
}

View File

@ -279,8 +279,7 @@ export class LaanaMessageUtils {
);
}
async rawMessageToLaana(msg: RawMessage): Promise<LaanaMessage> {
const msgContent = await this.createLaanaMessageContent(msg);
async rawMessageToLaana(msg: RawMessage, rootForwardMsgId?: string): Promise<LaanaMessage> {
return {
msgId: this.encodeMsgToLaanaMsgId(msg.msgId, msg.chatType, msg.peerUid),
time: BigInt(msg.msgTime),
@ -290,11 +289,11 @@ export class LaanaMessageUtils {
type: msg.chatType === ChatType.KCHATTYPEGROUP ?
Peer_Type.GROUP : Peer_Type.BUDDY,
},
content: msgContent,
content: await this.createLaanaMessageContent(msg, rootForwardMsgId),
};
}
private async createLaanaMessageContent(msg: RawMessage): Promise<LaanaMessage['content']> {
private async createLaanaMessageContent(msg: RawMessage, rootForwardMsgId?: string): Promise<LaanaMessage['content']> {
const firstElement = msg.elements[0];
if (!firstElement) {
@ -481,7 +480,10 @@ export class LaanaMessageUtils {
return {
oneofKind: 'forwardMsgRef',
forwardMsgRef: {
refId: this.encodeMsgToLaanaMsgId(msg.msgId, msg.chatType, msg.peerUid),
refId: this.encodeMsgToForwardMsgRefId(
rootForwardMsgId ?? this.encodeMsgToLaanaMsgId(msg.msgId, msg.chatType, msg.peerUid),
msg.msgId,
),
displayText: firstElement.multiForwardMsgElement.xmlContent,
}
};
@ -509,6 +511,9 @@ export class LaanaMessageUtils {
}
decodeLaanaMsgId(laanaMsgId: string) {
if (!laanaMsgId.startsWith('LaanaMsgId@')) {
throw Error('不合法的 LaanaMsgId');
}
const [msgId, chatType, peerUid] = laanaMsgId.split('@').slice(1);
return {
msgId,
@ -517,6 +522,24 @@ export class LaanaMessageUtils {
};
}
encodeMsgToForwardMsgRefId(
rootMsgLaanaId: string,
currentMsgId: string,
) {
return `LaanaForwardMsgRefId|${rootMsgLaanaId}|${currentMsgId}`;
}
decodeLaanaForwardMsgRefId(laanaForwardMsgId: string) {
if (!laanaForwardMsgId.startsWith('LaanaForwardMsgRefId|')) {
throw Error('不合法的 LaanaForwardMsgRefId');
}
const [rootMsgLaanaId, currentMsgId] = laanaForwardMsgId.split('|').slice(1);
return {
rootMsgLaanaId,
currentMsgId,
};
}
async getRepliedMsgId(element: ReplyElement, msg: RawMessage) {
const record = msg.records.find(
msgRecord => msgRecord.msgId === element.sourceMsgIdInRecords