mirror of
https://github.com/eiblog/eiblog.git
synced 2026-03-01 00:34:58 +08:00
完成评论服务端化
This commit is contained in:
71
disqus.go
71
disqus.go
@@ -22,18 +22,18 @@ type result struct {
|
||||
}
|
||||
}
|
||||
|
||||
func CommentsCount() {
|
||||
if setting.Conf.Disqus.URL == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
|
||||
func PostsCount() {
|
||||
if setting.Conf.Disqus.PostsCount == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
|
||||
return
|
||||
}
|
||||
baseUrl := setting.Conf.Disqus.URL + "?api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&"
|
||||
baseUrl := setting.Conf.Disqus.PostsCount + "?api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&"
|
||||
var count, index int
|
||||
for index < len(Ei.Articles) {
|
||||
logd.Debugf("count=====%d, index=======%d, length=======%d, bool=========%t", count, index, len(Ei.Articles), index < len(Ei.Articles) && count < 10)
|
||||
logd.Debugf("count=====%d, index=======%d, length=======%d, bool=========%t", count, index, len(Ei.Articles), index < len(Ei.Articles) && count < 50)
|
||||
var threads []string
|
||||
for ; index < len(Ei.Articles) && count < 20; index++ {
|
||||
for ; index < len(Ei.Articles) && count < 50; index++ {
|
||||
artc := Ei.Articles[index]
|
||||
threads = append(threads, fmt.Sprintf("thread=link:https://%s/post/%s.html", setting.Conf.Mode.Domain, artc.Slug))
|
||||
threads = append(threads, fmt.Sprintf("thread:ident=post-%s", artc.Slug))
|
||||
count++
|
||||
}
|
||||
count = 0
|
||||
@@ -49,16 +49,16 @@ func CommentsCount() {
|
||||
logd.Error(err)
|
||||
break
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logd.Error(string(b))
|
||||
break
|
||||
}
|
||||
rst := result{}
|
||||
err = json.Unmarshal(b, &rst)
|
||||
if err != nil {
|
||||
logd.Error(err)
|
||||
break
|
||||
}
|
||||
if rst.Code != SUCCESS {
|
||||
logd.Error(rst.Code)
|
||||
break
|
||||
}
|
||||
for _, v := range rst.Response {
|
||||
i := strings.Index(v.Identifiers[0], "-")
|
||||
artc := Ei.MapArticles[v.Identifiers[0][i+1:]]
|
||||
@@ -67,5 +67,54 @@ func CommentsCount() {
|
||||
}
|
||||
}
|
||||
}
|
||||
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, CommentsCount)
|
||||
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, PostsCount)
|
||||
}
|
||||
|
||||
type postsList 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PostsList(slug, cursor string) *postsList {
|
||||
if setting.Conf.Disqus.PostsList == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
|
||||
return nil
|
||||
}
|
||||
url := setting.Conf.Disqus.PostsList + "?limit=50&api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&cursor=" + cursor + "&thread:ident=post-" + slug
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logd.Error(err)
|
||||
return nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logd.Error(string(b))
|
||||
return nil
|
||||
}
|
||||
pl := &postsList{}
|
||||
err = json.Unmarshal(b, pl)
|
||||
if err != nil {
|
||||
logd.Error(err)
|
||||
return nil
|
||||
}
|
||||
return pl
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user