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.
This commit is contained in:
15 files changed
+1810
-68
No files matched your search
@@ -15,6 +15,14 @@ export interface StoreEventOptions {
|
||||
sequenceAuthoritative?: boolean;
|
||||
}
|
||||
|
||||
export interface StoreHistoryEventOptions extends StoreEventOptions {
|
||||
/**
|
||||
* Private history metadata that must become visible atomically with the
|
||||
* event body. Group history does not require a separate metadata write.
|
||||
*/
|
||||
meta?: MessageMeta;
|
||||
}
|
||||
|
||||
export class MessageStore {
|
||||
private readonly db: DatabaseSync;
|
||||
private readonly stmtStoreEvent: StatementSync;
|
||||
@@ -27,6 +35,7 @@ export class MessageStore {
|
||||
private readonly stmtListEventsAnchoredForward: StatementSync;
|
||||
private readonly stmtListEventsLatest: StatementSync;
|
||||
private readonly stmtFindLatestAuthoritativeSequence: StatementSync;
|
||||
private readonly stmtFindLatestPersistedAuthoritativeSequence: StatementSync;
|
||||
private readonly stmtListIncomingC2CSessions: StatementSync;
|
||||
private readonly stmtFindPrivateRecall: StatementSync;
|
||||
private readonly stmtFindPrivateRecallTombstone: StatementSync;
|
||||
@@ -139,6 +148,15 @@ export class MessageStore {
|
||||
LIMIT 1`,
|
||||
);
|
||||
|
||||
this.stmtFindLatestPersistedAuthoritativeSequence = this.db.prepare(
|
||||
`SELECT sequence
|
||||
FROM messages
|
||||
WHERE is_group = ? AND session_id = ? AND data IS NOT NULL
|
||||
AND sequence_authoritative = 1 AND sequence > 0
|
||||
ORDER BY sequence DESC
|
||||
LIMIT 1`,
|
||||
);
|
||||
|
||||
this.stmtListIncomingC2CSessions = this.db.prepare(
|
||||
`SELECT session_id
|
||||
FROM messages
|
||||
@@ -269,6 +287,55 @@ export class MessageStore {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store one fetched history event.
|
||||
*
|
||||
* Private history needs both metadata and the event body. Keep those writes
|
||||
* in one transaction so a failed body write can never leave an authoritative
|
||||
* sequence that makes a later login skip the missing message.
|
||||
*/
|
||||
storeHistoryEvent(
|
||||
messageId: number,
|
||||
isGroup: boolean,
|
||||
sessionId: number,
|
||||
sequence: number,
|
||||
eventName: string,
|
||||
event: JsonObject,
|
||||
options: StoreHistoryEventOptions = {},
|
||||
): void {
|
||||
const { meta, ...eventOptions } = options;
|
||||
if (!meta) {
|
||||
this.storeEvent(
|
||||
messageId,
|
||||
isGroup,
|
||||
sessionId,
|
||||
sequence,
|
||||
eventName,
|
||||
event,
|
||||
eventOptions,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.db.exec('BEGIN IMMEDIATE');
|
||||
try {
|
||||
this.storeMeta(messageId, meta);
|
||||
this.storeEvent(
|
||||
messageId,
|
||||
isGroup,
|
||||
sessionId,
|
||||
sequence,
|
||||
eventName,
|
||||
event,
|
||||
eventOptions,
|
||||
);
|
||||
this.db.exec('COMMIT');
|
||||
} catch (error) {
|
||||
this.db.exec('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
findEvent(messageId: number): JsonObject | null {
|
||||
if (!isValidMessageId(messageId)) return null;
|
||||
const row = this.stmtFindEvent.get(messageId) as { data: string } | undefined;
|
||||
@@ -415,6 +482,24 @@ export class MessageStore {
|
||||
return row.sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the latest server-confirmed sequence whose OneBot event body is
|
||||
* actually present. Automatic history backfill must use this cursor so a
|
||||
* metadata-only live row cannot hide a missing event body.
|
||||
*/
|
||||
findLatestPersistedAuthoritativeSequence(
|
||||
isGroup: boolean,
|
||||
sessionId: number,
|
||||
): number | null {
|
||||
if (!Number.isInteger(sessionId) || sessionId <= 0) return null;
|
||||
const row = this.stmtFindLatestPersistedAuthoritativeSequence.get(
|
||||
isGroup ? 1 : 0,
|
||||
sessionId,
|
||||
) as { sequence: number } | undefined;
|
||||
if (!row || !Number.isInteger(row.sequence) || row.sequence <= 0) return null;
|
||||
return row.sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a private recall's sender-local client sequence to the OneBot
|
||||
* message id and remove the cached row. The protocol recall notice does not
|
||||
|
||||
Reference in new issue
Block a user