Skip to content

Commit

Permalink
better info return for channel update/creation
Browse files Browse the repository at this point in the history
related to MrBrax#129
fixes MrBrax#132
  • Loading branch information
MrBrax committed Apr 26, 2022
1 parent 8e1b809 commit 0326bf9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
42 changes: 24 additions & 18 deletions client-vue/src/components/forms/ChannelAddForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="download_chat" value="1" v-model="formData.download_chat" />
<input type="checkbox" name="download_chat" v-model="formData.download_chat" />
Download chat after video capture is complete
</label>
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="live_chat" value="1" v-model="formData.live_chat" />
<input type="checkbox" name="live_chat" v-model="formData.live_chat" />
Live chat download
</label>
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="burn_chat" value="1" v-model="formData.burn_chat" />
<input type="checkbox" name="burn_chat" v-model="formData.burn_chat" />
Burn chat after downloading
</label>
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="no_capture" value="1" v-model="formData.no_capture" />
<input type="checkbox" name="no_capture" v-model="formData.no_capture" />
Do not capture video
</label>
</div>
Expand Down Expand Up @@ -92,18 +92,10 @@ export default defineComponent({
},
methods: {
submitForm(event: Event) {
// const form = event.target as HTMLFormElement;
// const inputs = new FormData(form);
this.formStatusText = "Loading...";
this.formStatus = "";
// console.log("form", form);
// console.log("entries", inputs, inputs.entries(), inputs.values());
// let data: Record<string, unknown> = {};
// inputs.forEach((value, key) => (data[key] = value));
this.$http
.post(`/api/v0/channels`, this.formData)
.then((response) => {
Expand All @@ -112,22 +104,36 @@ export default defineComponent({
this.formStatus = json.status;
if (json.status == "OK") {
this.$emit("formSuccess", json);
this.resetForm();
}
})
.catch((err) => {
console.error("form error", err.response);
if (err.response.data.status == "ERROR") {
this.formStatusText = err.response.data.message;
this.formStatus = err.response.data.status;
} else {
this.formStatusText = err.response.data;
this.formStatus = "ERROR";
if (this.axios.isAxiosError(err) && err.response) {
if (err.response.data.status == "ERROR") {
this.formStatusText = err.response.data.message;
this.formStatus = err.response.data.status;
} else {
this.formStatusText = err.response.data;
this.formStatus = "ERROR";
}
}
});
event.preventDefault();
return false;
},
resetForm() {
this.formData = {
login: "",
quality: "",
match: "",
download_chat: false,
live_chat: false,
burn_chat: false,
no_capture: false,
};
},
checkLogin() {
const match = this.formData.login.match(/^https?:\/\/www.twitch.tv\/(\w+)/);
if (match) {
Expand Down
54 changes: 30 additions & 24 deletions client-vue/src/components/forms/ChannelUpdateForm.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<template>
<div>
<form method="POST" enctype="multipart/form-data" action="#" @submit="submitForm">
<input type="hidden" name="login" :value="channel.login" />

<form method="POST" enctype="multipart/form-data" action="#" @submit.prevent="submitForm">
<div class="field">
<label class="label" :for="'input_' + channel.login + '_quality'">Quality</label>
<label class="label" for="input_quality">Quality</label>
<div class="control">
<input
class="input input-required"
type="text"
name="quality"
:id="'input_' + channel.login + '_quality'"
:value="channel.quality.join(' ')"
id="input_quality"
v-model="formData.quality"
required
/>
<p class="input-help">Separate by spaces, e.g. best 1080p 720p audio_only</p>
Expand All @@ -20,37 +17,37 @@
</div>

<div class="field">
<label class="label" :for="'input_' + channel.login + '_match'">Match keywords</label>
<label class="label" for="input_match">Match keywords</label>
<div class="control">
<input class="input" type="text" name="match" :id="'input_' + channel.login + '_match'" :value="channel.match?.join(', ')" />
<input class="input" type="text" id="input_match" v-model="formData.match" />
<p class="input-help">Separate by commas, e.g. christmas,media share,opening,po box</p>
</div>
</div>

<div class="field">
<label class="checkbox">
<input class="input" type="checkbox" name="download_chat" value="1" :checked="channel.download_chat" />
<input class="input" type="checkbox" v-model="formData.download_chat" />
Download chat after video capture is complete
</label>
</div>

<div class="field">
<label class="checkbox">
<input class="input" type="checkbox" name="live_chat" value="1" :checked="channel.live_chat" />
<input class="input" type="checkbox" v-model="formData.live_chat" />
Live chat download
</label>
</div>

<div class="field">
<label class="checkbox">
<input class="input" type="checkbox" name="burn_chat" value="1" :checked="channel.burn_chat" />
<input class="input" type="checkbox" v-model="formData.burn_chat" />
Burn chat after downloading
</label>
</div>

<div class="field">
<label class="checkbox">
<input class="input" type="checkbox" name="no_capture" value="1" :checked="channel.no_capture" />
<input class="input" type="checkbox" v-model="formData.no_capture" />
No capture
</label>
</div>
Expand Down Expand Up @@ -84,6 +81,7 @@ import { VideoQualityArray } from "../../../../common/Defs";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faSave } from "@fortawesome/free-solid-svg-icons";
import { ApiChannelConfig } from "@common/Api/Client";
import { AxiosError } from "axios";
library.add(faSave);
export default defineComponent({
Expand All @@ -99,27 +97,28 @@ export default defineComponent({
return { VideoQualityArray };
},
data() {
console.debug("match", this.channel, this.channel.match);
return {
formStatusText: "Ready",
formStatus: "",
formData: {
quality: this.channel.quality ? this.channel.quality.join(" ") : "",
match: this.channel.match ? this.channel.match.join(",") : "",
download_chat: this.channel.download_chat || false,
live_chat: this.channel.live_chat || false,
burn_chat: this.channel.burn_chat || false,
no_capture: this.channel.no_capture || false,
},
};
},
methods: {
submitForm(event: Event) {
const form = event.target as HTMLFormElement;
const inputs = new FormData(form);
this.formStatusText = "Loading...";
this.formStatus = "";
// console.log("form", form);
// console.log("entries", inputs, inputs.entries(), inputs.values());
let data: Record<string, unknown> = {};
inputs.forEach((value, key) => (data[key] = value));
this.$http
.put(`/api/v0/channels/${this.channel.login}`, data)
.put(`/api/v0/channels/${this.channel.login}`, this.formData)
.then((response) => {
const json = response.data;
this.formStatusText = json.message;
Expand All @@ -128,8 +127,15 @@ export default defineComponent({
this.$emit("formSuccess", json);
}
})
.catch((err) => {
console.error("form error", err.response);
.catch((err: Error | AxiosError) => {
if (this.axios.isAxiosError(err) && err.response) {
console.error("channel update form error", err.response);
this.formStatusText = err.response.data.message;
this.formStatus = err.response.data.status;
} else {
console.error("channel update form error", err);
alert(`Error: ${err.message}`);
}
});
event.preventDefault();
Expand Down
29 changes: 20 additions & 9 deletions server/src/Controllers/Channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export function UpdateChannel(req: express.Request, res: express.Response): void

const quality = formdata.quality ? formdata.quality.split(" ") as VideoQuality[] : [];
const match = formdata.match ? formdata.match.split(",").map(m => m.trim()) : [];
const download_chat = formdata.download_chat !== undefined;
const burn_chat = formdata.burn_chat !== undefined;
const no_capture = formdata.no_capture !== undefined;
const live_chat = formdata.live_chat !== undefined;
const download_chat = formdata.download_chat;
const burn_chat = formdata.burn_chat;
const no_capture = formdata.no_capture;
const live_chat = formdata.live_chat;

const channel_config: ChannelConfig = {
login: channel.login,
Expand All @@ -91,6 +91,7 @@ export function UpdateChannel(req: express.Request, res: express.Response): void

res.send({
status: "OK",
message: `Channel '${channel.login}' updated`,
});

}
Expand Down Expand Up @@ -126,6 +127,7 @@ export function DeleteChannel(req: express.Request, res: express.Response): void

res.send({
status: "OK",
message: `Channel '${channel.login}' deleted`,
});

}
Expand All @@ -146,10 +148,10 @@ export async function AddChannel(req: express.Request, res: express.Response): P
login: formdata.login,
quality: formdata.quality ? formdata.quality.split(" ") as VideoQuality[] : [],
match: formdata.match ? formdata.match.split(",").map(m => m.trim()) : [],
download_chat: formdata.download_chat !== undefined,
burn_chat: formdata.burn_chat !== undefined,
no_capture: formdata.no_capture !== undefined,
live_chat: formdata.live_chat !== undefined,
download_chat: formdata.download_chat,
burn_chat: formdata.burn_chat,
no_capture: formdata.no_capture,
live_chat: formdata.live_chat,
};

if (!channel_config.login) {
Expand Down Expand Up @@ -178,7 +180,6 @@ export async function AddChannel(req: express.Request, res: express.Response): P


const channel = TwitchChannel.getChannelByLogin(channel_config.login);

if (channel) {
Log.logAdvanced(LOGLEVEL.ERROR, "route.channels.add", `Failed to create channel, channel already exists: ${channel_config.login}`);
res.status(400).send({
Expand All @@ -188,6 +189,15 @@ export async function AddChannel(req: express.Request, res: express.Response): P
return;
}

const api_channel_data = await TwitchChannel.getChannelDataByLogin(channel_config.login);
if (api_channel_data && api_channel_data.login !== channel_config.login) {
res.status(400).send({
status: "ERROR",
message: "Channel login does not match data fetched from API",
} as ApiErrorResponse);
return;
}

let new_channel;
try {
new_channel = await TwitchChannel.create(channel_config);
Expand All @@ -205,6 +215,7 @@ export async function AddChannel(req: express.Request, res: express.Response): P
res.send({
data: new_channel,
status: "OK",
message: `Channel '${new_channel.login}' created`,
});

}
Expand Down
10 changes: 4 additions & 6 deletions server/src/Core/TwitchChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,14 @@ export class TwitchChannel {
const data = await TwitchChannel.getChannelDataByLogin(config.login, true);
if (!data) throw new Error(`Could not get channel data for channel login: ${config.login}`);

TwitchChannel.channels_config.push(config);
TwitchChannel.saveChannelsConfig();

const channel = await TwitchChannel.loadFromLogin(config.login, true);
if (!channel) throw new Error(`Channel ${config.login} could not be loaded`);

TwitchChannel.channels.push(channel);
if (channel.userid) await TwitchChannel.subscribe(channel.userid);

// @todo: subscribe
if (channel.userid) TwitchChannel.subscribe(channel.userid);
TwitchChannel.channels_config.push(config);
TwitchChannel.saveChannelsConfig();
TwitchChannel.channels.push(channel);

return channel;
}
Expand Down

0 comments on commit 0326bf9

Please sign in to comment.