wp-go/actions/comment.go

127 lines
3.2 KiB
Go
Raw Normal View History

2022-10-08 13:13:26 +00:00
package actions
import (
"bytes"
2022-10-08 13:13:26 +00:00
"errors"
"fmt"
2022-10-08 13:13:26 +00:00
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
2022-11-08 09:41:01 +00:00
"github/fthvgb1/wp-go/cache"
2022-11-04 02:38:59 +00:00
"github/fthvgb1/wp-go/config"
2022-11-17 08:29:39 +00:00
"github/fthvgb1/wp-go/config/wpconfig"
2022-11-10 12:53:25 +00:00
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/mail"
"io"
2022-10-08 13:13:26 +00:00
"net/http"
2022-11-08 09:41:01 +00:00
"net/url"
"strconv"
2022-10-08 13:13:26 +00:00
"strings"
"time"
)
2022-11-08 09:41:01 +00:00
var commentCache = cache.NewMapCacheByFn[string, string](nil, 15*time.Minute)
2022-10-08 13:13:26 +00:00
func PostComment(c *gin.Context) {
cli := &http.Client{
Timeout: time.Second * 3,
2022-11-08 09:41:01 +00:00
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
2022-10-08 13:13:26 +00:00
}
data, err := c.GetRawData()
2022-10-08 13:13:26 +00:00
defer func() {
if err != nil {
2022-11-09 15:06:48 +00:00
c.Writer.WriteHeader(http.StatusConflict)
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteString(err.Error())
2022-10-08 13:13:26 +00:00
}
}()
if err != nil {
return
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
i := c.PostForm("comment_post_ID")
author := c.PostForm("author")
m := c.PostForm("email")
comment := c.PostForm("comment")
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
2022-11-15 08:36:21 +00:00
req, err := http.NewRequest("POST", config.Conf.Load().PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode()))
2022-10-08 13:13:26 +00:00
if err != nil {
return
}
defer req.Body.Close()
2022-11-08 09:41:01 +00:00
req.Header = c.Request.Header.Clone()
2022-11-17 08:29:39 +00:00
home, err := url.Parse(wpconfig.Options.Value("siteurl"))
2022-11-09 15:06:48 +00:00
if err != nil {
return
}
req.Host = home.Host
2022-10-08 13:13:26 +00:00
res, err := cli.Do(req)
2022-11-08 09:41:01 +00:00
if err != nil && err != http.ErrUseLastResponse {
2022-10-08 13:13:26 +00:00
return
}
2022-11-08 09:41:01 +00:00
if res.StatusCode == http.StatusFound {
2022-11-08 09:49:10 +00:00
for _, cookie := range res.Cookies() {
c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
}
2022-11-08 09:41:01 +00:00
u := res.Header.Get("Location")
2022-11-10 10:15:19 +00:00
up, er := url.Parse(u)
if er != nil {
err = er
2022-11-08 09:41:01 +00:00
return
}
2022-11-15 08:36:21 +00:00
cu, er := url.Parse(config.Conf.Load().PostCommentUrl)
2022-11-10 10:15:19 +00:00
if er != nil {
err = er
2022-11-08 09:41:01 +00:00
return
}
up.Host = cu.Host
2022-11-10 10:15:19 +00:00
up.Scheme = "http"
newReq, er := http.NewRequest("GET", up.String(), nil)
if er != nil {
err = er
2022-11-09 15:06:48 +00:00
return
}
newReq.Host = home.Host
2022-11-10 12:53:25 +00:00
newReq.Header.Set("Cookie", strings.Join(helper.SliceMap(c.Request.Cookies(), func(t *http.Cookie) string {
return fmt.Sprintf("%s=%s", t.Name, t.Value)
}), "; "))
2022-11-10 10:15:19 +00:00
ress, er := http.DefaultClient.Do(newReq)
if er != nil {
err = er
2022-11-08 09:41:01 +00:00
return
2022-10-08 13:13:26 +00:00
}
cc := c.Copy()
go func() {
id, err := strconv.ParseUint(i, 10, 64)
if err != nil {
logs.ErrPrintln(err, "获取文档id", i)
return
}
post, err := common.GetPostById(cc, id)
if err != nil {
logs.ErrPrintln(err, "获取文档", id)
return
}
2022-11-17 08:29:39 +00:00
su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.Options.Value("siteurl"), author, m, post.PostTitle)
2022-11-15 08:36:21 +00:00
err = mail.SendMail([]string{config.Conf.Load().Mail.User}, su, comment)
logs.ErrPrintln(err, "发送邮件", config.Conf.Load().Mail.User, su, comment)
}()
2022-11-08 09:41:01 +00:00
2022-11-10 10:15:19 +00:00
s, er := io.ReadAll(ress.Body)
if er != nil {
err = er
return
}
2022-11-08 09:41:01 +00:00
commentCache.Set(up.RawQuery, string(s))
c.Redirect(http.StatusFound, res.Header.Get("Location"))
2022-10-08 13:13:26 +00:00
return
}
s, err := io.ReadAll(res.Body)
2022-10-08 13:13:26 +00:00
if err != nil {
return
}
err = errors.New(string(s))
}