Skip to content

Commit

Permalink
Few settings fixes, pw fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Splamy committed Oct 17, 2019
1 parent 2444b5a commit d0b1904
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 51 deletions.
16 changes: 8 additions & 8 deletions TS3AudioBot/MainCommands.cs
Expand Up @@ -1275,11 +1275,11 @@ public static void CommandSettings()
public static void CommandSettingsDelete(ConfRoot config, string name) => config.DeleteBotConfig(name).UnwrapThrow();

[Command("settings get")]
public static ConfigPart CommandSettingsGet(ConfBot config, string path = "")
public static ConfigPart CommandSettingsGet(ConfBot config, string path = null)
=> SettingsGet(config, path);

[Command("settings set")]
public static void CommandSettingsSet(ConfBot config, string path, string value = "")
public static void CommandSettingsSet(ConfBot config, string path, string value = null)
{
SettingsSet(config, path, value);
if (!config.SaveWhenExists())
Expand All @@ -1290,7 +1290,7 @@ public static void CommandSettingsSet(ConfBot config, string path, string value
}

[Command("settings bot get", "cmd_settings_get_help")]
public static ConfigPart CommandSettingsBotGet(BotManager bots, ConfRoot config, string bot, string path = "")
public static ConfigPart CommandSettingsBotGet(BotManager bots, ConfRoot config, string bot, string path = null)
{
using (var botlock = bots.GetBotLock(bot))
{
Expand All @@ -1300,7 +1300,7 @@ public static ConfigPart CommandSettingsBotGet(BotManager bots, ConfRoot config,
}

[Command("settings bot set", "cmd_settings_set_help")]
public static void CommandSettingsBotSet(BotManager bots, ConfRoot config, string bot, string path, string value = "")
public static void CommandSettingsBotSet(BotManager bots, ConfRoot config, string bot, string path, string value = null)
{
using (var botlock = bots.GetBotLock(bot))
{
Expand All @@ -1319,11 +1319,11 @@ public static void CommandSettingsReload(ConfRoot config, string name = null)
}

[Command("settings global get")]
public static ConfigPart CommandSettingsGlobalGet(ConfRoot config, string path = "")
public static ConfigPart CommandSettingsGlobalGet(ConfRoot config, string path = null)
=> SettingsGet(config, path);

[Command("settings global set")]
public static void CommandSettingsGlobalSet(ConfRoot config, string path, string value = "")
public static void CommandSettingsGlobalSet(ConfRoot config, string path, string value = null)
{
SettingsSet(config, path, value);
if (!config.Save())
Expand Down Expand Up @@ -1358,14 +1358,14 @@ private static ConfBot GetConf(Bot bot, ConfRoot config, string name)
}
}

private static ConfigPart SettingsGet(ConfigPart config, string path) => config.ByPathAsArray(path).SettingsGetSingle();
private static ConfigPart SettingsGet(ConfigPart config, string path = null) => config.ByPathAsArray(path ?? "").SettingsGetSingle();

private static void SettingsSet(ConfigPart config, string path, string value)
{
var setConfig = config.ByPathAsArray(path).SettingsGetSingle();
if (setConfig is IJsonSerializable jsonConfig)
{
var result = jsonConfig.FromJson(value);
var result = jsonConfig.FromJson(value ?? "");
if (!result.Ok)
throw new CommandException($"Failed to set the value ({result.Error}).", CommandExceptionReason.CommandError); // LOC: TODO
}
Expand Down
4 changes: 1 addition & 3 deletions TS3AudioBot/Sessions/TokenManager.cs
Expand Up @@ -22,12 +22,10 @@ public class TokenManager
private const string ApiTokenTable = "apiToken";
private readonly LiteCollection<DbApiToken> dbTokenList;
// Map: Uid => ApiToken
private readonly Dictionary<string, ApiToken> liveTokenList;
private readonly Dictionary<string, ApiToken> liveTokenList = new Dictionary<string, ApiToken>();

public TokenManager(DbStore database)
{
Util.Init(out liveTokenList);

dbTokenList = database.GetCollection<DbApiToken>(ApiTokenTable);
dbTokenList.EnsureIndex(x => x.UserUid, true);
dbTokenList.EnsureIndex(x => x.Token, true);
Expand Down
6 changes: 3 additions & 3 deletions WebInterface/src/ts/Api.ts
Expand Up @@ -8,7 +8,7 @@ export class ErrorObject<T = any> {

export class Get {
public static AuthData: ApiAuth = ApiAuth.Anonymous;
public static EndpointData: ApiEndpoint = ApiEndpoint.SameAddress;
public static EndpointData: ApiEndpoint = ApiEndpoint.Localhost;

public static async site(site: string): Promise<string> {
const response = await fetch(site);
Expand Down Expand Up @@ -67,12 +67,12 @@ export class Get {
export class Api<T extends ApiRet = ApiRet> {
public constructor(private buildAddr: string) { }

public static call<T>(...params: (string | number | Api)[]) {
public static call<T>(...params: (string | number | boolean | Api)[]) {
let buildStr = "";
for (const param of params) {
if (typeof param === "string") {
buildStr += "/" + encodeURIComponent(param).replace(/\(/, "%28").replace(/\)/, "%29");
} else if (typeof param === "number") {
} else if (typeof param === "number" || typeof param === "boolean") {
buildStr += "/" + param.toString();
} else if (param instanceof Api) {
buildStr += "/(" + param.done() + ")";
Expand Down
10 changes: 8 additions & 2 deletions WebInterface/src/ts/ApiObjects.ts
@@ -1,14 +1,20 @@
import { BotStatus } from "./Model/BotStatus";
import { TargetSendMode } from "./Model/TargetSendMode";

// tslint:disable: interface-name

export interface IVersion {
build: string;
platform: string;
sign: string;
}

export interface IPassword {
pw: string;
hashed: false;
autohash: false;
}

// tslint:disable: interface-name

export interface CmdBotInfo {
Id: number | null;
Name: string | null;
Expand Down
9 changes: 6 additions & 3 deletions WebInterface/src/ts/Components/SettingsField.vue
Expand Up @@ -15,11 +15,12 @@
<script lang="ts">
import Vue from "vue";
import SettingsGroup from "./SettingsGroup.vue";
import { SettLevel, ISettFilter } from "../Model/SettingsLevel";
export default Vue.component("settings-field", {
props: {
filter: {
type: Object as () => { text: string; level: number },
type: Object as () => ISettFilter,
required: false
},
path: { type: String, required: true },
Expand All @@ -34,8 +35,8 @@ export default Vue.component("settings-field", {
},
computed: {
is_visible(): boolean {
if (this.advanced && this.filter.level < 1) return false;
if (this.expert && this.filter.level < 2) return false;
if (this.advanced && this.filter.level < SettLevel.Advanced) return false;
if (this.expert && this.filter.level < SettLevel.Expert) return false;
const low_filter = this.filter.text.toLowerCase();
return (
this.path.toLowerCase().indexOf(low_filter) >= 0 ||
Expand All @@ -53,6 +54,8 @@ export default Vue.component("settings-field", {
},
data() {
return {
SettLevel,
parentIndex: 0
};
},
Expand Down
1 change: 0 additions & 1 deletion WebInterface/src/ts/Components/SettingsGroup.vue
Expand Up @@ -22,7 +22,6 @@

<script lang="ts">
import Vue from "vue";
import { get } from "lodash-es";
export default Vue.component("settings-group", {
props: {
Expand Down
52 changes: 52 additions & 0 deletions WebInterface/src/ts/Components/SettingsPassword.vue
@@ -0,0 +1,52 @@
<template>
<b-field grouped>
<b-input v-model="model.pw" @input="notify(true)" type="password" password-reveal expanded></b-input>
<b-switch v-show="filter.level >= SettLevel.Expert" v-model="model.hashed" @input="notify(false)">Hashed</b-switch>
<b-switch v-show="filter.level >= SettLevel.Expert" v-model="model.autohash" @input="notify(false)">Hash on read</b-switch>
</b-field>
</template>

<script lang="ts">
import Vue from "vue";
import { IPassword } from "../ApiObjects";
import { ISettFilter, SettLevel } from "../Model/SettingsLevel";
export default Vue.component("settings-password", {
props: {
value: {
type: Object as () => IPassword,
required: false
},
filter: {
type: Object as () => ISettFilter,
required: false,
default: () => ({ text: "", level: SettLevel.Beginner })
}
},
data() {
return {
SettLevel,
model: {
pw: "",
hashed: false,
autohash: false
} as IPassword
};
},
watch: {
value(val: IPassword) {
this.model = val;
}
},
methods: {
notify(pw_changed: boolean) {
if (pw_changed) {
this.model.hashed = false;
this.model.autohash = false;
}
this.$emit("input", this.model);
}
}
});
</script>
10 changes: 10 additions & 0 deletions WebInterface/src/ts/Model/SettingsLevel.ts
@@ -0,0 +1,10 @@
export enum SettLevel {
Beginner = 0,
Advanced = 1,
Expert = 2,
}

export interface ISettFilter {
text: string;
level: SettLevel;
}
85 changes: 54 additions & 31 deletions WebInterface/src/ts/Pages/BotSettings.vue
Expand Up @@ -52,26 +52,34 @@
<b-button class="control">Test (TODO)</b-button>
</settings-field>

<!-- Server password -->
<settings-field :filter="filter" path="connect.server_password" label="Server password">
<settings-password :filter="filter" v-model="model.connect.server_password" />
</settings-field>

<settings-field
:filter="filter"
path="connect.channel"
label="Default channel"
>(Cool dropdown i guess)
</settings-field>
>(TODO)</settings-field>

<!-- Channel password -->
<settings-field :filter="filter" path="connect.channel_password" label="Channel password">
<settings-password :filter="filter" v-model="model.connect.channel_password" />
</settings-field>

<settings-field :filter="filter" path="connect.client_version" label="Emulated client version">
<b-select v-model="bind_bot_version" placeholder="Select version">
<settings-field
:filter="filter"
path="connect.client_version"
label="Emulated client version"
advanced
>
<b-select v-model="model.connect.client_version" placeholder="Select version">
<option
v-for="ver in versions"
:key="ver.build"
:value="ver">{{ver.build}} : {{ver.platform}}</option>
:key="ver.build + ver.platform"
:value="ver"
>{{ver.build}} : {{ver.platform}}</option>
</b-select>
</settings-field>

</settings-group>

<settings-group label="Audio">
Expand Down Expand Up @@ -159,6 +167,7 @@
import Vue from "vue";
import SettingsField from "../Components/SettingsField.vue";
import SettingsGroup from "../Components/SettingsGroup.vue";
import SettingsPassword from "../Components/SettingsPassword.vue";
import { bot, cmd } from "../Api";
import { IVersion } from "../ApiObjects";
import { Util } from "../Util";
Expand Down Expand Up @@ -197,21 +206,35 @@ export default Vue.extend({
volume: {},
bitrate: 0
},
connect: {},
connect: {
server_password: {},
channel_password: {}
},
commands: {}
} as any
};
},
async created() {
const res = await this.requestModel();
fetch("https://raw.githubusercontent.com/ReSpeak/tsdeclarations/master/Versions.csv")
fetch(
"https://raw.githubusercontent.com/ReSpeak/tsdeclarations/master/Versions.csv"
)
.then(v => v.text())
.then(csv => {
this.versions = csv.split(/\n/gm).slice(1).map(line => line.split(/,/g)).map(parts => { return {
build: parts[0],
platform: parts[1],
sign: parts[2],
}})
this.versions = csv
.split(/\n/gm)
.slice(1)
.map(line => line.split(/,/g))
.map(parts => ({
build: parts[0],
platform: parts[1],
sign: parts[2]
}))
.filter(ver => {
const buildM = ver.build.match(/\[Build: (\d+)\]/);
if (buildM == null) return true;
return Number(buildM[1]) > 1513163251; // > 3.1.7 required
});
});
if (!Util.check(this, res, "Failed to retrieve settings")) return;
Expand All @@ -237,14 +260,6 @@ export default Vue.extend({
this.model.audio.volume.min = value[0];
this.model.audio.volume.max = value[1];
}
},
bind_bot_version: {
get(): IVersion {
return this.model.connect.client_version;
},
set(val: IVersion) {
this.model.connect.client_version = val;
}
}
},
methods: {
Expand All @@ -259,10 +274,11 @@ export default Vue.extend({
this.botId.toString()
).get();
},
sendValue(confVal: string, val: string | number) {
sendValue(confVal: string, val: string | number | object) {
if (typeof val === "object") val = JSON.stringify(val);
if (this.online)
return bot(
cmd<void>("settings", "set", confVal, val.toString()),
cmd<void>("settings", "set", confVal, val),
this.botId
).get();
else
Expand All @@ -272,17 +288,23 @@ export default Vue.extend({
"set",
this.botId.toString(),
confVal,
val.toString()
val
).get();
},
isSettingsObjectGroup(path: string): boolean {
return path.startsWith("connect.client_version");
},
bindRecursive(path: string, obj: any) {
for (const childKey of Object.keys(obj)) {
var child: any = obj[childKey];
var childPath = (path ? path + "." : "") + childKey;
if (typeof child === "object" && !Array.isArray(child)) {
if (
typeof child === "object" &&
!Array.isArray(child) &&
!this.isSettingsObjectGroup(childPath)
) {
this.bindRecursive(childPath, child);
} /*if (typeof child === "number" || typeof child === "string") */ else {
//console.log("binding", childPath);
} else {
this.doWatch(childPath, child);
}
}
Expand Down Expand Up @@ -325,7 +347,8 @@ export default Vue.extend({
},
components: {
SettingsField,
SettingsGroup
SettingsGroup,
SettingsPassword
}
});
</script>

0 comments on commit d0b1904

Please sign in to comment.