From 2fcb5874914dcc35ff161dbc68eae988313dd11f Mon Sep 17 00:00:00 2001 From: Wessie Date: Wed, 1 Jan 2025 16:36:15 +0100 Subject: [PATCH] templates: move theme resolving logic into ThemeValues this way users of the API can't forget to do it, this was intended from the start but I forgot to adjust the prototype. --- templates/middleware.go | 34 ++++++++++++++++++++++++++++------ templates/middleware_test.go | 2 +- website/main.go | 16 +++------------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/templates/middleware.go b/templates/middleware.go index cd437233..fdb7172e 100644 --- a/templates/middleware.go +++ b/templates/middleware.go @@ -44,20 +44,42 @@ func cookieDecode(value string) (theme string, overwrite_dj, overwrite_holiday b } type ThemeValues struct { - holiday util.TypedValue[radio.ThemeName] - dj util.TypedValue[radio.ThemeName] + resolver func(string) string + holiday util.TypedValue[radio.ThemeName] + dj util.TypedValue[radio.ThemeName] } -func NewThemeValues() *ThemeValues { - return &ThemeValues{} +func NewThemeValues(resolver func(string) string) *ThemeValues { + if resolver == nil { + resolver = func(s string) string { return s } + } + + return &ThemeValues{ + resolver: resolver, + } +} + +func (tv *ThemeValues) resolve(theme radio.ThemeName) radio.ThemeName { + if theme == "" { + return theme + } + // resolve the theme that was passed in + resolved := tv.resolver(theme) + if resolved != theme { + // if input and output are not the same it means the theme didn't exist + // and we shouldn't use it, so just unset it + return "" + } + + return resolved } func (tv *ThemeValues) StoreHoliday(theme radio.ThemeName) { - tv.holiday.Store(theme) + tv.holiday.Store(tv.resolve(theme)) } func (tv *ThemeValues) StoreDJ(theme radio.ThemeName) { - tv.dj.Store(theme) + tv.dj.Store(tv.resolve(theme)) } // ThemeCtx adds a theme entry into the context of the request, that is acquirable by diff --git a/templates/middleware_test.go b/templates/middleware_test.go index 2365dd37..3260a55c 100644 --- a/templates/middleware_test.go +++ b/templates/middleware_test.go @@ -87,7 +87,7 @@ func BenchmarkCookieEncode(b *testing.B) { func BenchmarkDecideTheme(b *testing.B) { fn := func(holiday, user radio.ThemeName) func(radio.ThemeName) radio.ThemeName { - tv := NewThemeValues() + tv := NewThemeValues(nil) tv.StoreHoliday(holiday) tv.StoreDJ(user) return tv.decide diff --git a/website/main.go b/website/main.go index 8ee326c3..1a36434f 100644 --- a/website/main.go +++ b/website/main.go @@ -73,9 +73,6 @@ func Execute(ctx context.Context, cfg config.Config) error { // status RPC value statusValue := util.StreamValue(ctx, cfg.Manager.CurrentStatus) - // template value, for deciding what theme to use - themeValues := templates.NewThemeValues() - // templates // construct our stateful template functions, it uses the latest values from the manager templateFuncs := templates.NewStatefulFunctions(statusValue) @@ -89,6 +86,8 @@ func Execute(ctx context.Context, cfg config.Config) error { } executor := siteTemplates.Executor() + // template value, for deciding what theme to use + themeValues := templates.NewThemeValues(siteTemplates.ResolveThemeName) // user RPC value _ = util.StreamValue(ctx, cfg.Manager.CurrentUser, func(ctx context.Context, u *radio.User) { // if either no user, or no theme set, unset the DJ theme @@ -97,16 +96,7 @@ func Execute(ctx context.Context, cfg config.Config) error { return } - // check if the theme configured by the DJ actually exists - resolved := siteTemplates.ResolveThemeName(u.DJ.Theme) - if resolved != u.DJ.Theme { - // if input and output are not the same it means the theme didn't exist - // and we shouldn't use it, so just unset it - themeValues.StoreDJ("") - return - } - - themeValues.StoreDJ(resolved) + themeValues.StoreDJ(u.DJ.Theme) }) // daypass generation