评论头像

This commit is contained in:
xing 2022-09-22 17:21:41 +08:00
parent 0f3ea7ad0b
commit 4d6af5167e
4 changed files with 56 additions and 10 deletions

View File

@ -6,15 +6,26 @@ import (
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"math/rand"
"net/http" "net/http"
"net/url"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type detailHandler struct {
*gin.Context
}
func Detail(c *gin.Context) { func Detail(c *gin.Context) {
var err error var err error
hh := detailHandler{
c,
}
ctx := context.TODO() ctx := context.TODO()
recent := common.RecentPosts(ctx) recent := common.RecentPosts(ctx)
archive := common.Archives() archive := common.Archives()
@ -29,10 +40,12 @@ func Detail(c *gin.Context) {
"recentComments": recentComments, "recentComments": recentComments,
} }
defer func() { defer func() {
c.HTML(http.StatusOK, "posts/detail.gohtml", h) status := http.StatusOK
if err != nil { if err != nil {
status = http.StatusInternalServerError
c.Error(err) c.Error(err)
} }
c.HTML(status, "posts/detail.gohtml", h)
}() }()
id := c.Param("id") id := c.Param("id")
Id := 0 Id := 0
@ -72,7 +85,7 @@ func Detail(c *gin.Context) {
h["post"] = post h["post"] = post
h["showComment"] = showComment h["showComment"] = showComment
h["prev"] = prev h["prev"] = prev
h["comments"] = formatComment(commentss, 1, 5) h["comments"] = hh.formatComment(commentss, 1, 5)
h["next"] = next h["next"] = next
} }
@ -95,7 +108,7 @@ func (c Comments) Swap(i, j int) {
c[i], c[j] = c[j], c[i] c[i], c[j] = c[j], c[i]
} }
func formatComment(comments Comments, depth, maxDepth int) (html string) { func (d detailHandler) formatComment(comments Comments, depth, maxDepth int) (html string) {
s := strings.Builder{} s := strings.Builder{}
if depth > maxDepth { if depth > maxDepth {
comments = findComments(comments) comments = findComments(comments)
@ -112,11 +125,11 @@ func formatComment(comments Comments, depth, maxDepth int) (html string) {
p = "parent" p = "parent"
fl = true fl = true
} }
s.WriteString(formatLi(comment.WpComments, depth, eo, p)) s.WriteString(d.formatLi(comment.WpComments, depth, eo, p))
if fl { if fl {
depth++ depth++
s.WriteString(`<ol class="children">`) s.WriteString(`<ol class="children">`)
s.WriteString(formatComment(comment.Children, depth, maxDepth)) s.WriteString(d.formatComment(comment.Children, depth, maxDepth))
s.WriteString(`</ol>`) s.WriteString(`</ol>`)
} }
s.WriteString("</li><!-- #comment-## -->") s.WriteString("</li><!-- #comment-## -->")
@ -171,7 +184,7 @@ func treeComments(comments []models.WpComments) Comments {
return top return top
} }
func formatLi(comments models.WpComments, d int, eo, parent string) string { func (d detailHandler) formatLi(comments models.WpComments, depth int, eo, parent string) string {
li := ` li := `
<li id="comment-{{CommentId}}" class="comment {{eo}} thread-even depth-{{Depth}} {{parent}}"> <li id="comment-{{CommentId}}" class="comment {{eo}} thread-even depth-{{Depth}} {{parent}}">
<article id="div-comment-{{CommentId}}" class="comment-body"> <article id="div-comment-{{CommentId}}" class="comment-body">
@ -210,8 +223,8 @@ func formatLi(comments models.WpComments, d int, eo, parent string) string {
` `
for k, v := range map[string]string{ for k, v := range map[string]string{
"{{CommentId}}": strconv.FormatUint(comments.CommentId, 10), "{{CommentId}}": strconv.FormatUint(comments.CommentId, 10),
"{{Depth}}": strconv.Itoa(d), "{{Depth}}": strconv.Itoa(depth),
"{{Gravatar}}": "http://1.gravatar.com/avatar/d7a973c7dab26985da5f961be7b74480?s=56&d=mm&r=g", "{{Gravatar}}": gravatar(d.Context, comments.CommentAuthorEmail),
"{{CommentAuthorUrl}}": comments.CommentAuthorUrl, "{{CommentAuthorUrl}}": comments.CommentAuthorUrl,
"{{CommentAuthor}}": comments.CommentAuthor, "{{CommentAuthor}}": comments.CommentAuthor,
"{{PostId}}": strconv.FormatUint(comments.CommentPostId, 10), "{{PostId}}": strconv.FormatUint(comments.CommentPostId, 10),
@ -225,3 +238,25 @@ func formatLi(comments models.WpComments, d int, eo, parent string) string {
} }
return li return li
} }
func gravatar(c *gin.Context, email string) (u string) {
email = strings.Trim(email, " \t\n\r\000\x0B")
rand.Seed(time.Now().UnixNano())
num := rand.Intn(3)
h := ""
if email != "" {
h = helper.StringMd5(strings.ToLower(email))
num = int(h[0] % 3)
}
if c.Request.TLS != nil {
u = fmt.Sprintf("%s%s", "https://secure.gravatar.com/avatar/", h)
} else {
u = fmt.Sprintf("http://%d.gravatar.com/avatar/%s", num, h)
}
q := url.Values{}
q.Add("s", "112")
q.Add("d", "mm")
q.Add("r", "g")
u = fmt.Sprintf("%s?%s", u, q.Encode())
return
}

View File

@ -1,6 +1,9 @@
package helper package helper
import ( import (
"crypto/md5"
"fmt"
"io"
"reflect" "reflect"
"strings" "strings"
) )
@ -58,3 +61,9 @@ func SlicePagination[T any](arr []T, page, pageSize int) []T {
} }
return arr[start:end] return arr[start:end]
} }
func StringMd5(str string) string {
h := md5.New()
io.WriteString(h, str)
return fmt.Sprintf("%x", h.Sum(nil))
}

View File

@ -10,6 +10,8 @@ import (
"time" "time"
) )
var _ ParseWhere = SqlBuilder{}
type Model interface { type Model interface {
PrimaryKey() string PrimaryKey() string
Table() string Table() string

View File

@ -2,9 +2,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width">
<link rel="profile" href="https://gmpg.org/xfn/11"> <link rel="profile" href="https://gmpg.org/xfn/11">
<link rel="pingback" href="http://wpp.test/xmlrpc.php"> <link rel="pingback" href="/xmlrpc.php">
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="http://wpp.test/wp-content/themes/twentyfifteen/js/html5.js?ver=3.7.0"></script> <script src="/wp-content/themes/twentyfifteen/js/html5.js?ver=3.7.0"></script>
<![endif]--> <![endif]-->
<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script> <script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>
<title>{{ .title }}</title> <title>{{ .title }}</title>