feat(onebot): expose bidirectional message history
Add reverse_order to group and private history actions, preserve latest-page behavior without an anchor, and keep local fallback results chronologically ordered. Key self-sent private messages and reply targets by the conversation peer, reject cross-conversation anchors, and include request context when a server fetch falls back to local history. Refs #229
This commit is contained in:
17 files changed
+468
-70
No files matched your search
@@ -16,6 +16,7 @@ export class MessageStore {
|
||||
private readonly stmtResolveReplyGroup: StatementSync;
|
||||
private readonly stmtResolveReplyPrivate: StatementSync;
|
||||
private readonly stmtListEventsAnchored: StatementSync;
|
||||
private readonly stmtListEventsAnchoredForward: StatementSync;
|
||||
private readonly stmtListEventsLatest: StatementSync;
|
||||
private readonly stmtListIncomingC2CSessions: StatementSync;
|
||||
|
||||
@@ -84,6 +85,14 @@ export class MessageStore {
|
||||
LIMIT ?`,
|
||||
);
|
||||
|
||||
this.stmtListEventsAnchoredForward = this.db.prepare(
|
||||
`SELECT data
|
||||
FROM messages
|
||||
WHERE is_group = ? AND session_id = ? AND data IS NOT NULL AND sequence >= ?
|
||||
ORDER BY sequence ASC
|
||||
LIMIT ?`,
|
||||
);
|
||||
|
||||
this.stmtListEventsLatest = this.db.prepare(
|
||||
`SELECT data
|
||||
FROM messages
|
||||
@@ -200,6 +209,7 @@ export class MessageStore {
|
||||
sessionId: number,
|
||||
count = 20,
|
||||
anchorSequence?: number,
|
||||
reverseOrder = true,
|
||||
): JsonObject[] {
|
||||
if (!Number.isInteger(sessionId) || sessionId <= 0) return [];
|
||||
|
||||
@@ -208,7 +218,9 @@ export class MessageStore {
|
||||
const anchor = hasAnchor ? (anchorSequence as number) : 0;
|
||||
|
||||
const rows = hasAnchor
|
||||
? this.stmtListEventsAnchored.all(isGroup ? 1 : 0, sessionId, anchor, limit)
|
||||
? reverseOrder
|
||||
? this.stmtListEventsAnchored.all(isGroup ? 1 : 0, sessionId, anchor, limit)
|
||||
: this.stmtListEventsAnchoredForward.all(isGroup ? 1 : 0, sessionId, anchor, limit)
|
||||
: this.stmtListEventsLatest.all(isGroup ? 1 : 0, sessionId, limit);
|
||||
|
||||
const result: JsonObject[] = [];
|
||||
@@ -222,8 +234,9 @@ export class MessageStore {
|
||||
}
|
||||
}
|
||||
|
||||
// Keep chronological order (oldest -> newest) for API consumers.
|
||||
result.reverse();
|
||||
// Backward/latest queries are selected DESC, while forward queries already
|
||||
// arrive ASC. Always expose chronological order to API consumers.
|
||||
if (!hasAnchor || reverseOrder) result.reverse();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user