mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Prepare common tmpl functions in a middleware (#33957)
Fix the TODO in `routers/web/web.go`, and avoid the unnecessary `GetActiveStopwatch` SQL query in non-related route handlers.
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
"code.gitea.io/gitea/modules/eventsource"
|
||||
@@ -72,39 +70,3 @@ func CancelStopwatch(c *context.Context) {
|
||||
|
||||
c.JSONRedirect("")
|
||||
}
|
||||
|
||||
// GetActiveStopwatch is the middleware that sets .ActiveStopwatch on context
|
||||
func GetActiveStopwatch(ctx *context.Context) {
|
||||
if strings.HasPrefix(ctx.Req.URL.Path, "/api") {
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsSigned {
|
||||
return
|
||||
}
|
||||
|
||||
_, sw, issue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
|
||||
if err != nil {
|
||||
ctx.ServerError("HasUserStopwatch", err)
|
||||
return
|
||||
}
|
||||
|
||||
if sw == nil || sw.ID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["ActiveStopwatch"] = StopwatchTmplInfo{
|
||||
issue.Link(),
|
||||
issue.Repo.FullName(),
|
||||
issue.Index,
|
||||
sw.Seconds() + 1, // ensure time is never zero in ui
|
||||
}
|
||||
}
|
||||
|
||||
// StopwatchTmplInfo is a view on a stopwatch specifically for template rendering
|
||||
type StopwatchTmplInfo struct {
|
||||
IssueLink string
|
||||
RepoSlug string
|
||||
IssueIndex int64
|
||||
Seconds int64
|
||||
}
|
||||
|
@@ -4,7 +4,6 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
goctx "context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -35,32 +34,6 @@ const (
|
||||
tplNotificationSubscriptions templates.TplName = "user/notification/notification_subscriptions"
|
||||
)
|
||||
|
||||
// GetNotificationCount is the middleware that sets the notification count in the context
|
||||
func GetNotificationCount(ctx *context.Context) {
|
||||
if strings.HasPrefix(ctx.Req.URL.Path, "/api") {
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsSigned {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["NotificationUnreadCount"] = func() int64 {
|
||||
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 err != goctx.Canceled {
|
||||
log.Error("Unable to GetNotificationCount for user:%-v: %v", ctx.Doer, err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
// Notifications is the notifications page
|
||||
func Notifications(ctx *context.Context) {
|
||||
getNotifications(ctx)
|
||||
|
@@ -280,10 +280,8 @@ func Routes() *web.Router {
|
||||
routes.Get("/api/swagger", append(mid, misc.Swagger)...) // Render V1 by default
|
||||
}
|
||||
|
||||
// TODO: These really seem like things that could be folded into Contexter or as helper functions
|
||||
mid = append(mid, user.GetNotificationCount)
|
||||
mid = append(mid, repo.GetActiveStopwatch)
|
||||
mid = append(mid, goGet)
|
||||
mid = append(mid, common.PageTmplFunctions)
|
||||
|
||||
others := web.NewRouter()
|
||||
others.Use(mid...)
|
||||
|
Reference in New Issue
Block a user