feat: sendPackedMessage & getMessages

This commit is contained in:
Wesley F. Young 2024-10-05 19:18:13 +08:00
parent 28f0030653
commit 5e06a491e3

View File

@ -4,6 +4,7 @@ import { LaanaActionHandler } from '../action';
import fs from 'fs';
import { ForwardMessagePing_Operation } from '@laana-proto/def';
// TODO: separate implementation and handler
export class LaanaMessageActionHandler {
constructor(
public core: NapCatCore,
@ -52,14 +53,31 @@ export class LaanaMessageActionHandler {
};
},
sendPackedMessages: async (params) => {
// first send every single message to self, then forward them to target peer
// send message one by one
const sendMsgIds: string[] = [];
for (const message of params.messages) {
sendMsgIds.push((await this.impl.sendMessage!({ targetPeer: params.targetPeer, message })).msgId);
}
await this.impl.forwardMessage!({
msgIds: sendMsgIds,
targetPeer: params.targetPeer,
operation: ForwardMessagePing_Operation.AS_PACKED,
});
return {
packedMsgId: '', // unimplemented
msgIds: sendMsgIds,
};
},
getMessage: async (params) => {
const { msgId, chatType, peerUid } = this.laana.utils.msg.decodeLaanaMsgId(params.msgId);
const msgListWrapper = await this.core.apis.MsgApi.getMsgsByMsgId(
{
chatType,
peerUid,
guildId: '',
},
{ chatType, peerUid, guildId: '' },
[msgId],
);
if (msgListWrapper.msgList.length === 0) {
@ -71,6 +89,46 @@ export class LaanaMessageActionHandler {
};
},
getMessages: async (params) => {
if (params.msgIds.length === 0) {
throw new Error('消息 ID 列表不能为空');
}
const msgIdWrappers = params.msgIds.map(msgId => this.laana.utils.msg.decodeLaanaMsgId(msgId));
// check whether chatType and peerUid for each message are the same
const firstMsg = msgIdWrappers[0];
if (msgIdWrappers.some(msg => msg.chatType !== firstMsg.chatType || msg.peerUid !== firstMsg.peerUid)) {
return {
// one request per message
messages: await Promise.all(
params.msgIds.map(msgId => this.laana.utils.msg.decodeLaanaMsgId(msgId))
.map(async ({ msgId, chatType, peerUid }) => {
const msgListWrapper = await this.core.apis.MsgApi.getMsgsByMsgId(
{ chatType, peerUid, guildId: '' },
[msgId],
);
if (msgListWrapper.msgList.length === 0) {
throw new Error('消息不存在');
}
return await this.laana.utils.msg.rawMessageToLaana(msgListWrapper.msgList[0]);
})
)
};
} else {
// a single request for all messages
const msgListWrapper = await this.core.apis.MsgApi.getMsgsByMsgId(
{ chatType: firstMsg.chatType, peerUid: firstMsg.peerUid, guildId: '' },
msgIdWrappers.map(msg => msg.msgId),
);
return {
messages: await Promise.all(
msgListWrapper.msgList.map(msg => 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);