fix: fist comment of disqus error

This commit is contained in:
henry.chen
2023-01-05 13:25:18 +08:00
parent 04289c633e
commit 17792e5a7e
4 changed files with 66 additions and 8 deletions

View File

@@ -301,7 +301,7 @@ func handleAPIPostCreate(c *gin.Context) {
}
// 旧文章
article.ID = cid
artc, _ := cache.Ei.FindArticleByID(article.ID)
artc, _ := cache.Ei.FindArticleByID(article.ID) // cache
if artc != nil {
article.IsDraft = false
article.Count = artc.Count

View File

@@ -3,6 +3,7 @@ package page
import (
"bytes"
"context"
"fmt"
htemplate "html/template"
"io/ioutil"
@@ -198,7 +199,8 @@ func handleDisqusList(c *gin.Context) {
slug := c.Param("slug")
cursor := c.Query("cursor")
if artc := cache.Ei.ArticlesMap[slug]; artc != nil {
artc := cache.Ei.ArticlesMap[slug]
if artc != nil {
dcs.Data.Thread = artc.Thread
}
postsList, err := internal.PostsList(slug, cursor)
@@ -229,6 +231,18 @@ func handleDisqusList(c *gin.Context) {
IsDeleted: v.IsDeleted,
}
}
// query thread & update
if artc != nil && artc.Thread == "" {
if dcs.Data.Thread != "" {
artc.Thread = dcs.Data.Thread
} else if internal.ThreadDetails(artc) == nil {
dcs.Data.Thread = artc.Thread
}
cache.Ei.UpdateArticle(context.Background(), artc.ID,
map[string]interface{}{
"thread": artc.Thread,
})
}
}
// handleDisqusPage 评论页

View File

@@ -16,11 +16,12 @@ import (
// disqus api
const (
apiPostsCount = "https://disqus.com/api/3.0/threads/set.json"
apiPostsList = "https://disqus.com/api/3.0/threads/listPosts.json"
apiPostCreate = "https://disqus.com/api/3.0/posts/create.json"
apiPostApprove = "https://disqus.com/api/3.0/posts/approve.json"
apiThreadCreate = "https://disqus.com/api/3.0/threads/create.json"
apiPostsCount = "https://disqus.com/api/3.0/threads/set.json"
apiPostsList = "https://disqus.com/api/3.0/threads/listPosts.json"
apiPostCreate = "https://disqus.com/api/3.0/posts/create.json"
apiPostApprove = "https://disqus.com/api/3.0/posts/approve.json"
apiThreadCreate = "https://disqus.com/api/3.0/threads/create.json"
apiThreadDetails = "https://disqus.com/api/3.0/threads/details.json"
)
func checkDisqusConfig() error {
@@ -295,3 +296,46 @@ func ThreadCreate(article *model.Article, btitle string) error {
article.Thread = result.Response.ID
return nil
}
// threadDetailsResp thread info
type threadDetailsResp struct {
Code int
Response struct {
ID string
}
}
// ThreadDetails thread详细
func ThreadDetails(article *model.Article) error {
if err := checkDisqusConfig(); err != nil {
return err
}
vals := url.Values{}
vals.Set("api_key", config.Conf.EiBlogApp.Disqus.PublicKey)
vals.Set("access_token", config.Conf.EiBlogApp.Disqus.AccessToken)
vals.Set("forum", config.Conf.EiBlogApp.Disqus.ShortName)
vals.Set("thread:ident", "post-"+article.Slug)
resp, err := httpPost(apiThreadDetails, vals)
if err != nil {
return err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(string(b))
}
result := &threadDetailsResp{}
err = json.Unmarshal(b, result)
if err != nil {
return err
}
article.Thread = result.Response.ID
return nil
}

View File

@@ -20,6 +20,7 @@ type Article struct {
SerieID int `gorm:"column:serie_id;not null" bson:"serie_id"` // 专题ID
Tags pq.StringArray `gorm:"column:tags;type:text[]" bson:"tags"` // tags
IsDraft bool `gorm:"column:is_draft;not null" bson:"is_draft"` // 是否是草稿
Thread string `gorm:"column:thread" bson:"thread"` // disqus thread
DeletedAt time.Time `gorm:"column:deleted_at;not null" bson:"deleted_at"` // 删除时间
UpdatedAt time.Time `gorm:"column:updated_at;default:current_timestamp" bson:"updated_at"` // 更新时间
@@ -28,7 +29,6 @@ type Article struct {
Header string `gorm:"-" bson:"-"` // header
Excerpt string `gorm:"-" bson:"-"` // 预览信息
Desc string `gorm:"-" bson:"-"` // 描述
Thread string `gorm:"-" bson:"-"` // disqus thread
Prev *Article `gorm:"-" bson:"-"` // 上篇文章
Next *Article `gorm:"-" bson:"-"` // 下篇文章
}