Files
SnowLuma/packages/onebot/src/history-persistence.ts
T
DrSmoothl dc2ec1235a feat(onebot): backfill cloud history after login
Add an opt-in per-account login history backfill that waits for roster warmup and a 30-second grace period before silently filling local message history. The setting defaults to disabled and applies on the next account lifecycle.

Run one account at a time, prioritize known local gaps, and process at most three groups plus three friends with one 20-message page per session. Failures are logged and never retried during the same login.

Persist reconstructed history without dispatching OneBot events or changing QQ read state. Keep private metadata and event bodies atomic, honor recall tombstones, suppress live-only history side effects, avoid extra media requests, and stop queued, in-flight, or converting work when the account closes.

Share the persistence path with manual history actions and add lifecycle, cancellation, corruption, partial-failure, peer-identity, and storage-atomicity regression coverage. Refs #280.
2026-07-25 23:44:28 +08:00

81 lines
2.8 KiB
TypeScript

import type { FriendMessage } from '@snowluma/protocol/events';
import {
GROUP_MESSAGE_EVENT,
privateMessageEventName,
} from './message-id';
import type { MessageStore } from './message-store';
import type { JsonObject } from './types';
/**
* Persist a converted server-history event without dispatching it.
*
* Private history needs the conversation peer explicitly because `user_id`
* names the sender and therefore equals the bot account on outgoing messages.
*/
export function persistHistoryEvent(
store: MessageStore,
event: JsonObject,
privatePeerId?: number,
source?: FriendMessage,
): void {
const messageId = toHistoryInt(event.message_id);
if (!Number.isSafeInteger(messageId) || messageId === 0) {
throw new Error(`history event has invalid message_id ${String(event.message_id)}`);
}
if (event.message_type !== 'group' && event.message_type !== 'private') {
throw new Error(`history event has invalid message_type ${String(event.message_type)}`);
}
const isGroup = event.message_type === 'group';
const sessionId = isGroup
? toHistoryInt(event.group_id)
: (privatePeerId && privatePeerId > 0 ? privatePeerId : toHistoryInt(event.user_id));
const messageSequence = toHistoryInt(event.message_seq);
const sequence = source?.ntMsgSeq ?? messageSequence;
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) {
throw new Error(`history event has invalid session id ${String(sessionId)}`);
}
if (!Number.isSafeInteger(sequence) || sequence <= 0) {
throw new Error(`history event has invalid sequence ${String(sequence)}`);
}
const sequenceAuthoritative = source?.sequenceAuthoritative !== false && sequence > 0;
const isOutgoingPrivate = !isGroup && source?.senderUin === source?.selfUin;
const eventName = isGroup
? GROUP_MESSAGE_EVENT
: source
? privateMessageEventName(isOutgoingPrivate, sequenceAuthoritative)
: privateMessageEventName(event.post_type === 'message_sent', false);
const meta = !isGroup && source
? {
isGroup: false,
targetId: sessionId,
sequence,
sequenceAuthoritative,
eventName,
clientSequence: source.clientSeq ?? source.msgSeq,
privateDirection: source.senderUin === source.selfUin
? 'outgoing'
: 'incoming',
random: source.msgId,
timestamp: source.time,
} satisfies Parameters<MessageStore['storeMeta']>[1]
: undefined;
store.storeHistoryEvent(
messageId,
isGroup,
sessionId,
sequence,
eventName,
event,
{ sequenceAuthoritative, meta },
);
}
export function toHistoryInt(value: unknown): number {
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
if (typeof value === 'string' && value.trim()) {
const number = Number(value);
if (Number.isFinite(number)) return Math.trunc(number);
}
return 0;
}