完善添加评论
This commit is contained in:
parent
5407a74aa3
commit
ec2aa30eb6
|
@ -6,23 +6,27 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"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/cache"
|
||||||
"github/fthvgb1/wp-go/config"
|
"github/fthvgb1/wp-go/config"
|
||||||
"github/fthvgb1/wp-go/logs"
|
"github/fthvgb1/wp-go/logs"
|
||||||
"github/fthvgb1/wp-go/mail"
|
"github/fthvgb1/wp-go/mail"
|
||||||
"github/fthvgb1/wp-go/models/wp"
|
"github/fthvgb1/wp-go/models/wp"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var commentCache = cache.NewMapCacheByFn[string, string](nil, 15*time.Minute)
|
||||||
|
|
||||||
func PostComment(c *gin.Context) {
|
func PostComment(c *gin.Context) {
|
||||||
jar, _ := cookiejar.New(nil)
|
|
||||||
cli := &http.Client{
|
cli := &http.Client{
|
||||||
Jar: jar,
|
|
||||||
Timeout: time.Second * 3,
|
Timeout: time.Second * 3,
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
}
|
}
|
||||||
data, err := c.GetRawData()
|
data, err := c.GetRawData()
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -44,18 +48,28 @@ func PostComment(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
for k, v := range c.Request.Header {
|
req.Header = c.Request.Header.Clone()
|
||||||
req.Header.Set(k, v[0])
|
|
||||||
}
|
|
||||||
res, err := cli.Do(req)
|
res, err := cli.Do(req)
|
||||||
if err != nil {
|
if err != nil && err != http.ErrUseLastResponse {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if res.Request.Response != nil && res.Request.Response.StatusCode == http.StatusFound {
|
if res.StatusCode == http.StatusFound {
|
||||||
for _, cookie := range res.Request.Response.Cookies() {
|
u := res.Header.Get("Location")
|
||||||
c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
|
up, err := url.Parse(u)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cu, err := url.Parse(config.Conf.PostCommentUrl)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
up.Host = cu.Host
|
||||||
|
|
||||||
|
ress, err := http.DefaultClient.Get(up.String())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
//c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location"))
|
|
||||||
cc := c.Copy()
|
cc := c.Copy()
|
||||||
go func() {
|
go func() {
|
||||||
id, err := strconv.ParseUint(i, 10, 64)
|
id, err := strconv.ParseUint(i, 10, 64)
|
||||||
|
@ -72,11 +86,13 @@ func PostComment(c *gin.Context) {
|
||||||
err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment)
|
err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment)
|
||||||
logs.ErrPrintln(err, "发送邮件", config.Conf.Mail.User, su, comment)
|
logs.ErrPrintln(err, "发送邮件", config.Conf.Mail.User, su, comment)
|
||||||
}()
|
}()
|
||||||
s, err := io.ReadAll(res.Body)
|
|
||||||
|
s, err := io.ReadAll(ress.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.String(http.StatusOK, string(s))
|
commentCache.Set(up.RawQuery, string(s))
|
||||||
|
c.Redirect(http.StatusFound, res.Header.Get("Location"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s, err := io.ReadAll(res.Body)
|
s, err := io.ReadAll(res.Body)
|
||||||
|
|
|
@ -39,12 +39,16 @@ func Detail(c *gin.Context) {
|
||||||
"categories": categoryItems,
|
"categories": categoryItems,
|
||||||
"recentComments": recentComments,
|
"recentComments": recentComments,
|
||||||
}
|
}
|
||||||
|
isApproveComment := false
|
||||||
defer func() {
|
defer func() {
|
||||||
status := http.StatusOK
|
status := http.StatusOK
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status = http.StatusInternalServerError
|
status = http.StatusInternalServerError
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
}
|
}
|
||||||
|
if isApproveComment == true {
|
||||||
|
return
|
||||||
|
}
|
||||||
c.HTML(status, "twentyfifteen/posts/detail.gohtml", h)
|
c.HTML(status, "twentyfifteen/posts/detail.gohtml", h)
|
||||||
}()
|
}()
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
@ -74,6 +78,12 @@ func Detail(c *gin.Context) {
|
||||||
if post.PostPassword != "" && pw != post.PostPassword {
|
if post.PostPassword != "" && pw != post.PostPassword {
|
||||||
common.PasswdProjectContent(&post)
|
common.PasswdProjectContent(&post)
|
||||||
showComment = false
|
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)
|
plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post)
|
||||||
comments, err := common.PostComments(c, post.Id)
|
comments, err := common.PostComments(c, post.Id)
|
||||||
|
|
|
@ -38,6 +38,7 @@ func InitFeed() {
|
||||||
|
|
||||||
func ClearCache() {
|
func ClearCache() {
|
||||||
postFeedCache.ClearExpired()
|
postFeedCache.ClearExpired()
|
||||||
|
commentCache.ClearExpired()
|
||||||
}
|
}
|
||||||
|
|
||||||
func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
|
func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
|
||||||
|
@ -55,7 +56,7 @@ func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Feed(c *gin.Context) {
|
func Feed(c *gin.Context) {
|
||||||
if !isCacheExpired(c, feedCache.SetTime()) {
|
if !isCacheExpired(c, feedCache.GetLastSetTime()) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
} else {
|
||||||
r, err := feedCache.GetCache(c, time.Second, c)
|
r, err := feedCache.GetCache(c, time.Second, c)
|
||||||
|
@ -65,7 +66,7 @@ func Feed(c *gin.Context) {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setFeed(r[0], c, feedCache.SetTime())
|
setFeed(r[0], c, feedCache.GetLastSetTime())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +127,7 @@ func setFeed(s string, c *gin.Context, t time.Time) {
|
||||||
|
|
||||||
func PostFeed(c *gin.Context) {
|
func PostFeed(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
if !isCacheExpired(c, postFeedCache.GetSetTime(id)) {
|
if !isCacheExpired(c, postFeedCache.GetLastSetTime(id)) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
} else {
|
||||||
s, err := postFeedCache.GetCache(c, id, time.Second, c, id)
|
s, err := postFeedCache.GetCache(c, id, time.Second, c, id)
|
||||||
|
@ -136,7 +137,7 @@ func PostFeed(c *gin.Context) {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setFeed(s, c, postFeedCache.GetSetTime(id))
|
setFeed(s, c, postFeedCache.GetLastSetTime(id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +206,7 @@ func postFeed(arg ...any) (x string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CommentsFeed(c *gin.Context) {
|
func CommentsFeed(c *gin.Context) {
|
||||||
if !isCacheExpired(c, commentsFeedCache.SetTime()) {
|
if !isCacheExpired(c, commentsFeedCache.GetLastSetTime()) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
} else {
|
||||||
r, err := commentsFeedCache.GetCache(c, time.Second, c)
|
r, err := commentsFeedCache.GetCache(c, time.Second, c)
|
||||||
|
@ -215,7 +216,7 @@ func CommentsFeed(c *gin.Context) {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setFeed(r[0], c, commentsFeedCache.SetTime())
|
setFeed(r[0], c, commentsFeedCache.GetLastSetTime())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
8
cache/map.go
vendored
8
cache/map.go
vendored
|
@ -31,7 +31,7 @@ func (m *MapCache[K, V]) SetCacheFunc(fn func(...any) (V, error)) {
|
||||||
m.cacheFunc = fn
|
m.cacheFunc = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MapCache[K, V]) GetSetTime(k K) (t time.Time) {
|
func (m *MapCache[K, V]) GetLastSetTime(k K) (t time.Time) {
|
||||||
r, ok := m.data.Load(k)
|
r, ok := m.data.Load(k)
|
||||||
if ok {
|
if ok {
|
||||||
t = r.setTime
|
t = r.setTime
|
||||||
|
@ -93,13 +93,13 @@ func (m *MapCache[K, V]) Flush() {
|
||||||
m.data = safety.NewMap[K, mapCacheStruct[V]]()
|
m.data = safety.NewMap[K, mapCacheStruct[V]]()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MapCache[K, V]) Get(k K) V {
|
func (m *MapCache[K, V]) Get(k K) (V, bool) {
|
||||||
r, ok := m.data.Load(k)
|
r, ok := m.data.Load(k)
|
||||||
if ok {
|
if ok {
|
||||||
return r.data
|
return r.data, ok
|
||||||
}
|
}
|
||||||
var rr V
|
var rr V
|
||||||
return rr
|
return rr, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MapCache[K, V]) Set(k K, v V) {
|
func (m *MapCache[K, V]) Set(k K, v V) {
|
||||||
|
|
3
cache/slice.go
vendored
3
cache/slice.go
vendored
|
@ -22,8 +22,7 @@ type slice[T any] struct {
|
||||||
incr int
|
incr int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SliceCache[T]) SetTime() time.Time {
|
func (c *SliceCache[T]) GetLastSetTime() time.Time {
|
||||||
|
|
||||||
return c.v.Load().setTime
|
return c.v.Load().setTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user