添加 disqus thread 创建接口

This commit is contained in:
henry.chen
2018-01-13 02:56:35 +08:00
parent 405fbaf24f
commit ceb9e2690b
5 changed files with 127 additions and 61 deletions

View File

@@ -42,6 +42,7 @@ disqus:
postslist: https://disqus.com/api/3.0/threads/listPosts.json postslist: https://disqus.com/api/3.0/threads/listPosts.json
postcreate: https://disqus.com/api/3.0/posts/create.json postcreate: https://disqus.com/api/3.0/posts/create.json
postapprove: https://disqus.com/api/3.0/posts/approve.json postapprove: https://disqus.com/api/3.0/posts/approve.json
threadcreate: https://disqus.com/api/3.0/threads/create.json
# disqus.js 文件名 # disqus.js 文件名
embed: disqus_7d3cf2.js embed: disqus_7d3cf2.js
# 获取评论数量间隔 # 获取评论数量间隔

147
disqus.go
View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
@@ -17,6 +18,12 @@ import (
var ErrDisqusConfig = errors.New("disqus config incorrect") var ErrDisqusConfig = errors.New("disqus config incorrect")
func correctDisqusConfig() bool {
return setting.Conf.Disqus.PostsCount != "" &&
setting.Conf.Disqus.PublicKey != "" &&
setting.Conf.Disqus.ShortName != ""
}
// 定时获取所有文章评论数量 // 定时获取所有文章评论数量
type postsCountResp struct { type postsCountResp struct {
Code int Code int
@@ -28,9 +35,7 @@ type postsCountResp struct {
} }
func PostsCount() error { func PostsCount() error {
if setting.Conf.Disqus.PostsCount == "" || if !correctDisqusConfig() {
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return ErrDisqusConfig return ErrDisqusConfig
} }
@@ -41,20 +46,19 @@ func PostsCount() error {
} }
}) })
baseUrl := setting.Conf.Disqus.PostsCount + vals := url.Values{}
"?api_key=" + setting.Conf.Disqus.PublicKey + vals.Set("api_key", setting.Conf.Disqus.PublicKey)
"&forum=" + setting.Conf.Disqus.ShortName + "&" vals.Set("forum", setting.Conf.Disqus.ShortName)
var count, index int var count, index int
for index < len(Ei.Articles) { for index < len(Ei.Articles) {
var threads []string
for ; index < len(Ei.Articles) && count < 50; index++ { for ; index < len(Ei.Articles) && count < 50; index++ {
artc := Ei.Articles[index] artc := Ei.Articles[index]
threads = append(threads, fmt.Sprintf("thread:ident=post-%s", artc.Slug)) vals.Add("thread:ident", "post-"+artc.Slug)
count++ count++
} }
count = 0 count = 0
url := baseUrl + strings.Join(threads, "&") resp, err := http.Get(setting.Conf.Disqus.PostsCount + "?" + vals.Encode())
resp, err := http.Get(url)
if err != nil { if err != nil {
return err return err
} }
@@ -113,16 +117,18 @@ type postDetail struct {
} }
func PostsList(slug, cursor string) (*postsListResp, error) { func PostsList(slug, cursor string) (*postsListResp, error) {
if setting.Conf.Disqus.PostsList == "" || if !correctDisqusConfig() {
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return nil, ErrDisqusConfig return nil, ErrDisqusConfig
} }
url := setting.Conf.Disqus.PostsList + "?limit=50&api_key=" + vals := url.Values{}
setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + vals.Set("api_key", setting.Conf.Disqus.PublicKey)
"&cursor=" + cursor + "&thread:ident=post-" + slug vals.Set("forum", setting.Conf.Disqus.ShortName)
resp, err := http.Get(url) vals.Set("thread:ident", "post-"+slug)
vals.Set("cursor", cursor)
vals.Set("limit", "50")
resp, err := http.Get(setting.Conf.Disqus.PostsList + "?" + vals.Encode())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -144,15 +150,15 @@ func PostsList(slug, cursor string) (*postsListResp, error) {
return result, nil return result, nil
} }
type PostCreate struct { type PostComment struct {
Message string `json:"message"` Message string
Parent string `json:"parent"` Parent string
Thread string `json:"thread"` Thread string
AuthorEmail string `json:"author_email"` AuthorEmail string
AuthorName string `json:"autor_name"` AuthorName string
IpAddress string `json:"ip_address"` IpAddress string
Identifier string `json:"identifier"` Identifier string
UserAgent string `json:"user_agent"` UserAgent string
} }
type postCreateResp struct { type postCreateResp struct {
@@ -161,19 +167,21 @@ type postCreateResp struct {
} }
// 评论文章 // 评论文章
func PostComment(pc *PostCreate) (*postCreateResp, error) { func PostCreate(pc *PostComment) (*postCreateResp, error) {
if setting.Conf.Disqus.PostsList == "" || if !correctDisqusConfig() {
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return nil, ErrDisqusConfig return nil, ErrDisqusConfig
} }
url := setting.Conf.Disqus.PostCreate +
"?api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F" +
"&message=" + pc.Message + "&parent=" + pc.Parent +
"&thread=" + pc.Thread + "&author_email=" + pc.AuthorEmail +
"&author_name=" + pc.AuthorName
request, err := http.NewRequest("POST", url, nil) vals := url.Values{}
vals.Set("api_key", "E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F")
vals.Set("message", pc.Message)
vals.Set("parent", pc.Parent)
vals.Set("thread", pc.Thread)
vals.Set("author_email", pc.AuthorEmail)
vals.Set("author_name", pc.AuthorName)
// vals.Set("state", "approved")
request, err := http.NewRequest("POST", setting.Conf.Disqus.PostCreate, strings.NewReader(vals.Encode()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -201,24 +209,23 @@ func PostComment(pc *PostCreate) (*postCreateResp, error) {
// 批准评论通过 // 批准评论通过
type approvedResp struct { type approvedResp struct {
Code int `json:"code"` Code int
Response []struct { Response []struct {
Id string `json:"id"` Id string
} `json:"response"` }
} }
func PostApprove(post string) error { func PostApprove(post string) error {
if setting.Conf.Disqus.PostsList == "" || if !correctDisqusConfig() {
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return ErrDisqusConfig return ErrDisqusConfig
} }
url := setting.Conf.Disqus.PostApprove + vals := url.Values{}
"?api_key=" + setting.Conf.Disqus.PublicKey + vals.Set("api_key", setting.Conf.Disqus.PublicKey)
"&access_token=" + setting.Conf.Disqus.AccessToken + vals.Set("access_token", setting.Conf.Disqus.AccessToken)
"&post=" + post vals.Set("post", post)
request, err := http.NewRequest("POST", url, nil)
request, err := http.NewRequest("POST", setting.Conf.Disqus.PostApprove, strings.NewReader(vals.Encode()))
if err != nil { if err != nil {
return err return err
} }
@@ -246,3 +253,49 @@ func PostApprove(post string) error {
return nil return nil
} }
// 创建thread
type threadCreateResp struct {
Code int
Response struct {
Id string
}
}
func ThreadCreate(artc *Article) error {
if !correctDisqusConfig() {
return ErrDisqusConfig
}
vals := url.Values{}
vals.Set("api_key", setting.Conf.Disqus.PublicKey)
vals.Set("access_token", setting.Conf.Disqus.AccessToken)
vals.Set("forum", setting.Conf.Disqus.ShortName)
vals.Set("title", artc.Title+" | "+Ei.BTitle)
vals.Set("identifier", "post-"+artc.Slug)
urlPath := fmt.Sprintf("https://%s/post/%s.html", setting.Conf.Mode.Domain, artc.Slug)
vals.Set("url", urlPath)
resp, err := http.PostForm(setting.Conf.Disqus.ThreadCreate, 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 := &threadCreateResp{}
err = json.Unmarshal(b, result)
if err != nil {
return err
}
artc.Thread = result.Response.Id
return nil
}

View File

@@ -8,18 +8,29 @@ func TestDisqus(t *testing.T) {
PostsCount() PostsCount()
} }
func TestPostComment(t *testing.T) { func TestPostCreate(t *testing.T) {
pc := &PostCreate{ pc := &PostComment{
Message: "hahahaha", Message: "hahahaha",
Thread: "52799014", Thread: "52799014",
AuthorEmail: "deepzz.qi@gmail.com", AuthorEmail: "deepzz.qi@gmail.com",
AuthorName: "deepzz", AuthorName: "deepzz",
} }
id, err := PostComment(pc) id, err := PostCreate(pc)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
t.Log("post success", id) t.Log("post success", id)
} }
func TestThreadCreate(t *testing.T) {
tc := &Article{
Title: "测试test7",
Slug: "test7",
}
err := ThreadCreate(tc)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -400,7 +400,7 @@ func HandleDisqusCreate(c *gin.Context) {
resp.ErrMsg = "参数错误" resp.ErrMsg = "参数错误"
return return
} }
pc := &PostCreate{ pc := &PostComment{
Message: msg, Message: msg,
Parent: c.PostForm("parent"), Parent: c.PostForm("parent"),
Thread: thread, Thread: thread,
@@ -410,7 +410,7 @@ func HandleDisqusCreate(c *gin.Context) {
IpAddress: c.ClientIP(), IpAddress: c.ClientIP(),
} }
postDetail, err := PostComment(pc) postDetail, err := PostCreate(pc)
if err != nil { if err != nil {
logd.Error(err) logd.Error(err)
resp.ErrNo = FAIL resp.ErrNo = FAIL

View File

@@ -35,15 +35,16 @@ type Config struct {
Clean int // 清理回收箱频率 Clean int // 清理回收箱频率
} }
Disqus struct { // 获取文章数量相关 Disqus struct { // 获取文章数量相关
ShortName string ShortName string
PublicKey string PublicKey string
AccessToken string AccessToken string
PostsCount string PostsCount string
PostsList string PostsList string
PostCreate string PostCreate string
PostApprove string PostApprove string
Embed string ThreadCreate string
Interval int Embed string
Interval int
} }
Google struct { // 谷歌统计 Google struct { // 谷歌统计
URL string URL string