Skip to content

Commit

Permalink
restrict config values to keys of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Apr 17, 2024
1 parent d32923f commit c622243
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 103 deletions.
2 changes: 1 addition & 1 deletion client-vue/src/components/forms/ChannelUpdateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ function deleteChannel() {
}
function subscribeChannel() {
if ((!store.cfg("app_url") || store.cfg("app_url") == "debug") && store.cfg("twitchapi.twitchapi.eventsub_type") === "webhook") {
if ((!store.cfg("app_url") || store.cfg("app_url") == "debug") && store.cfg("twitchapi.eventsub_type") === "webhook") {
alert("Please set the app url in the settings");
return;
}
Expand Down
26 changes: 15 additions & 11 deletions client-vue/src/components/forms/SettingsForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</h3>
<ul>
<li v-for="(setting, key) of newAndInterestingSettings" :key="key">
Under <strong>{{ setting.group }}</strong
>: {{ setting.text }} ({{ setting.help }})
Under <strong>{{ setting?.group }}</strong
>: {{ setting?.text }} ({{ setting !== undefined && "help" in setting ? setting.help : "No help" }})
</li>
</ul>
</div>
Expand Down Expand Up @@ -197,21 +197,22 @@ const { t, te } = useI18n();
const formStatusText = ref<string>("Ready");
const formStatus = ref<FormStatus>("IDLE");
const formData = ref<{ config: Record<string, string | number | boolean> }>({ config: {} });
const fetchedSettingsFields = ref<typeof settingsFields>({});
const fetchedSettingsFields = ref<typeof settingsFields>();
const loading = ref<boolean>(false);
const searchText = ref<string>("");
// computed
const settingsGroups = computed((): SettingsGroup[] => {
if (!fetchedSettingsFields.value) return [];
const groups: Record<string, SettingsGroup> = {};
for (const key in fetchedSettingsFields.value) {
for (const rawKey in fetchedSettingsFields.value) {
const key = rawKey as keyof typeof fetchedSettingsFields.value;
const field = fetchedSettingsFields.value[key];
if (!field.group) continue;
if (searchText.value) {
if (
!key.toLowerCase().includes(searchText.value.toLowerCase()) &&
!field.help?.toLowerCase().includes(searchText.value.toLowerCase()) &&
("help" in field && !field.help?.toLowerCase().includes(searchText.value.toLowerCase())) &&
!field.text?.toLowerCase().includes(searchText.value.toLowerCase())
)
continue;
Expand All @@ -232,11 +233,14 @@ const settingsGroups = computed((): SettingsGroup[] => {
*/
});
const newAndInterestingSettings = computed((): typeof settingsFields => {
const newSettings: typeof settingsFields = {};
for (const key in fetchedSettingsFields.value) {
const newAndInterestingSettings = computed((): Record<keyof typeof settingsFields, SettingField & { new: boolean }> => {
const newSettings: Record<keyof typeof settingsFields, SettingField & { new: boolean }> = {} as any;
for (const rawKey in fetchedSettingsFields.value) {
const key = rawKey as keyof typeof fetchedSettingsFields.value;
const field = fetchedSettingsFields.value[key];
if (field.new) newSettings[key] = field;
if (field !== undefined && "new" in field && field.new) {
newSettings[key] = field;
}
}
return newSettings;
// return fetchedSettingsFields.value.filter((field) => field.new);
Expand Down Expand Up @@ -286,8 +290,8 @@ function fetchData(): void {
// set defaults
for (const key in fetchedSettingsFields.value) {
const field = fetchedSettingsFields.value[key];
if (field.default !== undefined && formData.value.config[key] === undefined) {
const field = fetchedSettingsFields.value[key as keyof typeof fetchedSettingsFields.value];
if ("default" in field && field.default !== undefined && formData.value.config[key] === undefined) {
formData.value.config[key] = field.default;
}
}
Expand Down
2 changes: 1 addition & 1 deletion client-vue/src/components/menu/SideMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div v-if="store.config" class="top-menu-item title" :class="{ fancy: store.clientCfg('animationsEnabled') }">
<router-link to="/dashboard" class="link" aria-label="Go to dashboard">
<div class="bg" />
<img src="../../assets/logo.png" class="favicon" width="24" height="24" :alt="store.cfg('app_name', 'TA') ?? 'TA'" aria-hidden="true" />
<img src="../../assets/logo.png" class="favicon" width="24" height="24" :alt="store.app_name ?? 'TA'" aria-hidden="true" />
<h1 class="title" :title="verboseVersion">
<span>{{ store.app_name }}</span>
<template v-if="isDevelopmentBranch">
Expand Down
4 changes: 2 additions & 2 deletions client-vue/src/components/vod/VodItemStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</strong>
</span>
</em>
<template v-if="store.cfg('chat_dump')">
<!-- <template v-if="store.cfg('chat_dump')">
<br /><em>
<span v-if="vod.getChatDumpStatus()">
<span class="icon"><fa icon="sync" spin /></span>
Expand All @@ -82,7 +82,7 @@
</strong>
</span>
</em>
</template>
</template> -->
</template>
</template>
<template v-else-if="!vod.is_capturing && !vod.is_converting && !vod.is_finalized">
Expand Down
4 changes: 2 additions & 2 deletions client-vue/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const useStore = defineStore("twitchAutomator", {
streamerList: [],
streamerListLoaded: false,
jobList: [],
config: {},
config: {} as Record<keyof typeof settingsFields, any> | null,
favourite_games: [],
version: "?",
clientConfig: undefined,
Expand Down Expand Up @@ -462,7 +462,7 @@ export const useStore = defineStore("twitchAutomator", {
// console.debug(job_name, job.dt_started_at, elapsedSeconds, job.progress, calc);
return calc;
},
updateConfig(data: Record<string, any> | null) {
updateConfig(data: Record<keyof typeof settingsFields, any> | null) {
this.config = data;
},
updateClientConfig(data: ClientSettings) {
Expand Down
6 changes: 6 additions & 0 deletions common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,35 @@ export interface StringSettingField extends BaseSettingField {
stripslash?: boolean;
pattern?: string;
multiline?: boolean;
help?: string; // TODO: why is this here twice?
}

export interface NumberSettingField extends BaseSettingField {
type: "number";
default?: number;
min?: number;
max?: number;
help?: string; // TODO: why is this here twice?
}

export interface BooleanSettingField extends BaseSettingField {
type: "boolean";
default?: boolean;
help?: string; // TODO: why is this here twice?
}

export interface ArraySettingField extends BaseSettingField {
type: "array";
default?: string;
choices: Array<string>;
help?: string; // TODO: why is this here twice?
}

export interface ObjectSettingField extends BaseSettingField {
type: "object";
default?: string;
choices: Record<string, string>;
help?: string; // TODO: why is this here twice?
}

export interface TemplateSettingField extends BaseSettingField {
Expand All @@ -121,6 +126,7 @@ export interface TemplateSettingField extends BaseSettingField {
{ display: string; description?: string; deprecated?: boolean }
>;
context?: string;
help?: string; // TODO: why is this here twice?
}

export type SettingField =
Expand Down
12 changes: 10 additions & 2 deletions common/ServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import {
} from "./ReplacementsConsts";
import { YouTubeCategories } from "./YouTube";

export const settingsFields: Record<string, SettingField> = {
// export type SettingKey = keyof typeof settingsFields;

function createSettingsFields<T extends Record<string, SettingField>>(
fields: T
): T {
return fields;
}

export const settingsFields = createSettingsFields({
bin_dir: {
group: "Binaries",
text: "Python binary directory",
Expand Down Expand Up @@ -1098,4 +1106,4 @@ export const settingsFields: Record<string, SettingField> = {
choices: ["jpg", "png", "webp"] as string[],
default: "jpg",
},
} as const;
});
7 changes: 6 additions & 1 deletion server/src/Controllers/Channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,12 @@ export async function DownloadVideo(
game_id: extraData.game_id || "",
};

return formatString(Config.getInstance().cfg(what), variables);
return formatString(
Config.getInstance().cfg(
what as keyof typeof Config.settingsFields
),
variables
);
};

if (isTwitchChannel(channel)) {
Expand Down
28 changes: 21 additions & 7 deletions server/src/Controllers/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppName } from "@/Core/BaseConfig";
import { Config } from "@/Core/Config";
import { KeyValue } from "@/Core/KeyValue";
import { LiveStreamDVR } from "@/Core/LiveStreamDVR";
import { LOGLEVEL, log } from "@/Core/Log";
import { TwitchGame } from "@/Core/Providers/Twitch/TwitchGame";
import type { ApiSettingsResponse } from "@common/Api/Api";
import type express from "express";
Expand All @@ -17,10 +18,11 @@ export async function GetSettings(
!req.session.authenticated;

const config: Record<string, any> = {};
for (const key in Config.settingsFields) {
const field = Config.settingsFields[key];
for (const rawKey in Config.settingsFields) {
const key = rawKey as keyof typeof Config.settingsFields;
const field = Config.getSettingField(key);
// if (field.secret) continue;
if (is_guest && !field.guest) continue;
if (is_guest && field !== undefined && !field.guest) continue;
config[key] = Config.getInstance().cfg(key);
}

Expand Down Expand Up @@ -86,8 +88,14 @@ export function SaveSettings(

let fields = 0;
for (const key in Config.settingsFields) {
const setting = Config.settingsFields[key];
if (setting.required && postConfig[key] === undefined) {
const setting = Config.getSettingField(
key as keyof typeof Config.settingsFields
);
if (
setting !== undefined &&
setting.required &&
postConfig[key] === undefined
) {
res.api(400, {
status: "ERROR",
message: `Missing required setting: ${key}`,
Expand All @@ -107,8 +115,14 @@ export function SaveSettings(
return;
}

for (const key in Config.settingsFields) {
const setting = Config.settingsFields[key];
// validate and save settings
for (const rawKey in Config.settingsFields) {
const key = rawKey as keyof typeof Config.settingsFields;
const setting = Config.getSettingField(key);
if (setting === undefined) {
log(LOGLEVEL.ERROR, "Settings", `Setting not found: ${key}`);
continue;
}
if (setting.type === "boolean") {
Config.getInstance().setConfig<boolean>(key, postConfig[key]);
} else if (setting.type === "number") {
Expand Down
Loading

0 comments on commit c622243

Please sign in to comment.