wp-go/internal/theme/common/common.go

111 lines
2.5 KiB
Go
Raw Normal View History

package common
import (
2023-02-09 04:57:18 +00:00
"context"
2023-02-09 15:15:10 +00:00
"errors"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
2023-02-09 15:15:10 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
2023-02-09 15:15:10 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
2023-02-10 13:23:30 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
)
type Handle struct {
C *gin.Context
2023-02-14 11:47:47 +00:00
Theme string
2023-02-10 13:23:30 +00:00
Session sessions.Session
GinH gin.H
Password string
Scene int
Code int
Stats int
Templ string
2023-02-14 11:47:47 +00:00
Class []string
}
2023-02-14 11:47:47 +00:00
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
return &Handle{
C: c,
2023-02-14 11:47:47 +00:00
Theme: theme,
Session: sessions.Default(c),
GinH: gin.H{},
Scene: scene,
Code: http.StatusOK,
Stats: constraints.Ok,
}
2023-02-09 08:08:18 +00:00
}
func (h *Handle) GetPassword() {
pw := h.Session.Get("post_password")
if pw != nil {
h.Password = pw.(string)
}
2023-02-09 08:08:18 +00:00
}
2023-02-12 11:19:53 +00:00
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
2023-02-10 13:23:30 +00:00
2023-02-09 15:15:10 +00:00
pluginConf := config.GetConfig().ListPagePlugins
2023-02-10 13:23:30 +00:00
plugin := GetListPostPlugins(pluginConf, m)
2023-02-12 11:19:53 +00:00
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
2023-02-10 13:23:30 +00:00
2023-02-09 15:15:10 +00:00
}
2023-02-12 11:19:53 +00:00
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
2023-02-09 15:15:10 +00:00
return maps.Copy(pluginFns)
}
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
return func(posts models.Posts) models.Posts {
for _, fn := range call {
fn(&posts)
}
return posts
}
2023-02-08 16:07:53 +00:00
}
func Default[T any](t T) T {
return t
}
func ProjectTitle(t models.Posts) models.Posts {
if t.PostPassword != "" {
plugins.PasswordProjectTitle(&t)
}
return t
}
2023-02-12 11:19:53 +00:00
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
2023-02-09 15:15:10 +00:00
v, ok := m[t]
if ok {
return v, true
}
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
return nil, false
})
}
2023-02-09 09:12:33 +00:00
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] {
2023-02-09 04:57:18 +00:00
return func(post models.Posts) models.Posts {
if post.PostExcerpt != "" {
plugins.PostExcerpt(&post)
} else {
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
}
2023-02-09 09:12:33 +00:00
if len(calls) > 0 {
for _, call := range calls {
call(&post)
}
}
2023-02-09 04:57:18 +00:00
return post
}
}