fix disqus 基础评论bug

This commit is contained in:
deepzz0
2017-08-08 01:03:10 +08:00
parent ee7523b124
commit 3ddd2a0b33
2 changed files with 81 additions and 58 deletions

View File

@@ -17,7 +17,8 @@ import (
var ErrDisqusConfig = errors.New("disqus config incorrect") var ErrDisqusConfig = errors.New("disqus config incorrect")
type result struct { // 定时获取所有文章评论数量
type postsCountResp struct {
Code int Code int
Response []struct { Response []struct {
Id string Id string
@@ -26,7 +27,6 @@ type result struct {
} }
} }
// 定时获取所有文章评论数量
func PostsCount() error { func PostsCount() error {
if setting.Conf.Disqus.PostsCount == "" || if setting.Conf.Disqus.PostsCount == "" ||
setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.PublicKey == "" ||
@@ -68,12 +68,12 @@ func PostsCount() error {
return errors.New(string(b)) return errors.New(string(b))
} }
rst := result{} result := &postsCountResp{}
err = json.Unmarshal(b, &rst) err = json.Unmarshal(b, result)
if err != nil { if err != nil {
return err return err
} }
for _, v := range rst.Response { for _, v := range result.Response {
i := strings.Index(v.Identifiers[0], "-") i := strings.Index(v.Identifiers[0], "-")
artc := Ei.MapArticles[v.Identifiers[0][i+1:]] artc := Ei.MapArticles[v.Identifiers[0][i+1:]]
if artc != nil { if artc != nil {
@@ -86,35 +86,39 @@ func PostsCount() error {
return nil return nil
} }
type postsList struct { // 获取文章评论列表
type postsListResp struct {
Cursor struct { Cursor struct {
HasNext bool HasNext bool
Next string Next string
} }
Code int Code int
Response []struct { Response []postDetail
Parent int
Id string
CreatedAt string
Message string
Author struct {
Name string
ProfileUrl string
Avatar struct {
Cache string
}
}
Thread string
}
} }
// 获取文章评论列表 type postDetail struct {
func PostsList(slug, cursor string) (*postsList, error) { Parent int
Id string
CreatedAt string
Message string
IsDeleted bool
Author struct {
Name string
ProfileUrl string
Avatar struct {
Cache string
}
}
Thread string
}
func PostsList(slug, cursor string) (*postsListResp, error) {
if setting.Conf.Disqus.PostsList == "" || if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" { setting.Conf.Disqus.ShortName == "" {
return nil, ErrDisqusConfig return nil, ErrDisqusConfig
} }
url := setting.Conf.Disqus.PostsList + "?limit=50&api_key=" + url := setting.Conf.Disqus.PostsList + "?limit=50&api_key=" +
setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName +
"&cursor=" + cursor + "&thread:ident=post-" + slug "&cursor=" + cursor + "&thread:ident=post-" + slug
@@ -123,6 +127,7 @@ func PostsList(slug, cursor string) (*postsList, error) {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -130,12 +135,13 @@ func PostsList(slug, cursor string) (*postsList, error) {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, errors.New(string(b)) return nil, errors.New(string(b))
} }
pl := &postsList{}
err = json.Unmarshal(b, pl) result := &postsListResp{}
err = json.Unmarshal(b, result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return pl, nil return result, nil
} }
type PostCreate struct { type PostCreate struct {
@@ -149,19 +155,17 @@ type PostCreate struct {
UserAgent string `json:"user_agent"` UserAgent string `json:"user_agent"`
} }
type PostResponse struct { type postCreateResp struct {
Code int `json:"code"` Code int
Response struct { Response postDetail
Id string `json:"id"`
} `json:"response"`
} }
// 评论文章 // 评论文章
func PostComment(pc *PostCreate) (string, error) { func PostComment(pc *PostCreate) (*postCreateResp, error) {
if setting.Conf.Disqus.PostsList == "" || if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" { setting.Conf.Disqus.ShortName == "" {
return "", ErrDisqusConfig return nil, ErrDisqusConfig
} }
url := setting.Conf.Disqus.PostCreate + url := setting.Conf.Disqus.PostCreate +
"?api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F" + "?api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F" +
@@ -171,37 +175,38 @@ func PostComment(pc *PostCreate) (string, error) {
request, err := http.NewRequest("POST", url, nil) request, err := http.NewRequest("POST", url, nil)
if err != nil { if err != nil {
return "", err return nil, err
} }
request.Header.Set("Referer", "https://disqus.com") request.Header.Set("Referer", "https://disqus.com")
resp, err := http.DefaultClient.Do(request) resp, err := http.DefaultClient.Do(request)
if err != nil { if err != nil {
return "", err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", err return nil, err
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return "", errors.New(string(b)) return nil, errors.New(string(b))
} }
pr := &PostResponse{} result := &postCreateResp{}
err = json.Unmarshal(b, pr) err = json.Unmarshal(b, result)
if err != nil { if err != nil {
return "", err return nil, err
} }
return pr.Response.Id, nil return result, nil
} }
type ApprovedResponse struct { // 批准评论通过
type approvedResp struct {
Code int `json:"code"` Code int `json:"code"`
Response []struct { Response []struct {
Id string `json:"id"` Id string `json:"id"`
} `json:"response"` } `json:"response"`
} }
// 批准评论通过
func PostApprove(post string) error { func PostApprove(post string) error {
if setting.Conf.Disqus.PostsList == "" || if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.PublicKey == "" ||
@@ -223,6 +228,7 @@ func PostApprove(post string) error {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
@@ -232,8 +238,8 @@ func PostApprove(post string) error {
return errors.New(string(b)) return errors.New(string(b))
} }
ar := &ApprovedResponse{} result := &approvedResp{}
err = json.Unmarshal(b, ar) err = json.Unmarshal(b, result)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -321,9 +321,9 @@ type commentsDetail struct {
Name string `json:"name"` Name string `json:"name"`
Url string `json:"url"` Url string `json:"url"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
CreatedAt string `json:"createdAt"`
CreatedAtStr string `json:"createdAtStr"` CreatedAtStr string `json:"createdAtStr"`
Message string `json:"message"` Message string `json:"message"`
IsDeleted bool `json:"isDeleted"`
} }
func HandleDisqus(c *gin.Context) { func HandleDisqus(c *gin.Context) {
@@ -356,27 +356,35 @@ func HandleDisqus(c *gin.Context) {
Parent: v.Parent, Parent: v.Parent,
Url: v.Author.ProfileUrl, Url: v.Author.ProfileUrl,
Avatar: v.Author.Avatar.Cache, Avatar: v.Author.Avatar.Cache,
CreatedAt: v.CreatedAt,
CreatedAtStr: ConvertStr(v.CreatedAt), CreatedAtStr: ConvertStr(v.CreatedAt),
Message: v.Message, Message: v.Message,
IsDeleted: v.IsDeleted,
} }
} }
} }
c.JSON(http.StatusOK, dcs) c.JSON(http.StatusOK, dcs)
} }
// 发表评论
// [thread:[5279901489] parent:[] identifier:[post-troubleshooting-https] next:[] author_name:[你好] author_email:[chenqijing2@163.com] message:[fdsfdsf]] // [thread:[5279901489] parent:[] identifier:[post-troubleshooting-https] next:[] author_name:[你好] author_email:[chenqijing2@163.com] message:[fdsfdsf]]
type DisqusCreate struct {
ErrNo int `json:"errno"`
ErrMsg string `json:"errmsg"`
Data commentsDetail `json:"data"`
}
func HandleDisqusCreate(c *gin.Context) { func HandleDisqusCreate(c *gin.Context) {
rep := gin.H{"errno": SUCCESS, "errmsg": ""} resp := &DisqusCreate{}
defer c.JSON(http.StatusOK, rep) defer c.JSON(http.StatusOK, resp)
msg := c.PostForm("message") msg := c.PostForm("message")
email := c.PostForm("author_email") email := c.PostForm("author_email")
name := c.PostForm("author_name") name := c.PostForm("author_name")
thread := c.PostForm("thread") thread := c.PostForm("thread")
identifier := c.PostForm("identifier") identifier := c.PostForm("identifier")
if msg == "" || email == "" || name == "" || thread == "" || identifier == "" { if msg == "" || email == "" || name == "" || thread == "" || identifier == "" {
rep["errno"] = FAIL resp.ErrNo = FAIL
rep["errmsg"] = "参数错误" resp.ErrMsg = "参数错误"
return return
} }
pc := &PostCreate{ pc := &PostCreate{
@@ -389,22 +397,31 @@ func HandleDisqusCreate(c *gin.Context) {
IpAddress: c.ClientIP(), IpAddress: c.ClientIP(),
} }
id, err := PostComment(pc) postDetail, err := PostComment(pc)
if err != nil { if err != nil {
logd.Error(err) logd.Error(err)
rep["errno"] = FAIL resp.ErrNo = FAIL
rep["errmsg"] = "系统错误" resp.ErrMsg = "系统错误"
return return
} }
err = PostApprove(id) err = PostApprove(postDetail.Response.Id)
if err != nil { if err != nil {
logd.Error(err) logd.Error(err)
rep["errno"] = FAIL resp.ErrNo = FAIL
rep["errmsg"] = "系统错误" resp.ErrMsg = "系统错误"
return return
} }
rep["errno"] = SUCCESS resp.ErrNo = SUCCESS
rep["data"] = gin.H{"id": id} resp.Data = commentsDetail{
Id: postDetail.Response.Id,
Name: name,
Parent: postDetail.Response.Parent,
Url: postDetail.Response.Author.ProfileUrl,
Avatar: postDetail.Response.Author.Avatar.Cache,
CreatedAtStr: ConvertStr(postDetail.Response.CreatedAt),
Message: postDetail.Response.Message,
IsDeleted: postDetail.Response.IsDeleted,
}
} }
func RenderHTMLFront(c *gin.Context, name string, data gin.H) { func RenderHTMLFront(c *gin.Context, name string, data gin.H) {