From 5e3817c1a76d393d2f735e753d6b898f97ec56d6 Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Tue, 11 Feb 2025 22:11:56 +0100 Subject: [PATCH] Add migration --- static/data_migration.mts | 37 +++++++++++++++++++++++++++++++++++++ static/storage_manager.mts | 15 ++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 static/data_migration.mts diff --git a/static/data_migration.mts b/static/data_migration.mts new file mode 100644 index 0000000..163a5b0 --- /dev/null +++ b/static/data_migration.mts @@ -0,0 +1,37 @@ +import { LocalStorage } from "./storage_manager.mjs" + +type InstanceV0 = { + name: string, + origin: string, + software: string, + iconURL?: string, + preferredFor?: string[], +} +type LocalStorageV0 = { + version: undefined, + instances: InstanceV0[], +} + +type LocalStorageV1 = LocalStorage; + +function migrate0to1(s: LocalStorageV0): LocalStorageV1 { + return { + version: 1, + instances: s.instances.map(i => ({ + preferredFor: i.preferredFor ?? [], + ...i + })) + }; +} + +type AnyLocalStorage = LocalStorageV0 | LocalStorageV1; + +export default function migrate(storage: AnyLocalStorage): LocalStorage { + switch (storage.version) { + case undefined: + storage = migrate0to1(storage); + case 1: + default: + return storage; + } +} diff --git a/static/storage_manager.mts b/static/storage_manager.mts index bb77053..151b478 100644 --- a/static/storage_manager.mts +++ b/static/storage_manager.mts @@ -1,3 +1,5 @@ +import migrate from "./data_migration.mjs"; + export type Instance = { /** * The instance's (nick)name @@ -21,18 +23,19 @@ export type Instance = { software: string, /** * The instance's icon URL - * - * Make sure to sanitize this! Could lead to XSS + * @example undefined + * @example "https://void.lgbt/favicon.png" */ iconURL?: string, /** * The list of software names and groups the user prefers to autoredirect to this instance * @example ["sharkey", "misskey-compliant"] */ - preferredFor?: string[], + preferredFor: string[], } -type LocalStorage = { +export type LocalStorage = { + version: number, instances: Instance[], } @@ -46,12 +49,14 @@ export default new class StorageManager { default(): LocalStorage { return { + version: 1, instances: [] } } load() { - this.storage = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default(); + const data = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default(); + this.storage = migrate(data); } save() {