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