From d73e9f996234be6ddb14402f8796e244e367ff25 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 7 Oct 2018 11:25:53 +0800 Subject: [PATCH] feat: finish comment list json output --- src/disqus.js | 104 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 24 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index 6edf6da..5818083 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -17,6 +17,7 @@ * disqusjs.config.url - The url of the page * disqusjs.config,api - Where to get data * disqusjs.config.apikey - The apikey used to request Disqus API + * disqusjs.config.admin - The disqus forum admin username * * DisqusJS Info * disqusjs.page.id = The thread id, used at next API call @@ -25,7 +26,7 @@ * disqusjs.page.lenfth - How many comment in this thread */ -disqusjs.page = {}; +disqusjs.page = []; disqusjs.mode = 'proxy'; var xhr = new XMLHttpRequest(); @@ -80,10 +81,10 @@ function checkDisqus() { var setmode = function () { if (success = test) { disqusjs.mode = 'direct', - setLS('disqusjs_mode', 'direct'); + setLS('disqusjs_mode', 'direct'); } else { disqusjs.mode = 'proxy', - setLS('disqusjs_mode', 'proxy'); + setLS('disqusjs_mode', 'proxy'); } }; var check = function (domain) { @@ -119,6 +120,37 @@ function checkDisqus() { */ function getThreadInfo() { + + /* + * Name: getComment() + * Description: get the comment content + * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] + */ + + getComment = function () { + var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; + xhr.open('GET', url, true); + xhr.timeout = 4000; + xhr.send(); + xhr.onload = function () { + if (this.status == 200 || this.status == 304) { + var res = JSON.parse(this.responseText); + if (res.code === 0) { + renderCommentList(res.response); + } else { + // Have error when get comments. + } + + } + }; + xhr.ontimeout = function (e) { + console.log(e) + }; + xhr.onerror = function (e) { + console.log(e) + }; + } + var url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey; xhr.open('GET', url, true); xhr.timeout = 4000; @@ -132,7 +164,6 @@ function getThreadInfo() { isClosed: response.isClosed, length: response.posts }; - console.log(disqusjs); getComment(); } }; @@ -145,28 +176,53 @@ function getThreadInfo() { } /* - * Name: getComment() - * Description: get the comment content - * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] + * Name: renderCommentList(data) + * Description: Render JSON to comment list components */ -function getComment() { - var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; - xhr.open('GET', url, true); - xhr.timeout = 4000; - xhr.send(); - xhr.onload = function () { - if (this.status == 200 || this.status == 304) { - var response = JSON.parse(this.responseText); - console.log(response); +function renderCommentList(data) { + var topLevelComments = []; + var childComments = []; + + data.forEach(function (comment) { + (comment.parent ? childComments : topLevelComments)['push'](comment) + }) + + var commentLists = topLevelComments.map(function (comment) { + return { + comment: comment, + author: comment.author.name, + isPrimary: comment.author.username === disqusjs.config.admin, + children: getChildren(Number(comment.id)) + }; + }); + + function getChildren(id) { + if (childComments.length === 0) { + return null } - }; - xhr.ontimeout = function (e) { - console.log(e) - }; - xhr.onerror = function (e) { - console.log(e) - }; + + var list = [] + for (var i = 0; i < childComments.length; i++) { + var comment = childComments[i]; + if (comment.parent === id) { + list.unshift({ + comment, + author: comment.author.name, + isPrimary: comment.author.username === disqusjs.config.admin, + children: getChildren(+comment.id) + }) + } + } + + if (list.length) { + return list + } else { + return null + } + } + + console.log(commentLists) } -getMode(); +