Make sure images are purged on all message deletes
This commit is contained in:
parent
76b93ef3d0
commit
97ff211cdd
|
@ -7,7 +7,7 @@
|
||||||
const dbCheck = _hasIndexedDb && window.indexedDB.open('test')
|
const dbCheck = _hasIndexedDb && window.indexedDB.open('test')
|
||||||
if (_hasIndexedDb) dbCheck.onerror = () => { _hasIndexedDb = false }
|
if (_hasIndexedDb) dbCheck.onerror = () => { _hasIndexedDb = false }
|
||||||
|
|
||||||
const imageCache: Record<string, ChatImage> = {}
|
let imageCache: Record<string, ChatImage> = {}
|
||||||
|
|
||||||
class ChatImageStore extends Dexie {
|
class ChatImageStore extends Dexie {
|
||||||
images!: Table<ChatImage>
|
images!: Table<ChatImage>
|
||||||
|
@ -46,6 +46,13 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const clearAllImages = async (): Promise<void> => {
|
||||||
|
imageCache = {}
|
||||||
|
if (_hasIndexedDb) {
|
||||||
|
imageDb.images.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const setImage = async (chatId:number, image:ChatImage): Promise<ChatImage> => {
|
export const setImage = async (chatId:number, image:ChatImage): Promise<ChatImage> => {
|
||||||
image.id = image.id || uuidv4()
|
image.id = image.id || uuidv4()
|
||||||
let current: ChatImage
|
let current: ChatImage
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import { getProfile, getProfiles, isStaticProfile, newNameForProfile, restartProfile } from './Profiles.svelte'
|
import { getProfile, getProfiles, isStaticProfile, newNameForProfile, restartProfile } from './Profiles.svelte'
|
||||||
import { errorNotice } from './Util.svelte'
|
import { errorNotice } from './Util.svelte'
|
||||||
import { deleteImage, setImage } from './ImageStore.svelte'
|
import { clearAllImages, deleteImage, setImage } from './ImageStore.svelte'
|
||||||
|
|
||||||
// TODO: move chatsStorage to indexedDB with localStorage as a fallback for private browsing.
|
// TODO: move chatsStorage to indexedDB with localStorage as a fallback for private browsing.
|
||||||
// Enough long chats will overflow localStorage.
|
// Enough long chats will overflow localStorage.
|
||||||
|
@ -161,10 +161,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
export const clearChats = () => {
|
export const clearChats = () => {
|
||||||
const chats = get(chatsStorage)
|
chatsStorage.set([])
|
||||||
chats.forEach(c => deleteChat(c.id)) // make sure images are removed
|
clearAllImages()
|
||||||
// TODO: add a clear images option to make this faster
|
|
||||||
// chatsStorage.set([])
|
|
||||||
}
|
}
|
||||||
export const saveChatStore = () => {
|
export const saveChatStore = () => {
|
||||||
const chats = get(chatsStorage)
|
const chats = get(chatsStorage)
|
||||||
|
@ -293,6 +291,12 @@
|
||||||
chatsStorage.set(chats)
|
chatsStorage.set(chats)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clearImages = (chatId: number, messages: Message[]) => {
|
||||||
|
messages.forEach(m => {
|
||||||
|
if (m.image) deleteImage(chatId, m.image.id)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const truncateFromMessage = (chatId: number, uuid: string) => {
|
export const truncateFromMessage = (chatId: number, uuid: string) => {
|
||||||
const chats = get(chatsStorage)
|
const chats = get(chatsStorage)
|
||||||
const chat = chats.find((chat) => chat.id === chatId) as Chat
|
const chat = chats.find((chat) => chat.id === chatId) as Chat
|
||||||
|
@ -303,13 +307,15 @@
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw new Error(`Unable to find message with ID: ${uuid}`)
|
throw new Error(`Unable to find message with ID: ${uuid}`)
|
||||||
}
|
}
|
||||||
chat.messages.splice(index + 1) // remove every item after
|
const truncated = chat.messages.splice(index + 1) // remove every item after
|
||||||
|
clearImages(chatId, truncated)
|
||||||
chatsStorage.set(chats)
|
chatsStorage.set(chats)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const clearMessages = (chatId: number) => {
|
export const clearMessages = (chatId: number) => {
|
||||||
const chats = get(chatsStorage)
|
const chats = get(chatsStorage)
|
||||||
const chat = chats.find((chat) => chat.id === chatId) as Chat
|
const chat = chats.find((chat) => chat.id === chatId) as Chat
|
||||||
|
clearImages(chatId, chat.messages)
|
||||||
chat.messages = []
|
chat.messages = []
|
||||||
chatsStorage.set(chats)
|
chatsStorage.set(chats)
|
||||||
}
|
}
|
||||||
|
@ -317,9 +323,7 @@
|
||||||
export const deleteChat = (chatId: number) => {
|
export const deleteChat = (chatId: number) => {
|
||||||
const chats = get(chatsStorage)
|
const chats = get(chatsStorage)
|
||||||
const chat = getChat(chatId)
|
const chat = getChat(chatId)
|
||||||
chat?.messages?.forEach(m => {
|
clearImages(chatId, chat?.messages || [])
|
||||||
if (m.image) deleteImage(chatId, m.image.id)
|
|
||||||
})
|
|
||||||
chatsStorage.set(chats.filter((chat) => chat.id !== chatId))
|
chatsStorage.set(chats.filter((chat) => chat.id !== chatId))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue