Skip to content

Commit

Permalink
templates: move theme resolving logic into ThemeValues
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Wessie committed Jan 1, 2025
1 parent 24dcb42 commit 2fcb587
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
34 changes: 28 additions & 6 deletions templates/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions website/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2fcb587

Please sign in to comment.