From 44e8035ff00400c106369ea81530181447583b2c Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 14 May 2025 22:46:46 +0100 Subject: [PATCH] config: add http:// to all urls if needed, fix invite link generation changed invites.ts to generate links using window.Pages entirely, rather than chopping up window.location.href. --- config.go | 27 ++++++++++++++++++++------- ts/modules/activity.ts | 10 ---------- ts/modules/invites.ts | 7 +------ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/config.go b/config.go index 05acb5b..53f846f 100644 --- a/config.go +++ b/config.go @@ -45,6 +45,13 @@ func (app *appContext) MustSetURLPath(section, key, val string) { app.MustSetValue(section, key, val) } +func FixFullURL(v string) string { + if !strings.HasPrefix(v, "http://") && !strings.HasPrefix(v, "https://") { + v = "http://" + v + } + return v +} + func FormatSubpath(path string) string { if path == "/" { return "" @@ -52,6 +59,15 @@ func FormatSubpath(path string) string { return strings.TrimSuffix(path, "/") } +func (app *appContext) MustCorrectURL(section, key, value string) { + v := app.config.Section(section).Key(key).String() + if v == "" { + v = value + } + v = FixFullURL(v) + app.config.Section(section).Key(key).SetValue(v) +} + func (app *appContext) loadConfig() error { var err error app.config, err = ini.ShadowLoad(app.configPath) @@ -75,8 +91,10 @@ func (app *appContext) loadConfig() error { app.err.Printf(lm.BadURLBase, PAGES.Base) } app.info.Printf(lm.SubpathBlockMessage, PAGES.Base, PAGES.Admin, PAGES.MyAccount, PAGES.Form) - app.MustSetValue("jellyfin", "public_server", app.config.Section("jellyfin").Key("server").String()) - app.MustSetValue("ui", "redirect_url", app.config.Section("jellyfin").Key("public_server").String()) + + app.MustCorrectURL("jellyfin", "server", "") + app.MustCorrectURL("jellyfin", "public_server", app.config.Section("jellyfin").Key("server").String()) + app.MustCorrectURL("ui", "redirect_url", app.config.Section("jellyfin").Key("public_server").String()) for _, key := range app.config.Section("files").Keys() { if name := key.Name(); name != "html_templates" && name != "lang_files" { @@ -133,11 +151,6 @@ func (app *appContext) loadConfig() error { sc := app.config.Section("discord").Key("start_command").MustString("start") app.config.Section("discord").Key("start_command").SetValue(strings.TrimPrefix(strings.TrimPrefix(sc, "/"), "!")) - jfUrl := app.config.Section("jellyfin").Key("server").String() - if !(strings.HasPrefix(jfUrl, "http://") || strings.HasPrefix(jfUrl, "https://")) { - app.config.Section("jellyfin").Key("server").SetValue("http://" + jfUrl) - } - // Deletion template is good enough for these as well. app.MustSetValue("disable_enable", "disabled_html", "jfa-go:"+"deleted.html") app.MustSetValue("disable_enable", "disabled_text", "jfa-go:"+"deleted.txt") diff --git a/ts/modules/activity.ts b/ts/modules/activity.ts index 1049390..135d50b 100644 --- a/ts/modules/activity.ts +++ b/ts/modules/activity.ts @@ -48,16 +48,6 @@ export class Activity implements activity, SearchableItem { private _delete: HTMLElement; private _ip: HTMLElement; private _act: activity; - /* private _urlBase: string = ((): string => { - let link = window.location.href; - for (let split of ["#", "?", "/activity"]) { - link = link.split(split)[0]; - } - if (link.slice(-1) != "/") { link += "/"; } - // FIXME: I should probably just be using window.pages.Base, but incase thats not right, i'll put this warning here - if (link != window.pages.Base) console.error(`URL Bases don't match: "${link}" != "${window.pages.Base}"`); - return link; - })(); */ _genUserText = (): string => { return `${this._act.username || this._act.user_id.substring(0, 5)}`; diff --git a/ts/modules/invites.ts b/ts/modules/invites.ts index 4c34a90..3982ab5 100644 --- a/ts/modules/invites.ts +++ b/ts/modules/invites.ts @@ -63,12 +63,7 @@ class DOMInvite implements Invite { get code(): string { return this._code; } set code(code: string) { this._code = code; - let codeLink = window.location.href; - for (let split of ["#", "?"]) { - codeLink = codeLink.split(split)[0]; - } - if (codeLink.slice(-1) != "/") { codeLink += "/"; } - this._codeLink = codeLink + window.pages.Form + "/" + code; + this._codeLink = window.pages.Base + window.pages.Form + "/" + code; const linkEl = this._codeArea.querySelector("a") as HTMLAnchorElement; if (this.label == "") { linkEl.textContent = code.replace(/-/g, '-');