2022-10-08 13:13:26 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2022-10-31 11:47:29 +00:00
|
|
|
"bytes"
|
2022-10-08 13:13:26 +00:00
|
|
|
"errors"
|
2022-10-31 11:47:29 +00:00
|
|
|
"fmt"
|
2022-10-08 13:13:26 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-10-31 11:47:29 +00:00
|
|
|
"github/fthvgb1/wp-go/actions/common"
|
2022-11-04 02:38:59 +00:00
|
|
|
"github/fthvgb1/wp-go/config"
|
2022-10-31 11:47:29 +00:00
|
|
|
"github/fthvgb1/wp-go/logs"
|
|
|
|
"github/fthvgb1/wp-go/mail"
|
2022-11-05 02:32:57 +00:00
|
|
|
"github/fthvgb1/wp-go/models/wp"
|
2022-10-31 11:47:29 +00:00
|
|
|
"io"
|
2022-10-08 13:13:26 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
2022-10-31 11:47:29 +00:00
|
|
|
"strconv"
|
2022-10-08 13:13:26 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PostComment(c *gin.Context) {
|
|
|
|
jar, _ := cookiejar.New(nil)
|
|
|
|
cli := &http.Client{
|
|
|
|
Jar: jar,
|
|
|
|
Timeout: time.Second * 3,
|
|
|
|
}
|
2022-10-31 11:47:29 +00:00
|
|
|
data, err := c.GetRawData()
|
2022-10-08 13:13:26 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusConflict, err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-31 11:47:29 +00:00
|
|
|
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-04 02:38:59 +00:00
|
|
|
req, err := http.NewRequest("POST", config.Conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode()))
|
2022-10-08 13:13:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
for k, v := range c.Request.Header {
|
|
|
|
req.Header.Set(k, v[0])
|
|
|
|
}
|
|
|
|
res, err := cli.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-31 11:47:29 +00:00
|
|
|
if res.Request.Response != nil && res.Request.Response.StatusCode == http.StatusFound {
|
2022-10-08 13:13:26 +00:00
|
|
|
for _, cookie := range res.Request.Response.Cookies() {
|
|
|
|
c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
|
|
|
|
}
|
2022-11-07 08:04:13 +00:00
|
|
|
//c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location"))
|
2022-10-31 11:47:29 +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-05 14:40:02 +00:00
|
|
|
su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wp.Option["siteurl"], author, m, post.PostTitle)
|
2022-11-04 02:38:59 +00:00
|
|
|
err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment)
|
2022-11-07 08:04:13 +00:00
|
|
|
logs.ErrPrintln(err, "发送邮件", config.Conf.Mail.User, su, comment)
|
2022-10-31 11:47:29 +00:00
|
|
|
}()
|
2022-11-07 08:04:13 +00:00
|
|
|
s, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, string(s))
|
2022-10-08 13:13:26 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-31 11:47:29 +00:00
|
|
|
s, err := io.ReadAll(res.Body)
|
2022-10-08 13:13:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = errors.New(string(s))
|
|
|
|
}
|