2023-12-20 14:30:55 +00:00
|
|
|
package wp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RenderComment(ctx context.Context, page int, render plugins.CommentHtml, ids []uint64, timeout time.Duration, isTLS bool) (string, error) {
|
2024-01-04 15:32:10 +00:00
|
|
|
ca, _ := cachemanager.GetMapCache[uint64, models.Comments]("postCommentData")
|
|
|
|
children, _ := cachemanager.GetMapCache[uint64, []uint64]("commentChildren")
|
2023-12-20 14:30:55 +00:00
|
|
|
h := CommentHandle{
|
|
|
|
maxDepth: str.ToInteger(wpconfig.GetOption("thread_comments_depth"), 5),
|
|
|
|
depth: 1,
|
|
|
|
isTls: isTLS,
|
|
|
|
html: render,
|
|
|
|
order: wpconfig.GetOption("comment_order"),
|
|
|
|
ca: ca,
|
2024-01-04 15:32:10 +00:00
|
|
|
children: children,
|
2023-12-20 14:30:55 +00:00
|
|
|
threadComments: wpconfig.GetOption("thread_comments") == "1",
|
|
|
|
page: page,
|
|
|
|
}
|
|
|
|
return h.formatComments(ctx, ids, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommentHandle struct {
|
|
|
|
maxDepth int
|
|
|
|
depth int
|
|
|
|
isTls bool
|
|
|
|
html plugins.CommentHtml
|
|
|
|
order string
|
|
|
|
page int
|
2024-01-04 15:32:10 +00:00
|
|
|
ca *cache.MapCache[uint64, models.Comments]
|
|
|
|
children *cache.MapCache[uint64, []uint64]
|
2023-12-20 14:30:55 +00:00
|
|
|
threadComments bool
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:50:23 +00:00
|
|
|
func (c CommentHandle) findGrandchildComments(ctx context.Context, timeout time.Duration, comments []models.Comments) ([]models.Comments, error) {
|
2024-01-04 15:32:10 +00:00
|
|
|
parentIds := slice.Map(comments, func(t models.Comments) uint64 {
|
|
|
|
return t.CommentId
|
|
|
|
})
|
2024-01-04 16:01:57 +00:00
|
|
|
children, err := c.children.GetCacheBatch(ctx, parentIds, timeout)
|
2024-01-10 15:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
rr := slice.FilterAndMap(children, func(t []uint64) ([]uint64, bool) {
|
|
|
|
return t, len(t) > 0
|
2023-12-20 14:30:55 +00:00
|
|
|
})
|
|
|
|
if len(rr) < 1 {
|
|
|
|
return comments, nil
|
|
|
|
}
|
|
|
|
ids := slice.Decompress(rr)
|
|
|
|
r, err := c.ca.GetCacheBatch(ctx, ids, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
rrr, err := c.findGrandchildComments(ctx, timeout, r)
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
comments = append(comments, rrr...)
|
2024-01-04 15:36:00 +00:00
|
|
|
slice.Sort(comments, func(i, j models.Comments) bool {
|
2024-01-10 15:50:23 +00:00
|
|
|
return c.html.FloorOrder(i, j)
|
2024-01-04 15:36:00 +00:00
|
|
|
})
|
2023-12-20 14:30:55 +00:00
|
|
|
return comments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommentHandle) formatComments(ctx context.Context, ids []uint64, timeout time.Duration) (html string, err error) {
|
|
|
|
comments, err := c.ca.GetCacheBatch(ctx, ids, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
if c.depth > 1 && c.depth < c.maxDepth {
|
|
|
|
slice.Sort(comments, func(i, j models.Comments) bool {
|
2024-01-10 15:50:23 +00:00
|
|
|
return c.html.FloorOrder(i, j)
|
2024-01-04 15:32:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
fixChildren := false
|
2023-12-20 14:30:55 +00:00
|
|
|
if c.depth >= c.maxDepth {
|
2024-01-10 15:50:23 +00:00
|
|
|
comments, err = c.findGrandchildComments(ctx, timeout, comments)
|
2024-01-04 15:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fixChildren = true
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
|
|
|
s := str.NewBuilder()
|
|
|
|
for i, comment := range comments {
|
|
|
|
eo := "even"
|
|
|
|
if (i+1)%2 == 0 {
|
|
|
|
eo = "odd"
|
|
|
|
}
|
|
|
|
parent := ""
|
|
|
|
fl := false
|
2024-01-04 15:32:10 +00:00
|
|
|
var children []uint64
|
|
|
|
if !fixChildren {
|
|
|
|
children, err = c.children.GetCache(ctx, comment.CommentId, timeout)
|
2024-01-10 15:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
}
|
|
|
|
if c.threadComments && len(children) > 0 && c.depth < c.maxDepth+1 {
|
2023-12-20 14:30:55 +00:00
|
|
|
parent = "parent"
|
|
|
|
fl = true
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
s.WriteString(c.html.FormatLi(ctx, comment, c.depth, c.maxDepth, c.page, c.isTls, c.threadComments, eo, parent))
|
2023-12-20 14:30:55 +00:00
|
|
|
if fl {
|
|
|
|
c.depth++
|
2024-01-04 15:32:10 +00:00
|
|
|
ss, err := c.formatComments(ctx, children, timeout)
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.WriteString(`<ol class="children">`, ss, `</ol>`)
|
|
|
|
c.depth--
|
|
|
|
}
|
|
|
|
s.WriteString("</li><!-- #comment-## -->")
|
|
|
|
}
|
|
|
|
|
|
|
|
html = s.String()
|
|
|
|
return
|
|
|
|
}
|