fix(chat): isolate cached data by identity
Scope message and media caches to the active user or guest identity. Preserve identity snapshots across async chat flows and discard legacy unscoped message data during the IndexedDB upgrade.
This commit is contained in:
@@ -3,10 +3,8 @@
|
||||
/**
|
||||
* Dexie 数据库定义
|
||||
*
|
||||
* 唯一表 `messages`,只有 `++dbId` 自增主键,无次级索引(按本轮需求"最简表")。
|
||||
* 未来要按 session 查询可升级到:
|
||||
* this.version(2).stores({ messages: "++dbId, sessionId, createdAt" })
|
||||
* .upgrade(async (tx) => { ... });
|
||||
* `messages.sessionId` 是当前聊天身份的缓存命名空间。所有消息读写必须通过
|
||||
* 该索引限定身份,不能退化为整表操作。
|
||||
*
|
||||
* 构造时 `dbName` 可注入,便于测试时每个用例用独立 DB 互不污染。
|
||||
*/
|
||||
@@ -63,5 +61,15 @@ export class LocalChatDB extends Dexie {
|
||||
messages: "++dbId",
|
||||
media: "cacheKey, ownerKey, messageId, kind, remoteUrl, updatedAt, lastAccessedAt",
|
||||
});
|
||||
this.version(3)
|
||||
.stores({
|
||||
messages: "++dbId, sessionId",
|
||||
media: "cacheKey, ownerKey, messageId, kind, remoteUrl, updatedAt, lastAccessedAt",
|
||||
})
|
||||
.upgrade(async (transaction) => {
|
||||
// v1/v2 always wrote an empty sessionId. Its owner cannot be recovered,
|
||||
// so retaining it would expose one identity's history to another.
|
||||
await transaction.table("messages").clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user