增加tag的验证
This commit is contained in:
parent
8d5c989d97
commit
c842c3a293
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
||||||
Copyright (c) 2013, Jason Moiron
|
Copyright (c) 2023 星
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person
|
Permission is hereby granted, free of charge, to any person
|
||||||
obtaining a copy of this software and associated documentation
|
obtaining a copy of this software and associated documentation
|
||||||
|
|
|
@ -116,6 +116,17 @@ func Slice[T any](arr []T, offset, length int) (r []T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterAndToMap[K comparable, V, T any](arr []T, fn func(T) (K, V, bool)) map[K]V {
|
||||||
|
r := make(map[K]V)
|
||||||
|
for _, t := range arr {
|
||||||
|
k, v, ok := fn(t)
|
||||||
|
if ok {
|
||||||
|
r[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func Comb[T any](arr []T, m int) (r [][]T) {
|
func Comb[T any](arr []T, m int) (r [][]T) {
|
||||||
if m == 1 {
|
if m == 1 {
|
||||||
for _, t := range arr {
|
for _, t := range arr {
|
||||||
|
@ -135,7 +146,6 @@ func Comb[T any](arr []T, m int) (r [][]T) {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func IsContained[T comparable](a T, arr []T) bool {
|
func IsContained[T comparable](a T, arr []T) bool {
|
||||||
for _, v := range arr {
|
for _, v := range arr {
|
||||||
if a == v {
|
if a == v {
|
||||||
|
|
|
@ -132,6 +132,10 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
if category == "" {
|
if category == "" {
|
||||||
category = h.c.Param("tag")
|
category = h.c.Param("tag")
|
||||||
if category != "" {
|
if category != "" {
|
||||||
|
allNames := cache.AllTagsNames(h.c)
|
||||||
|
if _, ok := allNames[category]; !ok {
|
||||||
|
return errors.New(str.Join("not exists tag ", category))
|
||||||
|
}
|
||||||
h.categoryType = "post_tag"
|
h.categoryType = "post_tag"
|
||||||
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
|
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
|
||||||
}
|
}
|
||||||
|
|
45
internal/pkg/cache/cache.go
vendored
45
internal/pkg/cache/cache.go
vendored
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
var postContextCache *cache.MapCache[uint64, dao.PostContext]
|
var postContextCache *cache.MapCache[uint64, dao.PostContext]
|
||||||
var archivesCaches *Arch
|
var archivesCaches *Arch
|
||||||
var categoryCaches *cache.VarCache[[]models.TermsMy]
|
var categoryAndTagsCaches *cache.VarCache[[]models.TermsMy]
|
||||||
var recentPostsCaches *cache.VarCache[[]models.Posts]
|
var recentPostsCaches *cache.VarCache[[]models.Posts]
|
||||||
var recentCommentsCaches *cache.VarCache[[]models.Comments]
|
var recentCommentsCaches *cache.VarCache[[]models.Comments]
|
||||||
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
||||||
|
@ -62,7 +62,7 @@ func InitActionsCommonCache() {
|
||||||
|
|
||||||
postMetaCache = cache.NewMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.PostDataCacheTime)
|
postMetaCache = cache.NewMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.PostDataCacheTime)
|
||||||
|
|
||||||
categoryCaches = cache.NewVarCache(dao.Categories, c.CategoryCacheTime)
|
categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CategoryCacheTime)
|
||||||
|
|
||||||
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.RecentPostCacheTime)
|
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.RecentPostCacheTime)
|
||||||
|
|
||||||
|
@ -88,13 +88,6 @@ func InitActionsCommonCache() {
|
||||||
|
|
||||||
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.UserInfoCacheTime)
|
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.UserInfoCacheTime)
|
||||||
|
|
||||||
allCategories = cache.NewVarCache(func(a ...any) (map[string]struct{}, error) {
|
|
||||||
ctx := a[0].(context.Context)
|
|
||||||
return slice.ToMap(Categories(ctx), func(v models.TermsMy) (string, struct{}) {
|
|
||||||
return v.Name, struct{}{}
|
|
||||||
}, true), nil
|
|
||||||
}, c.CategoryCacheTime)
|
|
||||||
|
|
||||||
InitFeed()
|
InitFeed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,12 +147,40 @@ func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Categories(ctx context.Context) []models.TermsMy {
|
func Categories(ctx context.Context) []models.TermsMy {
|
||||||
r, err := categoryCaches.GetCache(ctx, time.Second, ctx)
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||||
logs.ErrPrintln(err, "get category err")
|
logs.ErrPrintln(err, "get category err")
|
||||||
|
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||||
|
return my.Taxonomy == "category"
|
||||||
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
func Tags(ctx context.Context) []models.TermsMy {
|
||||||
r, _ := allCategories.GetCache(ctx, time.Second, ctx)
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||||
|
logs.ErrPrintln(err, "get category err")
|
||||||
|
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||||
|
return my.Taxonomy == "post_tag"
|
||||||
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
func AllTagsNames(ctx context.Context) map[string]struct{} {
|
||||||
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||||
|
logs.ErrPrintln(err, "get category err")
|
||||||
|
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||||
|
if t.Taxonomy == "post_tag" {
|
||||||
|
return t.Name, struct{}{}, true
|
||||||
|
}
|
||||||
|
return "", struct{}{}, false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
||||||
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||||
|
logs.ErrPrintln(err, "get category err")
|
||||||
|
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||||
|
if t.Taxonomy == "category" {
|
||||||
|
return t.Name, struct{}{}, true
|
||||||
|
}
|
||||||
|
return "", struct{}{}, false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -26,9 +26,9 @@ func PasswordProjectTitle(post *models.Posts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Categories(a ...any) (terms []models.TermsMy, err error) {
|
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
||||||
ctx := a[0].(context.Context)
|
ctx := a[0].(context.Context)
|
||||||
var in = []any{"category"}
|
var in = []any{"category", "post_tag"}
|
||||||
terms, err = model.Find[models.TermsMy](ctx, model.SqlBuilder{
|
terms, err = model.Find[models.TermsMy](ctx, model.SqlBuilder{
|
||||||
{"tt.count", ">", "0", "int"},
|
{"tt.count", ">", "0", "int"},
|
||||||
{"tt.taxonomy", "in", ""},
|
{"tt.taxonomy", "in", ""},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user