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:
@@ -68,18 +68,23 @@ export class ChatRepository implements IChatRepository {
|
||||
async markPrivateMessageUnlockedInLocal(
|
||||
messageId: string,
|
||||
patch?: UnlockedPrivateMessageLocalPatch,
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<void>> {
|
||||
return this.localMessages.markMessageUnlocked(
|
||||
messageId,
|
||||
patch,
|
||||
cacheIdentity,
|
||||
);
|
||||
}
|
||||
|
||||
// ============ 本地(Dexie)操作 ============
|
||||
|
||||
/** 把一条消息写入本地存储。 */
|
||||
async saveMessageToLocal(message: ChatMessage): Promise<Result<void>> {
|
||||
return this.localMessages.saveMessage(message);
|
||||
async saveMessageToLocal(
|
||||
message: ChatMessage,
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<void>> {
|
||||
return this.localMessages.saveMessage(message, cacheIdentity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,26 +93,29 @@ export class ChatRepository implements IChatRepository {
|
||||
*/
|
||||
async saveMessagesToLocal(
|
||||
messages: readonly ChatMessage[],
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<void>> {
|
||||
return this.localMessages.saveMessages(messages);
|
||||
return this.localMessages.saveMessages(messages, cacheIdentity);
|
||||
}
|
||||
|
||||
/** 读取所有本地消息,按 dbId 升序。 */
|
||||
async getLocalMessages(): Promise<Result<ChatMessage[]>> {
|
||||
return this.localMessages.getMessages();
|
||||
async getLocalMessages(
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<ChatMessage[]>> {
|
||||
return this.localMessages.getMessages(cacheIdentity);
|
||||
}
|
||||
|
||||
/** 清空本地消息。 */
|
||||
async clearLocalMessages(): Promise<Result<void>> {
|
||||
return this.localMessages.clearMessages();
|
||||
async clearLocalMessages(cacheIdentity?: string): Promise<Result<void>> {
|
||||
return this.localMessages.clearMessages(cacheIdentity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地消息数量。
|
||||
* 替代 Dart 的同步 `int get localMessageCount`——Dexie 异步 API 决定必须 async。
|
||||
*/
|
||||
async getLocalMessageCount(): Promise<Result<number>> {
|
||||
return this.localMessages.getMessageCount();
|
||||
async getLocalMessageCount(cacheIdentity?: string): Promise<Result<number>> {
|
||||
return this.localMessages.getMessageCount(cacheIdentity);
|
||||
}
|
||||
|
||||
// ============ 本地媒体缓存 ============
|
||||
@@ -120,20 +128,26 @@ export class ChatRepository implements IChatRepository {
|
||||
|
||||
async cacheRemoteMedia(
|
||||
input: CacheRemoteChatMediaInput,
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<LocalChatMediaRow>> {
|
||||
return this.mediaCache.cacheRemoteMedia(input);
|
||||
return this.mediaCache.cacheRemoteMedia(input, cacheIdentity);
|
||||
}
|
||||
|
||||
async prefetchMediaForMessages(
|
||||
messages: readonly ChatMessage[],
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<void>> {
|
||||
return this.mediaCache.prefetchMediaForMessages(messages);
|
||||
return this.mediaCache.prefetchMediaForMessages(messages, cacheIdentity);
|
||||
}
|
||||
|
||||
async prefetchMediaForSendResponse(
|
||||
response: ChatSendResponse,
|
||||
cacheIdentity?: string,
|
||||
): Promise<Result<void>> {
|
||||
return this.mediaCache.prefetchMediaForSendResponse(response);
|
||||
return this.mediaCache.prefetchMediaForSendResponse(
|
||||
response,
|
||||
cacheIdentity,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user