This commit is contained in:
xing 2022-09-21 21:49:14 +08:00
parent 721e7e46a3
commit 00b09ac1f9
9 changed files with 260 additions and 511 deletions

View File

@ -8,6 +8,7 @@ import (
"github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/vars"
"log"
"strconv"
"strings"
"sync"
"time"
@ -21,6 +22,7 @@ var categoryCaches *cache.SliceCache[models.WpTermsMy]
var recentPostsCaches *cache.SliceCache[models.WpPosts]
var monthCaches *cache.MapCache[string, []models.WpPosts]
var recentCommentsCaches *cache.SliceCache[models.WpComments]
var postCommentCaches *cache.MapCache[uint64, []models.WpComments]
func InitCache() {
archivesCaches = &Arch{
@ -31,6 +33,7 @@ func InitCache() {
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute)
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5)
}
type Arch struct {
@ -68,6 +71,21 @@ func GetMonthPost(ctx context.Context, year, month string) ([]models.WpPosts, er
return monthCaches.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
}
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)
}
func RecentComments(ctx context.Context) (r []models.WpComments) {
r, _ = recentCommentsCaches.GetCache(ctx, time.Second)
return

View File

@ -8,7 +8,9 @@ import (
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/models"
"net/http"
"sort"
"strconv"
"strings"
)
func Detail(c *gin.Context) {
@ -17,14 +19,14 @@ func Detail(c *gin.Context) {
recent := common.RecentPosts(ctx)
archive := common.Archives()
categoryItems := common.Categories(ctx)
comments := common.RecentComments(ctx)
recentComments := common.RecentComments(ctx)
var h = gin.H{
"title": models.Options["blogname"],
"options": models.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"comments": comments,
"title": models.Options["blogname"],
"options": models.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"recentComments": recentComments,
}
defer func() {
c.HTML(http.StatusOK, "posts/detail.gohtml", h)
@ -63,10 +65,164 @@ func Detail(c *gin.Context) {
common.PasswdProjectContent(&post)
showComment = false
}
comments, err := common.PostComments(ctx, post.Id)
commentss := treeComments(comments)
prev, next, err := common.GetContextPost(post.Id, post.PostDate)
h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
h["post"] = post
h["showComment"] = showComment
h["prev"] = prev
h["comments"] = formatComment(commentss, 1)
h["next"] = next
}
type Comment struct {
models.WpComments
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 formatComment(comments Comments, depth int) (html string) {
s := strings.Builder{}
if depth > 5 {
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 < 6 {
p = "parent"
fl = true
}
s.WriteString(formatLi(comment.WpComments, depth, eo, p))
if fl {
depth++
s.WriteString(`<ol class="children">`)
s.WriteString(formatComment(comment.Children, depth))
s.WriteString(`</ol>`)
}
s.WriteString("</li><!-- #comment-## -->")
i++
if i >= len(comments) {
break
}
}
html = s.String()
return
}
func findComments(comments Comments) Comments {
var r Comments
for _, comment := range comments {
tmp := comment
tmp.Children = nil
r = append(r, tmp)
if len(comment.Children) > 0 {
t := findComments(comment.Children)
r = append(r, t...)
}
}
return r
}
func treeComments(comments []models.WpComments) Comments {
var r = map[uint64]*Comment{
0: {
WpComments: models.WpComments{},
},
}
var top []*Comment
for _, comment := range comments {
c := Comment{
WpComments: comment,
Children: []*Comment{},
}
r[comment.CommentId] = &c
if comment.CommentParent == 0 {
top = append(top, &c)
}
}
for id, son := range r {
if id == son.CommentParent {
continue
}
parent := r[son.CommentParent]
parent.Children = append(parent.Children, son)
}
return top
}
func formatLi(comments models.WpComments, d int, eo, parent string) string {
li := `
<li id="comment-{{CommentId}}" class="comment {{eo}} thread-even depth-{{Depth}} {{parent}}">
<article id="div-comment-{{CommentId}}" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<img alt=""
src="{{Gravatar}}"
srcset="{{Gravatar}} 2x"
class="avatar avatar-56 photo" height="56" width="56" loading="lazy">
<b class="fn">
<a href="{{CommentAuthorUrl}}" rel="external nofollow ugc"
class="url">{{CommentAuthor}}</a>
</b>
<span class="says">说道</span></div><!-- .comment-author -->
<div class="comment-metadata">
<a href="/p/{{PostId}}#comment-{{CommentId}}">
<time datetime="{{CommentDateGmt}}">{{CommentDate}}</time>
</a></div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<div class="comment-content">
<p>{{CommentContent}}</p>
</div><!-- .comment-content -->
<div class="reply">
<a rel="nofollow" class="comment-reply-link"
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"
data-replyto="回复给{{CommentAuthor}}"
aria-label="回复给{{CommentAuthor}}">回复</a>
</div>
</article><!-- .comment-body -->
`
for k, v := range map[string]string{
"{{CommentId}}": strconv.FormatUint(comments.CommentId, 10),
"{{Depth}}": strconv.Itoa(d),
"{{Gravatar}}": "http://1.gravatar.com/avatar/d7a973c7dab26985da5f961be7b74480?s=56&d=mm&r=g",
"{{CommentAuthorUrl}}": comments.CommentAuthorUrl,
"{{CommentAuthor}}": comments.CommentAuthor,
"{{PostId}}": strconv.FormatUint(comments.CommentPostId, 10),
"{{CommentDateGmt}}": comments.CommentDateGmt.String(),
"{{CommentDate}}": comments.CommentDate.Format("2006-01-02 15:04"),
"{{CommentContent}}": comments.CommentContent,
"{{eo}}": eo,
"{{parent}}": parent,
} {
li = strings.Replace(li, k, v, -1)
}
return li
}

View File

@ -153,16 +153,16 @@ func Index(c *gin.Context) {
archive := common.Archives()
recent := common.RecentPosts(ctx)
categoryItems := common.Categories(ctx)
comments := common.RecentComments(ctx)
recentComments := common.RecentComments(ctx)
ginH := gin.H{
"options": models.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"search": h.search,
"header": h.header,
"title": h.getTitle(),
"comments": comments,
"options": models.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"search": h.search,
"header": h.header,
"title": h.getTitle(),
"recentComments": recentComments,
}
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status)
defer func() {

View File

@ -39,7 +39,7 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
}
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in [][]any) (t bool) {
if ss[1] == "in" && len(in) > 0 {
if helper.IsContainInArr(ss[1], []string{"in", "not in"}) && len(in) > 0 {
s.WriteString(" (")
for _, p := range in[*c] {
s.WriteString("?,")

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -25,7 +25,7 @@
<aside id="recent-comments-2" class="widget widget_recent_comments"><h2 class="widget-title">近期评论</h2>
<nav aria-label="近期评论">
<ul id="recentcomments">
{{ range $i,$v := .comments}}
{{ range $i,$v := .recentComments}}
<li class="recentcomments">
<span class="comment-author-link">{{$v.CommentAuthor}}</span>发表在《<a class="wp-block-latest-comments__comment-link" href="/p/{{$v.CommentPostId}}#comment-{{$v.CommentId}}">{{$v.PostTitle}}</a>》
</li>

View File

@ -1,4 +1,5 @@
{{template "layout/base" .}}
{{define "content"}}
{{ if .post.PostContent}}
<div id="primary" class="content-area">
@ -44,43 +45,10 @@
<div id="comments" class="comments-area">
{{ if gt .post.CommentCount 0}}
<h2 class="comments-title">《{{.post.PostTitle}}》上有{{.post.CommentCount}}条评论 </h2>
<ol class="comment-list">
<li id="comment-1" class="comment even thread-even depth-1">
<article id="div-comment-1" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<img alt=""
src="http://1.gravatar.com/avatar/d7a973c7dab26985da5f961be7b74480?s=56&amp;d=mm&amp;r=g"
srcset="http://1.gravatar.com/avatar/d7a973c7dab26985da5f961be7b74480?s=112&amp;d=mm&amp;r=g 2x"
class="avatar avatar-56 photo" height="56" width="56" loading="lazy">
<b class="fn">
<a href="https://cn.wordpress.org/" rel="external nofollow ugc"
class="url">一位WordPress评论者</a>
</b>
<span class="says">说道:</span></div><!-- .comment-author -->
<ol class="comment-list">
{{.comments|unescaped}}
</ol>
<div class="comment-metadata">
<a href="/p/1#comment-1">
<time datetime="2022-08-19T09:26:37+08:00">2022年 8月 19日 上午9:26</time>
</a></div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<div class="comment-content">
<p>您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 <a
href="https://cn.gravatar.com/">Gravatar</a>。</p>
</div><!-- .comment-content -->
<div class="reply">
<a rel="nofollow" class="comment-reply-link"
href="/p/1?replytocom=1#respond" data-commentid="1" data-postid="1"
data-belowelement="div-comment-1" data-respondelement="respond"
data-replyto="回复给一位WordPress评论者"
aria-label="回复给一位WordPress评论者">回复</a>
</div>
</article><!-- .comment-body -->
</li><!-- #comment-## -->
</ol><!-- .comment-list -->
{{end}}
{{if eq .post.CommentStatus "open"}}
<div id="respond" class="comment-respond">
@ -156,3 +124,17 @@
{{template "layout/empty"}}
{{end }}
{{end}}
{{define "footer"}}
<style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<script src='/wp-content/themes/twentyfifteen/js/skip-link-focus-fix.js?ver=20141028' id='twentyfifteen-skip-link-focus-fix-js'></script>
<script src='/wp-includes/js/comment-reply.min.js?ver=6.0.2' id='comment-reply-js'></script>
<script id='twentyfifteen-script-js-extra'>
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
{{end}}

View File

@ -1,4 +1,7 @@
{{template "layout/base" .}}
{{define "head"}}
{{end}}
{{define "content" }}
{{if .posts}}
<div id="primary" class="content-area">
@ -70,3 +73,16 @@
{{end}}
{{end}}
{{define "footer"}}
<style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<script src='/wp-content/themes/twentyfifteen/js/skip-link-focus-fix.js?ver=20141028' id='twentyfifteen-skip-link-focus-fix-js'></script>
<script id='twentyfifteen-script-js-extra'>
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
{{end}}