package actions import ( "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/helper" cache2 "github/fthvgb1/wp-go/internal/pkg/cache" "github/fthvgb1/wp-go/internal/pkg/logs" "github/fthvgb1/wp-go/internal/pkg/models" "github/fthvgb1/wp-go/internal/plugins" "github/fthvgb1/wp-go/internal/wpconfig" "math/rand" "net/http" "net/url" "sort" "strconv" "strings" "time" ) type detailHandler struct { *gin.Context } func Detail(c *gin.Context) { var err error hh := detailHandler{ c, } recent := cache2.RecentPosts(c, 5) archive := cache2.Archives(c) categoryItems := cache2.Categories(c) recentComments := cache2.RecentComments(c, 5) var h = gin.H{ "title": wpconfig.Options.Value("blogname"), "options": wpconfig.Options, "recentPosts": recent, "archives": archive, "categories": categoryItems, "recentComments": recentComments, } isApproveComment := false defer func() { status := http.StatusOK if err != nil { status = http.StatusInternalServerError c.Error(err) return } if isApproveComment == true { return } c.HTML(status, helper.StrJoin(getTemplateName(), "/posts/detail.gohtml"), h) }() id := c.Param("id") Id := 0 if id != "" { Id, err = strconv.Atoi(id) if err != nil { return } } ID := uint64(Id) maxId, err := cache2.GetMaxPostId(c) logs.ErrPrintln(err, "get max post id") if ID > maxId || err != nil { return } post, err := cache2.GetPostById(c, ID) if post.Id == 0 || err != nil { return } pw := sessions.Default(c).Get("post_password") showComment := false if post.CommentCount > 0 || post.CommentStatus == "open" { showComment = true } user := cache2.GetUserById(c, post.PostAuthor) plugins.PasswordProjectTitle(&post) if post.PostPassword != "" && pw != post.PostPassword { plugins.PasswdProjectContent(&post) showComment = false } else if s, ok := commentCache.Get(c.Request.URL.RawQuery); ok && s != "" && (post.PostPassword == "" || post.PostPassword != "" && pw == post.PostPassword) { c.Writer.WriteHeader(http.StatusOK) c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") _, err = c.Writer.WriteString(s) isApproveComment = true return } plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post) comments, err := cache2.PostComments(c, post.Id) logs.ErrPrintln(err, "get post comment", post.Id) commentss := treeComments(comments) prev, next, err := cache2.GetContextPost(c, post.Id, post.PostDate) logs.ErrPrintln(err, "get pre and next post", post.Id, post.PostDate) h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.Options.Value("blogname")) h["post"] = post h["showComment"] = showComment h["prev"] = prev depth := wpconfig.Options.Value("thread_comments_depth") d, err := strconv.Atoi(depth) if err != nil { logs.ErrPrintln(err, "get comment depth") d = 5 } h["comments"] = hh.formatComment(commentss, 1, d) h["next"] = next h["user"] = user } type Comment struct { models.Comments Children []*Comment } type Comments []*Comment func (c Comments) Len() int { return len(c) } func (c Comments) Less(i, j int) bool { return c[i].CommentId < c[j].CommentId } func (c Comments) Swap(i, j int) { c[i], c[j] = c[j], c[i] } func (d detailHandler) formatComment(comments Comments, depth, maxDepth int) (html string) { s := strings.Builder{} if depth > maxDepth { comments = findComments(comments) } sort.Sort(comments) for i, comment := range comments { eo := "even" if (i+1)%2 == 0 { eo = "odd" } p := "" fl := false if len(comment.Children) > 0 && depth < maxDepth+1 { p = "parent" fl = true } s.WriteString(d.formatLi(comment.Comments, depth, eo, p)) if fl { depth++ s.WriteString(`
{{CommentContent}}