diff --git a/common/errors.go b/common/errors.go index 8a49f561..3d0be426 100644 --- a/common/errors.go +++ b/common/errors.go @@ -357,7 +357,7 @@ func handleErrorTemplate(w http.ResponseWriter, r *http.Request, pi ErrorPage) { if RunPreRenderHook("pre_render_error", w, r, &pi.Header.CurrentUser, &pi) { return } - err := RunThemeTemplate(pi.Header.Theme.Name, "error", pi, w) + err := pi.Header.Theme.RunTmpl("error", pi, w) if err != nil { LogError(err) } diff --git a/common/template_init.go b/common/template_init.go index a2952ee3..91a6bb46 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -587,7 +587,7 @@ func InitTemplates() error { fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} { header := headerInt.(*Header) - err := RunThemeTemplate(header.Theme.Name, nameInt.(string), pageInt, header.Writer) + err := header.Theme.RunTmpl(nameInt.(string), pageInt, header.Writer) if err != nil { return err } diff --git a/common/templates/templates.go b/common/templates/templates.go index 232cb165..6c25bcee 100644 --- a/common/templates/templates.go +++ b/common/templates/templates.go @@ -967,7 +967,7 @@ ArgLoop: // TODO: Refactor this // TODO: Call the template function directly rather than going through RunThemeTemplate to eliminate a round of indirection? - out = "{\nerr := common.RunThemeTemplate(" + headParam + ".Theme.Name," + nameParam + "," + pageParam + ",w)\n" + out = "{\nerr := " + headParam + ".Theme.RunTmpl(" + nameParam + "," + pageParam + ",w)\n" out += "if err != nil {\nreturn err\n}\n}\n" literal = true break ArgLoop diff --git a/common/theme_list.go b/common/theme_list.go index 5ff499bc..52eb9616 100644 --- a/common/theme_list.go +++ b/common/theme_list.go @@ -23,6 +23,7 @@ var Themes ThemeList = make(map[string]*Theme) // ? Refactor this into a store? var DefaultThemeBox atomic.Value var ChangeDefaultThemeMutex sync.Mutex +// TODO: Fallback to a random theme if this doesn't exist, so admins can remove themes they don't use // TODO: Use this when the default theme doesn't exist var fallbackTheme = "cosora" var overridenTemplates = make(map[string]bool) // ? What is this used for? @@ -301,14 +302,14 @@ func (w GzipResponseWriter) Write(b []byte) (int, error) { // NEW method of doing theme templates to allow one user to have a different theme to another. Under construction. // TODO: Generate the type switch instead of writing it by hand // TODO: Cut the number of types in half -func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer) error { +func (theme *Theme) RunTmpl(template string, pi interface{}, w io.Writer) error { // Unpack this to avoid an indirect call gzw, ok := w.(GzipResponseWriter) if ok { w = gzw.Writer } - var getTmpl = GetThemeTemplate(theme, template) + var getTmpl = theme.GetTmpl(template) switch tmplO := getTmpl.(type) { case *func(CustomPagePage, io.Writer) error: var tmpl = *tmplO @@ -366,7 +367,7 @@ func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer case func(Page, io.Writer) error: return tmplO(pi.(Page), w) case nil, string: - mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap[template] + mapping, ok := theme.TemplatesMap[template] if !ok { mapping = template } @@ -392,10 +393,10 @@ func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer } // GetThemeTemplate attempts to get the template for a specific theme, otherwise it falls back on the default template pointer, which if absent will fallback onto the template interpreter -func GetThemeTemplate(theme string, template string) interface{} { +func (theme *Theme) GetTmpl(template string) interface{} { // TODO: Figure out why we're getting a nil pointer here when transpiled templates are disabled, I would have assumed that we would just fall back to !ok on this // Might have something to do with it being the theme's TmplPtr map, investigate. - tmpl, ok := Themes[theme].TmplPtr[template] + tmpl, ok := theme.TmplPtr[template] if ok { return tmpl } diff --git a/common/widgets.go b/common/widgets.go index 1d8f2603..de90f155 100644 --- a/common/widgets.go +++ b/common/widgets.go @@ -186,7 +186,7 @@ func (widget *Widget) Build(hvars interface{}) (string, error) { } var header = hvars.(*Header) - err := RunThemeTemplate(header.Theme.Name, widget.Body, hvars, header.Writer) + err := header.Theme.RunTmpl(widget.Body, hvars, header.Writer) return "", err } diff --git a/extend/guilds/lib/guilds.go b/extend/guilds/lib/guilds.go index 964d1782..9a12328a 100644 --- a/extend/guilds/lib/guilds.go +++ b/extend/guilds/lib/guilds.go @@ -189,7 +189,7 @@ func RouteGuildList(w http.ResponseWriter, r *http.Request, user common.User) co } pi := ListPage{"Guild List", user, header, guildList} - err = common.RunThemeTemplate(header.Theme.Name, "guilds_guild_list", pi, w) + err = header.Theme.RunTmpl("guilds_guild_list", pi, w) if err != nil { return common.InternalError(err, w, r) } diff --git a/routes/account.go b/routes/account.go index 279f747b..d23a87b2 100644 --- a/routes/account.go +++ b/routes/account.go @@ -147,7 +147,7 @@ func AccountLoginMFAVerify(w http.ResponseWriter, r *http.Request, user common.U if common.RunPreRenderHook("pre_render_login_mfa_verify", w, r, &user, &pi) { return nil } - err = common.RunThemeTemplate(header.Theme.Name, "login_mfa_verify", pi, w) + err = header.Theme.RunTmpl("login_mfa_verify", pi, w) if err != nil { return common.InternalError(err, w, r) } diff --git a/routes/common.go b/routes/common.go index 41a7a7c7..bf1c63c7 100644 --- a/routes/common.go +++ b/routes/common.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/Azareal/Gosora/common" ) @@ -20,10 +21,13 @@ func ParseSEOURL(urlBit string) (slug string, id int, err error) { } func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, header *common.Header, pi interface{}) common.RouteError { + if header.CurrentUser.IsAdmin { + header.Elapsed1 = time.Since(header.StartedAt).String() + } if common.RunPreRenderHook("pre_render_"+tmplName, w, r, &header.CurrentUser, pi) { return nil } - err := common.RunThemeTemplate(header.Theme.Name, tmplName, pi, w) + err := header.Theme.RunTmpl(tmplName, pi, w) if err != nil { return common.InternalError(err, w, r) } diff --git a/routes/moderate.go b/routes/moderate.go index ba1b82a9..dba652f8 100644 --- a/routes/moderate.go +++ b/routes/moderate.go @@ -31,7 +31,7 @@ func IPSearch(w http.ResponseWriter, r *http.Request, user common.User, header * if common.RunPreRenderHook("pre_render_ip_search", w, r, &user, &pi) { return nil } - err = common.RunThemeTemplate(header.Theme.Name, "ip_search", pi, w) + err = header.Theme.RunTmpl("ip_search", pi, w) if err != nil { return common.InternalError(err, w, r) }