wp-go/internal/actions/detail.go

107 lines
2.9 KiB
Go
Raw Normal View History

2022-09-18 04:34:48 +00:00
package actions
2022-09-18 14:06:27 +00:00
import (
"fmt"
2023-01-19 13:02:39 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins"
2023-01-20 11:58:45 +00:00
"github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig"
2022-09-18 14:06:27 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
2022-09-18 04:34:48 +00:00
2022-09-22 09:21:41 +00:00
type detailHandler struct {
*gin.Context
}
2022-09-18 04:34:48 +00:00
func Detail(c *gin.Context) {
2022-09-18 14:06:27 +00:00
var err error
2023-01-19 13:02:39 +00:00
recent := cache.RecentPosts(c, 5)
archive := cache.Archives(c)
categoryItems := cache.Categories(c)
recentComments := cache.RecentComments(c, 5)
2023-01-23 15:42:37 +00:00
var ginH = gin.H{
2022-11-17 08:29:39 +00:00
"title": wpconfig.Options.Value("blogname"),
"options": wpconfig.Options,
2022-09-21 13:49:14 +00:00
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"recentComments": recentComments,
2022-09-19 11:11:36 +00:00
}
2022-11-08 09:41:01 +00:00
isApproveComment := false
2023-01-23 15:42:37 +00:00
status := plugins.Ok
2022-09-18 14:06:27 +00:00
defer func() {
2023-01-23 15:42:37 +00:00
code := http.StatusOK
2022-09-18 14:06:27 +00:00
if err != nil {
2023-01-23 15:42:37 +00:00
code = http.StatusNotFound
2022-09-18 14:06:27 +00:00
c.Error(err)
2023-01-23 15:42:37 +00:00
status = plugins.Error
2022-12-28 08:40:18 +00:00
return
2022-09-18 14:06:27 +00:00
}
2022-11-08 09:41:01 +00:00
if isApproveComment == true {
return
}
2023-01-23 15:42:37 +00:00
t := theme.GetTemplateName()
theme.Hook(t, code, c, ginH, plugins.Detail, status)
2022-09-18 14:06:27 +00:00
}()
2022-09-19 11:11:36 +00:00
id := c.Param("id")
2022-09-18 14:06:27 +00:00
Id := 0
if id != "" {
Id, err = strconv.Atoi(id)
if err != nil {
return
}
}
ID := uint64(Id)
2023-01-19 13:02:39 +00:00
maxId, err := cache.GetMaxPostId(c)
2022-10-04 03:13:14 +00:00
logs.ErrPrintln(err, "get max post id")
if ID > maxId || err != nil {
return
}
2023-01-19 13:02:39 +00:00
post, err := cache.GetPostById(c, ID)
2023-01-20 10:10:13 +00:00
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
return
2022-09-18 14:06:27 +00:00
}
pw := sessions.Default(c).Get("post_password")
2022-09-19 13:31:46 +00:00
showComment := false
if post.CommentCount > 0 || post.CommentStatus == "open" {
showComment = true
}
2023-01-19 13:02:39 +00:00
user := cache.GetUserById(c, post.PostAuthor)
2023-01-12 12:42:16 +00:00
plugins.PasswordProjectTitle(&post)
2022-09-18 14:06:27 +00:00
if post.PostPassword != "" && pw != post.PostPassword {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&post)
2022-09-18 14:06:27 +00:00
showComment = false
2023-01-19 13:02:39 +00:00
} else if s, ok := cache.NewCommentCache().Get(c.Request.URL.RawQuery); ok && s != "" && (post.PostPassword == "" || post.PostPassword != "" && pw == post.PostPassword) {
2022-11-08 09:41:01 +00:00
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
_, err = c.Writer.WriteString(s)
isApproveComment = true
return
2022-09-18 14:06:27 +00:00
}
2022-09-23 13:46:34 +00:00
plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post)
2023-01-19 13:02:39 +00:00
comments, err := cache.PostComments(c, post.Id)
2022-09-27 07:35:34 +00:00
logs.ErrPrintln(err, "get post comment", post.Id)
ginH["comments"] = comments
2023-01-19 13:02:39 +00:00
prev, next, err := cache.GetContextPost(c, post.Id, post.PostDate)
2022-09-27 07:35:34 +00:00
logs.ErrPrintln(err, "get pre and next post", post.Id, post.PostDate)
2023-01-23 15:42:37 +00:00
ginH["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.Options.Value("blogname"))
ginH["post"] = post
ginH["showComment"] = showComment
ginH["prev"] = prev
2022-11-17 08:29:39 +00:00
depth := wpconfig.Options.Value("thread_comments_depth")
2022-10-19 14:41:44 +00:00
d, err := strconv.Atoi(depth)
if err != nil {
2023-01-23 15:42:37 +00:00
logs.ErrPrintln(err, "get comment depth ", depth)
2022-10-19 14:41:44 +00:00
d = 5
}
ginH["maxDep"] = d
2023-01-23 15:42:37 +00:00
ginH["next"] = next
ginH["user"] = user
2023-01-25 18:26:36 +00:00
ginH["scene"] = plugins.Detail
2022-09-18 04:34:48 +00:00
}