2024-01-13 13:15:54 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
|
|
|
"github.com/fthvgb1/wp-go/app/theme/wp"
|
|
|
|
"github.com/fthvgb1/wp-go/app/theme/wp/components/widget"
|
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var plainRouteParam = reload.Vars([]Plain{
|
|
|
|
{
|
|
|
|
Action: "p",
|
|
|
|
Param: map[string]string{
|
|
|
|
"p": "id",
|
|
|
|
"cpage": "page",
|
|
|
|
},
|
|
|
|
Scene: constraints.Detail,
|
|
|
|
},
|
2024-01-14 14:07:16 +00:00
|
|
|
{
|
|
|
|
Action: "s",
|
|
|
|
Scene: constraints.Search,
|
|
|
|
},
|
2024-01-13 13:15:54 +00:00
|
|
|
{
|
|
|
|
Scene: constraints.Category,
|
|
|
|
Fn: func(h *wp.Handle) bool {
|
|
|
|
c, ok := widget.IsCategory(h)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h.C.AddParam("category", c.Name)
|
|
|
|
h.C.AddParam("page", h.C.Query("paged"))
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Scene: constraints.Tag,
|
|
|
|
Action: "tag",
|
|
|
|
Param: map[string]string{
|
|
|
|
"tag": "tag",
|
|
|
|
"paged": "page",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Scene: constraints.Archive,
|
|
|
|
Fn: func(h *wp.Handle) bool {
|
|
|
|
m := h.C.Query("m")
|
|
|
|
if m == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t, err := time.Parse("200601", m)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h.C.AddParam("year", strconv.Itoa(t.Year()))
|
|
|
|
h.C.AddParam("month", number.IntToString(t.Month()))
|
|
|
|
h.C.AddParam("page", h.C.Query("paged"))
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Scene: constraints.Author,
|
|
|
|
Fn: func(h *wp.Handle) bool {
|
|
|
|
u := h.C.Query("author")
|
|
|
|
if u == "" {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
users := GetUsersIds(h)
|
2024-01-13 13:15:54 +00:00
|
|
|
name, ok := users[str.ToInteger[uint64](u, 0)]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h.C.AddParam("author", name)
|
|
|
|
h.C.AddParam("page", h.C.Query("paged"))
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
2024-01-24 07:13:29 +00:00
|
|
|
}, "plainRouteParam")
|
2024-01-13 13:15:54 +00:00
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetUsersIds = reload.BuildValFnWithConfirm("usersIds", func(h *wp.Handle) (map[uint64]string, bool) {
|
|
|
|
users, err := cache.GetAllUsername(h.C)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return maps.Flip(users), true
|
|
|
|
}, 10)
|
|
|
|
|
2024-01-14 14:07:16 +00:00
|
|
|
func SetExplainRouteParam(p []Plain) {
|
|
|
|
plainRouteParam.Store(p)
|
|
|
|
}
|
|
|
|
func GetExplainRouteParam() []Plain {
|
|
|
|
return plainRouteParam.Load()
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
func PushExplainRouteParam(explain ...Plain) {
|
|
|
|
v := plainRouteParam.Load()
|
|
|
|
v = append(v, explain...)
|
|
|
|
plainRouteParam.Store(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Plain struct {
|
|
|
|
Action string
|
|
|
|
Param map[string]string
|
|
|
|
Scene string
|
|
|
|
Fn func(h *wp.Handle) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func MixWithPlain(h *wp.Handle) {
|
|
|
|
for _, explain := range plainRouteParam.Load() {
|
|
|
|
if explain.Action == "" && explain.Fn == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if explain.Fn != nil {
|
|
|
|
if !explain.Fn(h) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if explain.Scene != "" {
|
|
|
|
h.SetScene(explain.Scene)
|
|
|
|
}
|
|
|
|
wp.Run(h, nil)
|
|
|
|
h.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if explain.Scene == "" {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-14 14:07:16 +00:00
|
|
|
q := h.C.Query(explain.Action)
|
|
|
|
if q == "" {
|
2024-01-13 13:15:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
h.SetScene(explain.Scene)
|
|
|
|
for query, param := range explain.Param {
|
|
|
|
h.C.AddParam(param, h.C.Query(query))
|
|
|
|
}
|
|
|
|
wp.Run(h, nil)
|
|
|
|
h.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowPreComment(h *wp.Handle) {
|
|
|
|
v, ok := cache.NewCommentCache().Get(h.C, h.C.Request.URL.RawQuery)
|
|
|
|
if ok {
|
|
|
|
h.C.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
h.C.Writer.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = h.C.Writer.Write([]byte(v))
|
|
|
|
h.Abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CommonMiddleware(h *wp.Handle) {
|
|
|
|
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
|
|
|
|
wp.NewHandleFn(MixWithPlain, 100, "middleware.MixWithPlain"),
|
|
|
|
)
|
|
|
|
h.PushHandler(constraints.PipeMiddleware, constraints.Detail,
|
|
|
|
wp.NewHandleFn(ShowPreComment, 100, "middleware.ShowPreComment"),
|
|
|
|
)
|
|
|
|
}
|