1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-20 14:37:43 +01:00

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 { migrateV1Bookmarks } from "../watched/migrations/v2";
import { BookmarkStoreData } from "./types";
export const BookmarkStore = createVersionedStore<BookmarkStoreData>()
.setKey("mw-bookmarks")
.addVersion({
version: 0,
migrate() {
return {
bookmarks: [], // TODO migrate bookmarks
};
migrate(oldBookmarks) {
console.log(oldBookmarks);
return migrateV1Bookmarks(oldBookmarks);
},
})
.addVersion({

View file

@ -27,16 +27,14 @@ export interface OldData {
items: (OldMovie | OldSeries)[];
}
export async function migrateV2(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;
});
interface OldBookmarks {
bookmarks: (OldMovie | OldSeries)[];
}
async function getMetas(
uniqueMedias: Record<string, any>,
oldData?: OldData
): Promise<Record<string, Record<string, DetailedMeta | null>> | undefined> {
const yearsAreClose = (a: number, b: number) => {
return Math.abs(a - b) <= 1;
};
@ -74,14 +72,16 @@ export async function migrateV2(old: OldData) {
if (!meta || !meta?.meta.seasons) return;
const seasonNumbers = [
...new Set(
oldData.items
.filter((watchedEntry: any) => watchedEntry.mediaId === item.id)
.map((watchedEntry: any) => watchedEntry.seasonId)
oldData?.items
? oldData.items
.filter((watchedEntry: any) => watchedEntry.mediaId === item.id)
.map((watchedEntry: any) => watchedEntry.seasonId)
: ["0"]
),
];
const seasons = seasonNumbers.map((num) => ({
num,
season: meta.meta?.seasons?.[(num as number) - 1],
season: meta.meta?.seasons?.[Math.max(0, (num as number) - 1)],
}));
keys = seasons
.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
// Now let's convert stuff into the new format.
const newData: WatchedStoreData = {

View file

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