ef319c5c07
- 1 - I updated `modules/web/middleware/cookie.go`, `routers/common/pagetmpl.go`, and `routers/web/user/setting/profile.go` to add a dedicated `gitea_persistent_navbar` cookie, synchronize it when authenticated users save or load the preference, and reuse that cookie as the anonymous-page fallback so the navbar stays sticky after logout just like the footer already does.
121 lines
3.8 KiB
Go
121 lines
3.8 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
goctx "context"
|
|
"errors"
|
|
"strconv"
|
|
"sync"
|
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
// StopwatchTmplInfo is a view on a stopwatch specifically for template rendering
|
|
type StopwatchTmplInfo struct {
|
|
IssueLink string
|
|
RepoSlug string
|
|
IssueIndex int64
|
|
Seconds int64
|
|
}
|
|
|
|
func getActiveStopwatch(ctx *context.Context) *StopwatchTmplInfo {
|
|
if ctx.Doer == nil {
|
|
return nil
|
|
}
|
|
|
|
_, sw, issue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
|
|
if err != nil {
|
|
if !errors.Is(err, goctx.Canceled) {
|
|
log.Error("Unable to HasUserStopwatch for user:%-v: %v", ctx.Doer, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if sw == nil || sw.ID == 0 {
|
|
return nil
|
|
}
|
|
|
|
return &StopwatchTmplInfo{
|
|
issue.Link(),
|
|
issue.Repo.FullName(),
|
|
issue.Index,
|
|
sw.Seconds() + 1, // ensure time is never zero in ui
|
|
}
|
|
}
|
|
|
|
func notificationUnreadCount(ctx *context.Context) int64 {
|
|
if ctx.Doer == nil {
|
|
return 0
|
|
}
|
|
count, err := db.Count[activities_model.Notification](ctx, activities_model.FindNotificationOptions{
|
|
UserID: ctx.Doer.ID,
|
|
Status: []activities_model.NotificationStatus{activities_model.NotificationStatusUnread},
|
|
})
|
|
if err != nil {
|
|
if !errors.Is(err, goctx.Canceled) {
|
|
log.Error("Unable to find notification for user:%-v: %v", ctx.Doer, err)
|
|
}
|
|
return 0
|
|
}
|
|
return count
|
|
}
|
|
|
|
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
|
|
|
|
showPersistentFooter := false
|
|
showPersistentNavbar := false
|
|
showStickySideMenus := false
|
|
if ctx.Doer != nil {
|
|
val, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyPersistentFooter, "false")
|
|
if err != nil {
|
|
log.Error("GetUserSetting[%s] for user %d: %v", user_model.SettingsKeyPersistentFooter, ctx.Doer.ID, err)
|
|
} else {
|
|
showPersistentFooter, _ = strconv.ParseBool(val)
|
|
ctx.SetSiteCookie(middleware.CookiePersistentFooter, strconv.FormatBool(showPersistentFooter), 0)
|
|
}
|
|
|
|
val, err = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyPersistentNavbar, "false")
|
|
if err != nil {
|
|
log.Error("GetUserSetting[%s] for user %d: %v", user_model.SettingsKeyPersistentNavbar, ctx.Doer.ID, err)
|
|
} else {
|
|
showPersistentNavbar, _ = strconv.ParseBool(val)
|
|
ctx.SetSiteCookie(middleware.CookiePersistentNavbar, strconv.FormatBool(showPersistentNavbar), 0)
|
|
}
|
|
|
|
val, err = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyStickySideMenus, "false")
|
|
if err != nil {
|
|
log.Error("GetUserSetting[%s] for user %d: %v", user_model.SettingsKeyStickySideMenus, ctx.Doer.ID, err)
|
|
} else {
|
|
showStickySideMenus, _ = strconv.ParseBool(val)
|
|
}
|
|
} else {
|
|
showPersistentFooter, _ = strconv.ParseBool(middleware.GetSiteCookie(ctx.Req, middleware.CookiePersistentFooter))
|
|
showPersistentNavbar, _ = strconv.ParseBool(middleware.GetSiteCookie(ctx.Req, middleware.CookiePersistentNavbar))
|
|
}
|
|
ctx.Data["ShowPersistentFooter"] = showPersistentFooter
|
|
ctx.Data["ShowPersistentNavbar"] = showPersistentNavbar
|
|
ctx.Data["ShowStickySideMenus"] = showStickySideMenus
|
|
}
|