Migrate bookmarks from v2 > v3

This commit is contained in:
Jip Fr 2023-02-12 14:03:50 +01:00
parent 942a6cc9c0
commit bd48d929b9
3 changed files with 58 additions and 19 deletions

View File

@ -1,14 +1,14 @@
import { createVersionedStore } from "@/utils/storage"; import { createVersionedStore } from "@/utils/storage";
import { migrateV1Bookmarks } from "../watched/migrations/v2";
import { BookmarkStoreData } from "./types"; import { BookmarkStoreData } from "./types";
export const BookmarkStore = createVersionedStore<BookmarkStoreData>() export const BookmarkStore = createVersionedStore<BookmarkStoreData>()
.setKey("mw-bookmarks") .setKey("mw-bookmarks")
.addVersion({ .addVersion({
version: 0, version: 0,
migrate() { migrate(oldBookmarks) {
return { console.log(oldBookmarks);
bookmarks: [], // TODO migrate bookmarks return migrateV1Bookmarks(oldBookmarks);
};
}, },
}) })
.addVersion({ .addVersion({

View File

@ -27,16 +27,14 @@ export interface OldData {
items: (OldMovie | OldSeries)[]; items: (OldMovie | OldSeries)[];
} }
export async function migrateV2(old: OldData) { interface OldBookmarks {
const oldData = old; bookmarks: (OldMovie | OldSeries)[];
if (!oldData) return; }
const uniqueMedias: Record<string, any> = {};
oldData.items.forEach((item: any) => {
if (uniqueMedias[item.mediaId]) return;
uniqueMedias[item.mediaId] = item;
});
async function getMetas(
uniqueMedias: Record<string, any>,
oldData?: OldData
): Promise<Record<string, Record<string, DetailedMeta | null>> | undefined> {
const yearsAreClose = (a: number, b: number) => { const yearsAreClose = (a: number, b: number) => {
return Math.abs(a - b) <= 1; return Math.abs(a - b) <= 1;
}; };
@ -74,14 +72,16 @@ export async function migrateV2(old: OldData) {
if (!meta || !meta?.meta.seasons) return; if (!meta || !meta?.meta.seasons) return;
const seasonNumbers = [ const seasonNumbers = [
...new Set( ...new Set(
oldData.items oldData?.items
.filter((watchedEntry: any) => watchedEntry.mediaId === item.id) ? oldData.items
.map((watchedEntry: any) => watchedEntry.seasonId) .filter((watchedEntry: any) => watchedEntry.mediaId === item.id)
.map((watchedEntry: any) => watchedEntry.seasonId)
: ["0"]
), ),
]; ];
const seasons = seasonNumbers.map((num) => ({ const seasons = seasonNumbers.map((num) => ({
num, num,
season: meta.meta?.seasons?.[(num as number) - 1], season: meta.meta?.seasons?.[Math.max(0, (num as number) - 1)],
})); }));
keys = seasons keys = seasons
.map((season) => (season ? [season.num, season?.season?.id] : [])) .map((season) => (season ? [season.num, season?.season?.id] : []))
@ -101,6 +101,45 @@ export async function migrateV2(old: OldData) {
); );
} }
return mediaMetas;
}
export async function migrateV1Bookmarks(old: OldBookmarks) {
const oldData = old;
if (!oldData) return;
const uniqueMedias: Record<string, any> = {};
oldData.bookmarks.forEach((item: any) => {
if (uniqueMedias[item.mediaId]) return;
uniqueMedias[item.mediaId] = item;
});
const mediaMetas = await getMetas(uniqueMedias);
if (!mediaMetas) return;
const bookmarks = Object.keys(mediaMetas)
.map((key) => mediaMetas[key]["0"])
.map((t) => t?.meta)
.filter(Boolean);
return {
bookmarks,
};
}
export async function migrateV2Videos(old: OldData) {
const oldData = old;
if (!oldData) return;
const uniqueMedias: Record<string, any> = {};
oldData.items.forEach((item: any) => {
if (uniqueMedias[item.mediaId]) return;
uniqueMedias[item.mediaId] = item;
});
const mediaMetas = await getMetas(uniqueMedias, oldData);
if (!mediaMetas) return;
// We've got all the metadata you can dream of now // We've got all the metadata you can dream of now
// Now let's convert stuff into the new format. // Now let's convert stuff into the new format.
const newData: WatchedStoreData = { const newData: WatchedStoreData = {

View File

@ -1,5 +1,5 @@
import { createVersionedStore } from "@/utils/storage"; import { createVersionedStore } from "@/utils/storage";
import { migrateV2, OldData } from "./migrations/v2"; import { migrateV2Videos, OldData } from "./migrations/v2";
import { WatchedStoreData } from "./types"; import { WatchedStoreData } from "./types";
export const VideoProgressStore = createVersionedStore<WatchedStoreData>() export const VideoProgressStore = createVersionedStore<WatchedStoreData>()
@ -15,7 +15,7 @@ export const VideoProgressStore = createVersionedStore<WatchedStoreData>()
.addVersion({ .addVersion({
version: 1, version: 1,
async migrate(old: OldData) { async migrate(old: OldData) {
return migrateV2(old); return migrateV2Videos(old);
}, },
}) })
.addVersion({ .addVersion({