NapCatQQ/packages/napcat-onebot/action/user/GetRecentContact.ts
手瓜一十雪 b69352f6a1 Add payload and return schemas to OneBot actions
Introduced explicit payloadSchema and returnSchema definitions for all OneBotAction classes using @sinclair/typebox. This improves type safety, API documentation, and validation for action payloads and return values. Also refactored method signatures and types for consistency across the codebase.
2026-01-25 14:50:58 +08:00

66 lines
2.8 KiB
TypeScript

import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { ActionName } from '@/napcat-onebot/action/router';
import { NetworkAdapterConfig } from '@/napcat-onebot/config/config';
import { Static, Type } from '@sinclair/typebox';
export const GetRecentContactPayloadSchema = Type.Object({
count: Type.Union([Type.Number(), Type.String()], { default: 10, description: '获取的数量' }),
});
export type GetRecentContactPayload = Static<typeof GetRecentContactPayloadSchema>;
export const GetRecentContactReturnSchema = Type.Array(Type.Object({
lastestMsg: Type.Any({ description: '最后一条消息' }),
peerUin: Type.String({ description: '对象QQ' }),
remark: Type.String({ description: '备注' }),
msgTime: Type.String({ description: '消息时间' }),
chatType: Type.Number({ description: '聊天类型' }),
msgId: Type.String({ description: '消息ID' }),
sendNickName: Type.String({ description: '发送者昵称' }),
sendMemberName: Type.String({ description: '发送者群名片' }),
peerName: Type.String({ description: '对象名称' }),
}), { description: '最近会话列表' });
export type GetRecentContactReturn = Static<typeof GetRecentContactReturnSchema>;
export default class GetRecentContact extends OneBotAction<GetRecentContactPayload, GetRecentContactReturn> {
override actionName = ActionName.GetRecentContact;
override payloadSchema = GetRecentContactPayloadSchema;
override returnSchema = GetRecentContactReturnSchema;
async _handle (payload: GetRecentContactPayload, _adapter: string, config: NetworkAdapterConfig): Promise<GetRecentContactReturn> {
const ret = await this.core.apis.UserApi.getRecentContactListSnapShot(+payload.count);
// 烘焙消息
const results = await Promise.all(ret.info.changedList.map(async (t) => {
const FastMsg = await this.core.apis.MsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]);
if (FastMsg.msgList.length > 0 && FastMsg.msgList[0]) {
// 扩展ret.info.changedList
const lastestMsg = await this.obContext.apis.MsgApi.parseMessage(FastMsg.msgList[0], config.messagePostFormat);
return {
lastestMsg,
peerUin: t.peerUin,
remark: t.remark,
msgTime: t.msgTime,
chatType: t.chatType,
msgId: t.msgId,
sendNickName: t.sendNickName,
sendMemberName: t.sendMemberName,
peerName: t.peerName,
};
}
return {
lastestMsg: undefined,
peerUin: t.peerUin,
remark: t.remark,
msgTime: t.msgTime,
chatType: t.chatType,
msgId: t.msgId,
sendNickName: t.sendNickName,
sendMemberName: t.sendMemberName,
peerName: t.peerName,
};
}));
return results;
}
}