2022-10-08 06:01:05 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github/fthvgb1/wp-go/helper"
|
|
|
|
"github/fthvgb1/wp-go/logs"
|
|
|
|
"github/fthvgb1/wp-go/models"
|
2022-11-05 02:32:57 +00:00
|
|
|
"github/fthvgb1/wp-go/models/wp"
|
2022-10-08 06:01:05 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func RecentComments(ctx context.Context, n int) (r []wp.Comments) {
|
2022-10-08 06:01:05 +00:00
|
|
|
r, err := recentCommentsCaches.GetCache(ctx, time.Second)
|
|
|
|
if len(r) > n {
|
|
|
|
r = r[0:n]
|
|
|
|
}
|
|
|
|
logs.ErrPrintln(err, "get recent comment")
|
|
|
|
return
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
func recentComments(...any) (r []wp.Comments, err error) {
|
|
|
|
return models.Find[wp.Comments](models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"post_status", "publish"},
|
|
|
|
}, "comment_ID,comment_author,comment_post_ID,post_title", "", models.SqlBuilder{{"comment_date_gmt", "desc"}}, models.SqlBuilder{
|
|
|
|
{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"},
|
2022-11-04 09:42:11 +00:00
|
|
|
}, nil, 10)
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func PostComments(ctx context.Context, Id uint64) ([]wp.Comments, error) {
|
2022-10-10 14:46:04 +00:00
|
|
|
ids, err := postCommentCaches.GetCache(ctx, Id, time.Second, Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return GetCommentByIds(ctx, ids)
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 14:46:04 +00:00
|
|
|
func postComments(args ...any) ([]uint64, error) {
|
2022-10-08 06:01:05 +00:00
|
|
|
postId := args[0].(uint64)
|
2022-11-05 14:40:02 +00:00
|
|
|
r, err := models.Find[wp.Comments](models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"},
|
2022-10-10 14:46:04 +00:00
|
|
|
}, "comment_ID", "", models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"comment_date_gmt", "asc"},
|
|
|
|
{"comment_ID", "asc"},
|
2022-11-04 09:42:11 +00:00
|
|
|
}, nil, nil, 0)
|
2022-10-10 14:46:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
return helper.SliceMap(r, func(t wp.Comments) uint64 {
|
2022-10-10 14:46:04 +00:00
|
|
|
return t.CommentId
|
|
|
|
}), err
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetCommentById(ctx context.Context, id uint64) (wp.Comments, error) {
|
2022-10-08 06:01:05 +00:00
|
|
|
return commentsCache.GetCache(ctx, id, time.Second, id)
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetCommentByIds(ctx context.Context, ids []uint64) ([]wp.Comments, error) {
|
2022-10-08 06:01:05 +00:00
|
|
|
return commentsCache.GetCacheBatch(ctx, ids, time.Second, ids)
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func getCommentByIds(args ...any) (map[uint64]wp.Comments, error) {
|
2022-10-08 06:01:05 +00:00
|
|
|
ids := args[0].([]uint64)
|
2022-11-05 14:40:02 +00:00
|
|
|
m := make(map[uint64]wp.Comments)
|
|
|
|
r, err := models.SimpleFind[wp.Comments](models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"comment_ID", "in", ""}, {"comment_approved", "1"},
|
2022-11-04 09:58:31 +00:00
|
|
|
}, "*", helper.SliceMap(ids, helper.ToAny[uint64]))
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
return helper.SimpleSliceToMap(r, func(t wp.Comments) uint64 {
|
2022-10-08 06:01:05 +00:00
|
|
|
return t.CommentId
|
2022-10-10 13:33:41 +00:00
|
|
|
}), err
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|