From 69499847e413f5dc646c9fed1c35153f296a9dd8 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 11 Dec 2017 12:32:43 +0000 Subject: [PATCH] Refactor front end code into validateCaptcha service --- services/web/app/views/layout.pug | 12 +++++++--- .../web/app/views/project/editor/share.pug | 8 ------- services/web/public/coffee/ide.coffee | 1 + .../ShareProjectModalController.coffee | 21 +--------------- services/web/public/coffee/main.coffee | 1 + .../coffee/services/validateCaptcha.coffee | 24 +++++++++++++++++++ 6 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 services/web/public/coffee/services/validateCaptcha.coffee diff --git a/services/web/app/views/layout.pug b/services/web/app/views/layout.pug index 34e78a65f9..e12d7d6f81 100644 --- a/services/web/app/views/layout.pug +++ b/services/web/app/views/layout.pug @@ -53,9 +53,6 @@ html(itemscope, itemtype='http://schema.org/Product') - else script(type='text/javascript'). window.ga = function() { console.log("would send to GA", arguments) }; - - - if (settings.recaptcha) - script(src="https://www.google.com/recaptcha/api.js") script(type="text/javascript"). window.csrfToken = "#{csrfToken}"; @@ -100,6 +97,15 @@ html(itemscope, itemtype='http://schema.org/Product') } body + if(settings.recaptcha) + script(src="https://www.google.com/recaptcha/api.js?render=explicit") + div( + id="recaptcha" + class="g-recaptcha" + data-sitekey=settings.recaptcha.siteKey + data-size="invisible" + data-badge="inline" + ) - if(typeof(suppressSystemMessages) == "undefined") .system-messages( diff --git a/services/web/app/views/project/editor/share.pug b/services/web/app/views/project/editor/share.pug index 0b56bbc4f5..98568df122 100644 --- a/services/web/app/views/project/editor/share.pug +++ b/services/web/app/views/project/editor/share.pug @@ -102,14 +102,6 @@ script(type='text/ng-template', id='shareProjectModalTemplate') i.fa.fa-times .row.invite-controls form(ng-show="canAddCollaborators") - - if (settings.recaptcha) - div( - id="recaptcha" - class="g-recaptcha" - data-sitekey=settings.recaptcha.siteKey - data-size="invisible" - data-badge="inline" - ) .small #{translate("share_with_your_collabs")} .form-group tags-input( diff --git a/services/web/public/coffee/ide.coffee b/services/web/public/coffee/ide.coffee index 728300456c..e31985ec8a 100644 --- a/services/web/public/coffee/ide.coffee +++ b/services/web/public/coffee/ide.coffee @@ -33,6 +33,7 @@ define [ "directives/expandableTextArea" "directives/videoPlayState" "services/queued-http" + "services/validateCaptcha" "filters/formatDate" "main/event" "main/account-upgrade" diff --git a/services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee b/services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee index 70784e63cf..0c16dcbb39 100644 --- a/services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee +++ b/services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee @@ -1,7 +1,7 @@ define [ "base" ], (App) -> - App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http, ide) -> + App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http, ide, validateCaptcha) -> $scope.inputs = { privileges: "readAndWrite" contacts: [] @@ -55,25 +55,6 @@ define [ getCurrentInviteEmails = () -> ($scope.project.invites || []).map (u) -> u.email - _recaptchaCallbacks = [] - onRecaptchaSubmit = (token) -> - for cb in _recaptchaCallbacks - cb(token) - _recaptchaCallbacks = [] - - recaptchaId = null - validateCaptcha = (callback = (response) ->) => - if !grecaptcha? - return callback() - reset = () -> - grecaptcha.reset() - _recaptchaCallbacks.push callback - _recaptchaCallbacks.push reset - if !recaptchaId? - el = $('.g-recaptcha')[0] - recaptchaId = grecaptcha.render(el, {callback: onRecaptchaSubmit}) - grecaptcha.execute(recaptchaId) - $scope.filterAutocompleteUsers = ($query) -> currentMemberEmails = getCurrentMemberEmails() return $scope.autocompleteContacts.filter (contact) -> diff --git a/services/web/public/coffee/main.coffee b/services/web/public/coffee/main.coffee index 5ad6f37d34..e361f583c8 100644 --- a/services/web/public/coffee/main.coffee +++ b/services/web/public/coffee/main.coffee @@ -30,6 +30,7 @@ define [ "directives/maxHeight" "directives/creditCards" "services/queued-http" + "services/validateCaptcha" "filters/formatDate" "__MAIN_CLIENTSIDE_INCLUDES__" ], () -> diff --git a/services/web/public/coffee/services/validateCaptcha.coffee b/services/web/public/coffee/services/validateCaptcha.coffee new file mode 100644 index 0000000000..2f00c38f2a --- /dev/null +++ b/services/web/public/coffee/services/validateCaptcha.coffee @@ -0,0 +1,24 @@ +define [ + "base" +], (App) -> + App.factory "validateCaptcha", () -> + _recaptchaCallbacks = [] + onRecaptchaSubmit = (token) -> + for cb in _recaptchaCallbacks + cb(token) + _recaptchaCallbacks = [] + + recaptchaId = null + validateCaptcha = (callback = (response) ->) => + if !grecaptcha? + return callback() + reset = () -> + grecaptcha.reset() + _recaptchaCallbacks.push callback + _recaptchaCallbacks.push reset + if !recaptchaId? + el = $('.g-recaptcha')[0] + recaptchaId = grecaptcha.render(el, {callback: onRecaptchaSubmit}) + grecaptcha.execute(recaptchaId) + + return validateCaptcha