diff --git a/internal/actions/detail.go b/internal/actions/detail.go index 37dd564..4534738 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -2,6 +2,7 @@ package actions import ( "fmt" + "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/logs" @@ -18,7 +19,7 @@ import ( func Detail(c *gin.Context) { var err error var post models.Posts - recent := cache.RecentPosts(c, 5, true) + recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle) archive := cache.Archives(c) categoryItems := cache.CategoriesTags(c, plugins.Category) recentComments := cache.RecentComments(c, 5) @@ -56,14 +57,14 @@ func Detail(c *gin.Context) { Stats: status, }) }() - ID := str.ToInteger[uint64](c.Param("id"), 0) + id := str.ToInteger[uint64](c.Param("id"), 0) maxId, err := cache.GetMaxPostId(c) logs.ErrPrintln(err, "get max post id") - if ID > maxId || ID <= 0 || err != nil { + if id > maxId || id <= 0 || err != nil { return } - post, err = cache.GetPostById(c, ID) + post, err = cache.GetPostById(c, id) if post.Id == 0 || err != nil || post.PostStatus != "publish" { return } diff --git a/internal/actions/index.go b/internal/actions/index.go index 1afc260..57aed2a 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -221,7 +221,7 @@ func Index(c *gin.Context) { var totalRaw int var err error 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) recentComments := cache.RecentComments(c, 5) ginH := gin.H{ diff --git a/internal/pkg/cache/comments.go b/internal/pkg/cache/comments.go index 87dd333..4c4b922 100644 --- a/internal/pkg/cache/comments.go +++ b/internal/pkg/cache/comments.go @@ -9,7 +9,11 @@ import ( ) 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 { r = r[0:n] } diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index 7f1428a..1e1bd10 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -46,7 +46,7 @@ func PostFeedCache() *cache.MapCache[string, string] { func feed(arg ...any) (xml []string, err error) { c := arg[0].(*gin.Context) - r := RecentPosts(c, 10, true) + r := RecentPosts(c, 10) ids := slice.Map(r, func(t models.Posts) uint64 { return t.Id }) diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index d4ea374..52f2bef 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -6,7 +6,6 @@ import ( "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" - "github.com/fthvgb1/wp-go/internal/plugins" "github.com/gin-gonic/gin" "time" ) @@ -43,7 +42,7 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) { 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 if nn <= 5 { nn = 10 @@ -52,14 +51,6 @@ func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) { if n < len(r) { 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") return } diff --git a/internal/pkg/dao/comments.go b/internal/pkg/dao/comments.go index 86c1e7e..551489d 100644 --- a/internal/pkg/dao/comments.go +++ b/internal/pkg/dao/comments.go @@ -12,6 +12,7 @@ import ( // param context.Context func RecentComments(a ...any) (r []models.Comments, err error) { ctx := a[0].(context.Context) + n := a[1].(int) return model.Finds[models.Comments](ctx, model.Conditions( model.Where(model.SqlBuilder{ {"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.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}), model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}), - model.Limit(10), + model.Limit(n), )) } diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 1039610..f0cf430 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -54,6 +54,13 @@ func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r mode 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 { if post.PostExcerpt != "" { plugins.PostExcerpt(&post)