From 17792e5a7edb7e84623d9307555e7983ba306565 Mon Sep 17 00:00:00 2001 From: "henry.chen" Date: Thu, 5 Jan 2023 13:25:18 +0800 Subject: [PATCH] fix: fist comment of disqus error --- pkg/core/eiblog/admin/admin.go | 2 +- pkg/core/eiblog/page/fe.go | 16 +++++++++- pkg/internal/disqus.go | 54 ++++++++++++++++++++++++++++++---- pkg/model/article.go | 2 +- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/pkg/core/eiblog/admin/admin.go b/pkg/core/eiblog/admin/admin.go index 00152d7..2dbea06 100644 --- a/pkg/core/eiblog/admin/admin.go +++ b/pkg/core/eiblog/admin/admin.go @@ -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 diff --git a/pkg/core/eiblog/page/fe.go b/pkg/core/eiblog/page/fe.go index 5a05d23..202099c 100644 --- a/pkg/core/eiblog/page/fe.go +++ b/pkg/core/eiblog/page/fe.go @@ -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 评论页 diff --git a/pkg/internal/disqus.go b/pkg/internal/disqus.go index 68771a5..cdf8e57 100644 --- a/pkg/internal/disqus.go +++ b/pkg/internal/disqus.go @@ -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 +} diff --git a/pkg/model/article.go b/pkg/model/article.go index f9d77fb..61797cf 100644 --- a/pkg/model/article.go +++ b/pkg/model/article.go @@ -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:"-"` // 下篇文章 }