2022-09-26 13:25:41 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-08 06:01:05 +00:00
|
|
|
"database/sql"
|
2022-09-27 07:35:34 +00:00
|
|
|
"fmt"
|
2022-10-04 03:13:14 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-26 13:25:41 +00:00
|
|
|
"github/fthvgb1/wp-go/helper"
|
2022-10-08 06:01:05 +00:00
|
|
|
"github/fthvgb1/wp-go/logs"
|
2022-09-26 13:25:41 +00:00
|
|
|
"github/fthvgb1/wp-go/models"
|
2022-11-05 02:32:57 +00:00
|
|
|
"github/fthvgb1/wp-go/models/wp"
|
2022-09-27 07:35:34 +00:00
|
|
|
"strings"
|
2022-12-28 06:24:59 +00:00
|
|
|
"sync/atomic"
|
2022-09-26 13:25:41 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetPostById(ctx context.Context, id uint64) (wp.Posts, error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
return postsCache.GetCache(ctx, id, time.Second, ctx, id)
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetPostsByIds(ctx context.Context, ids []uint64) ([]wp.Posts, error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
return postsCache.GetCacheBatch(ctx, ids, time.Second, ctx, ids)
|
2022-09-27 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func SearchPost(ctx context.Context, key string, args ...any) (r []wp.Posts, total int, err error) {
|
2022-09-28 12:02:43 +00:00
|
|
|
ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2022-09-27 07:35:34 +00:00
|
|
|
}
|
2022-09-28 12:02:43 +00:00
|
|
|
total = ids.Length
|
|
|
|
r, err = GetPostsByIds(ctx, ids.Ids)
|
|
|
|
return
|
2022-09-27 07:35:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func getPostsByIds(ids ...any) (m map[uint64]wp.Posts, err error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
ctx := ids[0].(context.Context)
|
2022-11-05 14:40:02 +00:00
|
|
|
m = make(map[uint64]wp.Posts)
|
2022-11-07 08:04:13 +00:00
|
|
|
id := ids[1].([]uint64)
|
2022-09-26 13:25:41 +00:00
|
|
|
arg := helper.SliceMap(id, helper.ToAny[uint64])
|
2022-11-07 08:04:13 +00:00
|
|
|
rawPosts, err := models.Find[wp.Posts](ctx, models.SqlBuilder{{
|
2022-09-26 13:25:41 +00:00
|
|
|
"Id", "in", "",
|
|
|
|
}}, "a.*,ifnull(d.name,'') category_name,ifnull(taxonomy,'') `taxonomy`", "", nil, models.SqlBuilder{{
|
|
|
|
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
|
|
|
|
}, {
|
|
|
|
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
|
|
|
|
}, {
|
|
|
|
"left join", "wp_terms d", "c.term_id=d.term_id",
|
2022-11-04 09:42:11 +00:00
|
|
|
}}, nil, 0, arg)
|
2022-09-27 07:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
postsMap := make(map[uint64]wp.Posts)
|
2022-09-27 07:35:34 +00:00
|
|
|
for i, post := range rawPosts {
|
|
|
|
v, ok := postsMap[post.Id]
|
|
|
|
if !ok {
|
|
|
|
v = rawPosts[i]
|
|
|
|
}
|
|
|
|
if post.Taxonomy == "category" {
|
|
|
|
v.Categories = append(v.Categories, post.CategoryName)
|
|
|
|
} else if post.Taxonomy == "post_tag" {
|
|
|
|
v.Tags = append(v.Tags, post.CategoryName)
|
|
|
|
}
|
|
|
|
postsMap[post.Id] = v
|
|
|
|
}
|
2023-01-10 12:09:55 +00:00
|
|
|
meta, _ := getPostMetaByPostIds(ctx, id)
|
2022-09-27 07:35:34 +00:00
|
|
|
for k, pp := range postsMap {
|
|
|
|
if len(pp.Categories) > 0 {
|
|
|
|
t := make([]string, 0, len(pp.Categories))
|
|
|
|
for _, cat := range pp.Categories {
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
|
|
|
|
}
|
|
|
|
pp.CategoriesHtml = strings.Join(t, "、")
|
2023-01-10 12:09:55 +00:00
|
|
|
_, ok := meta[pp.Id]
|
|
|
|
if ok {
|
|
|
|
thumb := ToPostThumbnail(ctx, pp.Id)
|
|
|
|
if thumb.Path != "" {
|
|
|
|
pp.Thumbnail = thumb
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
}
|
|
|
|
if len(pp.Tags) > 0 {
|
|
|
|
t := make([]string, 0, len(pp.Tags))
|
|
|
|
for _, cat := range pp.Tags {
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/tag/%s" rel="tag">%s</a>`, cat, cat))
|
|
|
|
}
|
|
|
|
pp.TagsHtml = strings.Join(t, "、")
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
m[k] = pp
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-09-28 08:23:20 +00:00
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func PostLists(ctx context.Context, key string, args ...any) (r []wp.Posts, total int, err error) {
|
2022-09-28 12:02:43 +00:00
|
|
|
ids, err := postListIdsCache.GetCache(ctx, key, time.Second, args...)
|
2022-09-28 08:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
total = ids.Length
|
|
|
|
r, err = GetPostsByIds(ctx, ids.Ids)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchPostIds(args ...any) (ids PostIds, err error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
ctx := args[0].(context.Context)
|
|
|
|
where := args[1].(models.SqlBuilder)
|
|
|
|
page := args[2].(int)
|
|
|
|
limit := args[3].(int)
|
|
|
|
order := args[4].(models.SqlBuilder)
|
|
|
|
join := args[5].(models.SqlBuilder)
|
|
|
|
postType := args[6].([]any)
|
|
|
|
postStatus := args[7].([]any)
|
|
|
|
res, total, err := models.SimplePagination[wp.Posts](ctx, where, "ID", "", page, limit, order, join, nil, postType, postStatus)
|
2022-09-28 08:23:20 +00:00
|
|
|
for _, posts := range res {
|
|
|
|
ids.Ids = append(ids.Ids, posts.Id)
|
|
|
|
}
|
|
|
|
ids.Length = total
|
2022-12-28 06:24:59 +00:00
|
|
|
totalR := int(atomic.LoadInt64(&TotalRaw))
|
|
|
|
if total > totalR {
|
|
|
|
tt := int64(total)
|
|
|
|
atomic.StoreInt64(&TotalRaw, tt)
|
2022-10-04 03:13:14 +00:00
|
|
|
}
|
2022-09-28 08:23:20 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-04 03:13:14 +00:00
|
|
|
|
2022-11-07 08:04:13 +00:00
|
|
|
func getMaxPostId(a ...any) ([]uint64, error) {
|
|
|
|
ctx := a[0].(context.Context)
|
|
|
|
r, err := models.SimpleFind[wp.Posts](ctx, models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID")
|
2022-10-04 03:13:14 +00:00
|
|
|
var id uint64
|
|
|
|
if len(r) > 0 {
|
|
|
|
id = r[0].Id
|
|
|
|
}
|
|
|
|
return []uint64{id}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMaxPostId(ctx *gin.Context) (uint64, error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
Id, err := maxPostIdCache.GetCache(ctx, time.Second, ctx)
|
2022-10-04 03:13:14 +00:00
|
|
|
return Id[0], err
|
|
|
|
}
|
2022-10-08 06:01:05 +00:00
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func RecentPosts(ctx context.Context, n int) (r []wp.Posts) {
|
2022-11-07 08:04:13 +00:00
|
|
|
r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx)
|
2022-10-08 06:01:05 +00:00
|
|
|
if n < len(r) {
|
|
|
|
r = r[:n]
|
|
|
|
}
|
|
|
|
logs.ErrPrintln(err, "get recent post")
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 08:04:13 +00:00
|
|
|
func recentPosts(a ...any) (r []wp.Posts, err error) {
|
|
|
|
ctx := a[0].(context.Context)
|
|
|
|
r, err = models.Find[wp.Posts](ctx, models.SqlBuilder{{
|
2022-10-08 06:01:05 +00:00
|
|
|
"post_type", "post",
|
2022-11-04 09:42:11 +00:00
|
|
|
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, nil, 10)
|
2022-10-08 06:01:05 +00:00
|
|
|
for i, post := range r {
|
|
|
|
if post.PostPassword != "" {
|
|
|
|
PasswordProjectTitle(&r[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next wp.Posts, err error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
postCtx, err := postContextCache.GetCache(ctx, id, time.Second, ctx, date)
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
2022-11-05 14:40:02 +00:00
|
|
|
return wp.Posts{}, wp.Posts{}, err
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
prev = postCtx.prev
|
|
|
|
next = postCtx.next
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPostContext(arg ...any) (r PostContext, err error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
ctx := arg[0].(context.Context)
|
|
|
|
t := arg[1].(time.Time)
|
|
|
|
next, err := models.FirstOne[wp.Posts](ctx, models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"post_date", ">", t.Format("2006-01-02 15:04:05")},
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
{"post_type", "post"},
|
|
|
|
}, "ID,post_title,post_password", nil, []any{"publish"})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 08:04:13 +00:00
|
|
|
prev, err := models.FirstOne[wp.Posts](ctx, models.SqlBuilder{
|
2022-10-08 06:01:05 +00:00
|
|
|
{"post_date", "<", t.Format("2006-01-02 15:04:05")},
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
{"post_type", "post"},
|
|
|
|
}, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []any{"publish"})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = PostContext{
|
|
|
|
prev: prev,
|
|
|
|
next: next,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []wp.Posts, total int, err error) {
|
2022-11-10 12:03:52 +00:00
|
|
|
res, err := monthPostsCache.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, ctx, year, month)
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if order == "desc" {
|
|
|
|
res = helper.SliceReverse(res)
|
|
|
|
}
|
|
|
|
total = len(res)
|
|
|
|
rr := helper.SlicePagination(res, page, limit)
|
|
|
|
r, err = GetPostsByIds(ctx, rr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func monthPost(args ...any) (r []uint64, err error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
ctx := args[0].(context.Context)
|
|
|
|
year, month := args[1].(string), args[2].(string)
|
2022-10-08 06:01:05 +00:00
|
|
|
where := models.SqlBuilder{
|
|
|
|
{"post_type", "in", ""},
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
{"year(post_date)", year},
|
|
|
|
{"month(post_date)", month},
|
|
|
|
}
|
|
|
|
postType := []any{"post"}
|
|
|
|
status := []any{"publish"}
|
2022-11-07 08:04:13 +00:00
|
|
|
ids, err := models.Find[wp.Posts](ctx, where, "ID", "", models.SqlBuilder{{"Id", "asc"}}, nil, nil, 0, postType, status)
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, post := range ids {
|
|
|
|
r = append(r, post.Id)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|