diff options
Diffstat (limited to 'routers/common/pagetmpl.go')
-rw-r--r-- | routers/common/pagetmpl.go | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/routers/common/pagetmpl.go b/routers/common/pagetmpl.go index 52c9fceba3..c48596d48b 100644 --- a/routers/common/pagetmpl.go +++ b/routers/common/pagetmpl.go @@ -6,6 +6,7 @@ package common import ( goctx "context" "errors" + "sync" activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" @@ -22,8 +23,7 @@ type StopwatchTmplInfo struct { Seconds int64 } -func getActiveStopwatch(goCtx goctx.Context) *StopwatchTmplInfo { - ctx := context.GetWebContext(goCtx) +func getActiveStopwatch(ctx *context.Context) *StopwatchTmplInfo { if ctx.Doer == nil { return nil } @@ -48,8 +48,7 @@ func getActiveStopwatch(goCtx goctx.Context) *StopwatchTmplInfo { } } -func notificationUnreadCount(goCtx goctx.Context) int64 { - ctx := context.GetWebContext(goCtx) +func notificationUnreadCount(ctx *context.Context) int64 { if ctx.Doer == nil { return 0 } @@ -66,10 +65,19 @@ func notificationUnreadCount(goCtx goctx.Context) int64 { return count } -func PageTmplFunctions(ctx *context.Context) { - if ctx.IsSigned { - // defer the function call to the last moment when the tmpl renders - ctx.Data["NotificationUnreadCount"] = notificationUnreadCount - ctx.Data["GetActiveStopwatch"] = getActiveStopwatch - } +type pageGlobalDataType struct { + IsSigned bool + IsSiteAdmin bool + + GetNotificationUnreadCount func() int64 + GetActiveStopwatch func() *StopwatchTmplInfo +} + +func PageGlobalData(ctx *context.Context) { + var data pageGlobalDataType + data.IsSigned = ctx.Doer != nil + data.IsSiteAdmin = ctx.Doer != nil && ctx.Doer.IsAdmin + data.GetNotificationUnreadCount = sync.OnceValue(func() int64 { return notificationUnreadCount(ctx) }) + data.GetActiveStopwatch = sync.OnceValue(func() *StopwatchTmplInfo { return getActiveStopwatch(ctx) }) + ctx.Data["PageGlobalData"] = data } |