diff --git a/actions/common/common.go b/actions/common/common.go index 8c7f5f7..c39dca3 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -8,6 +8,7 @@ import ( "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/vars" "log" + "strconv" "strings" "sync" "time" @@ -21,6 +22,7 @@ var categoryCaches *cache.SliceCache[models.WpTermsMy] var recentPostsCaches *cache.SliceCache[models.WpPosts] var monthCaches *cache.MapCache[string, []models.WpPosts] var recentCommentsCaches *cache.SliceCache[models.WpComments] +var postCommentCaches *cache.MapCache[uint64, []models.WpComments] func InitCache() { archivesCaches = &Arch{ @@ -31,6 +33,7 @@ func InitCache() { recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute) recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) + postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5) } type Arch struct { @@ -68,6 +71,21 @@ func GetMonthPost(ctx context.Context, year, month string) ([]models.WpPosts, er return monthCaches.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month) } +func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) { + return postCommentCaches.GetCache(ctx, Id, time.Second, Id) +} + +func postComments(args ...any) ([]models.WpComments, error) { + postId := args[0].(uint64) + return models.Find[models.WpComments](models.SqlBuilder{ + {"comment_approved", "1"}, + {"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"}, + }, "*", "", models.SqlBuilder{ + {"comment_date_gmt", "asc"}, + {"comment_ID", "asc"}, + }, nil, 0) +} + func RecentComments(ctx context.Context) (r []models.WpComments) { r, _ = recentCommentsCaches.GetCache(ctx, time.Second) return diff --git a/actions/detail.go b/actions/detail.go index 870267a..e15d498 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -8,7 +8,9 @@ import ( "github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/models" "net/http" + "sort" "strconv" + "strings" ) func Detail(c *gin.Context) { @@ -17,14 +19,14 @@ func Detail(c *gin.Context) { recent := common.RecentPosts(ctx) archive := common.Archives() categoryItems := common.Categories(ctx) - comments := common.RecentComments(ctx) + recentComments := common.RecentComments(ctx) var h = gin.H{ - "title": models.Options["blogname"], - "options": models.Options, - "recentPosts": recent, - "archives": archive, - "categories": categoryItems, - "comments": comments, + "title": models.Options["blogname"], + "options": models.Options, + "recentPosts": recent, + "archives": archive, + "categories": categoryItems, + "recentComments": recentComments, } defer func() { c.HTML(http.StatusOK, "posts/detail.gohtml", h) @@ -63,10 +65,164 @@ func Detail(c *gin.Context) { common.PasswdProjectContent(&post) showComment = false } + comments, err := common.PostComments(ctx, post.Id) + commentss := treeComments(comments) prev, next, err := common.GetContextPost(post.Id, post.PostDate) h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]) h["post"] = post h["showComment"] = showComment h["prev"] = prev + h["comments"] = formatComment(commentss, 1) h["next"] = next } + +type Comment struct { + models.WpComments + Children []*Comment +} + +type Comments []*Comment + +func (c Comments) Len() int { + return len(c) +} + +func (c Comments) Less(i, j int) bool { + return c[i].CommentId < c[j].CommentId +} + +func (c Comments) Swap(i, j int) { + c[i], c[j] = c[j], c[i] +} + +func formatComment(comments Comments, depth int) (html string) { + s := strings.Builder{} + if depth > 5 { + comments = findComments(comments) + } + sort.Sort(comments) + for i, comment := range comments { + eo := "even" + if (i+1)%2 == 0 { + eo = "odd" + } + p := "" + + fl := false + if len(comment.Children) > 0 && depth < 6 { + p = "parent" + fl = true + } + s.WriteString(formatLi(comment.WpComments, depth, eo, p)) + if fl { + depth++ + s.WriteString(`
{{CommentContent}}
+