2023-01-21 13:13:33 +00:00
|
|
|
package dao
|
2023-01-12 12:42:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-28 14:46:22 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-11-16 14:52:14 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2023-02-06 10:34:35 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-01-21 11:31:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-12-20 14:30:55 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/model"
|
2023-01-12 12:42:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RecentComments
|
|
|
|
// param context.Context
|
2023-11-02 14:40:13 +00:00
|
|
|
func RecentComments(ctx context.Context, a ...any) (r []models.Comments, err error) {
|
2023-11-16 14:52:14 +00:00
|
|
|
n := helper.ParseArgs(10, a...)
|
2023-02-06 10:34:35 +00:00
|
|
|
return model.Finds[models.Comments](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"post_status", "publish"},
|
|
|
|
}),
|
|
|
|
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"}}),
|
2023-02-09 07:43:20 +00:00
|
|
|
model.Limit(n),
|
2023-02-06 10:34:35 +00:00
|
|
|
))
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostComments
|
|
|
|
// param1 context.Context
|
|
|
|
// param2 postId
|
2023-10-26 13:38:31 +00:00
|
|
|
func PostComments(ctx context.Context, postId uint64, _ ...any) ([]uint64, error) {
|
2023-11-16 14:52:14 +00:00
|
|
|
r, err := model.ChunkFind[models.Comments](ctx, 300, model.Conditions(
|
2023-02-06 10:34:35 +00:00
|
|
|
model.Where(model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
2023-05-21 11:53:37 +00:00
|
|
|
{"comment_post_ID", "=", number.IntToString(postId), "int"},
|
2023-02-06 10:34:35 +00:00
|
|
|
}),
|
|
|
|
model.Fields("comment_ID"),
|
|
|
|
model.Order(model.SqlBuilder{
|
|
|
|
{"comment_date_gmt", "asc"},
|
|
|
|
{"comment_ID", "asc"},
|
|
|
|
})),
|
|
|
|
)
|
2023-01-12 12:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-21 11:31:23 +00:00
|
|
|
return slice.Map(r, func(t models.Comments) uint64 {
|
2023-01-12 12:42:16 +00:00
|
|
|
return t.CommentId
|
|
|
|
}), err
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
func GetCommentByIds(ctx context.Context, ids []uint64, _ ...any) (map[uint64]models.Comments, error) {
|
2023-11-28 16:00:25 +00:00
|
|
|
if len(ids) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-01-12 12:42:16 +00:00
|
|
|
m := make(map[uint64]models.Comments)
|
2023-11-28 16:00:25 +00:00
|
|
|
off := 0
|
|
|
|
for {
|
|
|
|
id := slice.Slice(ids, off, 500)
|
|
|
|
if len(id) < 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r, err := model.Finds[models.Comments](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{
|
|
|
|
{"comment_ID", "in", ""}, {"comment_approved", "1"},
|
|
|
|
}),
|
|
|
|
model.Fields("*"),
|
|
|
|
model.In(slice.ToAnySlice(id)),
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
for _, comments := range r {
|
|
|
|
m[comments.CommentId] = comments
|
|
|
|
}
|
|
|
|
off += 500
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
2023-11-28 16:00:25 +00:00
|
|
|
|
|
|
|
return m, nil
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
2023-11-28 14:46:22 +00:00
|
|
|
|
2023-12-20 14:30:55 +00:00
|
|
|
func CommentNum(ctx context.Context, postId uint64, _ ...any) (int, error) {
|
|
|
|
n, err := model.GetField[models.Posts](ctx, "comment_count", model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{{"ID", "=", number.IntToString(postId), "int"}})))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return str.ToInteger(n, 0), err
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:50:23 +00:00
|
|
|
func PostTopCommentNum(ctx context.Context, postId uint64, _ ...any) (int, error) {
|
|
|
|
v, err := model.GetField[models.Comments](ctx, "count(*) num", model.Conditions(
|
|
|
|
model.Where(postTopCommentNumWhere(postId)),
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return str.ToInteger(v, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func postTopCommentNumWhere(postId uint64) model.SqlBuilder {
|
2023-12-20 14:30:55 +00:00
|
|
|
threadComments := wpconfig.GetOption("thread_comments")
|
2024-01-10 15:50:23 +00:00
|
|
|
pageComments := wpconfig.GetOption("page_comments")
|
2023-12-20 14:30:55 +00:00
|
|
|
where := model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"comment_post_ID", "=", number.IntToString(postId), "int"},
|
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
if pageComments != "1" || threadComments == "1" || "1" == wpconfig.GetOption("thread_comments_depth") {
|
2023-12-20 14:30:55 +00:00
|
|
|
where = append(where, []string{"comment_parent", "0"})
|
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
return where
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostCommentsIds(ctx context.Context, postId uint64, page, limit, totalRaw int, _ ...any) ([]uint64, int, error) {
|
|
|
|
order := wpconfig.GetOption("comment_order")
|
|
|
|
pageComments := wpconfig.GetOption("page_comments")
|
|
|
|
condition := model.Conditions(
|
|
|
|
model.Where(postTopCommentNumWhere(postId)),
|
2023-12-20 14:30:55 +00:00
|
|
|
model.TotalRaw(totalRaw),
|
|
|
|
model.Fields("comment_ID"),
|
|
|
|
model.Order(model.SqlBuilder{
|
|
|
|
{"comment_date_gmt", order},
|
|
|
|
{"comment_ID", "asc"},
|
|
|
|
}),
|
2024-01-10 15:50:23 +00:00
|
|
|
)
|
|
|
|
var r []models.Comments
|
|
|
|
var total int
|
|
|
|
var err error
|
|
|
|
if pageComments != "1" {
|
|
|
|
r, err = model.ChunkFind[models.Comments](ctx, 300, condition)
|
|
|
|
total = len(r)
|
|
|
|
} else {
|
|
|
|
r, total, err = model.Pagination[models.Comments](ctx, condition, page, limit)
|
|
|
|
}
|
|
|
|
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return slice.Map(r, func(t models.Comments) uint64 {
|
|
|
|
return t.CommentId
|
|
|
|
}), total, err
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:32:10 +00:00
|
|
|
func CommentChildren(ctx context.Context, commentIds []uint64, _ ...any) (r map[uint64][]uint64, err error) {
|
|
|
|
rr, err := model.Finds[models.Comments](ctx, model.Conditions(
|
2023-12-20 14:30:55 +00:00
|
|
|
model.Where(model.SqlBuilder{
|
2024-01-04 15:32:10 +00:00
|
|
|
{"comment_parent", "in", ""},
|
2023-12-20 14:30:55 +00:00
|
|
|
{"comment_approved", "1"},
|
|
|
|
}),
|
2024-01-04 15:32:10 +00:00
|
|
|
model.In(slice.ToAnySlice(commentIds)),
|
|
|
|
model.Fields("comment_ID,comment_parent"),
|
2023-12-20 14:30:55 +00:00
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
return
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
rrr := slice.GroupBy(rr, func(v models.Comments) (uint64, uint64) {
|
2024-01-04 15:32:10 +00:00
|
|
|
return v.CommentParent, v.CommentId
|
|
|
|
})
|
2024-01-10 15:50:23 +00:00
|
|
|
r = make(map[uint64][]uint64)
|
|
|
|
for _, id := range commentIds {
|
|
|
|
r[id] = rrr[id]
|
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
return
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
|
|
|
|
func PreviousCommentNum(ctx context.Context, commentId, postId uint64) (int, error) {
|
|
|
|
v, err := model.GetField[models.Comments](ctx, "count(*)", model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"comment_post_ID", "=", number.IntToString(postId), "int"},
|
|
|
|
{"comment_ID", "<", number.IntToString(commentId), "int"},
|
|
|
|
{"comment_parent", "=", "0", "int"},
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return str.ToInteger(v, 0), nil
|
|
|
|
}
|