修复提交疏漏

修改在查询群历史消息时,如未提供msg_seq,则返回最新消息
This commit is contained in:
Alen
2024-07-27 15:01:08 +08:00
parent 990a31e961
commit be605f11f2
2 changed files with 32 additions and 6 deletions

View File

@@ -61,6 +61,10 @@ class LimitedHashTable<K, V> {
this.valueToKey.delete(value); this.valueToKey.delete(value);
} }
} }
getKeyList(): K[] {
return Array.from(this.keyToValue.keys());
}
} }
class MessageUniqueWrapper { class MessageUniqueWrapper {
@@ -111,6 +115,25 @@ class MessageUniqueWrapper {
this.msgIdMap.resize(maxSize); this.msgIdMap.resize(maxSize);
this.msgDataMap.resize(maxSize); this.msgDataMap.resize(maxSize);
} }
getNthLatestShortIdByPeer(peer: Peer, index: number = 1): number | undefined {
const peerUid = peer.peerUid;
const chatType = peer.chatType;
const keys = this.msgDataMap.getKeyList();
const matches: number[] = [];
for (const key of keys) {
const [msgId, chatTypeStr, peerUidStr] = key.split('|');
if (peerUidStr === peerUid && chatTypeStr === chatType.toString()) {
const shortId = this.msgDataMap.getValue(key);
if (shortId) {
matches.push(shortId);
}
}
}
if (matches.length >= index) {
return matches[matches.length - index];
}
return undefined;
}
} }
export const MessageUnique: MessageUniqueWrapper = new MessageUniqueWrapper(); export const MessageUnique: MessageUniqueWrapper = new MessageUniqueWrapper();

View File

@@ -18,7 +18,7 @@ const SchemaData = {
message_seq: { type: 'number' }, message_seq: { type: 'number' },
count: { type: 'number' } count: { type: 'number' }
}, },
required: ['group_id', 'count'] required: [ 'group_id' ]
} as const satisfies JSONSchema; } as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>; type Payload = FromSchema<typeof SchemaData>;
@@ -31,13 +31,16 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction<Payload, Resp
if (!group) { if (!group) {
throw `${payload.group_id}不存在`; throw `${payload.group_id}不存在`;
} }
const startMsgId = (await MessageUnique.getMsgIdAndPeerByShortId(payload.message_seq || 0))?.MsgId || '0'; let targetMsgShortId, count = parseInt(payload.count?.toString() ?? '20');
// log("startMsgId", startMsgId) const peer = {
const historyResult = (await NTQQMsgApi.getMsgHistory({
chatType: ChatType.group, chatType: ChatType.group,
peerUid: group.groupCode peerUid: group.groupCode
}, startMsgId, parseInt(payload.count?.toString()) || 20)); }
//logDebug(historyResult); if ( !payload.message_seq ) {
targetMsgShortId = await MessageUnique.getNthLatestShortIdByPeer(peer, count)
}
const startMsgId = (await MessageUnique.getMsgIdAndPeerByShortId(targetMsgShortId ?? (payload.message_seq ?? 0)))?.MsgId || '0';
const historyResult = (await NTQQMsgApi.getMsgHistory(peer, startMsgId, count));
const msgList = historyResult.msgList; const msgList = historyResult.msgList;
await Promise.all(msgList.map(async msg => { await Promise.all(msgList.map(async msg => {
msg.id = await MessageUnique.createMsg({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId); msg.id = await MessageUnique.createMsg({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId);