2022-09-18 04:34:48 +00:00
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-20 04:00:09 +00:00
|
|
|
|
"context"
|
2022-09-20 08:55:13 +00:00
|
|
|
|
"database/sql"
|
2022-09-18 04:34:48 +00:00
|
|
|
|
"fmt"
|
2022-09-19 09:39:00 +00:00
|
|
|
|
"github/fthvgb1/wp-go/cache"
|
2022-09-27 13:52:15 +00:00
|
|
|
|
"github/fthvgb1/wp-go/helper"
|
2022-09-27 07:35:34 +00:00
|
|
|
|
"github/fthvgb1/wp-go/logs"
|
2022-09-18 04:34:48 +00:00
|
|
|
|
"github/fthvgb1/wp-go/models"
|
2022-09-19 11:11:36 +00:00
|
|
|
|
"github/fthvgb1/wp-go/vars"
|
2022-09-21 13:49:14 +00:00
|
|
|
|
"strconv"
|
2022-09-18 14:06:27 +00:00
|
|
|
|
"sync"
|
2022-09-19 09:39:00 +00:00
|
|
|
|
"time"
|
2022-09-18 04:34:48 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-09-27 07:35:34 +00:00
|
|
|
|
var postContextCache *cache.MapCache[uint64, PostContext]
|
2022-09-19 12:34:20 +00:00
|
|
|
|
var archivesCaches *Arch
|
2022-09-19 11:11:36 +00:00
|
|
|
|
var categoryCaches *cache.SliceCache[models.WpTermsMy]
|
|
|
|
|
var recentPostsCaches *cache.SliceCache[models.WpPosts]
|
2022-09-20 13:16:51 +00:00
|
|
|
|
var recentCommentsCaches *cache.SliceCache[models.WpComments]
|
2022-09-21 13:49:14 +00:00
|
|
|
|
var postCommentCaches *cache.MapCache[uint64, []models.WpComments]
|
2022-09-26 13:25:41 +00:00
|
|
|
|
var postsCache *cache.MapCache[uint64, models.WpPosts]
|
2022-09-27 13:52:15 +00:00
|
|
|
|
var monthPostsCache *cache.MapCache[string, []uint64]
|
2022-09-28 08:23:20 +00:00
|
|
|
|
var searchPostIdsCache *cache.MapCache[string, PostIds]
|
2022-09-19 11:11:36 +00:00
|
|
|
|
|
2022-09-26 13:25:41 +00:00
|
|
|
|
func InitActionsCommonCache() {
|
2022-09-19 12:34:20 +00:00
|
|
|
|
archivesCaches = &Arch{
|
2022-09-19 12:15:10 +00:00
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
|
setCacheFunc: archives,
|
|
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
|
|
2022-09-28 08:23:20 +00:00
|
|
|
|
searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, time.Hour)
|
|
|
|
|
|
2022-09-27 13:52:15 +00:00
|
|
|
|
monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, time.Hour)
|
|
|
|
|
|
2022-09-27 07:35:34 +00:00
|
|
|
|
postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime)
|
|
|
|
|
|
2022-09-28 08:23:20 +00:00
|
|
|
|
postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, time.Hour)
|
|
|
|
|
postsCache.SetCacheFunc(getPostById)
|
2022-09-26 13:25:41 +00:00
|
|
|
|
|
2022-09-19 11:11:36 +00:00
|
|
|
|
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
2022-09-26 13:25:41 +00:00
|
|
|
|
|
2022-09-19 11:11:36 +00:00
|
|
|
|
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
2022-09-26 13:25:41 +00:00
|
|
|
|
|
2022-09-20 13:16:51 +00:00
|
|
|
|
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
|
2022-09-21 13:49:14 +00:00
|
|
|
|
postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5)
|
2022-09-19 11:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 13:52:15 +00:00
|
|
|
|
func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []models.WpPosts, total int, err error) {
|
|
|
|
|
res, err := monthPostsCache.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if order == "desc" {
|
|
|
|
|
res = helper.SliceReverse(res)
|
|
|
|
|
}
|
|
|
|
|
total = len(res)
|
|
|
|
|
rr := helper.SlicePagination(res, page, limit)
|
2022-09-28 08:23:20 +00:00
|
|
|
|
r, err = GetPostsByIds(ctx, rr)
|
2022-09-27 13:52:15 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func monthPost(args ...any) (r []uint64, err error) {
|
|
|
|
|
year, month := args[0].(string), args[1].(string)
|
|
|
|
|
where := models.SqlBuilder{
|
|
|
|
|
{"post_type", "in", ""},
|
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
|
{"year(post_date)", year},
|
|
|
|
|
{"month(post_date)", month},
|
|
|
|
|
}
|
|
|
|
|
postType := []any{"post"}
|
|
|
|
|
status := []any{"publish"}
|
|
|
|
|
ids, err := models.Find[models.WpPosts](where, "ID", "", models.SqlBuilder{{"Id", "asc"}}, nil, 0, postType, status)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, post := range ids {
|
|
|
|
|
r = append(r, post.Id)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PostIds struct {
|
|
|
|
|
Ids []uint64
|
|
|
|
|
Length int
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 12:34:20 +00:00
|
|
|
|
type Arch struct {
|
|
|
|
|
data []models.PostArchive
|
2022-09-19 12:15:10 +00:00
|
|
|
|
mutex *sync.Mutex
|
2022-09-19 12:34:20 +00:00
|
|
|
|
setCacheFunc func() ([]models.PostArchive, error)
|
2022-09-19 12:15:10 +00:00
|
|
|
|
month time.Month
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 13:25:41 +00:00
|
|
|
|
func (c *Arch) getArchiveCache() []models.PostArchive {
|
2022-09-19 12:15:10 +00:00
|
|
|
|
l := len(c.data)
|
|
|
|
|
m := time.Now().Month()
|
|
|
|
|
if l > 0 && c.month != m || l < 1 {
|
|
|
|
|
r, err := c.setCacheFunc()
|
|
|
|
|
if err != nil {
|
2022-09-27 07:35:34 +00:00
|
|
|
|
logs.ErrPrintln(err, "set cache err[%s]")
|
2022-09-19 12:15:10 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
c.mutex.Lock()
|
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
c.month = m
|
|
|
|
|
c.data = r
|
|
|
|
|
}
|
|
|
|
|
return c.data
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 11:11:36 +00:00
|
|
|
|
type PostContext struct {
|
2022-09-27 07:35:34 +00:00
|
|
|
|
prev models.WpPosts
|
|
|
|
|
next models.WpPosts
|
2022-09-19 11:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 13:49:14 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 13:16:51 +00:00
|
|
|
|
func RecentComments(ctx context.Context) (r []models.WpComments) {
|
2022-09-27 07:35:34 +00:00
|
|
|
|
r, err := recentCommentsCaches.GetCache(ctx, time.Second)
|
|
|
|
|
logs.ErrPrintln(err, "get recent comment")
|
2022-09-20 13:16:51 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
func recentComments(...any) (r []models.WpComments, err error) {
|
|
|
|
|
return models.Find[models.WpComments](models.SqlBuilder{
|
|
|
|
|
{"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"},
|
|
|
|
|
}, 5)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 07:35:34 +00:00
|
|
|
|
func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next models.WpPosts, err error) {
|
|
|
|
|
postCtx, err := postContextCache.GetCache(ctx, id, time.Second, date)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return models.WpPosts{}, models.WpPosts{}, err
|
2022-09-19 11:11:36 +00:00
|
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
|
prev = postCtx.prev
|
|
|
|
|
next = postCtx.next
|
2022-09-19 11:11:36 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 07:35:34 +00:00
|
|
|
|
func getPostContext(arg ...any) (r PostContext, err error) {
|
|
|
|
|
t := arg[0].(time.Time)
|
|
|
|
|
next, err := models.FirstOne[models.WpPosts](models.SqlBuilder{
|
2022-09-19 11:11:36 +00:00
|
|
|
|
{"post_date", ">", t.Format("2006-01-02 15:04:05")},
|
2022-09-19 12:24:14 +00:00
|
|
|
|
{"post_status", "in", ""},
|
2022-09-19 11:11:36 +00:00
|
|
|
|
{"post_type", "post"},
|
2022-09-20 14:37:31 +00:00
|
|
|
|
}, "ID,post_title,post_password", nil, []any{"publish", "private"})
|
2022-09-27 07:35:34 +00:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
prev, err := models.FirstOne[models.WpPosts](models.SqlBuilder{
|
2022-09-19 11:11:36 +00:00
|
|
|
|
{"post_date", "<", t.Format("2006-01-02 15:04:05")},
|
2022-09-19 12:24:14 +00:00
|
|
|
|
{"post_status", "in", ""},
|
2022-09-19 11:11:36 +00:00
|
|
|
|
{"post_type", "post"},
|
2022-09-20 14:37:31 +00:00
|
|
|
|
}, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []any{"publish", "private"})
|
2022-09-20 08:55:13 +00:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2022-09-18 14:06:27 +00:00
|
|
|
|
}
|
2022-09-27 07:35:34 +00:00
|
|
|
|
r = PostContext{
|
|
|
|
|
prev: prev,
|
|
|
|
|
next: next,
|
2022-09-18 14:06:27 +00:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 09:39:00 +00:00
|
|
|
|
func archives() ([]models.PostArchive, error) {
|
|
|
|
|
return models.Find[models.PostArchive](models.SqlBuilder{
|
2022-09-18 04:34:48 +00:00
|
|
|
|
{"post_type", "post"}, {"post_status", "publish"},
|
|
|
|
|
}, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 09:39:00 +00:00
|
|
|
|
func Archives() (r []models.PostArchive) {
|
2022-09-26 13:25:41 +00:00
|
|
|
|
return archivesCaches.getArchiveCache()
|
2022-09-19 09:39:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 04:00:09 +00:00
|
|
|
|
func Categories(ctx context.Context) []models.WpTermsMy {
|
2022-09-27 07:35:34 +00:00
|
|
|
|
r, err := categoryCaches.GetCache(ctx, time.Second)
|
|
|
|
|
logs.ErrPrintln(err, "get category ")
|
2022-09-20 08:11:20 +00:00
|
|
|
|
return r
|
2022-09-19 09:39:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 04:00:09 +00:00
|
|
|
|
func categories(...any) (terms []models.WpTermsMy, err error) {
|
2022-09-20 14:37:31 +00:00
|
|
|
|
var in = []any{"category"}
|
2022-09-18 04:34:48 +00:00
|
|
|
|
terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{
|
|
|
|
|
{"tt.count", ">", "0", "int"},
|
|
|
|
|
{"tt.taxonomy", "in", ""},
|
|
|
|
|
}, "t.term_id", "", models.SqlBuilder{
|
|
|
|
|
{"t.name", "asc"},
|
|
|
|
|
}, models.SqlBuilder{
|
|
|
|
|
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
|
|
|
|
|
}, 0, in)
|
|
|
|
|
for i := 0; i < len(terms); i++ {
|
|
|
|
|
if v, ok := models.Terms[terms[i].WpTerms.TermId]; ok {
|
|
|
|
|
terms[i].WpTerms = v
|
|
|
|
|
}
|
|
|
|
|
if v, ok := models.TermTaxonomy[terms[i].WpTerms.TermId]; ok {
|
|
|
|
|
terms[i].WpTermTaxonomy = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 04:00:09 +00:00
|
|
|
|
func RecentPosts(ctx context.Context) (r []models.WpPosts) {
|
2022-09-27 07:35:34 +00:00
|
|
|
|
r, err := recentPostsCaches.GetCache(ctx, time.Second)
|
|
|
|
|
logs.ErrPrintln(err, "get recent post")
|
2022-09-20 08:11:20 +00:00
|
|
|
|
return
|
2022-09-19 09:39:00 +00:00
|
|
|
|
}
|
2022-09-20 04:00:09 +00:00
|
|
|
|
func recentPosts(...any) (r []models.WpPosts, err error) {
|
2022-09-18 04:34:48 +00:00
|
|
|
|
r, err = models.Find[models.WpPosts](models.SqlBuilder{{
|
|
|
|
|
"post_type", "post",
|
|
|
|
|
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5)
|
2022-09-18 14:06:27 +00:00
|
|
|
|
for i, post := range r {
|
|
|
|
|
if post.PostPassword != "" {
|
|
|
|
|
PasswordProjectTitle(&r[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-18 04:34:48 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 14:06:27 +00:00
|
|
|
|
func PasswordProjectTitle(post *models.WpPosts) {
|
|
|
|
|
if post.PostPassword != "" {
|
2022-09-18 04:34:48 +00:00
|
|
|
|
post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle)
|
|
|
|
|
}
|
2022-09-18 14:06:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PasswdProjectContent(post *models.WpPosts) {
|
2022-09-18 04:34:48 +00:00
|
|
|
|
if post.PostContent != "" {
|
|
|
|
|
format := `
|
|
|
|
|
<form action="/login" class="post-password-form" method="post">
|
|
|
|
|
<p>此内容受密码保护。如需查阅,请在下列字段中输入您的密码。</p>
|
|
|
|
|
<p><label for="pwbox-%d">密码: <input name="post_password" id="pwbox-%d" type="password" size="20"></label> <input type="submit" name="Submit" value="提交"></p>
|
|
|
|
|
</form>`
|
|
|
|
|
post.PostContent = fmt.Sprintf(format, post.Id, post.Id)
|
|
|
|
|
}
|
|
|
|
|
}
|