2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-11 15:51:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-27 14:58:35 +00:00
|
|
|
"errors"
|
2023-02-11 15:51:07 +00:00
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins/wpposts"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-02-11 15:51:07 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/plugin/pagination"
|
2024-01-10 15:50:23 +00:00
|
|
|
"strings"
|
2023-12-20 14:30:55 +00:00
|
|
|
"time"
|
2023-02-11 15:51:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DetailHandle struct {
|
|
|
|
*Handle
|
2023-12-23 12:17:42 +00:00
|
|
|
CommentRender plugins.CommentHtml
|
|
|
|
Comments []uint64
|
|
|
|
Page int
|
|
|
|
Limit int
|
|
|
|
Post models.Posts
|
|
|
|
CommentPageEle pagination.Render
|
|
|
|
TotalRaw int
|
2024-01-13 13:15:54 +00:00
|
|
|
TotalPage int
|
2024-03-18 11:12:16 +00:00
|
|
|
CommentStep int
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDetailHandle(handle *Handle) *DetailHandle {
|
2024-03-18 11:12:16 +00:00
|
|
|
return &DetailHandle{
|
|
|
|
Handle: handle,
|
|
|
|
Page: 1,
|
|
|
|
Limit: 5,
|
|
|
|
CommentStep: 1,
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) BuildDetailData() (err error) {
|
2023-02-28 15:38:23 +00:00
|
|
|
d.ginH["title"] = wpconfig.GetOption("blogname")
|
2023-02-11 15:51:07 +00:00
|
|
|
err = d.CheckAndGetPost()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
d.CommentData()
|
2023-02-11 15:51:07 +00:00
|
|
|
d.ContextPost()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) CheckAndGetPost() (err error) {
|
|
|
|
id := str.ToInteger[uint64](d.C.Param("id"), 0)
|
|
|
|
maxId, err := cache.GetMaxPostId(d.C)
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.IfError(err, "get max post id")
|
2023-02-27 14:58:35 +00:00
|
|
|
if id > maxId || id <= 0 {
|
|
|
|
d.Stats = constraints.ParamError
|
|
|
|
err = errors.New("无效的文档id")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-02-11 15:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
post, err := cache.GetPostById(d.C, id)
|
|
|
|
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
2023-02-27 14:58:35 +00:00
|
|
|
d.Stats = constraints.Error404
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.IfError(err, "获取id失败")
|
2023-02-27 14:58:35 +00:00
|
|
|
err = errors.New(str.Join("无效的文档id "))
|
2023-02-11 15:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Post = post
|
2023-02-28 15:38:23 +00:00
|
|
|
d.ginH["user"] = cache.GetUserById(d.C, post.PostAuthor)
|
|
|
|
d.ginH["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.GetOption("blogname"))
|
2023-02-11 15:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) PasswordProject() {
|
|
|
|
if d.Post.PostPassword != "" {
|
2023-03-02 15:49:28 +00:00
|
|
|
wpposts.PasswordProjectTitle(&d.Post)
|
2023-05-03 15:57:49 +00:00
|
|
|
if d.GetPassword() != d.Post.PostPassword {
|
2023-03-02 15:49:28 +00:00
|
|
|
wpposts.PasswdProjectContent(&d.Post)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
func (h *Handle) GetDetailHandle() *DetailHandle {
|
|
|
|
v, ok := h.C.Get("detailHandle")
|
|
|
|
if !ok {
|
|
|
|
vv := NewDetailHandle(h)
|
|
|
|
h.C.Set("detailHandle", vv)
|
|
|
|
return vv
|
|
|
|
}
|
|
|
|
return v.(*DetailHandle)
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
func (d *DetailHandle) CommentData() {
|
2023-12-20 14:30:55 +00:00
|
|
|
d.ginH["totalCommentNum"] = 0
|
|
|
|
d.ginH["totalCommentPage"] = 1
|
|
|
|
d.ginH["commentPageNav"] = ""
|
2024-01-10 15:50:23 +00:00
|
|
|
order := wpconfig.GetOption("comment_order")
|
|
|
|
d.ginH["commentOrder"] = order
|
2023-12-20 14:30:55 +00:00
|
|
|
d.Limit = str.ToInteger(wpconfig.GetOption("comments_per_page"), 5)
|
2024-01-10 15:50:23 +00:00
|
|
|
pageComments := wpconfig.GetOption("page_comments")
|
|
|
|
num, err := cachemanager.GetBy[int]("commentNumber", d.C, d.Post.Id, time.Second)
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil {
|
|
|
|
d.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-10 15:50:23 +00:00
|
|
|
if num < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
topNum, err := cachemanager.GetBy[int]("postTopCommentsNum", d.C, d.Post.Id, time.Second)
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil {
|
|
|
|
d.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
d.TotalPage = number.DivideCeil(topNum, d.Limit)
|
2024-01-10 15:50:23 +00:00
|
|
|
if !strings.Contains(d.C.Request.URL.Path, "comment-page") {
|
|
|
|
defaultCommentsPage := wpconfig.GetOption("default_comments_page")
|
|
|
|
if order == "desc" && defaultCommentsPage == "oldest" || order == "asc" && defaultCommentsPage == "newest" {
|
2024-01-13 13:15:54 +00:00
|
|
|
d.C.AddParam("page", number.IntToString(d.TotalPage))
|
2024-01-10 15:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Page = str.ToInteger(d.C.Param("page"), 1)
|
|
|
|
d.ginH["currentPage"] = d.Page
|
|
|
|
var key string
|
|
|
|
if pageComments != "1" {
|
|
|
|
key = number.IntToString(d.Post.Id)
|
2024-01-13 13:15:54 +00:00
|
|
|
d.Limit = 0
|
2024-01-10 15:50:23 +00:00
|
|
|
} else {
|
|
|
|
key = fmt.Sprintf("%d-%d-%d", d.Post.Id, d.Page, d.Limit)
|
|
|
|
}
|
|
|
|
d.ginH["page_comments"] = pageComments
|
2024-01-13 13:15:54 +00:00
|
|
|
d.ginH["totalCommentPage"] = d.TotalPage
|
|
|
|
if d.TotalPage < d.Page {
|
2024-01-10 15:50:23 +00:00
|
|
|
d.SetErr(errors.New("curren page above total page"))
|
|
|
|
return
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
data, err := cache.PostTopLevelCommentIds(d.C, d.Post.Id, d.Page, d.Limit, topNum, order, key)
|
2024-01-10 15:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
d.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.TotalRaw = topNum
|
2023-12-20 14:30:55 +00:00
|
|
|
d.ginH["totalCommentNum"] = num
|
2024-01-10 15:50:23 +00:00
|
|
|
d.Comments = data
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) RenderComment() {
|
2023-02-23 10:21:51 +00:00
|
|
|
if d.CommentRender == nil {
|
|
|
|
d.CommentRender = plugins.CommentRender()
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
ableComment := true
|
|
|
|
if d.Post.CommentStatus != "open" ||
|
2023-05-03 15:57:49 +00:00
|
|
|
(d.Post.PostPassword != "" && d.GetPassword() != d.Post.PostPassword) {
|
2023-02-11 15:51:07 +00:00
|
|
|
ableComment = false
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
d.ginH["showComment"] = ableComment
|
2023-12-20 14:30:55 +00:00
|
|
|
d.ginH["comments"] = ""
|
|
|
|
if len(d.Comments) < 0 || !ableComment {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
d.ginH["comments"], err = RenderComment(d.C, d.Page, d.CommentRender, d.Comments, 2*time.Second, d.IsHttps())
|
|
|
|
if err != nil {
|
|
|
|
d.SetErr(err)
|
|
|
|
return
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-12-23 12:17:42 +00:00
|
|
|
if d.CommentPageEle == nil {
|
|
|
|
d.CommentPageEle = plugins.TwentyFifteenCommentPagination()
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
if wpconfig.GetOption("page_comments") == "1" && d.TotalPage > 1 {
|
2024-03-18 11:12:16 +00:00
|
|
|
d.ginH["commentPageNav"] = pagination.Paginate(d.CommentPageEle, d.TotalRaw, d.Limit, d.Page, d.CommentStep, *d.C.Request.URL, d.IsHttps())
|
2024-01-10 15:50:23 +00:00
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) ContextPost() {
|
|
|
|
prev, next, err := cache.GetContextPost(d.C, d.Post.Id, d.Post.PostDate)
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.IfError(err, "get pre and next post", d.Post.Id, d.Post.PostDate)
|
2023-02-28 15:38:23 +00:00
|
|
|
d.ginH["next"] = next
|
|
|
|
d.ginH["prev"] = prev
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-02-14 15:28:45 +00:00
|
|
|
|
2023-04-04 15:02:54 +00:00
|
|
|
func DetailRender(h *Handle) {
|
|
|
|
if h.Stats != constraints.Ok {
|
|
|
|
return
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
d := h.GetDetailHandle()
|
2023-04-04 15:02:54 +00:00
|
|
|
d.PasswordProject()
|
|
|
|
d.RenderComment()
|
|
|
|
d.ginH["post"] = d.Post
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:45:20 +00:00
|
|
|
func Detail(h *Handle) {
|
2024-01-18 15:29:50 +00:00
|
|
|
d := h.GetDetailHandle()
|
|
|
|
err := d.BuildDetailData()
|
2023-05-05 12:45:20 +00:00
|
|
|
if err != nil {
|
2024-01-18 15:29:50 +00:00
|
|
|
d.SetErr(err)
|
2023-05-05 12:45:20 +00:00
|
|
|
}
|
|
|
|
h.SetData("scene", h.Scene())
|
2023-02-14 15:28:45 +00:00
|
|
|
}
|
2023-04-29 14:05:24 +00:00
|
|
|
|
|
|
|
func ReplyCommentJs(h *Handle) {
|
2023-04-30 13:17:33 +00:00
|
|
|
h.PushFooterScript(constraints.Detail, NewComponent("comment-reply.js", "", false, 10, func(h *Handle) string {
|
2023-04-29 14:05:24 +00:00
|
|
|
reply := ""
|
2024-01-18 15:29:50 +00:00
|
|
|
if h.GetDetailHandle().Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
|
2023-04-29 14:05:24 +00:00
|
|
|
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
|
|
|
|
}
|
|
|
|
return reply
|
|
|
|
}))
|
|
|
|
}
|