From 14246cbe4671e6796e429986cf21bb57295be884 Mon Sep 17 00:00:00 2001 From: hylom Date: Fri, 26 May 2017 21:51:30 +0900 Subject: [PATCH] fix comment post with captcha --- src/newslash_web/public/js/comment-tree.js | 91 ++++++++++++---------- src/newslash_web/public/js/vue-newslash.js | 24 +++++- .../templates/common/comment/comment_form.html.tt2 | 4 +- src/newslash_web/templates/common/js_vars.html.tt2 | 9 +++ src/newslash_web/templates/story/story.html.tt2 | 20 ++--- 5 files changed, 89 insertions(+), 59 deletions(-) diff --git a/src/newslash_web/public/js/comment-tree.js b/src/newslash_web/public/js/comment-tree.js index 8a4be5e5..a4ef57ef 100644 --- a/src/newslash_web/public/js/comment-tree.js +++ b/src/newslash_web/public/js/comment-tree.js @@ -4,19 +4,21 @@ var commentTree = {}; /* ************************************************/ commentTree.init = function () { - var getUrl = "/api/v1/comment?discussion_id=" + page.discussionID; - // comment base class - var Comment = function () { + var Comment = function (comment) { + comment = comment || {}; + this.rawTitle = ''; this.rawComment = ''; - this.subject = ''; - this.comment = ''; - this.author = ''; - this.date = ''; - this.cid = 0; - this.signature = ''; - this.pid = 0; + this.subject = comment.subject || '' ; + this.comment = comment.comment || ''; + this.author = comment.author || ''; + this.date = comment.date || ''; + this.cid = comment.cid || 0; + this.signature = comment.signature || ''; + this.pid = comment.pid || 0; + this.children = comment.children || []; + this.showForm = 0; this.showPreview = 0; this.message = ''; @@ -38,6 +40,23 @@ commentTree.init = function () { }); } + function countHiddenItems(comment) { + if (!comment || !comment.children) { + console.log("countHiddenItems: invalid parameter given"); + return 0; + } + var result = [1, 0]; + for (var i = 0; i < comment.children.length; i++) { + var ret = countHiddenItems(comment.children[i]); + result[0] += ret[0]; + result[1] += ret[1]; + } + if (comment.points < userConfig.show_threshold) { + result[1]++; + } + return result; + } + Vue.component('comment-tree', { template: '#comment-tree-template', data: function () { @@ -65,11 +84,11 @@ commentTree.init = function () { }, computed: {}, methods: { - countHiddenItems: countHiddenItems, + //countHiddenItems: countHiddenItems, saveConfig: saveConfig, }, created: function () { - this.$http.get(getUrl).then( + this.$newslash.getComments( (resp) => { // success initializeComments(resp.body.comments); this.comments = resp.body.comments; @@ -80,23 +99,6 @@ commentTree.init = function () { }, }); - function countHiddenItems(comment) { - if (!comment || !comment.children) { - console.log("countHiddenItems: invalid parameter given"); - return 0; - } - var result = [1, 0]; - for (var i = 0; i < comment.children.length; i++) { - var ret = this.countHiddenItems(comment.children[i]); - result[0] += ret[0]; - result[1] += ret[1]; - } - if (comment.points < this.userConfig.show_threshold) { - result[1]++; - } - return result; - } - function saveConfig() { const data = { type: "config", @@ -150,11 +152,14 @@ commentTree.init = function () { return this.comment.points < userConfig.show_threshold; }, hiddenBox: function () { + if (this.comment.showPreview) { + return 0; + } if (this.comment.points >= userConfig.show_threshold) { return 0; } // 子アイテムの少なくとも1つが表示なら -> 「1件の非表示コメント」表示 - var count = this.countHiddenItems(this.comment); + var count = countHiddenItems(this.comment); if (count[0] != count [1]) { return 1; } @@ -166,7 +171,7 @@ commentTree.init = function () { return 0; } // 子アイテムの少なくとも1つが表示なら -> 「1件の非表示コメント」表示 - const count = this.countHiddenItems(this.comment); + const count = countHiddenItems(this.comment); if (count[0] != count [1]) { return 1; } @@ -203,7 +208,8 @@ commentTree.init = function () { return; } // get moderation data - this.$http.get(this.urls.moderate + "?cid=" + this.comment.cid).then( + //this.$http.get(this.urls.moderate + "?cid=" + this.comment.cid).then( + this.$newslash.getModerations(this.comment.cid, (response) => { // success if (response.body.sec_token) { this.secToken = response.body.sec_token; @@ -235,7 +241,7 @@ commentTree.init = function () { function moderate() { var data = { reason: Number(this.moderateReason), - discussion_id: Number(this.discussion_id), + discussion_id: page.discussionID, cid: Number(this.comment.cid), action: 'moderate', }; @@ -323,12 +329,12 @@ commentTree.init = function () { var data = { title: reply.rawTitle, comment: reply.rawComment, - discussion_id: this.discussion_id, - stoid: this.stoid, + discussion_id: page.discussionID, + stoid: page.stoid, pid: reply.pid, action: 'preview', }; - this.$http.post(this.urls.commentPost, data).then( + this.$newslash.post("comment", data, {noCaptcha: 1}, (response) => { // success reply.subject = response.body.title; reply.comment = response.body.comment; @@ -355,18 +361,19 @@ commentTree.init = function () { var data = { title: reply.rawTitle, comment: reply.rawComment, - discussion_id: this.discussion_id, - stoid: this.stoid, + discussion_id: page.discussionID, + stoid: page.stoid, pid: reply.pid, action: 'post', }; - this.$http.post(this.urls.commentPost, data).then( + this.$newslash.post("comment", data, (response) => { // success - const newComment = response.body.new_comment; - comments.initComment(newComment, this.reply.parent); + var newComment = new Comment(response.body.new_comment); + //comments.initComment(newComment, this.reply.parent); + reply.showForm = false; reply.showPreview = false; - const children = reply.parent.children; + var children = reply.parent.children; if (children) { reply.parent.children.unshift(newComment); } else { diff --git a/src/newslash_web/public/js/vue-newslash.js b/src/newslash_web/public/js/vue-newslash.js index 5afcc56b..c603708a 100644 --- a/src/newslash_web/public/js/vue-newslash.js +++ b/src/newslash_web/public/js/vue-newslash.js @@ -8,6 +8,7 @@ VueNewslash.prototype.postUrlOf = { submission: '/api/v1/submission', story: '/api/v1/story', journal: '/api/v1/journal', + comment: '/api/v1/comment', }; VueNewslash.prototype.executeCaptcha = function executeCaptcha(onSuccess, onFail) { @@ -40,7 +41,8 @@ VueNewslash.prototype.post = function post(type, data, options, onSuccess, onFai onFail({}); return; } - + + options = options || {}; if (typeof options === "function") { onFail = onSuccess; onSuccess = options; @@ -51,7 +53,7 @@ VueNewslash.prototype.post = function post(type, data, options, onSuccess, onFai var noCaptcha = options.noCaptcha || 0; // check captcha - if (grecaptcha + if (typeof grecaptcha !== 'undefined' && !noCaptcha && (!user || !user.is_login)) { this.executeCaptcha( @@ -99,6 +101,10 @@ VueNewslash.prototype.getUrlOf = { submission: '/api/v1/submission', story: '/api/v1/story', journal: '/api/v1/journal', + comment: '/api/v1/comment', + moderation: "/api/v1/moderation", + metamoderation: "/api/v1/metamoderation", + user: "/api/v1/user", }; VueNewslash.prototype.getSubmissionByID = function getSubmissionByID(subid, onSuccess, onFail) { @@ -111,6 +117,20 @@ VueNewslash.prototype.getStoryByID = function getStoryByID(stoid, onSuccess, onF this.vm.$http.get(url).then(onSuccess, onFail); }; +VueNewslash.prototype.getComments = function getComments(discusID, onSuccess, onFail) { + if (typeof discusID === "function") { + onFail = onSuccess; + onSuccess = discusID; + discusID = page.discussionID || ""; + } + var url = this.getUrlOf.comment + "?discussion_id=" + discusID; + this.vm.$http.get(url).then(onSuccess, onFail); +}; + +VueNewslash.prototype.getModerations = function getModerations(cid, onSuccess, onFail) { + var url = this.getUrlOf.moderation + "?cid=" + cid; + this.vm.$http.get(url).then(onSuccess, onFail); +}; /* * class functions diff --git a/src/newslash_web/templates/common/comment/comment_form.html.tt2 b/src/newslash_web/templates/common/comment/comment_form.html.tt2 index 93c87e6b..8850e7dc 100644 --- a/src/newslash_web/templates/common/comment/comment_form.html.tt2 +++ b/src/newslash_web/templates/common/comment/comment_form.html.tt2 @@ -18,6 +18,8 @@

- +

+ +[%- INCLUDE common/captcha/recaptcha -%] diff --git a/src/newslash_web/templates/common/js_vars.html.tt2 b/src/newslash_web/templates/common/js_vars.html.tt2 index 813c0e65..1e6fbc5e 100644 --- a/src/newslash_web/templates/common/js_vars.html.tt2 +++ b/src/newslash_web/templates/common/js_vars.html.tt2 @@ -10,4 +10,13 @@ [%- END %] }; var page = {}; + + [%- IF story.content_type == "story" -%] + page.type = 'story'; + page.stoid = [% story.stoid %]; + page.sid = '[% story.sid %]'; + page.archived = [% IF discussion.type == 'archived' %]1[% ELSE %]0[% END %]; + page.discussionID = [% story.discussion %]; + [%- END -%] + diff --git a/src/newslash_web/templates/story/story.html.tt2 b/src/newslash_web/templates/story/story.html.tt2 index b772ae5c..414d5648 100644 --- a/src/newslash_web/templates/story/story.html.tt2 +++ b/src/newslash_web/templates/story/story.html.tt2 @@ -1,11 +1,4 @@ -[% WRAPPER common/layout enable_sidebar=1 enable_vuejs=1 short_header=1 %] - +[% WRAPPER common/layout enable_sidebar=1 short_header=1 %] [% INCLUDE common/components/comment_tree %] [% IF user.editor || user.is_admin -%] @@ -46,11 +39,10 @@
- -[% IF user.editor || user.is_admin -%] - -[% END %] + +[%- IF user.editor || user.is_admin -%] + +[%- END -%] + [% END %] -- 2.11.0