revert the changes that migrate datastore to array settings

This commit is contained in:
Elvyra 2025-01-19 19:40:53 +01:00
parent 0a96e1a508
commit 6ddf203609
4 changed files with 40 additions and 68 deletions

View file

@ -34,10 +34,10 @@ interface Tag {
const getTag = (name: string) => settings.store.data.find((tt: Tag) => tt.name === name); const getTag = (name: string) => settings.store.data.find((tt: Tag) => tt.name === name);
const addTag = (tag: Tag) => { const addTag = (tag: Tag) => {
settings.store.data = [...settings.store.data, tag]; settings.store.data.push(tag);
}; };
const removeTag = (name: string) => { const removeTag = (name: string) => {
settings.store.data = settings.store.data.filter((t: Tag) => t.name !== name); settings.store.data.filter((t: Tag) => t.name !== name);
}; };
function createTagCommand(tag: Tag) { function createTagCommand(tag: Tag) {
@ -64,16 +64,17 @@ function createTagCommand(tag: Tag) {
const settings = definePluginSettings({ const settings = definePluginSettings({
clyde: {
description: "If enabled, clyde will send you an ephemeral message when a tag was used.",
type: OptionType.BOOLEAN,
default: true
},
data: { data: {
type: OptionType.ARRAY, type: OptionType.ARRAY,
hidden: true, hidden: true,
description: "" description: ""
}, },
migrated: {
type: OptionType.BOOLEAN,
description: "",
default: false,
hidden: true
}
}); });
@ -81,16 +82,22 @@ export default definePlugin({
name: "MessageTags", name: "MessageTags",
description: "Allows you to save messages and to use them with a simple command.", description: "Allows you to save messages and to use them with a simple command.",
authors: [Devs.Luna], authors: [Devs.Luna],
options: {
clyde: {
name: "Clyde message on send",
description: "If enabled, clyde will send you an ephemeral message when a tag was used.",
type: OptionType.BOOLEAN,
default: true
}
},
settings, settings,
async start() { async start() {
const data = await DataStore.get(DATA_KEY); if (!settings.store.migrated) {
const data = await DataStore.get(DATA_KEY);
if (data != null) { if (!!data) settings.store.data = data;
settings.store.data = data; settings.store.migrated = true;
await DataStore.del(DATA_KEY);
} }
for (const tag of settings.store.data) createTagCommand(tag); for (const tag of settings.store.data) createTagCommand(tag);
}, },

View file

@ -28,18 +28,19 @@ const OLD_CATEGORY_KEY = "BetterPinDMsCategories-";
export let categories: Category[] = []; export let categories: Category[] = [];
export async function saveCats(cats: Category[]) { export async function saveCats(cats: Category[]) {
settings.store.data = cats; const { id } = UserStore.getCurrentUser();
await DataStore.set(CATEGORY_BASE_KEY + id, cats);
} }
export async function init() { export async function init() {
const id = UserStore.getCurrentUser()?.id; const id = UserStore.getCurrentUser()?.id;
await initCategories(); await initCategories(id);
await migrateData(id); await migrateData(id);
forceUpdate(); forceUpdate();
} }
export async function initCategories() { export async function initCategories(userId: string) {
categories = settings.store.data ?? []; categories = await DataStore.get<Category[]>(CATEGORY_BASE_KEY + userId) ?? [];
} }
export function getCategory(id: string) { export function getCategory(id: string) {
@ -206,22 +207,13 @@ async function migrateOldCategories(userId: string) {
await DataStore.set(CATEGORY_MIGRATED_KEY, true); await DataStore.set(CATEGORY_MIGRATED_KEY, true);
} }
async function migrateFromDatastore(userId: string) {
const cats = await DataStore.get<Category[]>(CATEGORY_BASE_KEY + userId);
if (cats !== undefined) settings.store.data = cats;
}
export async function migrateData(userId: string) { export async function migrateData(userId: string) {
if (settings.store.data.length > 0) return;
const m1 = await DataStore.get(CATEGORY_MIGRATED_KEY), m2 = await DataStore.get(CATEGORY_MIGRATED_PINDMS_KEY); const m1 = await DataStore.get(CATEGORY_MIGRATED_KEY), m2 = await DataStore.get(CATEGORY_MIGRATED_PINDMS_KEY);
if (m1 && m2) { if (m1 && m2) return;
return await migrateFromDatastore(userId);
}
// want to migrate the old categories first and then slove any conflicts with the PinDMs pins // want to migrate the old categories first and then slove any conflicts with the PinDMs pins
if (!m1) await migrateOldCategories(userId); if (!m1) await migrateOldCategories(userId);
if (!m2) await migratePinDMs(); if (!m2) await migratePinDMs();
if (settings.store.data.length === 0) await migrateFromDatastore(userId);
await saveCats(categories); await saveCats(categories);
} }

View file

@ -54,12 +54,7 @@ export const settings = definePluginSettings({
description: "Collapse DM sections", description: "Collapse DM sections",
default: false, default: false,
onChange: () => forceUpdate() onChange: () => forceUpdate()
}, }
data: {
type: OptionType.ARRAY,
hidden: true,
description: "",
},
}); });
export default definePlugin({ export default definePlugin({

View file

@ -46,6 +46,9 @@ const makeEmptyRule: () => Rule = () => ({
}); });
const makeEmptyRuleArray = () => [makeEmptyRule()]; const makeEmptyRuleArray = () => [makeEmptyRule()];
let stringRules = makeEmptyRuleArray();
let regexRules = makeEmptyRuleArray();
const settings = definePluginSettings({ const settings = definePluginSettings({
replace: { replace: {
type: OptionType.COMPONENT, type: OptionType.COMPONENT,
@ -56,13 +59,13 @@ const settings = definePluginSettings({
<> <>
<TextReplace <TextReplace
title="Using String" title="Using String"
rulesArray={settings.store.stringRules} rulesArray={stringRules}
rulesKey={STRING_RULES_KEY} rulesKey={STRING_RULES_KEY}
update={update} update={update}
/> />
<TextReplace <TextReplace
title="Using Regex" title="Using Regex"
rulesArray={settings.store.regexRules} rulesArray={regexRules}
rulesKey={REGEX_RULES_KEY} rulesKey={REGEX_RULES_KEY}
update={update} update={update}
/> />
@ -71,16 +74,6 @@ const settings = definePluginSettings({
); );
} }
}, },
stringRules: {
type: OptionType.ARRAY,
hidden: true,
description: ""
},
regexRules: {
type: OptionType.ARRAY,
hidden: true,
description: ""
},
}); });
function stringToRegex(str: string) { function stringToRegex(str: string) {
@ -134,8 +127,7 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps)
if (index === rulesArray.length - 1) return; if (index === rulesArray.length - 1) return;
rulesArray.splice(index, 1); rulesArray.splice(index, 1);
if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray]; await DataStore.set(rulesKey, rulesArray);
else settings.store.regexRules = [...rulesArray];
update(); update();
} }
@ -148,8 +140,7 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps)
if (rulesArray[index].find === "" && rulesArray[index].replace === "" && rulesArray[index].onlyIfIncludes === "" && index !== rulesArray.length - 1) if (rulesArray[index].find === "" && rulesArray[index].replace === "" && rulesArray[index].onlyIfIncludes === "" && index !== rulesArray.length - 1)
rulesArray.splice(index, 1); rulesArray.splice(index, 1);
if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray]; await DataStore.set(rulesKey, rulesArray);
else settings.store.regexRules = [...rulesArray];
update(); update();
} }
@ -220,8 +211,8 @@ function applyRules(content: string): string {
if (content.length === 0) if (content.length === 0)
return content; return content;
if (settings.store.stringRules) { if (stringRules) {
for (const rule of settings.store.stringRules) { for (const rule of stringRules) {
if (!rule.find) continue; if (!rule.find) continue;
if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue;
@ -229,8 +220,8 @@ function applyRules(content: string): string {
} }
} }
if (settings.store.regexRules) { if (regexRules) {
for (const rule of settings.store.regexRules) { for (const rule of regexRules) {
if (!rule.find) continue; if (!rule.find) continue;
if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue;
@ -258,21 +249,8 @@ export default definePlugin({
settings, settings,
async start() { async start() {
stringRules = await DataStore.get(STRING_RULES_KEY) ?? makeEmptyRuleArray();
if (settings.store.stringRules.length === 0 || settings.store.regexRules.length === 0) { regexRules = await DataStore.get(REGEX_RULES_KEY) ?? makeEmptyRuleArray();
const stringRules = await DataStore.get(STRING_RULES_KEY);
const regexRules = await DataStore.get(REGEX_RULES_KEY);
if (stringRules != null) {
settings.store.stringRules = stringRules;
await DataStore.del(STRING_RULES_KEY);
} else if (settings.store.stringRules.length === 0) settings.store.stringRules = makeEmptyRuleArray();
if (regexRules != null) {
settings.store.regexRules = regexRules;
await DataStore.del(REGEX_RULES_KEY);
} else if (settings.store.regexRules.length === 0) settings.store.regexRules = makeEmptyRuleArray();
}
this.preSend = addPreSendListener((channelId, msg) => { this.preSend = addPreSendListener((channelId, msg) => {
// Channel used for sharing rules, applying rules here would be messy // Channel used for sharing rules, applying rules here would be messy