From d18a9a057b7b916701ed9a7e582f105b618a20e3 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 20:47:05 +0700 Subject: [PATCH 01/13] Implement simple regexp based highlighter --- .../src/components/CommentBodyHighlighter.js | 69 ++++++++++++++----- .../coral-admin/src/components/IfHasLink.js | 5 +- .../routes/Moderation/components/Comment.js | 4 +- client/coral-admin/src/utils/index.js | 1 + client/coral-admin/src/utils/regexp.js | 1 + package.json | 2 - yarn.lock | 31 +-------- 7 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 client/coral-admin/src/utils/regexp.js diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index 9a430f7c7..f0c9f9c8d 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,25 +1,56 @@ import React from 'react'; -import Highlighter from 'react-highlight-words'; -import Linkify from 'react-linkify'; -const linkify = new Linkify(); +import {linkRegexp} from '../utils/regexp'; + +const wordSeperator = /([.\s'"?!])/; + +function markWords(body, words, index) { + const tokens = body.split(wordSeperator); + const content = []; + let tmp = []; + tokens.forEach((token, i) => { + if (words.indexOf(token.toLowerCase()) >= 0) { + content.push(...tmp); + tmp = []; + content.push({token}); + return; + } + tmp.push(token); + }); + content.push(...tmp); + return content; +} + +function markLinks(body) { + const tokens = body.split(linkRegexp); + const content = []; + let tmp = []; + tokens + .filter((token) => token) + .forEach((token, i) => { + if (token.match(linkRegexp)) { + content.push(...tmp); + tmp = []; + content.push({token}); + return; + } + tmp.push(token); + }); + content.push(...tmp); + return content; +} export default ({suspectWords, bannedWords, body, ...rest}) => { - - const links = linkify.getMatches(body); - const linkText = links ? links.map((link) => link.raw) : []; - - const searchWords = [ - ...suspectWords, - ...bannedWords, - ...linkText - ]; - + const words = [...suspectWords, ...bannedWords].map((word) => word.toLowerCase()); + const content = markLinks(body) + .map((element, index) => { + if (typeof element !== 'string') { + return element; + } + return markWords(element, words, index); + }); return ( - +
+ {content} +
); }; diff --git a/client/coral-admin/src/components/IfHasLink.js b/client/coral-admin/src/components/IfHasLink.js index cd37ea951..e6a96503d 100644 --- a/client/coral-admin/src/components/IfHasLink.js +++ b/client/coral-admin/src/components/IfHasLink.js @@ -1,9 +1,8 @@ import React from 'react'; -import Linkify from 'react-linkify'; -const linkify = new Linkify(); +import {linkRegexp} from '../utils/regexp'; export default ({text, children}) => { - const hasLinks = !!linkify.getMatches(text); + const hasLinks = text.match(linkRegexp); if (!hasLinks) { return null; diff --git a/client/coral-admin/src/routes/Moderation/components/Comment.js b/client/coral-admin/src/routes/Moderation/components/Comment.js index 9d9428bc4..a3edb4196 100644 --- a/client/coral-admin/src/routes/Moderation/components/Comment.js +++ b/client/coral-admin/src/routes/Moderation/components/Comment.js @@ -127,7 +127,7 @@ class Comment extends React.Component {
-

+

{t('comment.view_context')} -

+
mod === 'PRE'; export const getModPath = (type = 'all', assetId) => assetId ? `/admin/moderate/${type}/${assetId}` : `/admin/moderate/${type}`; + diff --git a/client/coral-admin/src/utils/regexp.js b/client/coral-admin/src/utils/regexp.js new file mode 100644 index 000000000..1544824dc --- /dev/null +++ b/client/coral-admin/src/utils/regexp.js @@ -0,0 +1 @@ +export const linkRegexp = /([-a-zA-Z0-9@:%_+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_+.~#?&//=]*)?)/; diff --git a/package.json b/package.json index 29ba80ae8..99939b46d 100644 --- a/package.json +++ b/package.json @@ -154,9 +154,7 @@ "react": "^15.4.2", "react-apollo": "^1.4.12", "react-dom": "^15.4.2", - "react-highlight-words": "^0.6.0", "react-input-autosize": "^1.1.4", - "react-linkify": "^0.1.3", "react-mdl": "^1.7.2", "react-mdl-selectfield": "^0.2.0", "react-recaptcha": "^2.2.6", diff --git a/yarn.lock b/yarn.lock index 1ca8acdbb..1fd1f5711 100644 --- a/yarn.lock +++ b/yarn.lock @@ -922,7 +922,7 @@ babel-register@^6.26.0: mkdirp "^0.5.1" source-map-support "^0.4.15" -babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0, babel-runtime@^6.6.1: +babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0, babel-runtime@^6.6.1: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" dependencies: @@ -3396,12 +3396,6 @@ hide-powered-by@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/hide-powered-by/-/hide-powered-by-1.0.0.tgz#4a85ad65881f62857fc70af7174a1184dccce32b" -highlight-words-core@^1.0.2: - version "1.0.3" - resolved "https://registry.yarnpkg.com/highlight-words-core/-/highlight-words-core-1.0.3.tgz#0886d0e757c8ca3928cbc873042bd544f8f6b2e5" - dependencies: - babel-runtime "^6.11.6" - history@^3.0.0: version "3.3.0" resolved "https://registry.yarnpkg.com/history/-/history-3.3.0.tgz#fcedcce8f12975371545d735461033579a6dae9c" @@ -4395,12 +4389,6 @@ license-webpack-plugin@^1.0.0: dependencies: ejs "^2.5.7" -linkify-it@^1.2.0: - version "1.2.4" - resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-1.2.4.tgz#0773526c317c8fd13bd534ee1d180ff88abf881a" - dependencies: - uc.micro "^1.0.1" - linkify-it@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.0.3.tgz#d94a4648f9b1c179d64fa97291268bdb6ce9434f" @@ -6379,12 +6367,6 @@ react-dom@^15.3.1, react-dom@^15.4.2: object-assign "^4.1.0" prop-types "~15.5.7" -react-highlight-words@^0.6.0: - version "0.6.0" - resolved "https://registry.yarnpkg.com/react-highlight-words/-/react-highlight-words-0.6.0.tgz#e12e9fedda4333e410ea408cdedffc77122020aa" - dependencies: - highlight-words-core "^1.0.2" - react-input-autosize@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/react-input-autosize/-/react-input-autosize-1.1.4.tgz#cbc45072d4084ddc57806db8e3b34e644b8366ac" @@ -6392,13 +6374,6 @@ react-input-autosize@^1.1.4: create-react-class "^15.5.2" prop-types "^15.5.8" -react-linkify@^0.1.3: - version "0.1.3" - resolved "https://registry.yarnpkg.com/react-linkify/-/react-linkify-0.1.3.tgz#6e886180bda6c8fdc5f9f8a7ebe82fc0f48db7ad" - dependencies: - linkify-it "^1.2.0" - tlds "^1.57.0" - react-mdl-selectfield@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/react-mdl-selectfield/-/react-mdl-selectfield-0.2.0.tgz#36e1a97233036c057ab2bdb31ec09ad8d9988411" @@ -7456,10 +7431,6 @@ title-case-minors@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/title-case-minors/-/title-case-minors-1.0.0.tgz#51f17037c294747a1d1cda424b5004c86d8eb115" -tlds@^1.57.0: - version "1.185.0" - resolved "https://registry.yarnpkg.com/tlds/-/tlds-1.185.0.tgz#9d5ddaae379778a98e3edc3a131d46a40cbc3ba4" - tmp@^0.0.31: version "0.0.31" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.31.tgz#8f38ab9438e17315e5dbd8b3657e8bfb277ae4a7" From 65a6c408a721c722f7b8f952b3f1dcdc322669a5 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 20:54:30 +0700 Subject: [PATCH 02/13] Add some docs --- .../src/components/CommentBodyHighlighter.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index f0c9f9c8d..aa3fa1274 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -3,7 +3,9 @@ import {linkRegexp} from '../utils/regexp'; const wordSeperator = /([.\s'"?!])/; -function markWords(body, words, index) { +// markWords looks for `words` inside `body` and highlights them by returning +// an array of React Elements. +function markWords(body, words, keyPrefix) { const tokens = body.split(wordSeperator); const content = []; let tmp = []; @@ -11,7 +13,7 @@ function markWords(body, words, index) { if (words.indexOf(token.toLowerCase()) >= 0) { content.push(...tmp); tmp = []; - content.push({token}); + content.push({token}); return; } tmp.push(token); @@ -20,6 +22,8 @@ function markWords(body, words, index) { return content; } +// markWords looks for links inside `body` and highlights them by returning +// an array of React Elements. function markLinks(body) { const tokens = body.split(linkRegexp); const content = []; @@ -41,11 +45,17 @@ function markLinks(body) { export default ({suspectWords, bannedWords, body, ...rest}) => { const words = [...suspectWords, ...bannedWords].map((word) => word.toLowerCase()); + + // First highlight links. const content = markLinks(body) .map((element, index) => { + + // Keep highlighted links. if (typeof element !== 'string') { return element; } + + // Highlight suspect and banned words inside this part of text. return markWords(element, words, index); }); return ( From ee3cee2a500b6a6e2fc6353deee0572bd16cea68 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 20:55:31 +0700 Subject: [PATCH 03/13] Typo --- client/coral-admin/src/components/CommentBodyHighlighter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index aa3fa1274..f58859648 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -22,7 +22,7 @@ function markWords(body, words, keyPrefix) { return content; } -// markWords looks for links inside `body` and highlights them by returning +// markLinks looks for links inside `body` and highlights them by returning // an array of React Elements. function markLinks(body) { const tokens = body.split(linkRegexp); From 98b95cab86acc5e3427cc2706f2efb14dfb87483 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 21:31:59 +0700 Subject: [PATCH 04/13] Reintroduce linkify --- .../src/components/CommentBodyHighlighter.js | 27 +++++++++---------- .../coral-admin/src/components/IfHasLink.js | 4 +-- client/coral-admin/src/services/linkify.js | 8 ++++++ client/coral-admin/src/utils/index.js | 9 +++++++ client/coral-admin/src/utils/regexp.js | 1 - package.json | 1 + yarn.lock | 4 +++ 7 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 client/coral-admin/src/services/linkify.js delete mode 100644 client/coral-admin/src/utils/regexp.js diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index f58859648..055d24822 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,5 +1,5 @@ import React from 'react'; -import {linkRegexp} from '../utils/regexp'; +import {matchLinks} from '../utils'; const wordSeperator = /([.\s'"?!])/; @@ -25,21 +25,18 @@ function markWords(body, words, keyPrefix) { // markLinks looks for links inside `body` and highlights them by returning // an array of React Elements. function markLinks(body) { - const tokens = body.split(linkRegexp); + const matches = matchLinks(body); const content = []; - let tmp = []; - tokens - .filter((token) => token) - .forEach((token, i) => { - if (token.match(linkRegexp)) { - content.push(...tmp); - tmp = []; - content.push({token}); - return; - } - tmp.push(token); - }); - content.push(...tmp); + let index = 0; + if (matches) { + matches + .forEach((match, i) => { + content.push(body.substring(index, match.index)); + content.push({match.text}); + index = match.lastIndex; + }); + } + content.push(body.substring(index)); return content; } diff --git a/client/coral-admin/src/components/IfHasLink.js b/client/coral-admin/src/components/IfHasLink.js index e6a96503d..73b335815 100644 --- a/client/coral-admin/src/components/IfHasLink.js +++ b/client/coral-admin/src/components/IfHasLink.js @@ -1,8 +1,8 @@ import React from 'react'; -import {linkRegexp} from '../utils/regexp'; +import {matchLinks} from '../utils'; export default ({text, children}) => { - const hasLinks = text.match(linkRegexp); + const hasLinks = !!matchLinks(text); if (!hasLinks) { return null; diff --git a/client/coral-admin/src/services/linkify.js b/client/coral-admin/src/services/linkify.js new file mode 100644 index 000000000..c51d7261b --- /dev/null +++ b/client/coral-admin/src/services/linkify.js @@ -0,0 +1,8 @@ +import LinkifyIt from 'linkify-it'; +import tlds from 'tlds'; + +export function createLinkify() { + const linkify = new LinkifyIt(); + linkify.tlds(tlds); + return linkify; +} diff --git a/client/coral-admin/src/utils/index.js b/client/coral-admin/src/utils/index.js index db3fb132d..f78a7b631 100644 --- a/client/coral-admin/src/utils/index.js +++ b/client/coral-admin/src/utils/index.js @@ -1,3 +1,12 @@ +import LinkifyIt from 'linkify-it'; +import tlds from 'tlds'; +const linkify = new LinkifyIt(); +linkify.tlds(tlds); + +export function matchLinks(text) { + return linkify.match(text); +} + export const isPremod = (mod) => mod === 'PRE'; export const getModPath = (type = 'all', assetId) => diff --git a/client/coral-admin/src/utils/regexp.js b/client/coral-admin/src/utils/regexp.js deleted file mode 100644 index 1544824dc..000000000 --- a/client/coral-admin/src/utils/regexp.js +++ /dev/null @@ -1 +0,0 @@ -export const linkRegexp = /([-a-zA-Z0-9@:%_+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_+.~#?&//=]*)?)/; diff --git a/package.json b/package.json index 99939b46d..dc81e26a0 100644 --- a/package.json +++ b/package.json @@ -175,6 +175,7 @@ "subscriptions-transport-ws": "^0.7.2", "timeago.js": "^2.0.3", "timekeeper": "^1.0.0", + "tlds": "^1.196.0", "url-loader": "^0.5.9", "url-search-params": "^0.9.0", "uuid": "^3.1.0", diff --git a/yarn.lock b/yarn.lock index 1fd1f5711..12a275e76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7431,6 +7431,10 @@ title-case-minors@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/title-case-minors/-/title-case-minors-1.0.0.tgz#51f17037c294747a1d1cda424b5004c86d8eb115" +tlds@^1.196.0: + version "1.196.0" + resolved "https://registry.yarnpkg.com/tlds/-/tlds-1.196.0.tgz#49d74ddbd1f9df30238b3bfef4df82862b5bbb48" + tmp@^0.0.31: version "0.0.31" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.31.tgz#8f38ab9438e17315e5dbd8b3657e8bfb277ae4a7" From 2a655331fca111aefb1b47853b83eda0be6b095a Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 21:46:03 +0700 Subject: [PATCH 05/13] Use same tlds list on server --- graph/mutators/comment.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index c6e335018..89ef47382 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -6,7 +6,9 @@ const ActionsService = require('../../services/actions'); const TagsService = require('../../services/tags'); const CommentsService = require('../../services/comments'); const KarmaService = require('../../services/karma'); -const linkify = require('linkify-it')(); +const tlds = require('tlds'); +const linkify = require('linkify-it')() + .tlds(tlds); const Wordlist = require('../../services/wordlist'); const { CREATE_COMMENT, From 7976aace00e56cec3dd8d9449f905e7028906548 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 22:46:24 +0700 Subject: [PATCH 06/13] Typo --- client/coral-admin/src/components/CommentBodyHighlighter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index 055d24822..3b735f9ce 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,12 +1,12 @@ import React from 'react'; import {matchLinks} from '../utils'; -const wordSeperator = /([.\s'"?!])/; +const wordSeparator = /([.\s'"?!])/; // markWords looks for `words` inside `body` and highlights them by returning // an array of React Elements. function markWords(body, words, keyPrefix) { - const tokens = body.split(wordSeperator); + const tokens = body.split(wordSeparator); const content = []; let tmp = []; tokens.forEach((token, i) => { From a16f101a12fd6cdc86f005fa0d12c99d3f24ea29 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 13 Sep 2017 22:20:32 +0700 Subject: [PATCH 07/13] Match phrases not just words --- .../src/components/CommentBodyHighlighter.js | 97 ++++++++++++++++--- 1 file changed, 81 insertions(+), 16 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index 3b735f9ce..fe5bcff96 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,24 +1,89 @@ import React from 'react'; import {matchLinks} from '../utils'; -const wordSeparator = /([.\s'"?!])/; +const capturingWordSeparator = /([.\s'"?!])/; +const wordSeparator = /[.\s'"?!]/; -// markWords looks for `words` inside `body` and highlights them by returning +// markPhrases looks for `phrases` inside `body` and highlights them by returning // an array of React Elements. -function markWords(body, words, keyPrefix) { - const tokens = body.split(wordSeparator); +function markPhrases(body, phrases, keyPrefix) { + const tokens = body.split(capturingWordSeparator); + const phraseWords = phrases.map((phrase) => phrase.toLowerCase().split(wordSeparator)); const content = []; let tmp = []; - tokens.forEach((token, i) => { - if (words.indexOf(token.toLowerCase()) >= 0) { - content.push(...tmp); - tmp = []; - content.push({token}); - return; + + for (let l = 0; l < tokens.length; l++) { + + // matchedWords is > 0 when a full match was found and contains + // the range length from this index to the end of the match. + let matchedWords = 0; + + // Skip word separators and ''. + if (tokens[l] !== '' && !tokens[l].match(wordSeparator)) { + for (let m = 0; m < phraseWords.length; m++) { + const words = phraseWords[m]; + + // We try to match the full phrase, index keeps track + // of where we are now on the tokens array while matching + // the words of the phrase. + let index = l; + for (let n = 0; n < words.length; n++, index++) { + + // Skip word separators and ''. + while (index < tokens.length && (tokens[index].match(wordSeparator) || tokens[index] === '')) { + index++; + } + + // No more tokens left. + if (index >= tokens.length) { + break; + } + + const token = tokens[index].toLowerCase(); + const word = words[n]; + if (token !== word) { + break; + } + + // Full match! + if (n === words.length - 1) { + + // Save the matched range length into matched words. + matchedWords = index - l + 1; + break; + } + } + + // We matched a word so break out the loop. + if (matchedWords) { + break; + } + } } - tmp.push(token); - }); - content.push(...tmp); + + // We have a match! + if (matchedWords) { + const match = tokens.slice(l, l + matchedWords).join(''); + + // Append whatever we have in `tmp` and clear it. + content.push(tmp.join('')); + tmp = []; + + content.push({match}); + + // Move index further if we matched more than one word. + l += matchedWords - 1; + + continue; + } + + // No match, we just push this into `tmp`. + tmp.push(tokens[l]); + } + + // Append any non matched tokens currently in `tmp`. + content.push(tmp.join('')); + return content; } @@ -41,7 +106,7 @@ function markLinks(body) { } export default ({suspectWords, bannedWords, body, ...rest}) => { - const words = [...suspectWords, ...bannedWords].map((word) => word.toLowerCase()); + const phrases = [...suspectWords, ...bannedWords]; // First highlight links. const content = markLinks(body) @@ -52,8 +117,8 @@ export default ({suspectWords, bannedWords, body, ...rest}) => { return element; } - // Highlight suspect and banned words inside this part of text. - return markWords(element, words, index); + // Highlight suspect and banned phrase inside this part of text. + return markPhrases(element, phrases, index); }); return (
From fa335564eda13e793403912740f3d47c80867ccb Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 14 Sep 2017 00:10:29 +0700 Subject: [PATCH 08/13] Generate RegExp from phrases --- .../src/components/CommentBodyHighlighter.js | 100 ++++-------------- 1 file changed, 21 insertions(+), 79 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index fe5bcff96..64b53b829 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,90 +1,32 @@ import React from 'react'; import {matchLinks} from '../utils'; -const capturingWordSeparator = /([.\s'"?!])/; -const wordSeparator = /[.\s'"?!]/; +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +function generateRegExp(phrases) { + const inner = phrases + .map((phrase) => { + return phrase.split(/\s+/) + .map((word) => escapeRegExp(word)) + .join('[\\s"?!.]+'); + }).join('|'); + + return `(^|[^\\w])(${inner})(?=[^\\w]|$)`; +} // markPhrases looks for `phrases` inside `body` and highlights them by returning // an array of React Elements. function markPhrases(body, phrases, keyPrefix) { - const tokens = body.split(capturingWordSeparator); - const phraseWords = phrases.map((phrase) => phrase.toLowerCase().split(wordSeparator)); - const content = []; - let tmp = []; - - for (let l = 0; l < tokens.length; l++) { - - // matchedWords is > 0 when a full match was found and contains - // the range length from this index to the end of the match. - let matchedWords = 0; - - // Skip word separators and ''. - if (tokens[l] !== '' && !tokens[l].match(wordSeparator)) { - for (let m = 0; m < phraseWords.length; m++) { - const words = phraseWords[m]; - - // We try to match the full phrase, index keeps track - // of where we are now on the tokens array while matching - // the words of the phrase. - let index = l; - for (let n = 0; n < words.length; n++, index++) { - - // Skip word separators and ''. - while (index < tokens.length && (tokens[index].match(wordSeparator) || tokens[index] === '')) { - index++; - } - - // No more tokens left. - if (index >= tokens.length) { - break; - } - - const token = tokens[index].toLowerCase(); - const word = words[n]; - if (token !== word) { - break; - } - - // Full match! - if (n === words.length - 1) { - - // Save the matched range length into matched words. - matchedWords = index - l + 1; - break; - } - } - - // We matched a word so break out the loop. - if (matchedWords) { - break; - } - } + const regexp = new RegExp(generateRegExp(phrases), 'iu'); + const tokens = body.split(regexp); + return tokens.map((token, i) => { + if (i % 3 === 2) { + return {token}; } - - // We have a match! - if (matchedWords) { - const match = tokens.slice(l, l + matchedWords).join(''); - - // Append whatever we have in `tmp` and clear it. - content.push(tmp.join('')); - tmp = []; - - content.push({match}); - - // Move index further if we matched more than one word. - l += matchedWords - 1; - - continue; - } - - // No match, we just push this into `tmp`. - tmp.push(tokens[l]); - } - - // Append any non matched tokens currently in `tmp`. - content.push(tmp.join('')); - - return content; + return token; + }); } // markLinks looks for links inside `body` and highlights them by returning From abc651c55d3d1d5a3bd495625cc7cac6c73172e0 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 14 Sep 2017 00:48:19 +0700 Subject: [PATCH 09/13] Add some optimizations --- .../src/components/CommentBodyHighlighter.js | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index 64b53b829..f0b085325 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -1,5 +1,6 @@ import React from 'react'; import {matchLinks} from '../utils'; +import memoize from 'lodash/memoize'; function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string @@ -7,26 +8,33 @@ function escapeRegExp(string) { function generateRegExp(phrases) { const inner = phrases - .map((phrase) => { - return phrase.split(/\s+/) + .map((phrase) => + phrase.split(/\s+/) .map((word) => escapeRegExp(word)) - .join('[\\s"?!.]+'); - }).join('|'); + .join('[\\s"?!.]+') + ).join('|'); - return `(^|[^\\w])(${inner})(?=[^\\w]|$)`; + return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, 'iu'); } -// markPhrases looks for `phrases` inside `body` and highlights them by returning +// Generate a regular expression detecting `suspectWords` and `bannedWords` phrases. +function getPhrasesRegexp(suspectWords, bannedWords) { + return generateRegExp([...suspectWords, ...bannedWords]); +} + +// Memoized version as arguments rarely change. +const getPhrasesRegexpMemoized = memoize(getPhrasesRegexp); + +// markPhrases looks for `supsectWords` and `bannedWords` inside `body` and highlights them by returning // an array of React Elements. -function markPhrases(body, phrases, keyPrefix) { - const regexp = new RegExp(generateRegExp(phrases), 'iu'); +function markPhrases(body, suspectWords, bannedWords, keyPrefix) { + const regexp = getPhrasesRegexpMemoized(suspectWords, bannedWords); const tokens = body.split(regexp); - return tokens.map((token, i) => { - if (i % 3 === 2) { - return {token}; - } - return token; - }); + return tokens.map((token, i) => + i % 3 === 2 + ? {token} + : token + ); } // markLinks looks for links inside `body` and highlights them by returning @@ -48,7 +56,6 @@ function markLinks(body) { } export default ({suspectWords, bannedWords, body, ...rest}) => { - const phrases = [...suspectWords, ...bannedWords]; // First highlight links. const content = markLinks(body) @@ -60,7 +67,7 @@ export default ({suspectWords, bannedWords, body, ...rest}) => { } // Highlight suspect and banned phrase inside this part of text. - return markPhrases(element, phrases, index); + return markPhrases(element, suspectWords, bannedWords, index); }); return (
From ad361413c799ad0b794418e7fa1ae1a13e2bb70d Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 14 Sep 2017 17:36:04 +0700 Subject: [PATCH 10/13] Reimplement wordlist on server using same regexp solution --- .../src/components/CommentBodyHighlighter.js | 1 + services/wordlist.js | 144 +++++------------- test/server/services/wordlist.js | 60 +------- 3 files changed, 45 insertions(+), 160 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index f0b085325..e27d3ce6d 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -6,6 +6,7 @@ function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } +// generate a regulare expression that catches the `phrases`. function generateRegExp(phrases) { const inner = phrases .map((phrase) => diff --git a/services/wordlist.js b/services/wordlist.js index 3a0cc2c71..e3aad5789 100644 --- a/services/wordlist.js +++ b/services/wordlist.js @@ -1,13 +1,39 @@ const debug = require('debug')('talk:services:wordlist'); const _ = require('lodash'); -const {RegexpTokenizer} = require('natural'); -const tokenizer = new RegexpTokenizer({pattern: /[.\s'"?!]/}); -const nameTokenizer = new RegexpTokenizer({pattern: /_/}); const SettingsService = require('./settings'); const Errors = require('../errors'); +const memoize = require('lodash/memoize'); -// REGEX to prevent emoji's from entering the wordlist. -const EMOJI_REGEX = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*/; +/** + * Escape string for special regular expression characters. + */ +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +/** + * Generate a regulare expression that catches the `phrases`. + */ +function generateRegExp(phrases) { + const inner = phrases + .map((phrase) => + phrase.split(/\s+/) + .map((word) => escapeRegExp(word)) + .join('[\\s"?!.]+') + ).join('|'); + + return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, 'iu'); +} + +/** + * Memoized version of generateRegExp. + */ +const generateRegExpMemoized = memoize(generateRegExp, (phrases) => phrases.join(',')); + +/** + * Never matching regexp that exits immediately. + */ +const neverMatch = /(?!)/; /** * The root wordlist object. @@ -16,9 +42,9 @@ const EMOJI_REGEX = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\ud class Wordlist { constructor() { - this.lists = { - banned: [], - suspect: [] + this.regexp = { + banned: neverMatch, + suspect: neverMatch, }; } @@ -48,7 +74,9 @@ class Wordlist { return; } - this.lists[k] = Wordlist.parseList(lists[k]); + this.regexp[k] = lists[k] && lists[k].length > 0 + ? generateRegExpMemoized(lists[k]) + : neverMatch; debug(`Added ${lists[k].length} words to the ${k} wordlist.`); }); @@ -56,92 +84,6 @@ class Wordlist { return Promise.resolve(this); } - /** - * Parses the list content. - * @param {Array} list array of words to parse for a list. - * @return {Array} the parsed list - */ - static parseList(list) { - return _.uniq(list.filter((word) => { - if (EMOJI_REGEX.test(word)) { - return false; - } - - return true; - }) - .map((word) => { - if (word.length === 1) { - return [word]; - } - - return tokenizer.tokenize(word.toLowerCase()); - }) - .filter((tokens) => { - if (tokens.length === 0) { - return false; - } - - return true; - })); - } - - /** - * Tests the phrase to see if it contains any of the defined blockwords. - * @param {String} phrase value to check for blockwords. - * @return {Boolean} true if a blockword is found, false otherwise. - */ - match(list, phrase, tk = tokenizer) { - - // Lowercase the word to ensure that we don't miss a match due to - // capitalization. - let lowerPhraseWords = tk.tokenize(phrase.toLowerCase()); - - // This will return true in the event that at least one blockword is found - // in the phrase. - return list.some((blockphrase) => { - - // First, let's see if we can find the first word in the blockphrase in the - // source phrase. - let idx = lowerPhraseWords.indexOf(blockphrase[0]); - - if (idx === -1) { - - // The first blockword in the blockphrase did not match the source phrase - // anywhere. - return false; - } - - // Here we'll quick respond with true in the event that the blockphrase was - // just a single word. - if (blockphrase.length === 1) { - return true; - } - - // We found the first word in the source phrase! Lets ensure it matches the - // rest of the blockphrase... - - // Check to see if it even has the length to support this word! - if (lowerPhraseWords.length < idx + blockphrase.length - 1) { - - // We couldn't possibly have the entire phrase here because we don't have - // enough entries! - return false; - } - - for (let i = 1; i < blockphrase.length; i++) { - - // Check to see if the next word also matches! - if (lowerPhraseWords[idx + i] !== blockphrase[i]) { - return false; - } - } - - // We've walked over all the words of the blockphrase, and haven't had a - // mismatch... It does contain the whole word! - return true; - }); - } - /** * Scans a specific field for wordlist violations. */ @@ -156,7 +98,7 @@ class Wordlist { } // Check if the field contains a banned word. - if (this.match(this.lists.banned, phrase)) { + if (this.regexp.banned.test(phrase)) { debug(`the field "${fieldName}" contained a phrase "${phrase}" which contained a banned word/phrase`); errors.banned = Errors.ErrContainsProfanity; @@ -166,8 +108,8 @@ class Wordlist { return errors; } - // Check if the field contains a banned word. - if (this.match(this.lists.suspect, phrase)) { + // Check if the field contains a suspected word. + if (this.regexp.suspect.test(phrase)) { debug(`the field "${fieldName}" contained a phrase "${phrase}" which contained a suspected word/phrase`); errors.suspect = Errors.ErrContainsProfanity; @@ -231,16 +173,12 @@ class Wordlist { return wl .load() .then(() => { - if (!wl.checkName(wl.lists.banned, username)) { + if (wl.regexp.banned.test(username)) { return Errors.ErrContainsProfanity; } }); } - checkName(list, name) { - return !this.match(list, name, nameTokenizer); - } - /** * Connect middleware for scanning request bodies for wordlisted words and * attaching a ErrContainsProfanity to the req.wordlisted parameter, otherwise diff --git a/test/server/services/wordlist.js b/test/server/services/wordlist.js index 27460f049..b43dd2bec 100644 --- a/test/server/services/wordlist.js +++ b/test/server/services/wordlist.js @@ -27,44 +27,10 @@ describe('services.Wordlist', () => { beforeEach(() => SettingsService.init(settings)); - describe('#init', () => { + describe('#regexp', () => { before(() => wordlist.upsert(wordlists)); - it('parses the wordlists correctly', () => { - expect(wordlist.lists.banned).to.deep.equal([ - [ 'cookies' ], - [ 'how', 'to', 'do', 'bad', 'things' ], - [ 'how', 'to', 'do', 'really', 'bad', 'things' ], - [ 's', 'h', 'i', 't' ], - [ '$hit' ], - [ 'p**ch' ], - [ 'p*ch' ], - ]); - expect(wordlist.lists.suspect).to.deep.equal([ - [ 'do', 'bad', 'things' ], - ]); - }); - - }); - - describe('#parseList', () => { - it('does not include emojis in the wordlist', () => { - let list = Wordlist.parseList([ - '🖕', - '🖕 asdf', - 'asd🖕asdf', - 'asd🖕', - ]); - - expect(list).to.have.length(0); - }); - }); - - const bannedList = Wordlist.parseList(wordlists.banned); - - describe('#match', () => { - it('does match on a bad word', () => { [ 'how to do really bad things', @@ -76,7 +42,7 @@ describe('services.Wordlist', () => { 'This stuff is $hit!', 'That\'s a p**ch!', ].forEach((word) => { - expect(wordlist.match(bannedList, word)).to.be.true; + expect(wordlist.regexp.banned.test(word)).to.be.true; }); }); @@ -90,7 +56,7 @@ describe('services.Wordlist', () => { 'I have bad $ hit lling', 'That\'s a p***ch!', ].forEach((word) => { - expect(wordlist.match(bannedList, word)).to.be.false; + expect(wordlist.regexp.banned.test(word)).to.be.false; }); }); @@ -129,26 +95,6 @@ describe('services.Wordlist', () => { }); - describe('#checkName', () => { - [ - 'flowers', - 'joy', - 'lots_of_candy' - ].forEach((username) => { - it(`does not match on list=banned name=${username}`, () => { - expect(wordlist.checkName(bannedList, username)).to.be.true; - }); - }); - - [ - 'cookies' - ].forEach((username) => { - it(`does match on list=banned name=${username}`, () => { - expect(wordlist.checkName(bannedList, username)).to.be.false; - }); - }); - }); - describe('#filter', () => { before(() => wordlist.upsert(wordlists)); From 76a6a0e82b9ea743916e80cdee53c3f6943a05c2 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 14 Sep 2017 18:10:24 +0700 Subject: [PATCH 11/13] Remove natural package --- package.json | 1 - yarn.lock | 23 +---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/package.json b/package.json index dc81e26a0..3db0f8d7a 100644 --- a/package.json +++ b/package.json @@ -136,7 +136,6 @@ "morgan": "^1.8.2", "ms": "^2.0.0", "murmurhash-js": "^1.0.0", - "natural": "^0.5.4", "node-emoji": "^1.8.1", "node-fetch": "^1.7.2", "nodemailer": "^2.6.4", diff --git a/yarn.lock b/yarn.lock index 12a275e76..585b0a4b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1016,10 +1016,6 @@ binary-extensions@^1.0.0: version "1.8.0" resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.8.0.tgz#48ec8d16df4377eae5fa5884682480af4d95c774" -bindings@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11" - block-stream@*: version "0.0.9" resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a" @@ -4970,7 +4966,7 @@ mute-stream@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" -nan@^2.3.0, nan@^2.4.0: +nan@^2.3.0: version "2.5.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.5.0.tgz#aa8f1e34531d807e9e27755b234b4a6ec0c152a8" @@ -4990,16 +4986,6 @@ natural@^0.2.0: sylvester ">= 0.0.12" underscore ">=1.3.1" -natural@^0.5.4: - version "0.5.4" - resolved "https://registry.yarnpkg.com/natural/-/natural-0.5.4.tgz#ace41c1655daca2912dfbf99ad7b05314e205f54" - dependencies: - apparatus ">= 0.0.9" - sylvester ">= 0.0.12" - underscore ">=1.3.1" - optionalDependencies: - webworker-threads ">=0.6.2" - negotiator@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9" @@ -7794,13 +7780,6 @@ webpack@^2.3.1: webpack-sources "^0.2.3" yargs "^6.0.0" -webworker-threads@>=0.6.2: - version "0.7.11" - resolved "https://registry.yarnpkg.com/webworker-threads/-/webworker-threads-0.7.11.tgz#9d54dfaa8d5ea3308833084680636b584a8aacaa" - dependencies: - bindings "^1.2.1" - nan "^2.4.0" - whatwg-fetch@>=0.10.0, whatwg-fetch@^2.0.0: version "2.0.3" resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84" From c41ab58109736b1b95b3700788e436386620fb51 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 14 Sep 2017 21:11:40 +0700 Subject: [PATCH 12/13] Allow Moderator to have read access on assets and settings --- routes/api/assets/index.js | 9 +- routes/api/index.js | 5 +- routes/api/settings/index.js | 5 +- test/server/routes/api/assets/index.js | 108 ++++++++++++++--------- test/server/routes/api/settings/index.js | 30 ++++--- 5 files changed, 95 insertions(+), 62 deletions(-) diff --git a/routes/api/assets/index.js b/routes/api/assets/index.js index 87014b86e..1b2b0c7a9 100644 --- a/routes/api/assets/index.js +++ b/routes/api/assets/index.js @@ -1,5 +1,6 @@ const express = require('express'); const router = express.Router(); +const authorization = require('../../../middleware/authorization'); const errors = require('../../../errors'); const AssetsService = require('../../../services/assets'); @@ -33,7 +34,7 @@ const FilterOpenAssets = (query, filter) => { }; // List assets. -router.get('/', async (req, res, next) => { +router.get('/', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, next) => { const { limit = 20, @@ -72,7 +73,7 @@ router.get('/', async (req, res, next) => { }); // Get an asset by id. -router.get('/:asset_id', async (req, res, next) => { +router.get('/:asset_id', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, next) => { try { // Send back the asset. @@ -87,7 +88,7 @@ router.get('/:asset_id', async (req, res, next) => { } }); -router.put('/:asset_id/settings', async (req, res, next) => { +router.put('/:asset_id/settings', authorization.needed('ADMIN'), async (req, res, next) => { try { await AssetsService.overrideSettings(req.params.asset_id, req.body); res.status(204).end(); @@ -96,7 +97,7 @@ router.put('/:asset_id/settings', async (req, res, next) => { } }); -router.put('/:asset_id/status', async (req, res, next) => { +router.put('/:asset_id/status', authorization.needed('ADMIN'), async (req, res, next) => { const { closedAt, closedMessage diff --git a/routes/api/index.js b/routes/api/index.js index eccd8d435..e44f1e7e5 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -1,5 +1,4 @@ const express = require('express'); -const authorization = require('../../middleware/authorization'); const pkg = require('../../package.json'); const router = express.Router(); @@ -8,8 +7,8 @@ router.get('/', (req, res) => { res.json({version: pkg.version}); }); -router.use('/assets', authorization.needed('ADMIN'), require('./assets')); -router.use('/settings', authorization.needed('ADMIN'), require('./settings')); +router.use('/assets', require('./assets')); +router.use('/settings', require('./settings')); router.use('/auth', require('./auth')); router.use('/users', require('./users')); router.use('/account', require('./account')); diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index fb54494d1..5202f9a56 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -1,9 +1,10 @@ const express = require('express'); const SettingsService = require('../../../services/settings'); +const authorization = require('../../../middleware/authorization'); const router = express.Router(); -router.get('/', async (req, res, next) => { +router.get('/', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, next) => { try { let settings = await SettingsService.retrieve(); res.json(settings); @@ -12,7 +13,7 @@ router.get('/', async (req, res, next) => { } }); -router.put('/', async (req, res, next) => { +router.put('/', authorization.needed('ADMIN'), async (req, res, next) => { try { await SettingsService.update(req.body); res.status(204).end(); diff --git a/test/server/routes/api/assets/index.js b/test/server/routes/api/assets/index.js index 855b30271..a07b154d4 100644 --- a/test/server/routes/api/assets/index.js +++ b/test/server/routes/api/assets/index.js @@ -37,78 +37,88 @@ describe('/api/v1/assets', () => { describe('#get', () => { it('should return all assets without a search query', async () => { - const res = await chai.request(app) - .get('/api/v1/assets') - .set(passport.inject({roles: ['ADMIN']})); + for (const role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/assets') + .set(passport.inject({roles: [role]})); - const body = res.body; + const body = res.body; - expect(body).to.have.property('count', 2); - expect(body).to.have.property('result'); + expect(body).to.have.property('count', 2); + expect(body).to.have.property('result'); - const assets = body.result; + const assets = body.result; - expect(assets).to.have.length(2); + expect(assets).to.have.length(2); + } }); it('should return assets that we search for', async () => { - const res = await chai.request(app) - .get('/api/v1/assets?search=term2') - .set(passport.inject({roles: ['ADMIN']})); + for (const role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/assets?search=term2') + .set(passport.inject({roles: [role]})); - const body = res.body; + const body = res.body; - expect(body).to.have.property('count', 1); - expect(body).to.have.property('result'); + expect(body).to.have.property('count', 1); + expect(body).to.have.property('result'); - const assets = body.result; + const assets = body.result; - expect(assets).to.have.length(1); + expect(assets).to.have.length(1); - const asset = assets[0]; + const asset = assets[0]; - expect(asset).to.have.property('url', 'https://coralproject.net/news/asset2'); - expect(asset).to.have.property('title', 'Asset 2'); + expect(asset).to.have.property('url', 'https://coralproject.net/news/asset2'); + expect(asset).to.have.property('title', 'Asset 2'); + } }); it('should not return assets that we do not search for', async () => { - const res = await chai.request(app) - .get('/api/v1/assets?search=term3') - .set(passport.inject({roles: ['ADMIN']})); - const body = res.body; + for (const role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/assets?search=term3') + .set(passport.inject({roles: [role]})); + const body = res.body; - expect(body).to.have.property('count', 0); - expect(body).to.have.property('result'); + expect(body).to.have.property('count', 0); + expect(body).to.have.property('result'); - expect(body.result).to.be.empty; + expect(body.result).to.be.empty; + } }); it('should return only closed assets', async () => { - const res = await chai.request(app) - .get('/api/v1/assets?filter=closed') - .set(passport.inject({roles: ['ADMIN']})); - const body = res.body; + for (const role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/assets?filter=closed') + .set(passport.inject({roles: [role]})); + const body = res.body; - expect(body).to.have.property('count', 1); - expect(body).to.have.property('result'); + expect(body).to.have.property('count', 1); + expect(body).to.have.property('result'); - const assets = body.result; + const assets = body.result; - expect(assets[0]).to.have.property('title', 'Asset 1'); + expect(assets[0]).to.have.property('title', 'Asset 1'); + } }); it('should return only opened assets', async () => { - const res = await chai.request(app) - .get('/api/v1/assets?filter=open') - .set(passport.inject({roles: ['ADMIN']})); - const body = res.body; + for (const role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/assets?filter=open') + .set(passport.inject({roles: [role]})); + const body = res.body; - expect(body).to.have.property('count', 1); - expect(body).to.have.property('result'); + expect(body).to.have.property('count', 1); + expect(body).to.have.property('result'); - const assets = body.result; + const assets = body.result; - expect(assets[0]).to.have.property('title', 'Asset 2'); + expect(assets[0]).to.have.property('title', 'Asset 2'); + } }); }); @@ -133,6 +143,20 @@ describe('/api/v1/assets', () => { expect(closedAsset).to.have.property('isClosed', true); expect(closedAsset).to.have.property('closedAt').and.to.not.equal(null); }); + + it('should require ADMIN role', async () => { + const today = Date.now(); + + const asset = await AssetsService.findOrCreateByUrl('http://test.com'); + expect(asset).to.have.property('isClosed', false); + expect(asset).to.have.property('closedAt', null); + + const promise = chai.request(app) + .put(`/api/v1/assets/${asset.id}/status`) + .set(passport.inject({roles: ['MODERATOR']})) + .send({closedAt: today}); + await expect(promise).to.eventually.be.rejected; + }); }); }); diff --git a/test/server/routes/api/settings/index.js b/test/server/routes/api/settings/index.js index c7704fc7e..d3728ff2c 100644 --- a/test/server/routes/api/settings/index.js +++ b/test/server/routes/api/settings/index.js @@ -16,17 +16,17 @@ describe('/api/v1/settings', () => { describe('#get', () => { - it('should return a settings object', () => { - return chai.request(app) - .get('/api/v1/settings') - .set(passport.inject({ - roles: ['ADMIN'] - })) - .then((res) => { - expect(res).to.have.status(200); - expect(res).to.be.json; - expect(res.body).to.have.property('moderation', 'PRE'); - }); + it('should return a settings object', async () => { + for (let role of ['ADMIN', 'MODERATOR']) { + const res = await chai.request(app) + .get('/api/v1/settings') + .set(passport.inject({ + roles: [role] + })); + expect(res).to.have.status(200); + expect(res).to.be.json; + expect(res.body).to.have.property('moderation', 'PRE'); + } }); }); @@ -46,6 +46,14 @@ describe('/api/v1/settings', () => { expect(settings).to.have.property('moderation', 'POST'); }); }); + + it('should require ADMIN role', () => { + const promise = chai.request(app) + .put('/api/v1/settings') + .set(passport.inject({roles: ['MODERATOR']})) + .send({moderation: 'POST'}); + return expect(promise).to.eventually.be.rejected; + }); }); }); From cf66c102629d4d6de072383c003f6f1a2a00f23b Mon Sep 17 00:00:00 2001 From: Kim Gardner Date: Thu, 14 Sep 2017 16:45:19 +0100 Subject: [PATCH 13/13] Update README --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4bec86d07..57562fc1a 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,14 @@ Online comments are broken. Our open-source Talk tool rethinks how moderation, c Third party licenses are available via the `/client/3rdpartylicenses.txt` endpoint when the server is running with built assets. -## Important Links +## Try Talk! -- Developer Documentation & Setup Guides: https://coralproject.github.io/talk/ +- Developer Documentation & Setup Guides: https://coralproject.github.io/talk/ (includes Installation Guide, Quickstart, Plugin Guide, API Docs, and more) + +## Roadmap and Release Schedule + +- Talk Roadmap: https://www.pivotaltracker.com/n/projects/1863625 -- Pivotal Tracker Backlog & Release Schedule: https://www.pivotaltracker.com/n/projects/1863625 ## Learn More about Coral