2023-02-11 15:51:07 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2023-02-27 14:58:35 +00:00
|
|
|
"errors"
|
2023-02-11 15:51:07 +00:00
|
|
|
"fmt"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
2023-02-14 15:28:45 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
2023-02-11 15:51:07 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DetailHandle struct {
|
|
|
|
*Handle
|
|
|
|
CommentRender plugins.CommentHtml
|
|
|
|
Comments []models.Comments
|
|
|
|
Post models.Posts
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDetailHandle(handle *Handle) *DetailHandle {
|
|
|
|
return &DetailHandle{Handle: handle}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) BuildDetailData() (err error) {
|
2023-02-20 17:07:32 +00:00
|
|
|
d.GinH["title"] = wpconfig.GetOption("blogname")
|
2023-02-11 15:51:07 +00:00
|
|
|
err = d.CheckAndGetPost()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.Comment()
|
|
|
|
d.ContextPost()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) CheckAndGetPost() (err error) {
|
|
|
|
id := str.ToInteger[uint64](d.C.Param("id"), 0)
|
|
|
|
maxId, err := cache.GetMaxPostId(d.C)
|
|
|
|
logs.ErrPrintln(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")
|
|
|
|
d.Class = append(d.Class, "error404")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
logs.ErrPrintln(err, "获取id失败")
|
|
|
|
err = errors.New(str.Join("无效的文档id "))
|
2023-02-11 15:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Post = post
|
|
|
|
d.GinH["user"] = cache.GetUserById(d.C, post.PostAuthor)
|
2023-02-20 17:07:32 +00:00
|
|
|
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 != "" {
|
|
|
|
plugins.PasswordProjectTitle(&d.Post)
|
|
|
|
if d.Password != d.Post.PostPassword {
|
|
|
|
plugins.PasswdProjectContent(&d.Post)
|
|
|
|
}
|
|
|
|
d.GinH["post"] = d.Post
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (d *DetailHandle) Comment() {
|
|
|
|
comments, err := cache.PostComments(d.C, d.Post.Id)
|
|
|
|
logs.ErrPrintln(err, "get d.Post comment", d.Post.Id)
|
|
|
|
d.GinH["comments"] = comments
|
|
|
|
d.Comments = comments
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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" ||
|
|
|
|
(d.Post.PostPassword != "" && d.Password != d.Post.PostPassword) {
|
|
|
|
ableComment = false
|
|
|
|
}
|
|
|
|
d.GinH["showComment"] = ableComment
|
|
|
|
if len(d.Comments) > 0 && ableComment {
|
2023-02-20 17:07:32 +00:00
|
|
|
dep := str.ToInteger(wpconfig.GetOption("thread_comments_depth"), 5)
|
2023-02-11 15:51:07 +00:00
|
|
|
d.GinH["comments"] = plugins.FormatComments(d.C, d.CommentRender, d.Comments, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) ContextPost() {
|
|
|
|
prev, next, err := cache.GetContextPost(d.C, d.Post.Id, d.Post.PostDate)
|
|
|
|
logs.ErrPrintln(err, "get pre and next post", d.Post.Id, d.Post.PostDate)
|
|
|
|
d.GinH["next"] = next
|
|
|
|
d.GinH["prev"] = prev
|
|
|
|
}
|
2023-02-14 15:28:45 +00:00
|
|
|
|
|
|
|
func (d *DetailHandle) Render() {
|
|
|
|
d.PasswordProject()
|
|
|
|
d.RenderComment()
|
2023-02-24 11:34:19 +00:00
|
|
|
d.GinH["post"] = d.Post
|
2023-02-23 10:21:51 +00:00
|
|
|
d.Handle.Render()
|
2023-02-14 15:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DetailHandle) Details() {
|
2023-02-27 14:58:35 +00:00
|
|
|
_ = d.BuildDetailData()
|
2023-02-14 15:28:45 +00:00
|
|
|
d.Render()
|
|
|
|
}
|