This commit is contained in:
xing 2023-02-09 15:43:20 +08:00
parent d88dd6b853
commit 1100b89312
7 changed files with 22 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package actions
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
@ -18,7 +19,7 @@ import (
func Detail(c *gin.Context) { func Detail(c *gin.Context) {
var err error var err error
var post models.Posts var post models.Posts
recent := cache.RecentPosts(c, 5, true) recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle)
archive := cache.Archives(c) archive := cache.Archives(c)
categoryItems := cache.CategoriesTags(c, plugins.Category) categoryItems := cache.CategoriesTags(c, plugins.Category)
recentComments := cache.RecentComments(c, 5) recentComments := cache.RecentComments(c, 5)
@ -56,14 +57,14 @@ func Detail(c *gin.Context) {
Stats: status, Stats: status,
}) })
}() }()
ID := str.ToInteger[uint64](c.Param("id"), 0) id := str.ToInteger[uint64](c.Param("id"), 0)
maxId, err := cache.GetMaxPostId(c) maxId, err := cache.GetMaxPostId(c)
logs.ErrPrintln(err, "get max post id") logs.ErrPrintln(err, "get max post id")
if ID > maxId || ID <= 0 || err != nil { if id > maxId || id <= 0 || err != nil {
return return
} }
post, err = cache.GetPostById(c, ID) post, err = cache.GetPostById(c, id)
if post.Id == 0 || err != nil || post.PostStatus != "publish" { if post.Id == 0 || err != nil || post.PostStatus != "publish" {
return return
} }

View File

@ -221,7 +221,7 @@ func Index(c *gin.Context) {
var totalRaw int var totalRaw int
var err error var err error
archive := cache.Archives(c) archive := cache.Archives(c)
recent := cache.RecentPosts(c, 5, true) recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle)
categoryItems := cache.CategoriesTags(c, plugins.Category) categoryItems := cache.CategoriesTags(c, plugins.Category)
recentComments := cache.RecentComments(c, 5) recentComments := cache.RecentComments(c, 5)
ginH := gin.H{ ginH := gin.H{

View File

@ -9,7 +9,11 @@ import (
) )
func RecentComments(ctx context.Context, n int) (r []models.Comments) { func RecentComments(ctx context.Context, n int) (r []models.Comments) {
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx) nn := n
if nn <= 5 {
nn = 10
}
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx, nn)
if len(r) > n { if len(r) > n {
r = r[0:n] r = r[0:n]
} }

View File

@ -46,7 +46,7 @@ func PostFeedCache() *cache.MapCache[string, string] {
func feed(arg ...any) (xml []string, err error) { func feed(arg ...any) (xml []string, err error) {
c := arg[0].(*gin.Context) c := arg[0].(*gin.Context)
r := RecentPosts(c, 10, true) r := RecentPosts(c, 10)
ids := slice.Map(r, func(t models.Posts) uint64 { ids := slice.Map(r, func(t models.Posts) uint64 {
return t.Id return t.Id
}) })

View File

@ -6,7 +6,6 @@ import (
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"time" "time"
) )
@ -43,7 +42,7 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) {
return maxPostIdCache.GetCache(ctx, time.Second, ctx) return maxPostIdCache.GetCache(ctx, time.Second, ctx)
} }
func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) { func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
nn := n nn := n
if nn <= 5 { if nn <= 5 {
nn = 10 nn = 10
@ -52,14 +51,6 @@ func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) {
if n < len(r) { if n < len(r) {
r = r[:n] r = r[:n]
} }
if project {
r = slice.Map(r, func(t models.Posts) models.Posts {
if t.PostPassword != "" {
plugins.PasswordProjectTitle(&t)
}
return t
})
}
logs.ErrPrintln(err, "get recent post") logs.ErrPrintln(err, "get recent post")
return return
} }

View File

@ -12,6 +12,7 @@ import (
// param context.Context // param context.Context
func RecentComments(a ...any) (r []models.Comments, err error) { func RecentComments(a ...any) (r []models.Comments, err error) {
ctx := a[0].(context.Context) ctx := a[0].(context.Context)
n := a[1].(int)
return model.Finds[models.Comments](ctx, model.Conditions( return model.Finds[models.Comments](ctx, model.Conditions(
model.Where(model.SqlBuilder{ model.Where(model.SqlBuilder{
{"comment_approved", "1"}, {"comment_approved", "1"},
@ -20,7 +21,7 @@ func RecentComments(a ...any) (r []models.Comments, err error) {
model.Fields("comment_ID,comment_author,comment_post_ID,post_title"), model.Fields("comment_ID,comment_author,comment_post_ID,post_title"),
model.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}), model.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}),
model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}), model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}),
model.Limit(10), model.Limit(n),
)) ))
} }

View File

@ -54,6 +54,13 @@ func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r mode
return return
} }
func ProjectTitle(t models.Posts) models.Posts {
if t.PostPassword != "" {
plugins.PasswordProjectTitle(&t)
}
return t
}
func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts { func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
if post.PostExcerpt != "" { if post.PostExcerpt != "" {
plugins.PostExcerpt(&post) plugins.PostExcerpt(&post)