diff --git a/internal/api/http/handlers/widget/widget.go b/internal/api/http/handlers/widget/widget.go index 317c44d..064a672 100644 --- a/internal/api/http/handlers/widget/widget.go +++ b/internal/api/http/handlers/widget/widget.go @@ -37,6 +37,15 @@ func CreateWidget(widgetService model.WidgetService, authClient model.AuthClient return request.JSON(http.StatusInternalServerError, err.Error()) } + if authData.StreamerID == 0 { + slog.Error("Unauthorized account") + return request.JSON(http.StatusUnauthorized, "Unauthorized") + } + if authData.StreamerID == -1 { + slog.Error("Expired token") + return request.JSON(http.StatusUnauthorized, "Expired token") + } + widgetID, err := widgetService.CreateWidget( ctx, model.StreamerID(authData.StreamerID), diff --git a/internal/model/widget-templates.go b/internal/model/widget-templates.go index b929423..d3023e6 100644 --- a/internal/model/widget-templates.go +++ b/internal/model/widget-templates.go @@ -41,16 +41,17 @@ func GetTemplate1( `, backgroundUrl) script := fmt.Sprintf(` -let baseUrl = 'http://147.45.237.174:8002/api' +let widgetUrl = 'http://147.45.237.174:8002/api/widget' +let ttsUrl = 'http://147.45.237.174:8005/api/tts' async function getDonatInfo(streamerID) { - let response = await fetch(baseUrl + '/widget/donat/get/' + String(streamerID)); + let response = await fetch(widgetUrl + '/donat/get/' + String(streamerID)); let donat = await response.json(); return donat } async function getWidgetInfo(widgetID) { - let response = await fetch(baseUrl + '/widget/info/' + widgetID); + let response = await fetch(widgetUrl + '/info/' + widgetID); let widget = await response.json(); return widget } @@ -67,12 +68,22 @@ function addText(text) { contentDiv.appendChild(p); } -function addAudio(audioUrl) { - audio = document.createElement('audio'); - audio.src = audioUrl; - audio.autoplay = true; - audio.controls = false; - contentDiv.appendChild(audio); +function playAudio(audioUrl) { + const audio = new Audio(audioUrl); + audio.play() + await delay(5000); + audio.pause(); + audio.remove(); +} + +function playSpeech(text) { + const audio = new Audio(ttsUrl + '/generate, { + method: 'POST', + body: JSON.stringify({ + text: text, + }), + }); + audio.play() } function delay(ms) { @@ -87,7 +98,7 @@ async function endDonat(donatID) { while (contentDiv.firstChild) { contentDiv.removeChild(contentDiv.firstChild); } - let response = await fetch(baseUrl + '/widget/donat/view/' + String(donatID), {method: 'POST'}); + let response = await fetch(widgetUrl + '/donat/view/' + String(donatID), {method: 'POST'}); } let audio; @@ -114,8 +125,11 @@ async function widgetView() { if (widget.ImageUrl != "") { addImage(widget.ImageUrl) } + if (widget.AudioUrl) { + playAudio(widget.AudioUrl) + } + - addAudio(widget.AudioUrl) addText(donat.Text + " " + donat.Amount) await delay(widget.Duration * 1000);