fix(mobile): clean outdated data when hydrating

This commit is contained in:
Stephen Zhou
2025-06-04 09:43:10 +08:00
parent 496b1d65b2
commit c601752e55
6 changed files with 68 additions and 6 deletions
@@ -63,6 +63,51 @@ class EntryServiceStatic implements Resetable {
getEntryAll() {
return db.query.entriesTable.findMany()
}
async getEntriesToHydrate() {
const [entries, subscriptions] = await Promise.all([
db.query.entriesTable.findMany({
orderBy: (t, { desc }) => desc(t.publishedAt),
}),
db.query.subscriptionsTable.findMany(),
])
const entryIdCountMap = new Map<string, number>(
subscriptions.map((s) => [s.listId || s.inboxId || s.feedId || "", 0] as const),
)
const result: typeof entries = []
const idsToClear = new Set<string>()
for (const entry of entries) {
const possibleIdList = [
...(entry.sources?.filter((s) => s !== "feed") ?? []),
entry.inboxHandle,
entry.feedId,
].filter(Boolean) as string[]
if (possibleIdList.length === 0) continue
for (const id of possibleIdList) {
const count = entryIdCountMap.get(id)
if (count === undefined) continue
if (count >= 20) {
idsToClear.add(entry.id)
} else {
result.push(entry)
entryIdCountMap.set(id, count + 1)
}
}
}
await db
.delete(entriesTable)
.where(inArray(entriesTable.id, Array.from(idsToClear)))
.execute()
return result
}
}
export const EntryService = new EntryServiceStatic()
@@ -8,8 +8,9 @@ class InboxServiceStatic implements Resetable {
async reset() {
await db.delete(inboxesTable).execute()
}
async getInbox(): Promise<InboxSchema[]> {
return await db.select().from(inboxesTable)
getInboxAll() {
return db.query.inboxesTable.findMany()
}
async upsertMany(inboxes: InboxSchema[]) {
@@ -1,4 +1,4 @@
import { eq } from "drizzle-orm"
import { eq, inArray } from "drizzle-orm"
import { db } from "../db"
import { translationsTable } from "../schemas"
@@ -10,6 +10,22 @@ class TranslationServiceStatic implements Resetable {
return db.query.translationsTable.findMany()
}
async getTranslationToHydrate() {
const translations = await db.query.translationsTable.findMany()
// Remove translations created before the last 7 days
const translationsToClean = translations.filter(
(translation) =>
new Date(translation.createdAt) > new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
)
await db.delete(translationsTable).where(
inArray(
translationsTable.entryId,
translationsToClean.map((t) => t.entryId),
),
)
return translations
}
async reset() {
await db.delete(translationsTable).execute()
}
+1 -1
View File
@@ -55,7 +55,7 @@ const immerSet = createImmerSetter(useEntryStore)
class EntryActions implements Hydratable {
async hydrate(options?: HydrationOptions) {
const entries = await EntryService.getEntryAll()
const entries = await EntryService.getEntriesToHydrate()
entryActions.upsertManyInSession(
entries.map((e) => dbStoreMorph.toEntryModel(e)),
+1 -1
View File
@@ -18,7 +18,7 @@ export const useInboxStore = createZustandStore<InboxState>("inbox")(() => defau
const set = useInboxStore.setState
class InboxActions implements Hydratable {
async hydrate() {
const inboxes = await InboxService.getInbox()
const inboxes = await InboxService.getInboxAll()
inboxActions.upsertManyInSession(inboxes)
}
async upsertManyInSession(inboxes: InboxSchema[]) {
@@ -25,7 +25,7 @@ const immerSet = createImmerSetter(useTranslationStore)
class TranslationActions implements Hydratable {
async hydrate() {
const translations = await TranslationService.getTranslationAll()
const translations = await TranslationService.getTranslationToHydrate()
translationActions.upsertManyInSession(translations)
}