diff --git a/src/state/bookmark/store.ts b/src/state/bookmark/store.ts index 1b7a2053..51de0ed0 100644 --- a/src/state/bookmark/store.ts +++ b/src/state/bookmark/store.ts @@ -2,6 +2,7 @@ import { createVersionedStore } from "@/utils/storage"; import { BookmarkStoreData } from "./types"; import { OldBookmarks, migrateV1Bookmarks } from "../watched/migrations/v2"; +import { migrateV2Bookmarks } from "../watched/migrations/v3"; export const BookmarkStore = createVersionedStore() .setKey("mw-bookmarks") @@ -13,6 +14,12 @@ export const BookmarkStore = createVersionedStore() }) .addVersion({ version: 1, + migrate(old: OldBookmarks) { + return migrateV2Bookmarks(old); + }, + }) + .addVersion({ + version: 2, create() { return { bookmarks: [], diff --git a/src/state/watched/migrations/v3.ts b/src/state/watched/migrations/v3.ts new file mode 100644 index 00000000..971dacf1 --- /dev/null +++ b/src/state/watched/migrations/v3.ts @@ -0,0 +1,87 @@ +import { getLegacyMetaFromId } from "@/backend/metadata/getmeta"; +import { getMovieFromExternalId } from "@/backend/metadata/tmdb"; +import { MWMediaType } from "@/backend/metadata/types/mw"; + +import { WatchedStoreData } from "../types"; + +async function migrateId( + id: number, + type: MWMediaType +): Promise { + console.log("migrating id", id, type); + const meta = await getLegacyMetaFromId(type, id.toString()); + console.log("migrating id", meta); + + if (!meta) return undefined; + const { tmdbId, imdbId } = meta; + if (!tmdbId && !imdbId) return undefined; + + // movies always have an imdb id on tmdb + if (imdbId && type === MWMediaType.MOVIE) { + const movieId = await getMovieFromExternalId(imdbId); + if (movieId) return movieId; + } + + if (tmdbId) { + return tmdbId; + } +} + +export async function migrateV2Bookmarks(old: any) { + const oldData = old; + if (!oldData) return; + + const updatedBookmarks = oldData.bookmarks.map( + async (item: { id: number; type: MWMediaType }) => ({ + ...item, + mediaId: await migrateId(item.id, item.type), + }) + ); + + return { + bookmarks: (await Promise.all(updatedBookmarks)).filter( + (item) => item.mediaId + ), + }; +} + +export async function migrateV3Videos(old: any) { + console.log("migrating watched"); + const oldData = old; + if (!oldData) return; + console.log(oldData); + + const updatedItems = await Promise.all( + oldData.items.map(async (item: any) => { + const migratedId = await migrateId( + item.item.meta.id, + item.item.meta.type + ); + + const migratedItem = { + ...item, + item: { + ...item.item, + meta: { + ...item.item.meta, + id: migratedId, + }, + }, + }; + + return { + ...item, + item: migratedId ? migratedItem : item.item, + }; + }) + ); + + const newData: WatchedStoreData = { + items: updatedItems.map((item) => item.item), // Extract the "item" object + }; + + return { + ...oldData, + items: newData.items, + }; +} diff --git a/src/state/watched/store.ts b/src/state/watched/store.ts index 95adef28..b59c37dc 100644 --- a/src/state/watched/store.ts +++ b/src/state/watched/store.ts @@ -1,6 +1,7 @@ import { createVersionedStore } from "@/utils/storage"; import { OldData, migrateV2Videos } from "./migrations/v2"; +import { migrateV3Videos } from "./migrations/v3"; import { WatchedStoreData } from "./types"; export const VideoProgressStore = createVersionedStore() @@ -21,6 +22,12 @@ export const VideoProgressStore = createVersionedStore() }) .addVersion({ version: 2, + migrate(old: OldData) { + return migrateV3Videos(old); + }, + }) + .addVersion({ + version: 3, create() { return { items: [],